File size: 873 Bytes
38eeb5b
 
4f18923
38eeb5b
26d4d7d
38eeb5b
4f18923
 
26d4d7d
38eeb5b
 
26d4d7d
38eeb5b
 
 
 
 
 
bbbb50e
26d4d7d
38eeb5b
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from fastapi import FastAPI
from pydantic import BaseModel
from transformers import pipeline
from fastapi.middleware.cors import CORSMiddleware

# Load the Arabic dialect classifier
model_name = "IbrahimAmin/marbertv2-arabic-written-dialect-classifier"
dialect = pipeline("text-classification", model=model_name)

# FastAPI app
app = FastAPI(title="Arabic Dialect Detector API")

# Allow CORS (so n8n or browser can call it)
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["*"],
    allow_headers=["*"]
)

# Input schema
class InputText(BaseModel):
    text: str

# API endpoint
@app.post("/predict")
def predict(input: InputText):
    if not input.text:
        return {"label": None, "score": None, "error": "No input text"}
    result = dialect(input.text)[0]
    return {"label": result['label'], "score": round(result['score'], 3)}