X-iZhang commited on
Commit
b7846fd
·
verified ·
1 Parent(s): 99ff09f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -31
app.py CHANGED
@@ -10,13 +10,10 @@ from libra.eval.run_libra import load_model
10
  DEFAULT_MODEL_PATH = "X-iZhang/libra-v1.0-7b"
11
 
12
  def get_model_short_name(model_path: str) -> str:
13
- """
14
- 提取模型路径最后一个 '/' 之后的部分,作为在下拉菜单中显示的名字。
15
- 例如: "X-iZhang/libra-v1.0-7b" -> "libra-v1.0-7b"
16
- """
17
  return model_path.rstrip("/").split("/")[-1]
18
 
19
- # 全局/或在main里定义都行,这里示例放在外层
20
  loaded_models = {} # {model_key: reuse_model_object}
21
 
22
  def generate_radiology_description(
@@ -31,32 +28,27 @@ def generate_radiology_description(
31
  max_new_tokens: int,
32
  model_paths_dict: dict
33
  ) -> str:
34
- """
35
- 执行放射学报告推理:
36
- 1) 根据下拉选的模型名称 -> 找到实际 model_path
37
- 2) 确保用户选了 Current & Prior 图片
38
- 3) 调用 libra_eval
39
- """
40
  real_model_path = model_paths_dict[selected_model_name]
41
 
42
- # 若用户没选/没上传 Current Image,一定报错
43
  if not current_img_data:
44
  return "Error: Please select or upload the Current Image."
45
 
46
- # 如果用户勾选了 without prior image,就把 prior_img_data 设为 current_img_data
47
  if use_no_prior:
48
  prior_img_data = current_img_data
49
  else:
50
- # 未勾选时,需要prior_img_data
51
  if not prior_img_data:
52
  return "Error: Please select or upload the Prior Image, or check 'Without Prior Image'."
53
 
54
- # 若已经加载过该模型,则直接复用
55
  if selected_model_name in loaded_models:
56
  reuse_model = loaded_models[selected_model_name]
57
  else:
58
  reuse_model = load_model(real_model_path)
59
- # 缓存起来
60
  loaded_models[selected_model_name] = reuse_model
61
 
62
  try:
@@ -77,23 +69,21 @@ def generate_radiology_description(
77
  return f"An error occurred during model inference: {str(e)}"
78
 
79
  def main():
80
- # ========== 获取当前脚本 (app.py) 所在目录 ==========
81
  cur_dir = os.path.abspath(os.path.dirname(__file__))
82
 
83
- # ========== 准备本地示例图片的绝对路径 ==========
84
- # 向上回退两级: app.py -> serve/ -> libra/ -> Libra/ (同级)
85
  example_curent_path = os.path.join(cur_dir, "examples", "curent.jpg")
86
  example_curent_path = os.path.abspath(example_curent_path)
87
 
88
  example_prior_path = os.path.join(cur_dir, "examples", "prior.jpg")
89
  example_prior_path = os.path.abspath(example_prior_path)
90
 
91
- # Gradio Examples 要求:对单个 gr.Image 而言,每个示例写成 ["本地文件路径"]
92
  IMAGE_EXAMPLES = [
93
  [example_curent_path],
94
  [example_prior_path]
95
  ]
96
- # ========== 命令行解析 (可选) ==========
97
  parser = argparse.ArgumentParser(description="Demo for Radiology Image Description Generator (Local Examples)")
98
  parser.add_argument(
99
  "--model-path",
@@ -104,21 +94,19 @@ def main():
104
  args = parser.parse_args()
105
  cmd_model_path = args.model_path
106
 
107
- # ========== 设置多模型下拉菜单 ==========
108
  model_paths_dict = {}
109
  user_key = get_model_short_name(cmd_model_path)
110
  model_paths_dict[user_key] = cmd_model_path
111
 
112
- # 如果用户传入的模型 != 默认模型,则加上默认模型选项
113
  if cmd_model_path != DEFAULT_MODEL_PATH:
114
  default_key = get_model_short_name(DEFAULT_MODEL_PATH)
115
  model_paths_dict[default_key] = DEFAULT_MODEL_PATH
116
 
117
- # (可选)若想预先加载模型,避免重复加载,可在此处:
118
- # reuse_model = load_model(cmd_model_path)
119
- # 然后在 generate_radiology_description 里改造传 reuse_model
120
 
121
- # ========== 搭建 Gradio 界面 ==========
122
  with gr.Blocks(title="Libra: Radiology Analysis with Direct URL Examples") as demo:
123
  gr.Markdown("""
124
  ## 🩻 Libra: Leveraging Temporal Images for Biomedical Radiology Analysis
@@ -127,7 +115,7 @@ def main():
127
  **Requires a GPU to run effectively!**
128
  """)
129
 
130
- # 下拉模型选择
131
  model_dropdown = gr.Dropdown(
132
  label="Select Model",
133
  choices=list(model_paths_dict.keys()),
@@ -135,7 +123,7 @@ def main():
135
  interactive=True
136
  )
137
 
138
- # 临床Prompt
139
  prompt_input = gr.Textbox(
140
  label="Clinical Prompt",
141
  value="Provide a detailed description of the findings in the radiology image.",
@@ -147,7 +135,7 @@ def main():
147
  )
148
  )
149
 
150
- # Current & Prior 画像
151
  with gr.Row():
152
  with gr.Column():
153
  gr.Markdown("### Current Image")
@@ -235,7 +223,7 @@ def main():
235
  outputs=output_text
236
  )
237
 
238
- # 界面底部插入条款说明
239
  gr.Markdown("""
240
  ### Terms of Use
241
 
 
10
  DEFAULT_MODEL_PATH = "X-iZhang/libra-v1.0-7b"
11
 
12
  def get_model_short_name(model_path: str) -> str:
13
+
 
 
 
14
  return model_path.rstrip("/").split("/")[-1]
15
 
16
+
17
  loaded_models = {} # {model_key: reuse_model_object}
18
 
19
  def generate_radiology_description(
 
28
  max_new_tokens: int,
29
  model_paths_dict: dict
30
  ) -> str:
31
+
 
 
 
 
 
32
  real_model_path = model_paths_dict[selected_model_name]
33
 
34
+
35
  if not current_img_data:
36
  return "Error: Please select or upload the Current Image."
37
 
38
+
39
  if use_no_prior:
40
  prior_img_data = current_img_data
41
  else:
42
+
43
  if not prior_img_data:
44
  return "Error: Please select or upload the Prior Image, or check 'Without Prior Image'."
45
 
46
+
47
  if selected_model_name in loaded_models:
48
  reuse_model = loaded_models[selected_model_name]
49
  else:
50
  reuse_model = load_model(real_model_path)
51
+
52
  loaded_models[selected_model_name] = reuse_model
53
 
54
  try:
 
69
  return f"An error occurred during model inference: {str(e)}"
70
 
71
  def main():
72
+
73
  cur_dir = os.path.abspath(os.path.dirname(__file__))
74
 
 
 
75
  example_curent_path = os.path.join(cur_dir, "examples", "curent.jpg")
76
  example_curent_path = os.path.abspath(example_curent_path)
77
 
78
  example_prior_path = os.path.join(cur_dir, "examples", "prior.jpg")
79
  example_prior_path = os.path.abspath(example_prior_path)
80
 
81
+
82
  IMAGE_EXAMPLES = [
83
  [example_curent_path],
84
  [example_prior_path]
85
  ]
86
+
87
  parser = argparse.ArgumentParser(description="Demo for Radiology Image Description Generator (Local Examples)")
88
  parser.add_argument(
89
  "--model-path",
 
94
  args = parser.parse_args()
95
  cmd_model_path = args.model_path
96
 
97
+
98
  model_paths_dict = {}
99
  user_key = get_model_short_name(cmd_model_path)
100
  model_paths_dict[user_key] = cmd_model_path
101
 
102
+
103
  if cmd_model_path != DEFAULT_MODEL_PATH:
104
  default_key = get_model_short_name(DEFAULT_MODEL_PATH)
105
  model_paths_dict[default_key] = DEFAULT_MODEL_PATH
106
 
107
+
 
 
108
 
109
+
110
  with gr.Blocks(title="Libra: Radiology Analysis with Direct URL Examples") as demo:
111
  gr.Markdown("""
112
  ## 🩻 Libra: Leveraging Temporal Images for Biomedical Radiology Analysis
 
115
  **Requires a GPU to run effectively!**
116
  """)
117
 
118
+
119
  model_dropdown = gr.Dropdown(
120
  label="Select Model",
121
  choices=list(model_paths_dict.keys()),
 
123
  interactive=True
124
  )
125
 
126
+
127
  prompt_input = gr.Textbox(
128
  label="Clinical Prompt",
129
  value="Provide a detailed description of the findings in the radiology image.",
 
135
  )
136
  )
137
 
138
+
139
  with gr.Row():
140
  with gr.Column():
141
  gr.Markdown("### Current Image")
 
223
  outputs=output_text
224
  )
225
 
226
+
227
  gr.Markdown("""
228
  ### Terms of Use
229