Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
+
import torch
|
| 4 |
+
import re
|
| 5 |
+
from typing import Dict
|
| 6 |
+
import textstat
|
| 7 |
+
|
| 8 |
+
# ---------- تحميل الموديل ----------
|
| 9 |
+
MODEL_PATH = "GhadaAlothman/arabert_readability_3class" # بعد شوي نشرح كيف ترفعين مودلك له
|
| 10 |
+
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
|
| 12 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_PATH)
|
| 13 |
+
model.eval()
|
| 14 |
+
|
| 15 |
+
# ---------- كود التحليل الإحصائي ----------
|
| 16 |
+
DIACRITICS = re.compile(r"[\u0610-\u061A\u064B-\u065F\u0670\u06D6-\u06ED]")
|
| 17 |
+
AR_LETTERS = r"[\u0600-\u06FF]"
|
| 18 |
+
SENT_SEP = re.compile(r"[\.!\?؟؛…]+")
|
| 19 |
+
WORD_RE = re.compile(fr"{AR_LETTERS}+")
|
| 20 |
+
|
| 21 |
+
def strip_diacritics(s: str): return DIACRITICS.sub("", s)
|
| 22 |
+
def normalize_arabic(s: str): return re.sub("[\u0622\u0623\u0625]", "ا", strip_diacritics(s)).replace("ى","ي").replace("ة","ه")
|
| 23 |
+
def split_sentences(text: str): return [p.strip() for p in SENT_SEP.split(text) if p.strip()]
|
| 24 |
+
def tokenize_words(text: str): return WORD_RE.findall(text)
|
| 25 |
+
def difficult_word(w: str, min_len: int = 6): return len(w) >= min_len
|
| 26 |
+
|
| 27 |
+
def compute_metrics(ar_text: str) -> Dict[str, float]:
|
| 28 |
+
text_norm = normalize_arabic(ar_text)
|
| 29 |
+
sents = split_sentences(text_norm)
|
| 30 |
+
words = tokenize_words(text_norm)
|
| 31 |
+
n_sents, n_words = max(len(sents),1), max(len(words),1)
|
| 32 |
+
diff_count = sum(1 for w in words if difficult_word(w))
|
| 33 |
+
try: osman = float(textstat.osman(ar_text))
|
| 34 |
+
except Exception: osman = 0.0
|
| 35 |
+
return {
|
| 36 |
+
"Word count": n_words,
|
| 37 |
+
"Sentence count": n_sents,
|
| 38 |
+
"OSMAN_Score": round(osman,3),
|
| 39 |
+
"Difficult_Words_Count": diff_count,
|
| 40 |
+
"Average_Sentence_Length_in_Words": round(n_words/n_sents,3)
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
def analyze_text(text):
|
| 44 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=128)
|
| 45 |
+
with torch.no_grad():
|
| 46 |
+
outputs = model(**inputs)
|
| 47 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 48 |
+
label_id = torch.argmax(probs, dim=1).item()
|
| 49 |
+
|
| 50 |
+
label_map = {0: "سهل", 1: "متوسط", 2: "صعب"}
|
| 51 |
+
label = label_map[label_id]
|
| 52 |
+
stats = compute_metrics(text)
|
| 53 |
+
return f"🔹 التصنيف: {label}\n\n" + "\n".join([f"{k}: {v}" for k,v in stats.items()])
|
| 54 |
+
|
| 55 |
+
demo = gr.Interface(
|
| 56 |
+
fn=analyze_text,
|
| 57 |
+
inputs=gr.Textbox(label="أدخل النص العربي هنا", lines=6, placeholder="ألصق النشرة أو النص العربي هنا..."),
|
| 58 |
+
outputs=gr.Textbox(label="نتائج التحليل", lines=10),
|
| 59 |
+
title="Arabic Readability Analyzer",
|
| 60 |
+
description="أداة ذكية لتقييم مقروئية النصوص العربية وحساب مؤشراتها الإحصائية باستخدام نموذج AraBERT"
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
demo.launch()
|