Medster / app.py
vilarin's picture
Update app.py
f05ed48 verified
raw
history blame
3.07 kB
import gradio as gr
import openai
from openai import OpenAI
import os
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, base_url=base_url)
sys_prompt = read('system_prompt.txt')
# Define the model to be used
MODEL = "gpt-4o"
def process_text(text_input,unit):
if text_input:
completion = client.chat.completions.create(
model=MODEL,
messages=[
{"role": "system", "content": f" You are a helpful {unit} doctor." + sys_prompt},
{"role": "user", "content": f"Hello! Could you solve {text_input}?"}
]
)
return "Assistant: " + completion.choices[0].message.content
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")
response = client.chat.completions.create(
model=MODEL,
messages=[
{"role": "system", "content": f" You are a helpful {unit} doctor." + sys_prompt},
{"role": "user", "content": [
{"type": "text", "text": "Help me understand what this image"},
{"type": "image_url", "image_url": {
"url": f"data:image/png;base64,{base64_image}"}
}
]}
],
temperature=0.0,
)
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() as iface:
with gr.Accordion(""):
gr.Markdown("""医学报告助手 GPT-4o""")
unit = gr.Dropdown(label="🩺科室", value='内科', elem_id="units",
choices=["内科", "外科", "妇产科", "男科", "儿科", \
"五官科", "肿瘤科", "皮肤性病科", "中医科", "传染科", "精神心理科", \
"整形美容科", "营养科", "生殖中心", "麻醉医学科", "医学影像科", \
"急诊科", "检验科"])
with gr.Row():
output_box = gr.Textbox(label="Output") # Create an output textbox
with gr.Row():
image_btn = 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
clear_btn = gr.ClearButton(output_box, value="🗑️ 清空") # Create a clear button
# Set up the event listeners
submit_btn.click(main, inputs=[text_input, image_btn, unit], outputs=output_box)
iface.launch() # Launch the Gradio interface