Update app.py
Browse files
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 |
-
|
| 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 |
-
|
| 43 |
if not current_img_data:
|
| 44 |
return "Error: Please select or upload the Current Image."
|
| 45 |
|
| 46 |
-
|
| 47 |
if use_no_prior:
|
| 48 |
prior_img_data = current_img_data
|
| 49 |
else:
|
| 50 |
-
|
| 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 |
-
|
| 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 |
-
|
| 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 |
-
|
| 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 |
-
|
| 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 |
-
|
| 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 |
|