Update app.py
Browse files
app.py
CHANGED
@@ -6,7 +6,6 @@ from transformers import DistilBertTokenizer, DistilBertModel, AutoModel, AutoTo
|
|
6 |
from langdetect import detect
|
7 |
from huggingface_hub import snapshot_download
|
8 |
import os
|
9 |
-
from typing import List
|
10 |
|
11 |
|
12 |
# Device
|
@@ -86,45 +85,35 @@ app.add_middleware(
|
|
86 |
|
87 |
|
88 |
class TextIn(BaseModel):
|
89 |
-
|
90 |
|
91 |
|
92 |
@app.post("/api/predict")
|
93 |
@app.post("/api/predict")
|
94 |
def predict(data: TextIn):
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
predictions = dict(zip(hinglish_labels, probs))
|
119 |
-
|
120 |
-
results.append({
|
121 |
-
"text": text,
|
122 |
-
"language": lang if lang in ["en", "hi"] else "unknown",
|
123 |
-
"predictions": predictions
|
124 |
-
})
|
125 |
-
|
126 |
-
return {"results": results}
|
127 |
-
|
128 |
|
129 |
@app.get("/")
|
130 |
def root():
|
|
|
6 |
from langdetect import detect
|
7 |
from huggingface_hub import snapshot_download
|
8 |
import os
|
|
|
9 |
|
10 |
|
11 |
# Device
|
|
|
85 |
|
86 |
|
87 |
class TextIn(BaseModel):
|
88 |
+
text: str
|
89 |
|
90 |
|
91 |
@app.post("/api/predict")
|
92 |
@app.post("/api/predict")
|
93 |
def predict(data: TextIn):
|
94 |
+
text = data.text
|
95 |
+
try:
|
96 |
+
lang = detect(text)
|
97 |
+
except:
|
98 |
+
lang = "unknown"
|
99 |
+
|
100 |
+
if lang == "en":
|
101 |
+
tokenizer = english_tokenizer
|
102 |
+
model = english_model
|
103 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True).to(device)
|
104 |
+
with torch.no_grad():
|
105 |
+
outputs = model(**inputs)
|
106 |
+
probs = torch.sigmoid(outputs).squeeze().cpu().tolist()
|
107 |
+
return {"language": "English", "predictions": dict(zip(english_labels, probs))}
|
108 |
+
|
109 |
+
else:
|
110 |
+
tokenizer = hinglish_tokenizer
|
111 |
+
model = hinglish_model
|
112 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True).to(device)
|
113 |
+
with torch.no_grad():
|
114 |
+
outputs = model(**inputs)
|
115 |
+
probs = torch.softmax(outputs, dim=1).squeeze().cpu().tolist()
|
116 |
+
return {"language": "Hinglish", "predictions": dict(zip(hinglish_labels, probs))}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
|
118 |
@app.get("/")
|
119 |
def root():
|