orrinin commited on
Commit
1413b56
·
verified ·
1 Parent(s): 73ebbf1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +107 -0
app.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import base64
3
+ import gradio as gr
4
+ from gradio_client import Client
5
+
6
+ MODEL_NAME = "QWEN"
7
+ client_chat = os.environ.get("CHAT_URL")
8
+ client_vl = os.environ.get("VL_URL")
9
+
10
+ def read(filename):
11
+ with open(filename) as f:
12
+ data = f.read()
13
+ return data
14
+
15
+ SYS_PROMPT = read('system_prompt.txt')
16
+
17
+
18
+ DESCRIPTION = '''
19
+ <div>
20
+ <h1 style="text-align: center;">家庭医生demo</h1>
21
+ <p>🩺一个帮助您分析症状和检验报告的家庭医生(AI诊疗助手)。</p>
22
+ <p>🔎 选择您需要咨询的科室医生,在输入框中输入症状描述或者体检信息等;您也可以在图片框中上传检测报告图。</p>
23
+ <p>🦕 请注意生成信息可能不准确,且不具备任何实际参考价值,如有需要请联系专业医生。</p>
24
+ </div>
25
+ '''
26
+
27
+
28
+ css = """
29
+ h1 {
30
+ text-align: center;
31
+ display: block;
32
+ }
33
+ footer {
34
+ display:none !important
35
+ }
36
+ """
37
+
38
+
39
+ LICENSE = '采用 ' + MODEL_NAME + ' 模型'
40
+
41
+
42
+
43
+
44
+ def process_text(text_input, unit):
45
+ client = Client(chat_url)
46
+ response = client.predict(
47
+ query=text_input,
48
+ history=[],
49
+ system = f" You are a experienced {unit} doctor AI assistant." + SYS_PROMPT,
50
+ api_name="/model_chat"
51
+ )
52
+ return response
53
+
54
+
55
+ def encode_image_to_base64(image_input):
56
+ buffered = io.BytesIO()
57
+ image_input.save(buffered, format="JPEG")
58
+ img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
59
+ return img_str
60
+
61
+ def process_image(image_input, unit):
62
+ if image_input is not None:
63
+ print(image_input)
64
+ #with open(image_input.name, "rb") as f:
65
+ # base64_image = base64.b64encode(f.read()).decode("utf-8")
66
+ client = Client(client_vl)
67
+ # base64_image = encode_image_to_base64(image_input)
68
+ prompt = f" You are a experienced {unit} doctor AI assistant." + SYS_PROMPT + "Help me understand what is in this picture and analysis."
69
+ response = client.predict(
70
+ prompt,
71
+ image_input,
72
+ fn_index=5
73
+ )
74
+ return response
75
+
76
+
77
+ def main(text_input="", image_input=None, unit=""):
78
+ if text_input and image_input is None:
79
+ return process_text(text_input,unit)
80
+ elif image_input is not None:
81
+ return process_image(image_input,unit)
82
+
83
+ with gr.Blocks(theme='shivi/calm_seafoam', css=css, title="家庭医生AI助手") as iface:
84
+ with gr.Accordion(""):
85
+ gr.Markdown(DESCRIPTION)
86
+ unit = gr.Dropdown(label="🩺科室", value='中医科', elem_id="units",
87
+ choices=["中医科", "内科", "外科", "妇产科", "儿科", \
88
+ "五官科", "男科", "皮肤性病科", "传染科", "精神心理科", \
89
+ "整形美容科", "营养科", "生殖中心", "麻醉医学科", "医学影像科", \
90
+ "骨科", "肿瘤科", "急诊科", "检验科"])
91
+ with gr.Row():
92
+ output_box = gr.Markdown(label="分析") # Create an output textbox
93
+ with gr.Row():
94
+ image_input = gr.Image(type="pil", label="上传图片") # Create an image upload button
95
+ text_input = gr.Textbox(label="输入") # Create a text input box
96
+ with gr.Row():
97
+ submit_btn = gr.Button("🚀 确认") # Create a submit button
98
+ clear_btn = gr.ClearButton([output_box,image_input,text_input], value="🗑️ 清空") # Create a clear button
99
+
100
+ # Set up the event listeners
101
+ submit_btn.click(main, inputs=[text_input, image_input, unit], outputs=output_box)
102
+
103
+ gr.Markdown(LICENSE)
104
+
105
+ #gr.close_all()
106
+
107
+ iface.queue().launch(show_api=False) # Launch the Gradio interface