Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,51 +1,14 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
import requests
|
3 |
import base64
|
4 |
-
import os
|
5 |
-
|
6 |
-
# argparse 的简单替代,如有需要可替换为 argparse
|
7 |
-
from argparse import Namespace
|
8 |
|
9 |
# 假设 libra_eval 在你的 python 包 libra.eval 中
|
10 |
from libra.eval import libra_eval
|
11 |
|
12 |
-
# 预定义图像及其链接(或者本地路径)
|
13 |
-
DEFAULT_IMAGES = {
|
14 |
-
"Image 1": "./examples/curent.jpg",
|
15 |
-
"Image 2": "./examples/prior.jpg"
|
16 |
-
}
|
17 |
-
|
18 |
-
def image_url_to_base64(image_url: str) -> str:
|
19 |
-
"""
|
20 |
-
将远程图片 URL 转换为 Base64 数据 URI。
|
21 |
-
如果请求失败,则返回提示文本。
|
22 |
-
"""
|
23 |
-
try:
|
24 |
-
response = requests.get(image_url)
|
25 |
-
response.raise_for_status()
|
26 |
-
base64_image = base64.b64encode(response.content).decode("utf-8")
|
27 |
-
return f"data:image/jpeg;base64,{base64_image}"
|
28 |
-
except Exception as e:
|
29 |
-
return f"<p style='color: red;'>Failed to load image: {e}</p>"
|
30 |
-
|
31 |
-
def generate_image_html(image_url: str) -> str:
|
32 |
-
"""
|
33 |
-
生成一个 <img> 标签的 HTML,用于在 Gradio 中以预览形式显示图片。
|
34 |
-
如果是 http(s) 链接,则尝试转换为 Base64;如果是本地路径,直接使用 file://。
|
35 |
-
"""
|
36 |
-
# 判断是否以 http(s) 开头
|
37 |
-
if image_url.startswith("http"):
|
38 |
-
base64_image = image_url_to_base64(image_url)
|
39 |
-
return f'<img src="{base64_image}" style="width: 200px; height: auto; display: inline-block; margin: 10px; border-radius: 10px;" />'
|
40 |
-
else:
|
41 |
-
# 直接使用本地路径
|
42 |
-
return f'<img src="file://{image_url}" style="width: 200px; height: auto; display: inline-block; margin: 10px; border-radius: 10px;" />'
|
43 |
-
|
44 |
def generate_radiology_description(
|
45 |
prompt: str,
|
46 |
-
selected_current: str,
|
47 |
uploaded_current: str,
|
48 |
-
selected_prior: str,
|
49 |
uploaded_prior: str,
|
50 |
temperature: float,
|
51 |
top_p: float,
|
@@ -54,28 +17,25 @@ def generate_radiology_description(
|
|
54 |
) -> str:
|
55 |
"""
|
56 |
核心推理函数:
|
57 |
-
1.
|
58 |
2. 调用 libra_eval 来生成报告描述
|
59 |
3. 返回生成的结果或错误消息
|
60 |
"""
|
61 |
-
# 如果用户上传了图片,则优先使用上传的图片;否则使用默认图片
|
62 |
-
current_image = uploaded_current if uploaded_current else DEFAULT_IMAGES.get(selected_current)
|
63 |
-
prior_image = uploaded_prior if uploaded_prior else DEFAULT_IMAGES.get(selected_prior)
|
64 |
|
65 |
-
#
|
66 |
-
if not
|
67 |
-
return "Please
|
68 |
|
69 |
-
#
|
70 |
-
model_path = "
|
71 |
conv_mode = "libra_v1"
|
72 |
|
73 |
try:
|
74 |
# 调用 libra_eval 进行推理
|
75 |
output = libra_eval(
|
76 |
model_path=model_path,
|
77 |
-
model_base=None,
|
78 |
-
image_file=[
|
79 |
query=prompt,
|
80 |
temperature=temperature,
|
81 |
top_p=top_p,
|
@@ -89,84 +49,57 @@ def generate_radiology_description(
|
|
89 |
except Exception as e:
|
90 |
return f"An error occurred: {str(e)}"
|
91 |
|
92 |
-
#
|
93 |
-
# Blocks 为最新的容器API,可以更好地对布局进行控制
|
94 |
with gr.Blocks() as demo:
|
95 |
# 标题和简单说明
|
96 |
-
gr.Markdown("# Libra Radiology Report Generator")
|
97 |
-
gr.Markdown("
|
98 |
|
99 |
-
#
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
)
|
105 |
|
106 |
-
#
|
107 |
with gr.Row():
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
choices=list(DEFAULT_IMAGES.keys()),
|
117 |
-
value="Image 1"
|
118 |
-
)
|
119 |
-
# 或者上传一张新的
|
120 |
-
uploaded_current = gr.Image(
|
121 |
-
label="Or Upload Current Image",
|
122 |
-
type="filepath",
|
123 |
-
tool="editor"
|
124 |
-
)
|
125 |
-
|
126 |
-
with gr.Column():
|
127 |
-
gr.Markdown("### Prior Image")
|
128 |
-
# 同样显示默认图像
|
129 |
-
for img in DEFAULT_IMAGES.values():
|
130 |
-
gr.HTML(generate_image_html(img))
|
131 |
-
selected_prior = gr.Radio(
|
132 |
-
label="Select Prior Image",
|
133 |
-
choices=list(DEFAULT_IMAGES.keys()),
|
134 |
-
value="Image 2"
|
135 |
-
)
|
136 |
-
uploaded_prior = gr.Image(
|
137 |
-
label="Or Upload Prior Image",
|
138 |
-
type="filepath",
|
139 |
-
tool="editor"
|
140 |
-
)
|
141 |
|
142 |
-
#
|
143 |
with gr.Row():
|
144 |
temperature_slider = gr.Slider(
|
145 |
-
label="Temperature",
|
146 |
-
minimum=0.1,
|
147 |
-
maximum=1.0,
|
148 |
-
step=0.1,
|
149 |
value=0.7
|
150 |
)
|
151 |
top_p_slider = gr.Slider(
|
152 |
-
label="Top P",
|
153 |
-
minimum=0.1,
|
154 |
-
maximum=1.0,
|
155 |
-
step=0.1,
|
156 |
value=0.8
|
157 |
)
|
158 |
num_beams_slider = gr.Slider(
|
159 |
-
label="Number of Beams",
|
160 |
-
minimum=1,
|
161 |
-
maximum=20,
|
162 |
-
step=1,
|
163 |
value=2
|
164 |
)
|
165 |
max_tokens_slider = gr.Slider(
|
166 |
-
label="Max New Tokens",
|
167 |
-
minimum=10,
|
168 |
-
maximum=4096,
|
169 |
-
step=10,
|
170 |
value=128
|
171 |
)
|
172 |
|
@@ -181,19 +114,18 @@ with gr.Blocks() as demo:
|
|
181 |
generate_button.click(
|
182 |
fn=generate_radiology_description,
|
183 |
inputs=[
|
184 |
-
prompt_input,
|
185 |
-
selected_current,
|
186 |
uploaded_current,
|
187 |
-
selected_prior,
|
188 |
uploaded_prior,
|
189 |
-
temperature_slider,
|
190 |
-
top_p_slider,
|
191 |
-
num_beams_slider,
|
192 |
max_tokens_slider
|
193 |
],
|
194 |
outputs=output_text
|
195 |
)
|
196 |
|
197 |
-
# 启动 Gradio 应用(将 share 设置为 True 以便在 Hugging Face Spaces 中分享)
|
198 |
if __name__ == "__main__":
|
|
|
|
|
199 |
demo.launch(share=True)
|
|
|
1 |
import gradio as gr
|
2 |
+
import os
|
3 |
import requests
|
4 |
import base64
|
|
|
|
|
|
|
|
|
5 |
|
6 |
# 假设 libra_eval 在你的 python 包 libra.eval 中
|
7 |
from libra.eval import libra_eval
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
def generate_radiology_description(
|
10 |
prompt: str,
|
|
|
11 |
uploaded_current: str,
|
|
|
12 |
uploaded_prior: str,
|
13 |
temperature: float,
|
14 |
top_p: float,
|
|
|
17 |
) -> str:
|
18 |
"""
|
19 |
核心推理函数:
|
20 |
+
1. 仅通过用户上传的图片获取图像文件路径
|
21 |
2. 调用 libra_eval 来生成报告描述
|
22 |
3. 返回生成的结果或错误消息
|
23 |
"""
|
|
|
|
|
|
|
24 |
|
25 |
+
# 确保用户上传了两张图片
|
26 |
+
if not uploaded_current or not uploaded_prior:
|
27 |
+
return "Please upload both current and prior images."
|
28 |
|
29 |
+
# 模型路径
|
30 |
+
model_path = "X-iZhang/libra-v1.0-7b"
|
31 |
conv_mode = "libra_v1"
|
32 |
|
33 |
try:
|
34 |
# 调用 libra_eval 进行推理
|
35 |
output = libra_eval(
|
36 |
model_path=model_path,
|
37 |
+
model_base=None, # 如果有必要,可指定基础模型
|
38 |
+
image_file=[uploaded_current, uploaded_prior], # 两张本地图片路径
|
39 |
query=prompt,
|
40 |
temperature=temperature,
|
41 |
top_p=top_p,
|
|
|
49 |
except Exception as e:
|
50 |
return f"An error occurred: {str(e)}"
|
51 |
|
52 |
+
# 构建 Gradio 界面
|
|
|
53 |
with gr.Blocks() as demo:
|
54 |
# 标题和简单说明
|
55 |
+
gr.Markdown("# Libra Radiology Report Generator (Local Upload Only)")
|
56 |
+
gr.Markdown("Upload **Current** and **Prior** images below to generate a radiology description using the Libra model.")
|
57 |
|
58 |
+
# 用户输入:文本提示
|
59 |
+
prompt_input = gr.Textbox(
|
60 |
+
label="Prompt",
|
61 |
+
value="Describe the key findings in these two images."
|
62 |
+
)
|
|
|
63 |
|
64 |
+
# 上传本地图像(Current & Prior)
|
65 |
with gr.Row():
|
66 |
+
uploaded_current = gr.Image(
|
67 |
+
label="Upload Current Image",
|
68 |
+
type="filepath"
|
69 |
+
)
|
70 |
+
uploaded_prior = gr.Image(
|
71 |
+
label="Upload Prior Image",
|
72 |
+
type="filepath"
|
73 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
+
# 参数调节
|
76 |
with gr.Row():
|
77 |
temperature_slider = gr.Slider(
|
78 |
+
label="Temperature",
|
79 |
+
minimum=0.1,
|
80 |
+
maximum=1.0,
|
81 |
+
step=0.1,
|
82 |
value=0.7
|
83 |
)
|
84 |
top_p_slider = gr.Slider(
|
85 |
+
label="Top P",
|
86 |
+
minimum=0.1,
|
87 |
+
maximum=1.0,
|
88 |
+
step=0.1,
|
89 |
value=0.8
|
90 |
)
|
91 |
num_beams_slider = gr.Slider(
|
92 |
+
label="Number of Beams",
|
93 |
+
minimum=1,
|
94 |
+
maximum=20,
|
95 |
+
step=1,
|
96 |
value=2
|
97 |
)
|
98 |
max_tokens_slider = gr.Slider(
|
99 |
+
label="Max New Tokens",
|
100 |
+
minimum=10,
|
101 |
+
maximum=4096,
|
102 |
+
step=10,
|
103 |
value=128
|
104 |
)
|
105 |
|
|
|
114 |
generate_button.click(
|
115 |
fn=generate_radiology_description,
|
116 |
inputs=[
|
117 |
+
prompt_input,
|
|
|
118 |
uploaded_current,
|
|
|
119 |
uploaded_prior,
|
120 |
+
temperature_slider,
|
121 |
+
top_p_slider,
|
122 |
+
num_beams_slider,
|
123 |
max_tokens_slider
|
124 |
],
|
125 |
outputs=output_text
|
126 |
)
|
127 |
|
|
|
128 |
if __name__ == "__main__":
|
129 |
+
# 启动 Gradio 应用
|
130 |
+
# 将 share 设置为 True 以便在 Hugging Face Spaces 中分享
|
131 |
demo.launch(share=True)
|