Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| from models.space_a import summarize_question | |
| from models.space_b import generate_mentalqa_answer | |
| from models.space_er import extract_entities | |
| from models.space_classify import classifyA, classifyQ | |
| # CSS styling with layout control | |
| css = """ | |
| .rtl { | |
| direction: rtl; | |
| text-align: right; | |
| } | |
| .sidebar-box { | |
| background-color: #f2f2f2; | |
| padding: 16px; | |
| border-radius: 8px; | |
| } | |
| /* Desktop: move sidebar to right */ | |
| @media (min-width: 769px) { | |
| #main-layout { | |
| flex-direction: row-reverse !important; | |
| } | |
| } | |
| """ | |
| with gr.Blocks(css=css) as demo: | |
| with gr.Row(elem_id="main-layout"): | |
| # Sidebar | |
| with gr.Column(scale=1): | |
| with gr.Group(elem_classes="sidebar-box rtl"): | |
| gr.Markdown("### القائمة الجانبية", elem_classes="rtl") | |
| task_type = gr.Radio( | |
| choices=[ | |
| ("تصنيف الأسئلة والأجوبة", "classification"), | |
| ("التعرف على الكيانات", "entity_recognition"), | |
| ("الإجابة على الأسئلة", "question_answering"), | |
| ("تلخيص النصوص", "summarization") | |
| ], | |
| label="اختر نوع المهمة", | |
| elem_classes="rtl" | |
| ) | |
| with gr.Group(visible=False) as classify_box: | |
| classify_type = gr.Radio( | |
| choices=["تصنيف سؤال", "تصنيف إجابة"], | |
| label="نوع التصنيف", | |
| elem_classes="rtl" | |
| ) | |
| # Main content | |
| with gr.Column(scale=3): | |
| gr.Markdown("### 🧠 تجربة منصة الصحة النفسية", elem_classes="rtl") | |
| text_input = gr.Textbox(label="أدخل النص هنا لتحليله", lines=7, elem_classes="rtl") | |
| analyze_btn = gr.Button("ابدأ التحليل", elem_classes="rtl") | |
| output_table = gr.Dataframe(headers=["النص", "التصنيف"], visible=False) | |
| output_text = gr.Textbox(label="النتيجة", elem_classes="rtl", visible=True) | |
| gr.Markdown( | |
| "**⚠️ تقدم هذه المنصة دعمًا نفسيًا آليًا، لا تُعد بديلاً عن التشخيص، أو العلاج، أو الاستشارة الطبية المتخصصة.**", | |
| elem_classes="rtl" | |
| ) | |
| def toggle_classification(task_choice): | |
| return gr.update(visible=(task_choice == "classification")) | |
| task_type.change( | |
| toggle_classification, | |
| inputs=task_type, | |
| outputs=classify_box | |
| ) | |
| def analyze(text, task, classify=None): | |
| if not text.strip(): | |
| return "❗ الرجاء إدخال نص للتحليل." | |
| try: | |
| if task == "summarization": | |
| return summarize_question(text) | |
| elif task == "entity_recognition": | |
| return extract_entities(text) | |
| elif task == "question_answering": | |
| return generate_mentalqa_answer(text) | |
| elif task == "classification": | |
| if classify == "تصنيف سؤال": | |
| result = classifyQ(text) | |
| tags = [tag.strip() for tag in result.split(",") if tag.strip()] | |
| return "📌 تصنيف السؤال: تم تصنيف السؤال إلى العناصر التالية:\n" + "\n".join([f"- {tag}" for tag in tags]) if tags else "📌 لم يتم تصنيف السؤال إلى أي عنصر واضح." | |
| elif classify == "تصنيف إجابة": | |
| result = classifyA(text) | |
| tags = [tag.strip() for tag in result.split(",") if tag.strip()] | |
| return "📌 تصنيف الإجابة: تم استخدام الاستراتيجيات التالية:\n" + "\n".join([f"- {tag}" for tag in tags]) if tags else "📌 لم يتم تصنيف أي استراتيجية واضحة في الإجابة." | |
| else: | |
| return "❌ يرجى اختيار نوع التصنيف." | |
| elif task in ["adverse_drug_reaction", "relation_extraction"]: | |
| return "❌ هذا النموذج غير متاح حالياً. سيتم إضافته لاحقاً." | |
| else: | |
| return "❌ المهمة غير مدعومة حالياً." | |
| except Exception as e: | |
| return f"⚠️ حدث خطأ أثناء التحليل: {str(e)}" | |
| def route_output(text, task, classify): | |
| result = analyze(text, task, classify) | |
| if task == "entity_recognition": | |
| if isinstance(result, list) and len(result) > 0: | |
| return gr.update(value=result, visible=True), gr.update(visible=False) | |
| else: | |
| return gr.update(visible=False), gr.update(value="⚠️ لم يتم التعرف على أي كيان", visible=True) | |
| else: | |
| return gr.update(visible=False), gr.update(value=result, visible=True) | |
| analyze_btn.click( | |
| route_output, | |
| inputs=[text_input, task_type, classify_type], | |
| outputs=[output_table, output_text] # ✅ ترتيب المخرجات الصحيح | |
| ) | |
| demo.launch() | |