Spaces:
Runtime error
Runtime error
File size: 5,367 Bytes
f291881 6887849 e1fd560 de5713b c57e262 0ae0889 f81786c 0ae0889 0b15ad6 f81786c 0b15ad6 a852c0d 3107413 f81786c 3107413 c57e262 3107413 a852c0d 69f0b9b 0b15ad6 793d728 0b15ad6 1c74a5d 2de97d6 793d728 e1fd560 413ccff e1fd560 1c74a5d 0b15ad6 4b2b6d5 0b15ad6 35c9edb 3ba82cb 0b15ad6 793d728 413ccff 2de97d6 413ccff 2de97d6 793d728 2de97d6 793d728 0b15ad6 793d728 0b15ad6 2de97d6 0b15ad6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
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()
|