X-iZhang commited on
Commit
97a468f
·
verified ·
1 Parent(s): 3f3122f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -129
app.py CHANGED
@@ -5,146 +5,122 @@ import os
5
  import requests
6
  import base64
7
 
8
- # 假设 libra_eval 在你的 python 包 libra.eval 中
9
  from libra.eval import libra_eval
10
 
11
- model_path = "X-iZhang/libra-v1.0-7b"
12
- image_files = ["./examples/curent.jpg",
13
- "./examples/prior.jpg"]
14
- prompt = "Provide a detailed description of the findings in the radiology image."
15
- conv_mode = "libra_v1"
 
 
 
 
16
 
17
- result = libra_eval(
18
- model_path=model_path,
19
- model_base=None,
20
- image_file=image_files,
21
- query=prompt,
22
- temperature=0.9,
23
- top_p=0.8,
24
- max_new_tokens=512
25
- )
26
- print(result)
27
 
28
- # def generate_radiology_description(
29
- # prompt: str,
30
- # uploaded_current: str,
31
- # uploaded_prior: str,
32
- # temperature: float,
33
- # top_p: float,
34
- # num_beams: int,
35
- # max_new_tokens: int
36
- # ) -> str:
37
- # """
38
- # 核心推理函数:
39
- # 1. 仅通过用户上传的图片获取图像文件路径
40
- # 2. 调用 libra_eval 来生成报告描述
41
- # 3. 返回生成的结果或错误消息
42
- # """
43
 
44
- # # 确保用户上传了两张图片
45
- # if not uploaded_current or not uploaded_prior:
46
- # return "Please upload both current and prior images."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
- # # 模型路径
49
- # model_path = "X-iZhang/libra-v1.0-7b"
50
- # conv_mode = "libra_v1"
51
 
52
- # try:
53
- # # 调用 libra_eval 进行推理
54
- # print("Before calling libra_eval")
55
- # output = libra_eval(
56
- # model_path=model_path,
57
- # model_base=None, # 如果有必要,可指定基础模型
58
- # image_file=[uploaded_current, uploaded_prior], # 两张本地图片路径
59
- # query=prompt,
60
- # temperature=temperature,
61
- # top_p=top_p,
62
- # num_beams=num_beams,
63
- # length_penalty=1.0,
64
- # num_return_sequences=1,
65
- # conv_mode=conv_mode,
66
- # max_new_tokens=max_new_tokens
67
- # )
68
- # print("After calling libra_eval, result:", output)
69
- # return output
70
- # except Exception as e:
71
- # return f"An error occurred: {str(e)}"
72
 
73
- # # 构建 Gradio 界面
74
- # with gr.Blocks() as demo:
75
- # # 标题和简单说明
76
- # gr.Markdown("# Libra Radiology Report Generator (Local Upload Only)")
77
- # gr.Markdown("Upload **Current** and **Prior** images below to generate a radiology description using the Libra model.")
78
 
79
- # # 用户输入:文本提示
80
- # prompt_input = gr.Textbox(
81
- # label="Prompt",
82
- # value="Describe the key findings in these two images."
83
- # )
 
 
 
 
 
84
 
85
- # # 上传本地图像(Current & Prior)
86
- # with gr.Row():
87
- # uploaded_current = gr.Image(
88
- # label="Upload Current Image",
89
- # type="filepath"
90
- # )
91
- # uploaded_prior = gr.Image(
92
- # label="Upload Prior Image",
93
- # type="filepath"
94
- # )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
96
- # # 参数调节
97
- # with gr.Row():
98
- # temperature_slider = gr.Slider(
99
- # label="Temperature",
100
- # minimum=0.1,
101
- # maximum=1.0,
102
- # step=0.1,
103
- # value=0.7
104
- # )
105
- # top_p_slider = gr.Slider(
106
- # label="Top P",
107
- # minimum=0.1,
108
- # maximum=1.0,
109
- # step=0.1,
110
- # value=0.8
111
- # )
112
- # num_beams_slider = gr.Slider(
113
- # label="Number of Beams",
114
- # minimum=1,
115
- # maximum=20,
116
- # step=1,
117
- # value=2
118
- # )
119
- # max_tokens_slider = gr.Slider(
120
- # label="Max New Tokens",
121
- # minimum=10,
122
- # maximum=4096,
123
- # step=10,
124
- # value=128
125
- # )
126
 
