awacke1 commited on
Commit
d0e6378
·
verified ·
1 Parent(s): 415515c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -13
app.py CHANGED
@@ -1,6 +1,7 @@
1
  #!/usr/bin/env python3
2
  import os
3
- import glob # Added missing import
 
4
  import streamlit as st
5
  from PIL import Image
6
  import torch
@@ -163,49 +164,57 @@ with tab2:
163
  st.header("Test OCR 🔍")
164
  captured_images = get_gallery_files(["png"])
165
  if captured_images:
166
- selected_image = st.selectbox("Select Image", captured_images)
167
  image = Image.open(selected_image)
168
  st.image(image, caption="Input Image", use_container_width=True)
169
- ocr_model = st.selectbox("Select OCR Model", ["Qwen2-VL-OCR-2B", "GOT-OCR2_0"])
170
- prompt = st.text_area("Prompt", "Extract text from the image")
171
- if st.button("Run OCR 🚀"):
172
  if ocr_model == "Qwen2-VL-OCR-2B":
173
  processor, model = load_ocr_qwen2vl()
174
- inputs = processor(text=[prompt], images=[image], return_tensors="pt").to("cpu")
 
 
175
  outputs = model.generate(**inputs, max_new_tokens=1024)
176
- text = processor.decode(outputs[0], skip_special_tokens=True)
177
  else: # GOT-OCR2_0
178
  tokenizer, model = load_ocr_got()
179
  with open(selected_image, "rb") as f:
180
  img_bytes = f.read()
181
  img = Image.open(BytesIO(img_bytes))
182
  text = model.chat(tokenizer, img, ocr_type='ocr')
183
- st.text_area("OCR Result", text, height=200)
 
 
184
 
185
  with tab3:
186
  st.header("Test Image Gen 🎨")
187
  captured_images = get_gallery_files(["png"])
188
  if captured_images:
189
- selected_image = st.selectbox("Select Image", captured_images)
190
  image = Image.open(selected_image)
191
  st.image(image, caption="Reference Image", use_container_width=True)
192
- prompt = st.text_area("Prompt", "Generate a similar superhero image")
193
- if st.button("Run Image Gen 🚀"):
194
  pipeline = load_image_gen()
195
  gen_image = pipeline(prompt, num_inference_steps=50).images[0]
196
  st.image(gen_image, caption="Generated Image", use_container_width=True)
 
 
197
 
198
  with tab4:
199
  st.header("Test Line Drawings ✏️")
200
  captured_images = get_gallery_files(["png"])
201
  if captured_images:
202
- selected_image = st.selectbox("Select Image", captured_images)
203
  image = Image.open(selected_image)
204
  st.image(image, caption="Input Image", use_container_width=True)
205
- if st.button("Run Line Drawing 🚀"):
206
  edge_fn = load_line_drawer()
207
  line_drawing = edge_fn(image)
208
  st.image(line_drawing, caption="Line Drawing", use_container_width=True)
 
 
209
 
210
  # Initial Gallery Update
211
  update_gallery()
 
1
  #!/usr/bin/env python3
2
  import os
3
+ import glob
4
+ import time # Added missing import
5
  import streamlit as st
6
  from PIL import Image
7
  import torch
 
164
  st.header("Test OCR 🔍")
165
  captured_images = get_gallery_files(["png"])
166
  if captured_images:
167
+ selected_image = st.selectbox("Select Image", captured_images, key="ocr_select")
168
  image = Image.open(selected_image)
169
  st.image(image, caption="Input Image", use_container_width=True)
170
+ ocr_model = st.selectbox("Select OCR Model", ["Qwen2-VL-OCR-2B", "GOT-OCR2_0"], key="ocr_model_select")
171
+ prompt = st.text_area("Prompt", "Extract text from the image", key="ocr_prompt")
172
+ if st.button("Run OCR 🚀", key="ocr_run"):
173
  if ocr_model == "Qwen2-VL-OCR-2B":
174
  processor, model = load_ocr_qwen2vl()
175
+ # Prepare inputs correctly for Qwen2-VL
176
+ messages = [{"role": "user", "content": [{"type": "image", "image": image}, {"type": "text", "text": prompt}]}]
177
+ inputs = processor(messages, return_tensors="pt").to("cpu")
178
  outputs = model.generate(**inputs, max_new_tokens=1024)
179
+ text = processor.batch_decode(outputs, skip_special_tokens=True)[0]
180
  else: # GOT-OCR2_0
181
  tokenizer, model = load_ocr_got()
182
  with open(selected_image, "rb") as f:
183
  img_bytes = f.read()
184
  img = Image.open(BytesIO(img_bytes))
185
  text = model.chat(tokenizer, img, ocr_type='ocr')
186
+ st.text_area("OCR Result", text, height=200, key="ocr_result")
187
+ else:
188
+ st.warning("No images captured yet. Use Camera Snap first!")
189
 
190
  with tab3:
191
  st.header("Test Image Gen 🎨")
192
  captured_images = get_gallery_files(["png"])
193
  if captured_images:
194
+ selected_image = st.selectbox("Select Image", captured_images, key="gen_select")
195
  image = Image.open(selected_image)
196
  st.image(image, caption="Reference Image", use_container_width=True)
197
+ prompt = st.text_area("Prompt", "Generate a similar superhero image", key="gen_prompt")
198
+ if st.button("Run Image Gen 🚀", key="gen_run"):
199
  pipeline = load_image_gen()
200
  gen_image = pipeline(prompt, num_inference_steps=50).images[0]
201
  st.image(gen_image, caption="Generated Image", use_container_width=True)
202
+ else:
203
+ st.warning("No images captured yet. Use Camera Snap first!")
204
 
205
  with tab4:
206
  st.header("Test Line Drawings ✏️")
207
  captured_images = get_gallery_files(["png"])
208
  if captured_images:
209
+ selected_image = st.selectbox("Select Image", captured_images, key="line_select")
210
  image = Image.open(selected_image)
211
  st.image(image, caption="Input Image", use_container_width=True)
212
+ if st.button("Run Line Drawing 🚀", key="line_run"):
213
  edge_fn = load_line_drawer()
214
  line_drawing = edge_fn(image)
215
  st.image(line_drawing, caption="Line Drawing", use_container_width=True)
216
+ else:
217
+ st.warning("No images captured yet. Use Camera Snap first!")
218
 
219
  # Initial Gallery Update
220
  update_gallery()