machinelearnAn commited on
Commit
bce5f7b
·
verified ·
1 Parent(s): af13f6f

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +112 -38
src/streamlit_app.py CHANGED
@@ -1,40 +1,114 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
5
 
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
 
 
2
  import streamlit as st
3
 
4
+ # --- Dữ liệu Demo (Chuẩn bị sẵn) ---
5
+ # Dữ liệu này được lấy sẵn để demo nhanh và ổn định.
6
+ DEMO_DATA = {
7
+ "Ví dụ 1: Dấu hiệu trầm cảm ràng": {
8
+ "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.",
9
+ "mentallama_output": {
10
+ "answer": "Depression",
11
+ "reasoning": "The post conveys a deep sense of emotional numbness, loss of interest (anhedonia), and lack of motivation, which are core symptoms of depression."
12
+ },
13
+ "mentalroberta_output": "Depression"
14
+ },
15
+ "Ví dụ 2: Dấu hiệu stress tinh tế": {
16
+ "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.",
17
+ "mentallama_output": {
18
+ "answer": "Stress",
19
+ "reasoning": "The user mentions sleepless nights, a racing mind, and feeling overwhelmed by work. These are classic indicators of high stress levels."
20
+ },
21
+ "mentalroberta_output": "Stress"
22
+ },
23
+ "Ví dụ 3: Bài đăng tích cực, không có vấn đề": {
24
+ "post": "Just finished a great hike with friends! The view from the top was incredible. Feeling so refreshed and happy.",
25
+ "mentallama_output": {
26
+ "answer": "None",
27
+ "reasoning": "The post expresses positive emotions, mentions social activity (hiking with friends), and feelings of refreshment. There are no indicators of mental distress."
28
+ },
29
+ "mentalroberta_output": "None"
30
+ },
31
+ "Ví dụ 4: Tự tử (Nghiêm trọng)": {
32
+ "post": "I can't take this pain anymore. I've been thinking about ending it all. It feels like the only way out.",
33
+ "mentallama_output": {
34
+ "answer": "Suicide",
35
+ "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."
36
+ },
37
+ "mentalroberta_output": "Suicide"
38
+ }
39
+ }
40
+
41
+ # --- Giao diện ứng dụng Streamlit ---
42
+ st.set_page_config(layout="wide", page_title="MentaLLaMA Demo")
43
+
44
+ # Header
45
+ st.title("🔬 Demo: MentaLLaMA - Phân Tích Sức Khỏe Tâm Thần Có Thể Giải Thích")
46
+ st.markdown("""
47
+ Chào mừng đến với bản demo của **MentaLLaMA**!
48
+
49
+ Demo này minh họa sự khác biệt cốt lõi giữa:
50
+ 1. **Phương pháp truyền thống "Hộp đen"** (như MentalRoBERTa): Chỉ đưa ra dự đoán.
51
+ 2. **MentaLLaMA**: Đưa ra cả dự đoán và **lời giải thích chi tiết**.
52
+
53
+ *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.*
54
+ """)
55
+
56
+ # --- Phần tương tác ---
57
+ st.sidebar.header("Bảng điều khiển")
58
+ option = st.sidebar.selectbox(
59
+ 'Chọn một bài đăng mẫu để phân tích:',
60
+ list(DEMO_DATA.keys()),
61
+ index=0
62
+ )
63
+
64
+ # Hiển thị bài đăng được chọn
65
+ selected_example = DEMO_DATA[option]
66
+ post_text = selected_example["post"]
67
+
68
+ st.subheader("📝 Nội dung bài đăng được chọn:")
69
+ st.info(post_text)
70
+
71
+ # Nút phân tích
72
+ if st.sidebar.button('Chạy Phân Tích So Sánh', type="primary"):
73
+ st.header("📊 Kết quả phân tích so sánh")
74
+
75
+ col1, col2 = st.columns(2, gap="large")
76
+
77
+ # Cột 1: Phương pháp truyền thống (Hộp đen)
78
+ with col1:
79
+ st.subheader("Mô hình 'Hộp đen' (ví dụ: MentalRoBERTa)")
80
+
81
+ st.metric(label="Dự đoán", value=selected_example['mentalroberta_output'])
82
+
83
+ with st.expander("Phân tích về phương pháp này", expanded=True):
84
+ st.warning("⚠️ **Không có lời giải thích!**")
85
+ st.write("""
86
+ 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.
87
+ - **Khó tin tưởng:** Làm sao để chắc chắn nó không mắc sai lầm?
88
+ - **Khó xác thực:** Bác sĩ không có cơ sở để kiểm tra lại logic của AI.
89
+ - **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.
90
+ """)
91
+
92
+ # Cột 2: MentaLLaMA (Giải thích được)
93
+ with col2:
94
+ st.subheader("MentaLLaMA (Mô hình giải thích được)")
95
+
96
+ st.metric(label="Dự đoán", value=selected_example['mentallama_output']['answer'])
97
+
98
+ st.write("**Lý do giải thích (Reasoning):**")
99
+ st.success(f"“{selected_example['mentallama_output']['reasoning']}”")
100
+
101
+ with st.expander("Phân tích về phương pháp này", expanded=True):
102
+ st.info("💡 **Cung cấp lý do chi tiết!**")
103
+ st.write("""
104
+ 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.
105
+ - **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ả.
106
+ - **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.
107
+ - **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.
108
+ """)
109
+ else:
110
+ 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.")
111
+
112
+ # Footer
113
+ st.markdown("---")
114
+ 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*.")