127
- # # 用于显示模型生成的结果
128
- # output_text = gr.Textbox(
129
- # label="Generated Description",
130
- # lines=10
131
- # )
132
 
133
- # # 点击按钮时触发的推理逻辑
134
- # generate_button = gr.Button("Generate Description")
135
- # generate_button.click(
136
- # fn=generate_radiology_description,
137
- # inputs=[
138
- # prompt_input,
139
- # uploaded_current,
140
- # uploaded_prior,
141
- # temperature_slider,
142
- # top_p_slider,
143
- # num_beams_slider,
144
- # max_tokens_slider
145
- # ],
146
- # outputs=output_text
147
- # )
148
 
149
- # if __name__ == "__main__":
150
- # demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  import requests
6
  import base64
7
 
 
8
  from libra.eval import libra_eval
9
 
10
+ def generate_radiology_description(
11
+ prompt: str,
12
+ uploaded_current: str,
13
+ uploaded_prior: str,
14
+ temperature: float,
15
+ top_p: float,
16
+ num_beams: int,
17
+ max_new_tokens: int
18
+ ) -> str:
19
 
20
+
21
+ if not uploaded_current or not uploaded_prior:
22
+ return "Please upload both current and prior images."
 
 
 
 
 
 
 
23
 
24
+
25
+ model_path = "X-iZhang/libra-v1.0-7b"
26
+ conv_mode = "libra_v1"
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
+ try:
29
+
30
+ print("Before calling libra_eval")
31
+ output = libra_eval(
32
+ model_path=model_path,
33
+ model_base=None,
34
+ image_file=[uploaded_current, uploaded_prior],
35
+ query=prompt,
36
+ temperature=temperature,
37
+ top_p=top_p,
38
+ num_beams=num_beams,
39
+ length_penalty=1.0,
40
+ num_return_sequences=1,
41
+ conv_mode=conv_mode,
42
+ max_new_tokens=max_new_tokens
43
+ )
44
+ print("After calling libra_eval, result:", output)
45
+ return output
46
+ except Exception as e:
47
+ return f"An error occurred: {str(e)}"
48
 
 
 
 
49
 
50
+ with gr.Blocks() as demo:
51
+
52
+ gr.Markdown("# Libra Radiology Report Generator (Local Upload Only)")
53
+ gr.Markdown("Upload **Current** and **Prior** images below to generate a radiology description using the Libra model.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
+
56
+ prompt_input = gr.Textbox(
57
+ label="Prompt",
58
+ value="Describe the key findings in these two images."
59
+ )
60
 
61
+
62
+ with gr.Row():
63
+ uploaded_current = gr.Image(
64
+ label="Upload Current Image",
65
+ type="filepath"
66
+ )
67
+ uploaded_prior = gr.Image(
68
+ label="Upload Prior Image",
69
+ type="filepath"
70
+ )
71
 
72
+
73
+ with gr.Row():
74
+ temperature_slider = gr.Slider(
75
+ label="Temperature",
76
+ minimum=0.1,
77
+ maximum=1.0,
78
+ step=0.1,
79
+ value=0.7
80
+ )
81
+ top_p_slider = gr.Slider(
82
+ label="Top P",
83
+ minimum=0.1,
84
+ maximum=1.0,
85
+ step=0.1,
86
+ value=0.8
87
+ )
88
+ num_beams_slider = gr.Slider(
89
+ label="Number of Beams",
90
+ minimum=1,
91
+ maximum=20,
92
+ step=1,
93
+ value=2
94
+ )
95
+ max_tokens_slider = gr.Slider(
96
+ label="Max New Tokens",
97
+ minimum=10,
98
+ maximum=4096,
99
+ step=10,
100
+ value=128
101
+ )
102
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
 
104
+ output_text = gr.Textbox(
105
+ label="Generated Description",
106
+ lines=10
107
+ )
 
108
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
 
110
+ generate_button = gr.Button("Generate Description")
111
+ generate_button.click(
112
+ fn=generate_radiology_description,
113
+ inputs=[
114
+ prompt_input,
115
+ uploaded_current,
116
+ uploaded_prior,
117
+ temperature_slider,
118
+ top_p_slider,
119
+ num_beams_slider,
120
+ max_tokens_slider
121
+ ],
122
+ outputs=output_text
123
+ )
124
+
125
+ if __name__ == "__main__":
126
+ demo.launch()