Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,31 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
from transformers import pipeline
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
model_name = "IbrahimAmin/marbertv2-arabic-written-dialect-classifier"
|
| 6 |
dialect = pipeline("text-classification", model=model_name)
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
return "No input"
|
| 11 |
-
result = dialect(text)[0]
|
| 12 |
-
return f"{result['label']} ({round(result['score'], 3)})"
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
| 19 |
)
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
from transformers import pipeline
|
| 4 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
|
| 6 |
+
# Load the Arabic dialect classifier
|
| 7 |
model_name = "IbrahimAmin/marbertv2-arabic-written-dialect-classifier"
|
| 8 |
dialect = pipeline("text-classification", model=model_name)
|
| 9 |
|
| 10 |
+
# FastAPI app
|
| 11 |
+
app = FastAPI(title="Arabic Dialect Detector API")
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# Allow CORS (so n8n or browser can call it)
|
| 14 |
+
app.add_middleware(
|
| 15 |
+
CORSMiddleware,
|
| 16 |
+
allow_origins=["*"],
|
| 17 |
+
allow_methods=["*"],
|
| 18 |
+
allow_headers=["*"]
|
| 19 |
)
|
| 20 |
|
| 21 |
+
# Input schema
|
| 22 |
+
class InputText(BaseModel):
|
| 23 |
+
text: str
|
| 24 |
+
|
| 25 |
+
# API endpoint
|
| 26 |
+
@app.post("/predict")
|
| 27 |
+
def predict(input: InputText):
|
| 28 |
+
if not input.text:
|
| 29 |
+
return {"label": None, "score": None, "error": "No input text"}
|
| 30 |
+
result = dialect(input.text)[0]
|
| 31 |
+
return {"label": result['label'], "score": round(result['score'], 3)}
|