Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,14 +8,13 @@ from arabic_support import support_arabic_text
|
|
8 |
from pathlib import Path
|
9 |
import json
|
10 |
from PIL import Image
|
11 |
-
from config.
|
12 |
-
|
13 |
|
14 |
st.set_page_config(
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
)
|
20 |
|
21 |
# مسیر فایل فعلی
|
@@ -37,8 +36,8 @@ with open(css_file_path, "r", encoding="utf-8") as file:
|
|
37 |
|
38 |
# اعمال CSS
|
39 |
st.markdown(f"<style>{css_code}</style>", unsafe_allow_html=True)
|
40 |
-
def go_to_level(selected):
|
41 |
-
|
42 |
support_arabic_text(all=True)
|
43 |
state = st.session_state
|
44 |
st.title("AI-Medical Questionnaire")
|
@@ -46,6 +45,13 @@ side = st.sidebar
|
|
46 |
|
47 |
if "messages" not in state:
|
48 |
state["messages"] = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
if "level" not in state:
|
50 |
state.level = "L01"
|
51 |
state.next = ""
|
@@ -67,75 +73,58 @@ question_text = question_class.get_question_text(state.level)
|
|
67 |
with side:
|
68 |
img = Image.open(assets_dir / "logo.png")
|
69 |
st.image(
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
)
|
74 |
-
colm1, colm2 = st.columns(2)
|
75 |
-
with colm1:
|
76 |
-
|
77 |
-
if st.button("change"):
|
78 |
-
prv = get_response(state.previous)
|
79 |
-
next_level = option_class.get_next_level(prv, state.previous)
|
80 |
-
feedback = option_class.get_feedback(prompt, state.previous)
|
81 |
-
|
82 |
-
if next_level != "L98" and next_level != "L99":
|
83 |
-
my_bar = st.progress(0, text=feedback)
|
84 |
-
for percent_complete in range(100):
|
85 |
-
time.sleep(0.01)
|
86 |
-
my_bar.progress(percent_complete + 1, text=feedback)
|
87 |
-
time.sleep(1)
|
88 |
-
my_bar.empty()
|
89 |
-
state.level = state.previous
|
90 |
-
state.level = next_level
|
91 |
-
|
92 |
-
resp = question_class.get_question_text(state.level)[0]
|
93 |
-
with st.chat_message("assistant"):
|
94 |
-
response = st.write_stream(response_generator(resp))
|
95 |
-
state.messages.append({"role": "assistant", "content": f"checkpoint: {state.previous}"})
|
96 |
-
state.messages.append({"role": "assistant", "content": response})
|
97 |
-
with colm2:
|
98 |
-
if st.button("reset"):
|
99 |
-
|
100 |
-
del state["messages"]
|
101 |
-
st.rerun()
|
102 |
|
|
|
|
|
|
|
|
|
103 |
|
104 |
def response_generator(resp):
|
105 |
-
|
|
|
106 |
for word in resp.split():
|
107 |
character = ""
|
108 |
for char in word:
|
109 |
yield character + char
|
110 |
-
time.sleep(0.
|
111 |
yield " "
|
112 |
-
time.sleep(0.
|
113 |
|
114 |
-
if "start" not in state:
|
115 |
-
state.start = """ سلام من دستیار شما هستم. برای بررسی و تصمیم گیری وضعیت بیماران مرحله به مرحله بررسی وضعیت بیمار را با کمک من انجام دهید. برای پیشنهاد می کنم
|
116 |
-
اختلالات حرکتی را بررسی کنید. آیا بیمار دارای چنین اختلالی است؟)"""
|
117 |
|
118 |
-
st.chat_message("assistant", avatar=str(assets_dir / "robot_icon.png")).markdown(
|
119 |
-
state.start
|
120 |
-
)
|
121 |
for messages in state.messages:
|
122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
st.markdown(messages["content"])
|
124 |
|
|
|
125 |
prompt = st.chat_input("""نوشتن پیام""")
|
126 |
|
127 |
if prompt:
|
128 |
-
|
|
|
|
|
|
|
|
|
|
|
129 |
st.switch_page("pages/result.py")
|
130 |
-
prompt = is_valid_input(prompt)
|
131 |
st.chat_message("user", avatar=str(assets_dir / "user.png")).markdown(prompt)
|
132 |
state.messages.append({"role": "user", "content": prompt})
|
133 |
|
134 |
-
if
|
135 |
|
136 |
-
|
137 |
-
|
138 |
-
feedback = option_class.get_feedback(prompt, state.level)
|
139 |
|
140 |
if next_level != "L98" and next_level != "L99":
|
141 |
my_bar = st.progress(0, text=feedback)
|
@@ -146,32 +135,35 @@ if prompt:
|
|
146 |
my_bar.empty()
|
147 |
state.previous = state.level
|
148 |
state.level = next_level
|
149 |
-
|
150 |
resp = question_class.get_question_text(state.level)[0]
|
151 |
with st.chat_message("assistant"):
|
152 |
response = st.write_stream(response_generator(resp))
|
153 |
-
state.messages.append({"role": "assistant", "content": f"checkpoint: {state.previous}"})
|
154 |
state.messages.append({"role": "assistant", "content": response})
|
155 |
-
|
156 |
elif next_level == "L98":
|
|
|
|
|
|
|
|
|
157 |
st.switch_page("pages/result.py")
|
158 |
elif next_level == "L99":
|
|
|
159 |
st.switch_page("pages/result.py")
|
160 |
-
else:
|
161 |
-
prompt = get_response(prompt)
|
162 |
|
163 |
-
|
164 |
-
feedback = "تغییر بخش مورد بررسی, تا چند لحظه دیگر"
|
165 |
-
state.previous = state.level
|
166 |
-
next_level = prompt
|
167 |
-
state.level = next_level
|
168 |
-
resp = question_class.get_question_text(state.level)[0]
|
169 |
-
with st.chat_message("assistant"):
|
170 |
-
response = st.write_stream(response_generator(resp))
|
171 |
-
state.messages.append({"role": "assistant", "content": f"checkpoint: {state.previous}"})
|
172 |
-
state.messages.append({"role": "assistant", "content": response})
|
173 |
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
from pathlib import Path
|
9 |
import json
|
10 |
from PIL import Image
|
11 |
+
from config.conf import *
|
|
|
12 |
|
13 |
st.set_page_config(
|
14 |
+
page_title="AI-Medical Questionnaire",
|
15 |
+
page_icon="💊",
|
16 |
+
layout="wide",
|
17 |
+
initial_sidebar_state="expanded",
|
18 |
)
|
19 |
|
20 |
# مسیر فایل فعلی
|
|
|
36 |
|
37 |
# اعمال CSS
|
38 |
st.markdown(f"<style>{css_code}</style>", unsafe_allow_html=True)
|
39 |
+
# def go_to_level(selected):
|
40 |
+
# state.level = levels()
|
41 |
support_arabic_text(all=True)
|
42 |
state = st.session_state
|
43 |
st.title("AI-Medical Questionnaire")
|
|
|
45 |
|
46 |
if "messages" not in state:
|
47 |
state["messages"] = []
|
48 |
+
start = """ سلام من دستیار شما هستم. برای بررسی و تصمیم گیری وضعیت بیماران مرحله به مرحله بررسی وضعیت بیمار را با کمک من انجام دهید. برای پیشنهاد می کنم
|
49 |
+
اختلالات حرکتی را بررسی کنید. آیا بیمار دارای چنین اختلالی است؟)"""
|
50 |
+
state.messages.append({"role": "assistant", "content": start})
|
51 |
+
# st.chat_message("user", avatar=str(assets_dir / "user.png")).markdown(
|
52 |
+
# state["messages"] = []
|
53 |
+
if "result" not in state:
|
54 |
+
state.result = ""
|
55 |
if "level" not in state:
|
56 |
state.level = "L01"
|
57 |
state.next = ""
|
|
|
73 |
with side:
|
74 |
img = Image.open(assets_dir / "logo.png")
|
75 |
st.image(
|
76 |
+
img,
|
77 |
+
width=250,
|
78 |
+
caption="جهت بازنشانی از دکمه ریست استفاده کنید",
|
79 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
|
81 |
+
if st.button("reset"):
|
82 |
+
|
83 |
+
del state["messages"]
|
84 |
+
st.rerun()
|
85 |
|
86 |
def response_generator(resp):
|
87 |
+
yield f"checkpoint: {state.previous}"
|
88 |
+
yield "\n\n"
|
89 |
for word in resp.split():
|
90 |
character = ""
|
91 |
for char in word:
|
92 |
yield character + char
|
93 |
+
time.sleep(0.03)
|
94 |
yield " "
|
95 |
+
time.sleep(0.05)
|
96 |
|
|
|
|
|
|
|
97 |
|
|
|
|
|
|
|
98 |
for messages in state.messages:
|
99 |
+
|
100 |
+
if messages["role"] == "assistant":
|
101 |
+
avatar = str(assets_dir / "robot_icon.png")
|
102 |
+
elif messages["role"] == "user":
|
103 |
+
avatar = str(assets_dir / "user.png")
|
104 |
+
else:
|
105 |
+
avatar = None # Default
|
106 |
+
|
107 |
+
with st.chat_message(messages["role"], avatar=avatar):
|
108 |
st.markdown(messages["content"])
|
109 |
|
110 |
+
|
111 |
prompt = st.chat_input("""نوشتن پیام""")
|
112 |
|
113 |
if prompt:
|
114 |
+
answer = prompt
|
115 |
+
answer = get_response(answer)
|
116 |
+
if answer == "L99":
|
117 |
+
state.result = "L99"
|
118 |
+
|
119 |
+
feedback = option_class.get_feedback("نتیجه", state.result)
|
120 |
st.switch_page("pages/result.py")
|
|
|
121 |
st.chat_message("user", avatar=str(assets_dir / "user.png")).markdown(prompt)
|
122 |
state.messages.append({"role": "user", "content": prompt})
|
123 |
|
124 |
+
if answer == "بله" or answer == "خیر":
|
125 |
|
126 |
+
next_level = option_class.get_next_level(answer, state.level)
|
127 |
+
feedback = option_class.get_feedback(answer, state.level)
|
|
|
128 |
|
129 |
if next_level != "L98" and next_level != "L99":
|
130 |
my_bar = st.progress(0, text=feedback)
|
|
|
135 |
my_bar.empty()
|
136 |
state.previous = state.level
|
137 |
state.level = next_level
|
138 |
+
|
139 |
resp = question_class.get_question_text(state.level)[0]
|
140 |
with st.chat_message("assistant"):
|
141 |
response = st.write_stream(response_generator(resp))
|
|
|
142 |
state.messages.append({"role": "assistant", "content": response})
|
143 |
+
|
144 |
elif next_level == "L98":
|
145 |
+
state.previous = state.level
|
146 |
+
state.level = next_level
|
147 |
+
|
148 |
+
state.result = feedback
|
149 |
st.switch_page("pages/result.py")
|
150 |
elif next_level == "L99":
|
151 |
+
state.result = feedback
|
152 |
st.switch_page("pages/result.py")
|
|
|
|
|
153 |
|
154 |
+
elif answer[0] == "L":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
155 |
|
156 |
+
feedback = "تغییر بخش مورد بررسی, تا چند لحظه دیگر"
|
157 |
+
state.previous = state.level
|
158 |
+
next_level = answer
|
159 |
+
state.level = next_level
|
160 |
+
resp = question_class.get_question_text(state.level)[0]
|
161 |
+
with st.chat_message("assistant"):
|
162 |
+
response = st.write_stream(response_generator(resp))
|
163 |
+
state.messages.append({"role": "assistant", "content": response})
|
164 |
+
|
165 |
+
else:
|
166 |
+
with st.chat_message("assistant"):
|
167 |
+
answer = unknown()
|
168 |
+
response = st.write_stream(response_generator(answer))
|
169 |
+
state.messages.append({"role": "assistant", "content": response})
|