CoolFace
Apppublic

GhadaAlothman/readability-api

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
app.py42 linesDownload Raw Back to root
1import gradio as gr2from transformers import AutoTokenizer, AutoModelForSequenceClassification3import torch4import torch.nn.functional as F5 6# تحميل نموذج جاهز (غيّري الاسم إلى موديلك الخاص إذا عندك)7MODEL_NAME = "aubmindlab/bert-base-arabertv2"8 9tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)10model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)11model.eval()12 13# تعريف أسماء المستويات حسب الفئات (LABEL_0, LABEL_1, LABEL_2)14# غيّري الترتيب لو عندك موديل مدرَّب بترتيب مختلف15id2label = {16    0: "سهل",17    1: "متوسط",18    2: "صعب"19}20 21# دالة التنبؤ بالنص22def analyze_readability(text):23    inputs = tokenizer(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        confidence = float(probs[0][pred])29    30    label = id2label.get(pred, "غير معروف")31    return f"تصنيف هذه النشرة: {label}"32 33# واجهة Gradio34iface = gr.Interface(35    fn=analyze_readability,36    inputs=gr.Textbox(label="أدخل نصًا بالعربية"),37    outputs="text",38    title="تحليل قابلية القراءة للنصوص الطبية",39    description="أدخل فقرة من النشرة الطبية ليقوم النظام بتقدير مستوى القراءة: سهل، متوسط، أو صعب."40)41 42iface.launch()