Spaces:
Runtime error
Runtime error
Commit
·
14d1947
1
Parent(s):
6c2e3e1
Upload 3 files
Browse files- app/main.py +26 -0
- app/model/model.py +40 -0
- app/model/trained_pipeline-0.1.0.pkl +3 -0
app/main.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from app.model.model import predict_pipeline
|
| 4 |
+
from app.model.model import __version__ as model_version
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class TextIn(BaseModel):
|
| 11 |
+
text: str
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class PredictionOut(BaseModel):
|
| 15 |
+
language: str
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
@app.get("/")
|
| 19 |
+
def home():
|
| 20 |
+
return {"health_check": "OK", "model_version": model_version}
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
@app.post("/predict", response_model=PredictionOut)
|
| 24 |
+
def predict(payload: TextIn):
|
| 25 |
+
language = predict_pipeline(payload.text)
|
| 26 |
+
return {"language": language}
|
app/model/model.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pickle
|
| 2 |
+
import re
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
|
| 5 |
+
__version__ = "0.1.0"
|
| 6 |
+
|
| 7 |
+
BASE_DIR = Path(__file__).resolve(strict=True).parent
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
with open(f"{BASE_DIR}/trained_pipeline-{__version__}.pkl", "rb") as f:
|
| 11 |
+
model = pickle.load(f)
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
classes = [
|
| 15 |
+
"Arabic",
|
| 16 |
+
"Danish",
|
| 17 |
+
"Dutch",
|
| 18 |
+
"English",
|
| 19 |
+
"French",
|
| 20 |
+
"German",
|
| 21 |
+
"Greek",
|
| 22 |
+
"Hindi",
|
| 23 |
+
"Italian",
|
| 24 |
+
"Kannada",
|
| 25 |
+
"Malayalam",
|
| 26 |
+
"Portugeese",
|
| 27 |
+
"Russian",
|
| 28 |
+
"Spanish",
|
| 29 |
+
"Sweedish",
|
| 30 |
+
"Tamil",
|
| 31 |
+
"Turkish",
|
| 32 |
+
]
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def predict_pipeline(text):
|
| 36 |
+
text = re.sub(r'[!@#$(),\n"%^*?\:;~`0-9]', " ", text)
|
| 37 |
+
text = re.sub(r"[[]]", " ", text)
|
| 38 |
+
text = text.lower()
|
| 39 |
+
pred = model.predict([text])
|
| 40 |
+
return classes[pred[0]]
|
app/model/trained_pipeline-0.1.0.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ab7135c7421b2636cd9fe030d05b883c5ba19ccd13e57195e755a9e43df2bc53
|
| 3 |
+
size 10251835
|