Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,11 @@ import torch
|
|
| 4 |
import re
|
| 5 |
from typing import Dict
|
| 6 |
import textstat
|
|
|
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
MODEL_PATH = "GhadaAlothman/arabert_readability_3class"
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
|
| 10 |
model = AutoModelForSequenceClassification.from_pretrained(MODEL_PATH)
|
|
@@ -15,39 +19,56 @@ AR_LETTERS = r"[\u0600-\u06FF]"
|
|
| 15 |
SENT_SEP = re.compile(r"[\.!\?؟؛…]+")
|
| 16 |
WORD_RE = re.compile(fr"{AR_LETTERS}+")
|
| 17 |
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
return DIACRITICS.sub("", s)
|
| 20 |
|
| 21 |
-
def normalize_arabic(s: str):
|
| 22 |
-
return re.sub("[\u0622\u0623\u0625]", "ا", strip_diacritics(s)).replace("ى","ي").replace("ة","ه")
|
| 23 |
|
| 24 |
-
def split_sentences(text: str):
|
| 25 |
return [p.strip() for p in SENT_SEP.split(text) if p.strip()]
|
| 26 |
|
| 27 |
-
def tokenize_words(text: str):
|
| 28 |
return WORD_RE.findall(text)
|
| 29 |
|
| 30 |
-
def difficult_word(w: str, min_len: int = 6):
|
| 31 |
return len(w) >= min_len
|
| 32 |
-
|
| 33 |
def compute_metrics(ar_text: str) -> Dict[str, float]:
|
| 34 |
text_norm = normalize_arabic(ar_text)
|
| 35 |
sents = split_sentences(text_norm)
|
| 36 |
words = tokenize_words(text_norm)
|
| 37 |
-
n_sents, n_words = max(len(sents),1), max(len(words),1)
|
| 38 |
diff_count = sum(1 for w in words if difficult_word(w))
|
| 39 |
try:
|
| 40 |
osman = float(textstat.osman(ar_text))
|
| 41 |
except Exception:
|
| 42 |
osman = 0.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
return {
|
| 44 |
"Word count": n_words,
|
| 45 |
"Sentence count": n_sents,
|
| 46 |
-
"
|
|
|
|
|
|
|
| 47 |
"Difficult_Words_Count": diff_count,
|
| 48 |
-
"Average_Sentence_Length_in_Words": round(n_words/n_sents,3)
|
| 49 |
}
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
def analyze_text(text):
|
| 52 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=128)
|
| 53 |
with torch.no_grad():
|
|
@@ -58,7 +79,13 @@ def analyze_text(text):
|
|
| 58 |
label = label_map[label_id]
|
| 59 |
stats = compute_metrics(text)
|
| 60 |
return {"Predicted_Label": label, **stats}
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
demo = gr.Interface(
|
| 63 |
fn=analyze_text,
|
| 64 |
inputs=gr.Textbox(label="أدخل النص العربي هنا", lines=6),
|
|
@@ -67,6 +94,11 @@ demo = gr.Interface(
|
|
| 67 |
description="أداة ذكية لتقييم مقروئية النصوص العربية باستخدام نموذج AraBERT.",
|
| 68 |
)
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import re
|
| 5 |
from typing import Dict
|
| 6 |
import textstat
|
| 7 |
+
from fastapi import FastAPI
|
| 8 |
|
| 9 |
+
# =======================
|
| 10 |
+
# إعداد النموذج والمكتبات
|
| 11 |
+
# =======================
|
| 12 |
MODEL_PATH = "GhadaAlothman/arabert_readability_3class"
|
| 13 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
|
| 14 |
model = AutoModelForSequenceClassification.from_pretrained(MODEL_PATH)
|
|
|
|
| 19 |
SENT_SEP = re.compile(r"[\.!\?؟؛…]+")
|
| 20 |
WORD_RE = re.compile(fr"{AR_LETTERS}+")
|
| 21 |
|
| 22 |
+
|
| 23 |
+
# =======================
|
| 24 |
+
# دوال المعالجة المساعدة
|
| 25 |
+
# =======================
|
| 26 |
+
def strip_diacritics(s: str):
|
| 27 |
return DIACRITICS.sub("", s)
|
| 28 |
|
| 29 |
+
def normalize_arabic(s: str):
|
| 30 |
+
return re.sub("[\u0622\u0623\u0625]", "ا", strip_diacritics(s)).replace("ى", "ي").replace("ة", "ه")
|
| 31 |
|
| 32 |
+
def split_sentences(text: str):
|
| 33 |
return [p.strip() for p in SENT_SEP.split(text) if p.strip()]
|
| 34 |
|
| 35 |
+
def tokenize_words(text: str):
|
| 36 |
return WORD_RE.findall(text)
|
| 37 |
|
| 38 |
+
def difficult_word(w: str, min_len: int = 6):
|
| 39 |
return len(w) >= min_len
|
| 40 |
+
|
| 41 |
def compute_metrics(ar_text: str) -> Dict[str, float]:
|
| 42 |
text_norm = normalize_arabic(ar_text)
|
| 43 |
sents = split_sentences(text_norm)
|
| 44 |
words = tokenize_words(text_norm)
|
| 45 |
+
n_sents, n_words = max(len(sents), 1), max(len(words), 1)
|
| 46 |
diff_count = sum(1 for w in words if difficult_word(w))
|
| 47 |
try:
|
| 48 |
osman = float(textstat.osman(ar_text))
|
| 49 |
except Exception:
|
| 50 |
osman = 0.0
|
| 51 |
+
|
| 52 |
+
# 🔹 حساب عدد الأحرف (بدون مسافات)
|
| 53 |
+
n_chars = sum(len(w) for w in words)
|
| 54 |
+
|
| 55 |
+
# 🔹 حساب مؤشر ARI للعربية
|
| 56 |
+
ari_ar_score = round((4.71 * (n_chars / n_words)) + (0.5 * (n_words / n_sents)) - 21.43, 3)
|
| 57 |
+
|
| 58 |
return {
|
| 59 |
"Word count": n_words,
|
| 60 |
"Sentence count": n_sents,
|
| 61 |
+
"Character count": n_chars,
|
| 62 |
+
"OSMAN_Score": round(osman, 3),
|
| 63 |
+
"ARI_ArScore": ari_ar_score,
|
| 64 |
"Difficult_Words_Count": diff_count,
|
| 65 |
+
"Average_Sentence_Length_in_Words": round(n_words / n_sents, 3)
|
| 66 |
}
|
| 67 |
|
| 68 |
+
|
| 69 |
+
# =======================
|
| 70 |
+
# دالة التنبؤ بالنص
|
| 71 |
+
# =======================
|
| 72 |
def analyze_text(text):
|
| 73 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=128)
|
| 74 |
with torch.no_grad():
|
|
|
|
| 79 |
label = label_map[label_id]
|
| 80 |
stats = compute_metrics(text)
|
| 81 |
return {"Predicted_Label": label, **stats}
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
# =======================
|
| 85 |
+
# واجهة Gradio + FastAPI
|
| 86 |
+
# =======================
|
| 87 |
+
app = FastAPI()
|
| 88 |
+
|
| 89 |
demo = gr.Interface(
|
| 90 |
fn=analyze_text,
|
| 91 |
inputs=gr.Textbox(label="أدخل النص العربي هنا", lines=6),
|
|
|
|
| 94 |
description="أداة ذكية لتقييم مقروئية النصوص العربية باستخدام نموذج AraBERT.",
|
| 95 |
)
|
| 96 |
|
| 97 |
+
# واجهة المستخدم في المسار الرئيسي "/"
|
| 98 |
+
app = gr.mount_gradio_app(app, demo, path="/")
|
| 99 |
|
| 100 |
+
# ✅ واجهة REST API بسيطة في /predict
|
| 101 |
+
@app.post("/predict")
|
| 102 |
+
async def predict(request: dict):
|
| 103 |
+
text = request.get("text", "")
|
| 104 |
+
return analyze_text(text)
|