Update app.py
Browse files
app.py
CHANGED
@@ -34,92 +34,36 @@
|
|
34 |
|
35 |
# # Afficher l'image de test par défaut lorsque l'interface est ouverte
|
36 |
# iface.launch(share=True, inline=True)
|
37 |
-
import gradio as gr
|
38 |
-
from segmentation import segment_image
|
39 |
-
|
40 |
-
import
|
41 |
-
import requests # 导入 requests 库,用于向外部 API 发送 HTTP 请求
|
42 |
-
import base64 # 导入 base64 库,用于图像数据的编码和解码
|
43 |
|
44 |
-
#
|
45 |
-
|
46 |
-
# 调用 segment_image 函数(来自 segmentation.py)获取原始图像和分割后的图像
|
47 |
-
original_image, segmented_image = segment_image(image_path)
|
48 |
-
# 返回原始图像、分割后的图像以及图像路径,供后续分析使用
|
49 |
-
return original_image, segmented_image, image_path
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
with open(image_path, "rb") as img_file:
|
55 |
-
# 读取图像内容为字节
|
56 |
-
image_bytes = img_file.read()
|
57 |
-
# 将图像字节编码为 base64 字符串,并解码为 UTF-8 格式,以便通过 API 传输
|
58 |
-
image_base64 = base64.b64encode(image_bytes).decode("utf-8")
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
"inputs": {
|
63 |
-
"image": image_base64, # 包含 base64 编码的图像数据
|
64 |
-
"text": question # 包含用户的提问
|
65 |
-
}
|
66 |
-
}
|
67 |
|
68 |
-
|
69 |
-
headers = {
|
70 |
-
"Authorization": "Bearer api" # 占位符:请替换为你的实际 HuggingFace API 访问令牌
|
71 |
-
}
|
72 |
-
|
73 |
-
# 向 MedGemma API 终端发送 POST 请求
|
74 |
-
response = requests.post("https://api-inference.huggingface.co/models/google/medgemma-4b-it",
|
75 |
-
headers=headers, json=payload)
|
76 |
-
|
77 |
-
# 检查 API 请求是否成功(状态码为 200)
|
78 |
-
if response.status_code == 200:
|
79 |
-
# 解析 API 返回的 JSON 响应
|
80 |
-
result = response.json()
|
81 |
-
# 提取并返回 API 响应中生成的文本
|
82 |
-
return result[0]["generated_text"]
|
83 |
-
else:
|
84 |
-
# 如果 API 请求失败,返回错误消息,包括状态码和响应文本
|
85 |
-
return f"Error: {response.status_code} - {response.text}"
|
86 |
-
|
87 |
-
# 使用 Gradio Blocks 组件创建界面,以实现自定义布局
|
88 |
-
with gr.Blocks() as iface:
|
89 |
-
# 在界面中添加一个 Markdown 标题
|
90 |
-
gr.Markdown("# 🧠 医学图像分割 + 专家分析")
|
91 |
-
# 创建一个行,用于水平排列组件
|
92 |
with gr.Row():
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
image_input = gr.Image(type="filepath", label="上传皮肤图像")
|
97 |
-
# 为显示输出图像创建另一个列
|
98 |
-
with gr.Column():
|
99 |
-
# 用于显示原始图像的输出组件(NumPy 数组)
|
100 |
-
original_output = gr.Image(type="numpy", label="原始图像")
|
101 |
-
# 用于显示分割后图像的输出组件(NumPy 数组)
|
102 |
-
segmented_output = gr.Image(type="numpy", label="分割后图像")
|
103 |
|
104 |
-
|
105 |
-
image_path_state = gr.State()
|
106 |
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
|
|
111 |
|
112 |
-
|
113 |
-
gr.Markdown("## 🩺 询问上传图像相关的医学问题")
|
114 |
-
# 文本框,供用户输入医学问题
|
115 |
-
question_input = gr.Textbox(label="输入你的问题(例如:'这是什么类型的病变?')")
|
116 |
-
# 文本框,用于显示 MedGemma 的回答
|
117 |
-
answer_output = gr.Textbox(label="MedGemma 回答")
|
118 |
|
119 |
-
|
120 |
-
question_input.submit(fn=analyze_image_with_question, # 调用 analyze_image_with_question 函数
|
121 |
-
inputs=[image_path_state, question_input], # 传递存储的图像路径和问题
|
122 |
-
outputs=answer_output) # 更新 answer_output 文本框
|
123 |
|
124 |
-
# 启动 Gradio 界面
|
125 |
-
iface.launch()
|
|
|
34 |
|
35 |
# # Afficher l'image de test par défaut lorsque l'interface est ouverte
|
36 |
# iface.launch(share=True, inline=True)
|
37 |
+
import gradio as gr
|
38 |
+
from segmentation import segment_image
|
39 |
+
from medgemma_api import query_medgemma
|
40 |
+
import os
|
|
|
|
|
41 |
|
42 |
+
# 默认图片路径
|
43 |
+
default_image_path = "./image.png"
|
|
|
|
|
|
|
|
|
44 |
|
45 |
+
def segment_only(image_path):
|
46 |
+
_, segmented_image = segment_image(image_path)
|
47 |
+
return segmented_image
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
+
def analyze_with_medgemma(image, question):
|
50 |
+
return query_medgemma(image, question)
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
+
with gr.Blocks() as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
with gr.Row():
|
54 |
+
with gr.Column(scale=1):
|
55 |
+
image_input = gr.Image(type="filepath", label="Upload Image")
|
56 |
+
segmented_output = gr.Image(type="numpy", label="Segmented Image")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
+
image_input.change(fn=segment_only, inputs=image_input, outputs=segmented_output)
|
|
|
59 |
|
60 |
+
with gr.Column(scale=2):
|
61 |
+
chatbot = gr.Textbox(label="Ask MedGemma", placeholder="Enter your medical question...")
|
62 |
+
image_for_analysis = gr.Image(type="filepath", label="Upload image for analysis (optional)")
|
63 |
+
analyze_button = gr.Button("Analyze")
|
64 |
+
response_output = gr.Textbox(label="Response")
|
65 |
|
66 |
+
analyze_button.click(fn=analyze_with_medgemma, inputs=[image_for_analysis, chatbot], outputs=response_output)
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
+
demo.launch()
|
|
|
|
|
|
|
69 |
|
|
|
|