Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 4 |
+
import torch
|
| 5 |
+
import torch.nn.functional as F
|
| 6 |
+
|
| 7 |
+
app = FastAPI(title="Arabic Readability API")
|
| 8 |
+
|
| 9 |
+
class InputText(BaseModel):
|
| 10 |
+
text: str
|
| 11 |
+
|
| 12 |
+
MODEL_NAME = "aubmindlab/bert-base-arabertv2" # أو ضعي رابط موديلك الخاص
|
| 13 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 14 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
| 15 |
+
model.eval()
|
| 16 |
+
|
| 17 |
+
@app.get("/")
|
| 18 |
+
def home():
|
| 19 |
+
return {"message": "Arabic Readability API is running!"}
|
| 20 |
+
|
| 21 |
+
@app.post("/predict")
|
| 22 |
+
def predict(inp: InputText):
|
| 23 |
+
inputs = tokenizer(inp.text, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
| 24 |
+
with torch.no_grad():
|
| 25 |
+
outputs = model(**inputs)
|
| 26 |
+
probs = F.softmax(outputs.logits, dim=-1)
|
| 27 |
+
pred = int(torch.argmax(probs))
|
| 28 |
+
return {"label": pred, "confidence": float(probs[0][pred])}
|