Spaces:
Runtime error
Runtime error
luigi12345
commited on
Commit
β’
a0f004d
1
Parent(s):
0371a98
Update app.py
Browse files
app.py
CHANGED
@@ -1,84 +1,43 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
from transformers import Qwen2VLForConditionalGeneration, AutoProcessor
|
4 |
from qwen_vl_utils import process_vision_info
|
5 |
-
import torch
|
6 |
from PIL import Image
|
7 |
-
import subprocess
|
8 |
-
from datetime import datetime
|
9 |
import numpy as np
|
|
|
10 |
import os
|
11 |
|
12 |
-
|
13 |
-
# subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
14 |
-
|
15 |
-
# models = {
|
16 |
-
# "Qwen/Qwen2-VL-7B-Instruct": AutoModelForCausalLM.from_pretrained("Qwen/Qwen2-VL-7B-Instruct", trust_remote_code=True, torch_dtype="auto", _attn_implementation="flash_attention_2").cuda().eval()
|
17 |
-
|
18 |
-
# }
|
19 |
def array_to_image_path(image_array):
|
20 |
if image_array is None:
|
21 |
raise ValueError("No image provided. Please upload an image before submitting.")
|
22 |
-
# Convert numpy array to PIL Image
|
23 |
img = Image.fromarray(np.uint8(image_array))
|
24 |
-
|
25 |
-
# Generate a unique filename using timestamp
|
26 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
27 |
filename = f"image_{timestamp}.png"
|
28 |
-
|
29 |
-
# Save the image
|
30 |
img.save(filename)
|
31 |
-
|
32 |
-
# Get the full path of the saved image
|
33 |
-
full_path = os.path.abspath(filename)
|
34 |
-
|
35 |
-
return full_path
|
36 |
-
|
37 |
-
models = {
|
38 |
-
"Qwen/Qwen2-VL-7B-Instruct": Qwen2VLForConditionalGeneration.from_pretrained("Qwen/Qwen2-VL-7B-Instruct", trust_remote_code=True, torch_dtype="auto").cuda().eval()
|
39 |
-
|
40 |
-
}
|
41 |
-
|
42 |
-
processors = {
|
43 |
-
"Qwen/Qwen2-VL-7B-Instruct": AutoProcessor.from_pretrained("Qwen/Qwen2-VL-7B-Instruct", trust_remote_code=True)
|
44 |
-
}
|
45 |
-
|
46 |
-
DESCRIPTION = "[Qwen2-VL-7B Demo](https://huggingface.co/Qwen/Qwen2-VL-7B-Instruct)"
|
47 |
|
48 |
-
|
49 |
-
|
|
|
50 |
|
51 |
-
|
52 |
-
assistant_prompt = '<|assistant|>\n'
|
53 |
-
prompt_suffix = "<|end|>\n"
|
54 |
|
55 |
-
@
|
56 |
-
def
|
57 |
image_path = array_to_image_path(image)
|
|
|
58 |
|
59 |
-
print(image_path)
|
60 |
-
model = models[model_id]
|
61 |
-
processor = processors[model_id]
|
62 |
-
|
63 |
-
prompt = f"{user_prompt}<|image_1|>\n{text_input}{prompt_suffix}{assistant_prompt}"
|
64 |
-
image = Image.fromarray(image).convert("RGB")
|
65 |
messages = [
|
66 |
-
|
67 |
"role": "user",
|
68 |
"content": [
|
69 |
-
{
|
70 |
-
|
71 |
-
"image": image_path,
|
72 |
-
},
|
73 |
-
{"type": "text", "text": text_input},
|
74 |
],
|
75 |
}
|
76 |
]
|
77 |
|
78 |
-
|
79 |
-
text = processor.apply_chat_template(
|
80 |
-
messages, tokenize=False, add_generation_prompt=True
|
81 |
-
)
|
82 |
image_inputs, video_inputs = process_vision_info(messages)
|
83 |
inputs = processor(
|
84 |
text=[text],
|
@@ -88,11 +47,9 @@ def run_example(image, text_input=None, model_id="Qwen/Qwen2-VL-7B-Instruct"):
|
|
88 |
return_tensors="pt",
|
89 |
)
|
90 |
inputs = inputs.to("cuda")
|
91 |
-
|
92 |
-
# Inference: Generation of the output
|
93 |
generated_ids = model.generate(**inputs, max_new_tokens=1024)
|
94 |
generated_ids_trimmed = [
|
95 |
-
out_ids[len(in_ids)
|
96 |
]
|
97 |
output_text = processor.batch_decode(
|
98 |
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
@@ -109,18 +66,12 @@ css = """
|
|
109 |
"""
|
110 |
|
111 |
with gr.Blocks(css=css) as demo:
|
112 |
-
gr.Markdown(
|
113 |
-
with gr.Tab(label="IOL
|
114 |
with gr.Row():
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
submit_btn = gr.Button(value="Submit")
|
120 |
-
with gr.Column():
|
121 |
-
output_text = gr.Textbox(label="Output Text")
|
122 |
-
|
123 |
-
submit_btn.click(run_example, [input_img, text_input, model_selector], [output_text])
|
124 |
|
125 |
-
demo.
|
126 |
-
demo.launch(debug=True)
|
|
|
1 |
import gradio as gr
|
2 |
+
import torch
|
3 |
from transformers import Qwen2VLForConditionalGeneration, AutoProcessor
|
4 |
from qwen_vl_utils import process_vision_info
|
|
|
5 |
from PIL import Image
|
|
|
|
|
6 |
import numpy as np
|
7 |
+
from datetime import datetime
|
8 |
import os
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
def array_to_image_path(image_array):
|
11 |
if image_array is None:
|
12 |
raise ValueError("No image provided. Please upload an image before submitting.")
|
|
|
13 |
img = Image.fromarray(np.uint8(image_array))
|
|
|
|
|
14 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
15 |
filename = f"image_{timestamp}.png"
|
|
|
|
|
16 |
img.save(filename)
|
17 |
+
return os.path.abspath(filename)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
model = Qwen2VLForConditionalGeneration.from_pretrained(
|
20 |
+
"Qwen/Qwen2-VL-7B-Instruct", trust_remote_code=True, torch_dtype="auto"
|
21 |
+
).cuda().eval()
|
22 |
|
23 |
+
processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-7B-Instruct", trust_remote_code=True)
|
|
|
|
|
24 |
|
25 |
+
@gr.GPU
|
26 |
+
def analyze_iol_report(image):
|
27 |
image_path = array_to_image_path(image)
|
28 |
+
prompt = "Extract all ophthalmic measurements including AL, K1, K2, ACD, LT, WTW, and CCT. Calculate IOL power based on formulas like Barrett Universal II, Cooke K6, EVO, Hill-RBF, Hoffer QST, Kane, and PEARL GDS."
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
messages = [
|
31 |
+
{
|
32 |
"role": "user",
|
33 |
"content": [
|
34 |
+
{"type": "image", "image": image_path},
|
35 |
+
{"type": "text", "text": prompt},
|
|
|
|
|
|
|
36 |
],
|
37 |
}
|
38 |
]
|
39 |
|
40 |
+
text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
|
|
|
|
|
|
41 |
image_inputs, video_inputs = process_vision_info(messages)
|
42 |
inputs = processor(
|
43 |
text=[text],
|
|
|
47 |
return_tensors="pt",
|
48 |
)
|
49 |
inputs = inputs.to("cuda")
|
|
|
|
|
50 |
generated_ids = model.generate(**inputs, max_new_tokens=1024)
|
51 |
generated_ids_trimmed = [
|
52 |
+
out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
53 |
]
|
54 |
output_text = processor.batch_decode(
|
55 |
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
|
|
66 |
"""
|
67 |
|
68 |
with gr.Blocks(css=css) as demo:
|
69 |
+
gr.Markdown("Fixed Model - Qwen2-VL-7B for IOL Report Analysis")
|
70 |
+
with gr.Tab(label="IOL Analysis"):
|
71 |
with gr.Row():
|
72 |
+
input_img = gr.Image(label="Upload IOL Report Image")
|
73 |
+
submit_btn = gr.Button(value="Analyze Report")
|
74 |
+
output = gr.Textbox(label="Analysis Result", elem_id="output")
|
75 |
+
submit_btn.click(analyze_iol_report, inputs=[input_img], outputs=[output])
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
+
demo.launch()
|
|