File size: 3,565 Bytes
1ac84a9
 
 
 
56c2a68
1ac84a9
 
 
d0a1d12
66ec133
 
1ac84a9
 
 
 
fa530b9
 
 
 
 
 
 
9180e08
1ac84a9
3c671c6
1ac84a9
 
f05ed48
1ac84a9
 
 
6a0bec7
3c671c6
 
 
5ae861e
84a19ec
24cfdc1
84a19ec
 
1ac84a9
723d90c
1ac84a9
84a19ec
 
5ae861e
1ac84a9
 
7f990d9
 
 
 
84a19ec
 
 
 
7f990d9
 
 
1ac84a9
84a19ec
1ac84a9
723d90c
1ac84a9
3c671c6
1ac84a9
f05ed48
1ac84a9
f05ed48
1ac84a9
f05ed48
1ac84a9
f05ed48
 
 
 
 
 
 
 
 
20eb2c5
f05ed48
bf30b16
f05ed48
 
 
 
 
 
5ae861e
f05ed48
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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 = "gpt-4o"

def read(filename):
    with open(filename) as f:
        data = f.read()
    return data
    
sys_prompt = read('system_prompt.txt')
    
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}?"}
            ]
        )
        print(completion)
        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 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/jpeg;base64,{base64_image}",
                        "detail":"low"}
                    }
                ]}
            ],
            temperature=0.0,
            max_tokens=1024,
        )
        print(response)
        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.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)

iface.launch()  # Launch the Gradio interface