luigi12345 commited on
Commit
a0f004d
β€’
1 Parent(s): 0371a98

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -71
app.py CHANGED
@@ -1,84 +1,43 @@
1
  import gradio as gr
2
- import spaces
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
- kwargs = {}
49
- kwargs['torch_dtype'] = torch.bfloat16
 
50
 
51
- user_prompt = '<|user|>\n'
52
- assistant_prompt = '<|assistant|>\n'
53
- prompt_suffix = "<|end|>\n"
54
 
55
- @spaces.GPU
56
- def run_example(image, text_input=None, model_id="Qwen/Qwen2-VL-7B-Instruct"):
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
- "type": "image",
71
- "image": image_path,
72
- },
73
- {"type": "text", "text": text_input},
74
  ],
75
  }
76
  ]
77
 
78
- # Preparation for inference
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) :] for in_ids, out_ids in zip(inputs.input_ids, generated_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(DESCRIPTION)
113
- with gr.Tab(label="IOL Analyzer"):
114
  with gr.Row():
115
- with gr.Column():
116
- input_img = gr.Image(label="Input Picture")
117
- model_selector = "Qwen/Qwen2-VL-7B-Instruct"
118
- text_input = "Please analyze the uploaded IOL report image. Extract all available ophthalmic measurements, including Axial Length (AL), Keratometry Readings (K1, K2), Anterior Chamber Depth (ACD), Lens Thickness (LT), White-to-White Distance (WTW), and Central Corneal Thickness (CCT). Using these measurements, provide IOL power calculations based on Barrett Universal II, Cooke K6, EVO, Hill-RBF, Hoffer QST, Kane, PEARL GDS, and any other relevant formulas. Summarize all calculated values with any additional observations relevant to IOL selection."
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.queue(api_open=False)
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()