import os import base64 import gradio as gr from gradio_client import Client, file import time import json import threading MODEL_NAME = "QWEN" client_chat = os.environ.get("CHAT_URL") client_vl = os.environ.get("VL_URL") def read(filename): with open(filename) as f: data = f.read() return data SYS_PROMPT = read('system_prompt.txt') DESCRIPTION = '''

家庭医生demo

🩺一个帮助您分析症状和检验报告的家庭医生(AI诊疗助手)。

🔎 选择您需要咨询的科室医生,在输入框中输入症状描述或者体检信息等;您也可以在图片框中上传检测报告图。

🦕 请注意生成信息可能不准确,且不具备任何实际参考价值,如有需要请联系专业医生。

💾 由于网络环境问题,如果长时间未出现分析内容,您可以点击重载,尝试获取结果。

''' css = """ h1 { text-align: center; display: block; } footer { display:none !important } """ LICENSE = '采用 ' + MODEL_NAME + ' 模型' result = "" json_path = "" def process_text(text_input, unit): global result client = Client(client_chat) print(client.view_api()) job = client.submit( query=str(text_input), history=None, system=f"You are a experienced {unit} doctor AI assistant." + SYS_PROMPT, api_name="/model_chat" ) response = job.result() print(response) result = response[1][0][1] def process_image(image_input, unit): global result, json_path if image_input is not None: image = str(image_input) print(image) #with open(image_input, "rb") as f: # base64_image = base64.b64encode(f.read()).decode("utf-8") client = Client(client_vl) print(client.view_api()) prompt = f" You are a experienced {unit} doctor AI assistant." + SYS_PROMPT + "Help me understand what is in this picture and analysis." res5 = client.predict( "", image, fn_index=5 ) print(res5) res0 = client.predict( res5, prompt, fn_index=0 ) print(res0) json_path = res0 def update(): global result job = client.submit( json_path, fn_index=1 ) response = job.result() with open(response, 'r') as f: data = json.load(f) print(data) result = data[-1][1] result = "正在分析....." threading.Thread(target=update).start() def fetch_result(): return result def reset_result(): result = 0 def main(text_input="", image_input=None, unit=""): global result reset_result() if text_input and image_input is None: process_text(text_input, unit) elif image_input is not None: process_image(image_input, unit) return result with gr.Blocks(css=css, title="家庭医生AI助手", theme="soft") as iface: with gr.Accordion(""): gr.Markdown(DESCRIPTION) unit = gr.Dropdown(label="🩺科室", value='中医科', elem_id="units", choices=["中医科", "内科", "外科", "妇产科", "儿科", \ "五官科", "男科", "皮肤性病科", "传染科", "精神心理科", \ "整形美容科", "营养科", "生殖中心", "麻醉医学科", "医学影像科", \ "骨科", "肿瘤科", "急诊科", "检验科"]) with gr.Row(): output_box = gr.Markdown(value=result, every=10, label="分析") with gr.Row(): image_input = gr.Image(type="filepath", label="上传图片") # Create an image upload button text_input = gr.Textbox(label="输入") # Create a text input box with gr.Row(): submit_btn = gr.Button("🚀 发送") # Create a submit button fresh_btn = gr.Button("✨ 重载") clear_btn = gr.ClearButton([output_box, image_input, text_input], value="🗑️ 清空") # Create a clear button # Set up the event listeners submit_btn.click(main, inputs=[text_input, image_input, unit], outputs=output_box) fresh_btn.click(fn=fetch_result, outputs=output_box) clear_btn.click(fn=reset_result) gr.Markdown(LICENSE) #gr.close_all() iface.queue().launch(show_api=False) # Launch the Gradio interface