Spaces:
Runtime error
Runtime error
import gradio as gr | |
import torch | |
from transformers import Qwen2VLForConditionalGeneration, AutoProcessor | |
from qwen_vl_utils import process_vision_info | |
from PIL import Image | |
import numpy as np | |
from datetime import datetime | |
import os | |
def array_to_image_path(image_array): | |
if image_array is None: | |
raise ValueError("No image provided. Please upload an image before submitting.") | |
img = Image.fromarray(np.uint8(image_array)) | |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") | |
filename = f"image_{timestamp}.png" | |
img.save(filename) | |
return os.path.abspath(filename) | |
model = Qwen2VLForConditionalGeneration.from_pretrained( | |
"Qwen/Qwen2-VL-7B-Instruct", trust_remote_code=True, torch_dtype="auto" | |
).cuda().eval() | |
processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-7B-Instruct", trust_remote_code=True) | |
def analyze_iol_report(image): | |
image_path = array_to_image_path(image) | |
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." | |
messages = [ | |
{ | |
"role": "user", | |
"content": [ | |
{"type": "image", "image": image_path}, | |
{"type": "text", "text": prompt}, | |
], | |
} | |
] | |
text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) | |
image_inputs, video_inputs = process_vision_info(messages) | |
inputs = processor( | |
text=[text], | |
images=image_inputs, | |
videos=video_inputs, | |
padding=True, | |
return_tensors="pt", | |
) | |
inputs = inputs.to("cuda") | |
generated_ids = model.generate(**inputs, max_new_tokens=1024) | |
generated_ids_trimmed = [ | |
out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids) | |
] | |
output_text = processor.batch_decode( | |
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False | |
) | |
return output_text[0] | |
css = """ | |
#output { | |
height: 500px; | |
overflow: auto; | |
border: 1px solid #ccc; | |
} | |
""" | |
with gr.Blocks(css=css) as demo: | |
gr.Markdown("Fixed Model - Qwen2-VL-7B for IOL Report Analysis") | |
with gr.Tab(label="IOL Analysis"): | |
with gr.Row(): | |
input_img = gr.Image(label="Upload IOL Report Image") | |
submit_btn = gr.Button(value="Analyze Report") | |
output = gr.Textbox(label="Analysis Result", elem_id="output") | |
submit_btn.click(analyze_iol_report, inputs=[input_img], outputs=[output]) | |
demo.launch() |