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