Spaces:
Running
Running
| import gradio as gr | |
| from transformers import pipeline | |
| # تحميل النماذج بشكل آمن متوافق مع السيرفر السحابي | |
| try: | |
| classifier_en = pipeline("text-classification", model="distilbert-base-uncased") | |
| classifier_ar = pipeline("text-classification", model="CAMeL-Lab/bert-base-arabic-camelbert-mix") | |
| except Exception as e: | |
| print(f"Error loading models: {e}") | |
| # الدالة الأساسية لمعالجة النصوص وتوقع الإجهاد | |
| def predict_stress(text): | |
| if not text.strip(): | |
| return "الرجاء إدخال نص للتحليل." | |
| # تحديد لغة النص المسلمة (عربي أم إنجليزي) بشكل مبسط | |
| is_arabic = any("\u0600" <= char <= "\u06FF" for char in text) | |
| try: | |
| if is_arabic: | |
| res = classifier_ar(text)[0] | |
| else: | |
| res = classifier_en(text)[0] | |
| label = res['label'] | |
| score = res['score'] * 100 | |
| return f"النتيجة: {label} (نسبة التأكد: {score:.2f}%)" | |
| except Exception as e: | |
| return f"حدث خطأ أثناء التحليل: {str(e)}" | |
| # بناء واجهة المستخدم باستخدام Gradio | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# 🧠 نظام التنبؤ بمستويات الإجهاد النفسي باستخدام الذكاء الاصطناعي") | |
| with gr.Row(): | |
| input_text = gr.Textbox(label="أدخل النص المراد تحليله (يدعم العربية والإنجليزية)", placeholder="اكتب شعورك هنا...") | |
| with gr.Row(): | |
| output_text = gr.Textbox(label="تحليل النظام") | |
| submit_btn = gr.Button("تحليل النص") | |
| submit_btn.click(fn=predict_stress, inputs=input_text, outputs=output_text) | |
| # تشغيل الواجهة (يجب تركها فارغة تماماً لتعمل على السيرفر) | |
| demo.launch() |