mentallama-demo / app.py
machinelearnAn's picture
Upload 2 files
af13f6f verified
# app.py
import streamlit as st
# --- Dữ liệu Demo (Chuẩn bị sẵn) ---
# Dữ liệu này được lấy sẵn để demo nhanh và ổn định.
DEMO_DATA = {
"Ví dụ 1: Dấu hiệu trầm cảm rõ ràng": {
"post": "Lately, I just feel so empty and numb inside. Nothing brings me joy anymore, not even the things I used to love. It's a struggle just to get out of bed.",
"mentallama_output": {
"answer": "Depression",
"reasoning": "The post conveys a deep sense of emotional numbness, loss of interest (anhedonia), and lack of motivation, which are core symptoms of depression."
},
"mentalroberta_output": "Depression"
},
"Ví dụ 2: Dấu hiệu stress tinh tế": {
"post": "Another sleepless night. My mind just won't shut off. I feel so tired but I can't rest. Work is piling up and I just don't have the energy to face it.",
"mentallama_output": {
"answer": "Stress",
"reasoning": "The user mentions sleepless nights, a racing mind, and feeling overwhelmed by work. These are classic indicators of high stress levels."
},
"mentalroberta_output": "Stress"
},
"Ví dụ 3: Bài đăng tích cực, không có vấn đề": {
"post": "Just finished a great hike with friends! The view from the top was incredible. Feeling so refreshed and happy.",
"mentallama_output": {
"answer": "None",
"reasoning": "The post expresses positive emotions, mentions social activity (hiking with friends), and feelings of refreshment. There are no indicators of mental distress."
},
"mentalroberta_output": "None"
},
"Ví dụ 4: Tự tử (Nghiêm trọng)": {
"post": "I can't take this pain anymore. I've been thinking about ending it all. It feels like the only way out.",
"mentallama_output": {
"answer": "Suicide",
"reasoning": "The post contains explicit suicidal ideation ('thinking about ending it all') and expresses feelings of unbearable pain and hopelessness, indicating a high-risk situation."
},
"mentalroberta_output": "Suicide"
}
}
# --- Giao diện ứng dụng Streamlit ---
st.set_page_config(layout="wide", page_title="MentaLLaMA Demo")
# Header
st.title("🔬 Demo: MentaLLaMA - Phân Tích Sức Khỏe Tâm Thần Có Thể Giải Thích")
st.markdown("""
Chào mừng đến với bản demo của **MentaLLaMA**!
Demo này minh họa sự khác biệt cốt lõi giữa:
1. **Phương pháp truyền thống "Hộp đen"** (như MentalRoBERTa): Chỉ đưa ra dự đoán.
2. **MentaLLaMA**: Đưa ra cả dự đoán và **lời giải thích chi tiết**.
*Lưu ý: Demo này sử dụng kết quả được tạo sẵn để đảm bảo tốc độ và sự ổn định.*
""")
# --- Phần tương tác ---
st.sidebar.header("Bảng điều khiển")
option = st.sidebar.selectbox(
'Chọn một bài đăng mẫu để phân tích:',
list(DEMO_DATA.keys()),
index=0
)
# Hiển thị bài đăng được chọn
selected_example = DEMO_DATA[option]
post_text = selected_example["post"]
st.subheader("📝 Nội dung bài đăng được chọn:")
st.info(post_text)
# Nút phân tích
if st.sidebar.button('Chạy Phân Tích So Sánh', type="primary"):
st.header("📊 Kết quả phân tích so sánh")
col1, col2 = st.columns(2, gap="large")
# Cột 1: Phương pháp truyền thống (Hộp đen)
with col1:
st.subheader("Mô hình 'Hộp đen' (ví dụ: MentalRoBERTa)")
st.metric(label="Dự đoán", value=selected_example['mentalroberta_output'])
with st.expander("Phân tích về phương pháp này", expanded=True):
st.warning("⚠️ **Không có lời giải thích!**")
st.write("""
Mô hình chỉ đưa ra kết quả cuối cùng. Chúng ta không biết **tại sao** nó lại đưa ra dự đoán này.
- **Khó tin tưởng:** Làm sao để chắc chắn nó không mắc sai lầm?
- **Khó xác thực:** Bác sĩ không có cơ sở để kiểm tra lại logic của AI.
- **Rủi ro cao:** Trong lĩnh vực y tế, một quyết định không thể giải thích là rất nguy hiểm.
""")
# Cột 2: MentaLLaMA (Giải thích được)
with col2:
st.subheader("MentaLLaMA (Mô hình giải thích được)")
st.metric(label="Dự đoán", value=selected_example['mentallama_output']['answer'])
st.write("**Lý do giải thích (Reasoning):**")
st.success(f"“{selected_example['mentallama_output']['reasoning']}”")
with st.expander("Phân tích về phương pháp này", expanded=True):
st.info("💡 **Cung cấp lý do chi tiết!**")
st.write("""
MentaLLaMA không chỉ dự đoán mà còn chỉ ra các **bằng chứng cụ thể** trong văn bản.
- **Tăng sự tin tưởng:** Lời giải thích hợp lý giúp chúng ta tin vào kết quả.
- **Hỗ trợ chuyên gia:** Bác sĩ có thể nhanh chóng xác thực logic và đưa ra quyết định cuối cùng.
- **An toàn hơn:** Sự minh bạch giúp giảm thiểu rủi ro và tăng tính trách nhiệm.
""")
else:
st.info("Hãy chọn một ví dụ và nhấn nút 'Chạy Phân Tích So Sánh' ở thanh bên trái.")
# Footer
st.markdown("---")
st.markdown("Demo được xây dựng dựa trên bài báo: *MentaLLaMA: Interpretable Mental Health Analysis on Social Media with Large Language Models*.")