import gradio as gr import openai from openai import OpenAI import os import io import base64 # Set API key and organization ID from environment variables api_key = os.environ.get("OPENAI_API_KEY") #base_url = os.environ.get("OPENAI_API_BASE") client = OpenAI(api_key=api_key) # Define the model to be used MODEL = os.environ.get("MODEL") def read(filename): with open(filename) as f: data = f.read() return data SYS_PROMPT = read('system_prompt.txt') DESCRIPTION = '''

诊疗助手

一个帮助您分析症状和检验报告的AI工具。

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

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

''' css = """ h1 { text-align: center; display: block; } footer { display:none !important } """ LICENSE = '采用 ' + MODEL + ' 模型' def process_text(text_input, unit): if text_input: completion = client.chat.completions.create( model=MODEL, messages=[ {"role": "system", "content": f" You are a experienced {unit} doctor." + SYS_PROMPT}, {"role": "user", "content": f"Hello! Could you solve {text_input}?"} ] ) return completion.choices[0].message.content return "" def encode_image_to_base64(image_input): buffered = io.BytesIO() image_input.save(buffered, format="JPEG") img_str = base64.b64encode(buffered.getvalue()).decode("utf-8") return img_str def process_image(image_input, unit): if image_input is not None: #with open(image_input.name, "rb") as f: # base64_image = base64.b64encode(f.read()).decode("utf-8") base64_image = encode_image_to_base64(image_input) response = client.chat.completions.create( model=MODEL, messages=[ {"role": "system", "content": f" You are a experienced {unit} doctor." + SYS_PROMPT}, {"role": "user", "content": [ {"type": "text", "text": "Help me understand what is in this picture and analysis."}, {"type": "image_url", "image_url": { "url": f"data:image/jpeg;base64,{base64_image}", "detail":"low"} } ]} ], temperature=0.0, max_tokens=1024, ) return response.choices[0].message.content def main(text_input="", image_input=None, unit=""): if text_input and image_input is None: return process_text(text_input,unit) elif image_input is not None: return process_image(image_input,unit) with gr.Blocks(theme='shivi/calm_seafoam') as iface: with gr.Accordion(""): gr.Markdown(DESCRIPTION) unit = gr.Dropdown(label="🩺科室", value='中医科', elem_id="units", choices=["中医科", "内科", "外科", "妇产科", "儿科", \ "五官科", "男科", "皮肤性病科", "传染科", "精神心理科", \ "整形美容科", "营养科", "生殖中心", "麻醉医学科", "医学影像科", \ "骨科", "肿瘤科", "急诊科", "检验科"]) with gr.Row(): output_box = gr.Markdown(label="分析") # Create an output textbox with gr.Row(): image_input = gr.Image(type="pil", 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 clear_btn = gr.ClearButton(output_box, value="🗑️ 清空") # Create a clear button # Set up the event listeners submit_btn.click(main, inputs=[text_input, image_input, unit], outputs=output_box) gr.Markdown(LICENSE) #gr.close_all() iface.queue().launch(show_api=False) # Launch the Gradio interface