Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -25,7 +25,7 @@ def strip_diacritics(s: str):
|
|
| 25 |
return DIACRITICS.sub("", s)
|
| 26 |
|
| 27 |
def normalize_arabic(s: str):
|
| 28 |
-
return re.sub("[\u0622\u0623\u0625]", "ا", strip_diacritics(s)).replace("ى","ي").replace("ة","ه")
|
| 29 |
|
| 30 |
def split_sentences(text: str):
|
| 31 |
return [p.strip() for p in SENT_SEP.split(text) if p.strip()]
|
|
@@ -40,7 +40,7 @@ def compute_metrics(ar_text: str) -> Dict[str, float]:
|
|
| 40 |
text_norm = normalize_arabic(ar_text)
|
| 41 |
sents = split_sentences(text_norm)
|
| 42 |
words = tokenize_words(text_norm)
|
| 43 |
-
n_sents, n_words = max(len(sents),1), max(len(words),1)
|
| 44 |
diff_count = sum(1 for w in words if difficult_word(w))
|
| 45 |
try:
|
| 46 |
osman = float(textstat.osman(ar_text))
|
|
@@ -52,10 +52,10 @@ def compute_metrics(ar_text: str) -> Dict[str, float]:
|
|
| 52 |
"Word count": n_words,
|
| 53 |
"Sentence count": n_sents,
|
| 54 |
"Character count": n_chars,
|
| 55 |
-
"OSMAN_Score": round(osman,3),
|
| 56 |
"ARI_ArScore": ari_ar_score,
|
| 57 |
"Difficult_Words_Count": diff_count,
|
| 58 |
-
"Average_Sentence_Length_in_Words": round(n_words/n_sents,3)
|
| 59 |
}
|
| 60 |
|
| 61 |
# =======================
|
|
@@ -63,4 +63,27 @@ def compute_metrics(ar_text: str) -> Dict[str, float]:
|
|
| 63 |
# =======================
|
| 64 |
def analyze_text(text):
|
| 65 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=128)
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
return DIACRITICS.sub("", s)
|
| 26 |
|
| 27 |
def normalize_arabic(s: str):
|
| 28 |
+
return re.sub("[\u0622\u0623\u0625]", "ا", strip_diacritics(s)).replace("ى", "ي").replace("ة", "ه")
|
| 29 |
|
| 30 |
def split_sentences(text: str):
|
| 31 |
return [p.strip() for p in SENT_SEP.split(text) if p.strip()]
|
|
|
|
| 40 |
text_norm = normalize_arabic(ar_text)
|
| 41 |
sents = split_sentences(text_norm)
|
| 42 |
words = tokenize_words(text_norm)
|
| 43 |
+
n_sents, n_words = max(len(sents), 1), max(len(words), 1)
|
| 44 |
diff_count = sum(1 for w in words if difficult_word(w))
|
| 45 |
try:
|
| 46 |
osman = float(textstat.osman(ar_text))
|
|
|
|
| 52 |
"Word count": n_words,
|
| 53 |
"Sentence count": n_sents,
|
| 54 |
"Character count": n_chars,
|
| 55 |
+
"OSMAN_Score": round(osman, 3),
|
| 56 |
"ARI_ArScore": ari_ar_score,
|
| 57 |
"Difficult_Words_Count": diff_count,
|
| 58 |
+
"Average_Sentence_Length_in_Words": round(n_words / n_sents, 3),
|
| 59 |
}
|
| 60 |
|
| 61 |
# =======================
|
|
|
|
| 63 |
# =======================
|
| 64 |
def analyze_text(text):
|
| 65 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=128)
|
| 66 |
+
with torch.no_grad():
|
| 67 |
+
outputs = model(**inputs)
|
| 68 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 69 |
+
label_id = torch.argmax(probs, dim=1).item()
|
| 70 |
+
label_map = {0: "سهل", 1: "متوسط", 2: "صعب"}
|
| 71 |
+
label = label_map[label_id]
|
| 72 |
+
stats = compute_metrics(text)
|
| 73 |
+
return {"Predicted_Label": label, **stats}
|
| 74 |
+
|
| 75 |
+
# =======================
|
| 76 |
+
# واجهة Gradio
|
| 77 |
+
# =======================
|
| 78 |
+
demo = gr.Interface(
|
| 79 |
+
fn=analyze_text,
|
| 80 |
+
inputs=gr.Textbox(label="أدخل النص العربي هنا", lines=6, placeholder="اكتب النص هنا..."),
|
| 81 |
+
outputs=gr.JSON(label="نتائج التحليل"),
|
| 82 |
+
title="Arabic Readability Analyzer",
|
| 83 |
+
description="أداة ذكية لتقييم مقروئية النصوص العربية باستخدام نموذج AraBERT.",
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
# =======================
|
| 87 |
+
# تشغيل التطبيق (بدون share=True)
|
| 88 |
+
# =======================
|
| 89 |
+
demo.launch()
|