Spaces:
Running
Running
Upload 2 files
Browse files- app.py +119 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# 1. Load both Arabic and English optimized models locally
|
| 5 |
+
print("Loading English and Arabic AI Models... Please wait...")
|
| 6 |
+
try:
|
| 7 |
+
# Optimized English Model
|
| 8 |
+
en_classifier = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
|
| 9 |
+
|
| 10 |
+
# Optimized & Stable Arabic Model
|
| 11 |
+
ar_classifier = pipeline("sentiment-analysis", model="CAMeL-Lab/bert-base-arabic-camelbert-mix-sentiment")
|
| 12 |
+
|
| 13 |
+
models_loaded = True
|
| 14 |
+
print("✅ All models loaded successfully!")
|
| 15 |
+
except Exception as e:
|
| 16 |
+
models_loaded = False
|
| 17 |
+
error_msg = str(e)
|
| 18 |
+
print(f"❌ Error loading models: {error_msg}")
|
| 19 |
+
|
| 20 |
+
# 2. Define the core analysis function supporting both languages
|
| 21 |
+
def analyze_stress(user_text, language):
|
| 22 |
+
if not user_text.strip():
|
| 23 |
+
if language == "English":
|
| 24 |
+
return "⚠️ Please enter some text first.", ""
|
| 25 |
+
else:
|
| 26 |
+
return "⚠️ الرجاء كتابة نص أولاً.", ""
|
| 27 |
+
|
| 28 |
+
if not models_loaded:
|
| 29 |
+
return f"❌ Error loading models: {error_msg}", ""
|
| 30 |
+
|
| 31 |
+
# --- English Analysis Logic ---
|
| 32 |
+
if language == "English":
|
| 33 |
+
result = en_classifier(user_text)[0]
|
| 34 |
+
label = result['label']
|
| 35 |
+
score = result['score'] * 100
|
| 36 |
+
|
| 37 |
+
if label == 'NEGATIVE':
|
| 38 |
+
status = f"🚨 Early Stress Detected! (Confidence: {score:.1f}%)"
|
| 39 |
+
recommendations = (
|
| 40 |
+
"💡 **Early Preventive Recommendations:**\n"
|
| 41 |
+
"* Your text indicates high tension or emotional fatigue.\n"
|
| 42 |
+
"* Take a 10-minute break away from screens immediately.\n"
|
| 43 |
+
"* Practice deep breathing exercises (4-7-8 technique).\n"
|
| 44 |
+
"* Consider offloading urgent tasks to reduce cognitive load."
|
| 45 |
+
)
|
| 46 |
+
else:
|
| 47 |
+
status = f"🟢 Stable Mental State / Positive Tone (Confidence: {score:.1f}%)"
|
| 48 |
+
recommendations = (
|
| 49 |
+
"💪 **Keep it up!**\n"
|
| 50 |
+
"* No immediate signs of psychological stress detected.\n"
|
| 51 |
+
"* Continue managing your schedule efficiently to prevent sudden burnout."
|
| 52 |
+
)
|
| 53 |
+
return status, recommendations
|
| 54 |
+
|
| 55 |
+
# --- Arabic Analysis Logic ---
|
| 56 |
+
else:
|
| 57 |
+
result = ar_classifier(user_text)[0]
|
| 58 |
+
label = result['label']
|
| 59 |
+
score = result['score'] * 100
|
| 60 |
+
|
| 61 |
+
if label.lower() == 'negative':
|
| 62 |
+
status = f"🚨 تم رصد مؤشرات إجهاد نفسي مبكر! (نسبة الثقة: {score:.1f}%)"
|
| 63 |
+
recommendations = (
|
| 64 |
+
"💡 **التوصيات الوقائية المبكرة:**\n"
|
| 65 |
+
"* تشير الكلمات إلى وجود ضغوطات نفسية أو إرهاق عاطفي متراكم.\n"
|
| 66 |
+
"* خذ استراحة لمدة 10 دقائق بعيداً عن الشاشات فوراً.\n"
|
| 67 |
+
"* مارس تمارين التنفس العميق (تقنية 4-7-8).\n"
|
| 68 |
+
"* حاول تنظيم مهامك العاجلة للتقليل من العبء الذهني اليومي."
|
| 69 |
+
)
|
| 70 |
+
elif label.lower() == 'positive':
|
| 71 |
+
status = f"🟢 الحالة النفسية مستقرة / نبرة إيجابية (نسبة الثقة: {score:.1f}%)"
|
| 72 |
+
recommendations = (
|
| 73 |
+
"💪 **استمر في هذا الأداء الرائع!**\n"
|
| 74 |
+
"* لا توجد علامات فورية تدل على الإجهاد النفسي الحاد في النص.\n"
|
| 75 |
+
"* حافظ على تنظيم وقتك بشكل متوازن للوقاية من الاحتراق النفسي المفاجئ."
|
| 76 |
+
)
|
| 77 |
+
else:
|
| 78 |
+
status = f"🟡 نبرة محايدة / حالة مستقرة (نسبة الثقة: {score:.1f}%)"
|
| 79 |
+
recommendations = (
|
| 80 |
+
"الحالة تبدو متوازنة حالياً، احرص دائماً على أخذ فترات راحة منتظمة أثناء الدراسة أو العمل."
|
| 81 |
+
)
|
| 82 |
+
return status, recommendations
|
| 83 |
+
|
| 84 |
+
# 3. Build the Dual-Language Gradio UI Layout
|
| 85 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 86 |
+
gr.Markdown("# 🧠 Early Psychological Stress Detection System | نظام كشف الإجهاد النفسي المبكر")
|
| 87 |
+
gr.Markdown("### Project Prototype for the Evaluation Committee | نسخة تجريبية للجنة التقييم")
|
| 88 |
+
|
| 89 |
+
with gr.Row():
|
| 90 |
+
with gr.Column():
|
| 91 |
+
lang_select = gr.Radio(
|
| 92 |
+
choices=["English", "العربية"],
|
| 93 |
+
value="English",
|
| 94 |
+
label="Choose Input Language | اختر لغة الإدخال:"
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
input_text = gr.Textbox(
|
| 98 |
+
label="Express your feelings or daily thoughts here | عبر عن مشاعرك أو يومياتك هنا:",
|
| 99 |
+
placeholder="Type your text based on the selected language...",
|
| 100 |
+
lines=5
|
| 101 |
+
)
|
| 102 |
+
submit_btn = gr.Button("Analyze Mental State | تحليل الحالة النفسية 📊", variant="primary")
|
| 103 |
+
|
| 104 |
+
with gr.Column():
|
| 105 |
+
output_status = gr.Textbox(label="Analysis Result | نتيجة التحليل:")
|
| 106 |
+
output_tips = gr.Markdown(label="Recommendations | التوصيات المخصصة:")
|
| 107 |
+
|
| 108 |
+
gr.Markdown("---")
|
| 109 |
+
gr.Markdown("_✨ Future Roadmap: This backend will transition to a FastAPI server, connecting seamlessly to a mobile application (built via FlutterFlow) supporting speech and facial expression recognition._")
|
| 110 |
+
|
| 111 |
+
# [مصحح] ربط الأحداث يجب أن يكون داخل نطاق Blocks
|
| 112 |
+
submit_btn.click(
|
| 113 |
+
fn=analyze_stress,
|
| 114 |
+
inputs=[input_text, lang_select],
|
| 115 |
+
outputs=[output_status, output_tips]
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
# 4. Launch locally
|
| 119 |
+
demo.launch(share=False)
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
transformers
|
| 3 |
+
torch
|