Spaces:
Runtime error
Runtime error
Update scale_n_consult.py
Browse files- scale_n_consult.py +114 -21
scale_n_consult.py
CHANGED
@@ -1,32 +1,74 @@
|
|
1 |
import os
|
2 |
-
|
3 |
import gradio as gr
|
|
|
4 |
|
5 |
OpenAI.api_key = os.getenv("OPENAI_API_KEY")
|
6 |
api_key = os.getenv("OPENAI_API_KEY")
|
7 |
client = OpenAI(api_key=api_key)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
{
|
12 |
-
"
|
13 |
-
"
|
14 |
-
"
|
15 |
-
"
|
16 |
-
"
|
17 |
-
"
|
18 |
-
"
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
23 |
首先,你需要根据用户的基���情况【展开详细的咨询】,但是【不能一下抛出所有问题,需要逐条提问】,主动向用户【提问】相关的问题并基于相应的【建议】。
|
24 |
询问完后后,你需要生成一个【咨询报告】,包含【用户情况】、【相应建议】、【最终总结】,先以Markdown格式输出,再以JSON格式输出。
|
25 |
-
最后你需要告诉用户,诊断已结束,你不会主动提问,但受咨询者可以继续向你提问感兴趣的问题,你会作出回答。
|
26 |
-
|
|
|
|
|
27 |
|
28 |
|
|
|
29 |
def predict(message, history):
|
|
|
30 |
history_openai_format = []
|
31 |
history_openai_format.append({"role": "assistant", "content":initial_prompt})
|
32 |
for human, assistant in history:
|
@@ -44,20 +86,71 @@ def predict(message, history):
|
|
44 |
if chunk.choices[0].delta.content is not None:
|
45 |
partial_message = partial_message + chunk.choices[0].delta.content
|
46 |
yield partial_message
|
|
|
47 |
|
48 |
-
|
|
|
49 |
predict,
|
50 |
chatbot=gr.Chatbot(
|
51 |
height=400,
|
52 |
bubble_full_width=False,
|
53 |
-
avatar_images=(os.path.join(os.path.dirname(__file__), "patient_ava.png"), os.path.join(os.path.dirname(__file__), "docter_ava.png")),
|
54 |
likeable=True,
|
55 |
),
|
56 |
-
# textbox=gr.Textbox(placeholder="
|
57 |
retry_btn="🔄重新生成",
|
58 |
undo_btn="↩️撤回信息",
|
59 |
clear_btn="🗑️清空信息",
|
60 |
stop_btn="停止生成",
|
61 |
submit_btn="发送",
|
62 |
|
63 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
|
3 |
import gradio as gr
|
4 |
+
from openai import OpenAI
|
5 |
|
6 |
OpenAI.api_key = os.getenv("OPENAI_API_KEY")
|
7 |
api_key = os.getenv("OPENAI_API_KEY")
|
8 |
client = OpenAI(api_key=api_key)
|
9 |
+
os.environ["no_proxy"] = "localhost,127.0.0.1,::1"
|
10 |
+
###量表函数###
|
11 |
+
def visibility():
|
12 |
+
return gr.Column(visible=True)
|
13 |
+
|
14 |
+
def scale2json(choice1,choice2,choice3,choice4,choice5,choice6,choice7,choice8,choice9,choice10,choice11,choice12,choice13,choice14,choice15,choice16,choice17,choice18,choice19,choice20):
|
15 |
+
score = 0
|
16 |
+
global initial_prompt
|
17 |
+
choices = [choice1,choice2,choice3,choice4,choice5,choice6,choice7,choice8,choice9,choice10,choice11,choice12,choice13,choice14,choice15,choice16,choice17,choice18,choice19,choice20]
|
18 |
+
if None in choices:
|
19 |
+
gr.Warning("您有未勾选的题目,请确认后再次提交")
|
20 |
+
else:
|
21 |
+
for i in range(len(choices)):
|
22 |
+
match i:
|
23 |
+
case 1|4|5|10|11|13|15|16|17|19:
|
24 |
+
score += 4 - choices[i]
|
25 |
+
case _:
|
26 |
+
score += choices[i] + 1
|
27 |
+
s = round(1.25*score, 0)
|
28 |
+
DEPRESS="无抑郁体验" if s<53 else "可能有轻度抑郁倾向" if s<=62 else "可能有中度抑郁倾向" if s<=72 else "可能有重度抑郁倾向"
|
29 |
+
SLEEP="夜间睡眠良好"if choice4==0 else "有时夜间睡眠不好" if choice4==1 else "经常夜间睡眠不好" if choice4==2 else "总是夜间睡眠不好"
|
30 |
+
EAT="饮食状态差,不如平时" if choice5==0 else "饮食状态经常不如平时" if choice5==1 else "饮食状态正常" if choice5==2 else "饮食状态良好"
|
31 |
+
WEIGHT="体重正常" if choice7==0 else "有时体重会减轻" if choice7==1 else "体重经常会减轻" if choice7==2 else "体重相较于平时总是减轻"
|
32 |
+
MOOD1="很少感到沮丧郁闷" if choice1==0 else "有时感到沮丧郁闷" if choice1==1 else "经常感到沮丧郁闷" if choice1==2 else "总是感到沮丧郁闷"
|
33 |
+
MOOD2="几乎没有想哭的感觉" if choice3==0 else "有时会有想哭的感觉" if choice3==1 else "经常会有想哭的感觉" if choice3==2 else "总是会想哭"
|
34 |
+
MOOD3="不容易发怒" if choice15==0 else "有时容易发怒" if choice15==1 else "经常容易发怒" if choice15==2 else "总是容易发怒"
|
35 |
+
MOOD=MOOD1+", "+MOOD2+", "+MOOD3
|
36 |
+
TIRED="很少感到疲劳" if choice10==0 else "有时会感到疲劳" if choice10==1 else "经常会感到疲劳" if choice10==2 else "总是感到疲劳"
|
37 |
+
ANXIETY="几乎不会烦躁" if choice13==0 else "有时会坐立不安、难以平静" if choice13==1 else "经常会坐立不安、难以平静" if choice13==2 else"总是坐立不安、感觉难以平静"
|
38 |
+
PHYSICAL=TIRED+", "+ANXIETY
|
39 |
+
VALUE1="从来不觉得自己有用、不可或缺" if choice17==0 else "只有有时会觉得自己有用、不可或缺" if choice17==1 else "觉得自己是有用的、不可或缺的"
|
40 |
+
VALUE2="很少觉得自己的生活有意义" if choice18==0 else "有时才会觉得自己的生活有意义" if choice18==1 else "觉得自己的生活是有意义的"
|
41 |
+
VALUE=VALUE1+", "+VALUE2
|
42 |
+
SUICIDE="无自杀倾向" if choice19==0 else "有时会觉得自己死了别人会过得更好" if choice19==1 else "经常会觉得自己死了别人会过得更好" if choice19==2 else "总觉得自己死了别人会过得更好"
|
43 |
|
44 |
+
json_info=r"""
|
45 |
+
{{
|
46 |
+
"睡眠状况":"{}",
|
47 |
+
"饮食状况":"{}",
|
48 |
+
"体重状况":"{}",
|
49 |
+
"心情状况":"{}",
|
50 |
+
"日常生理状况":"{}",
|
51 |
+
"价值感":"{}",
|
52 |
+
"自杀倾向":"{}",
|
53 |
+
"抑郁情况":"{}",
|
54 |
+
}}
|
55 |
+
""".format(SLEEP,EAT,WEIGHT,MOOD,PHYSICAL,VALUE,SUICIDE,DEPRESS)
|
56 |
+
|
57 |
+
if choice19>=2:
|
58 |
+
gr.Info("如果你感到难以坚持下去,想要结束自己的生命,请一定不要灰心,请通过电话联系我们(4000-100-525, 再拨2),我们会陪伴在你身边!")
|
59 |
+
|
60 |
+
initial_prompt="""你是一名【专业的心理咨询师】,以下是你的用户的【基本情况】,由JSON格式给出:"""+json_info+"""当你和用户交谈时,你需要【主动】向用户提问,并【围绕】用户的【基本情况】展开,【负面】的基本情况需要【着重】展开。
|
61 |
首先,你需要根据用户的基���情况【展开详细的咨询】,但是【不能一下抛出所有问题,需要逐条提问】,主动向用户【提问】相关的问题并基于相应的【建议】。
|
62 |
询问完后后,你需要生成一个【咨询报告】,包含【用户情况】、【相应建议】、【最终总结】,先以Markdown格式输出,再以JSON格式输出。
|
63 |
+
最后你需要告诉用户,诊断已结束,你不会主动提问,但受咨询者可以继续向你提问感兴趣的问题,你会作出回答。"""
|
64 |
+
|
65 |
+
return gr.Column(visible=True),s,initial_prompt
|
66 |
+
############
|
67 |
|
68 |
|
69 |
+
###Chatbot函数###
|
70 |
def predict(message, history):
|
71 |
+
global initial_prompt
|
72 |
history_openai_format = []
|
73 |
history_openai_format.append({"role": "assistant", "content":initial_prompt})
|
74 |
for human, assistant in history:
|
|
|
86 |
if chunk.choices[0].delta.content is not None:
|
87 |
partial_message = partial_message + chunk.choices[0].delta.content
|
88 |
yield partial_message
|
89 |
+
##############
|
90 |
|
91 |
+
|
92 |
+
CHAT_SECTION=gr.ChatInterface(
|
93 |
predict,
|
94 |
chatbot=gr.Chatbot(
|
95 |
height=400,
|
96 |
bubble_full_width=False,
|
97 |
+
#avatar_images=(os.path.join(os.path.dirname(__file__), "patient_ava.png"), os.path.join(os.path.dirname(__file__), "docter_ava.png")),
|
98 |
likeable=True,
|
99 |
),
|
100 |
+
# textbox=gr.Textbox(placeholder="在此输入消息...",),
|
101 |
retry_btn="🔄重新生成",
|
102 |
undo_btn="↩️撤回信息",
|
103 |
clear_btn="🗑️清空信息",
|
104 |
stop_btn="停止生成",
|
105 |
submit_btn="发送",
|
106 |
|
107 |
+
)
|
108 |
+
|
109 |
+
with gr.Blocks() as SCALE_AND_CONSULT:
|
110 |
+
###量表环节
|
111 |
+
with gr.Column():
|
112 |
+
gr.Markdown("**在此项检测模式中,用户需要首先填写一份量表,随后系统将根据量表的填写情况对用户进行更细致的咨询,最终结合用户的回答状况给出诊断结果。**")
|
113 |
+
gr.Markdown("首先进行量表填写。本评定量表为临床上常用于抑郁症检测的SDS(Self-rating depression scale)量表,共有20个项目,请您根据最近一星期以来的实际感受,选择与您的情况最相符的答案。")
|
114 |
+
gr.Markdown("A:从无或偶尔(过去一周内,出现这类情况的日子不超过一天)")
|
115 |
+
gr.Markdown("B:有时(过去一周内,有1-2天有过这类情况)")
|
116 |
+
gr.Markdown("C:经常(过去一周内,有3-4天有过这类情况)")
|
117 |
+
gr.Markdown("D:总是如此(过去一周内,有5-7天有过类似情况)")
|
118 |
+
# gr.Markdown("评分范围与标准:")
|
119 |
+
# gr.Markdown("0-52: 无抑郁体验  53-62: 轻度抑郁  63-72: 中度抑郁  72-80: 重度抑郁")
|
120 |
+
btn = gr.Button("继续")
|
121 |
+
with gr.Column(visible=False) as question:
|
122 |
+
r1 = gr.Radio(["A.从无或偶尔","B.有时","C.经常","D.总是如此"],label = "1.我感到心里沮丧,郁闷",type = "index")
|
123 |
+
r2 = gr.Radio(["A.从无或偶尔","B.有时","C.经常","D.总是如此"],label = "2.我感到早晨心情最好",type = "index")
|
124 |
+
r3 = gr.Radio(["A.从无或偶尔","B.有时","C.经常","D.总是如此"],label = "3.我要哭或想哭",type = "index")
|
125 |
+
r4 = gr.Radio(["A.从无或偶尔","B.有时","C.经常","D.总是如此"],label = "4.我夜间睡眠不好",type = "index")
|
126 |
+
r5 = gr.Radio(["A.从无或偶尔","B.有时","C.经常","D.总是如此"],label = "5.我吃饭像平常一样多",type = "index")
|
127 |
+
r6 = gr.Radio(["A.从无或偶尔","B.有时","C.经常","D.总是如此"],label = "6.我的性功能正常",type = "index")
|
128 |
+
r7 = gr.Radio(["A.从无或偶尔","B.有时","C.经常","D.总是如此"],label = "7.我感到体重减轻",type = "index")
|
129 |
+
r8 = gr.Radio(["A.从无或偶尔","B.有时","C.经常","D.总是如此"],label = "8.我为便秘烦恼",type = "index")
|
130 |
+
r9 = gr.Radio(["A.从无或偶尔","B.有时","C.经常","D.总是如此"],label = "9.我的心跳比平时快",type = "index")
|
131 |
+
r10 = gr.Radio(["A.从无或偶尔","B.有时","C.经常","D.总是如此"],label = "10.我无故感到疲劳",type = "index")
|
132 |
+
r11 = gr.Radio(["A.从无或偶尔","B.有时","C.经常","D.总是如此"],label = "11.我的头脑像往常一样清楚",type = "index")
|
133 |
+
r12 = gr.Radio(["A.从无或偶尔","B.有时","C.经常","D.总是如此"],label = "12.我做事情像平时一样不感到困难",type = "index")
|
134 |
+
r13 = gr.Radio(["A.从无或偶尔","B.有时","C.经常","D.总是如此"],label = "13.我坐卧不安,难以保持平静",type = "index")
|
135 |
+
r14 = gr.Radio(["A.从无或偶尔","B.有时","C.经常","D.总是如此"],label = "14.我对未来感到有希望",type = "index")
|
136 |
+
r15 = gr.Radio(["A.从无或偶尔","B.有时","C.经常","D.总是如此"],label = "15.我比平时更容易激怒",type = "index")
|
137 |
+
r16 = gr.Radio(["A.从无或偶尔","B.有时","C.经常","D.总是如此"],label = "16.我觉得决定什么事情很容易",type = "index")
|
138 |
+
r17 = gr.Radio(["A.从无或偶尔","B.有时","C.经常","D.总是如此"],label = "17.我感到自己是有用的和不可缺少的人",type = "index")
|
139 |
+
r18 = gr.Radio(["A.从无或偶尔","B.有时","C.经常","D.总是如此"],label = "18.我的生活很有意义",type = "index")
|
140 |
+
r19 = gr.Radio(["A.从无或偶尔","B.有时","C.经常","D.总是如此"],label = "19.假若我死了别人会过得更好",type = "index")
|
141 |
+
r20 = gr.Radio(["A.从无或偶尔","B.有时","C.经常","D.总是如此"],label = "20.我仍旧喜爱自己平时喜爱的东西",type = "index")
|
142 |
+
# btn2 = gr.Button("提交")
|
143 |
+
btn_json=gr.Button("提交问卷并进入咨询")
|
144 |
+
with gr.Column(visible=False) as JSON:
|
145 |
+
with gr.Row():
|
146 |
+
score = gr.Textbox(label="得分",visible=False)
|
147 |
+
with gr.Row():
|
148 |
+
output_json=gr.Textbox(label="scale2jsonprompt",visible=True)
|
149 |
+
with gr.Row():
|
150 |
+
with gr.Column():
|
151 |
+
CHAT_SECTION.render()
|
152 |
+
with gr.Row():
|
153 |
+
generate=gr.Button("结束聊天,生成最终结果(待开发)")
|
154 |
+
|
155 |
+
btn.click(visibility,outputs=question)
|
156 |
+
btn_json.click(scale2json,[r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r13,r14,r15,r16,r17,r18,r19,r20],[JSON,score,output_json])
|