Skip to content

Commit 3686af7

Browse files
committed
Demo compatible with responseDocument - Adithya S K
1 parent eb0d194 commit 3686af7

File tree

1 file changed

+33
-18
lines changed

1 file changed

+33
-18
lines changed

omniparse/demo.py

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@
99

1010
single_task_list = [
1111
'Caption', 'Detailed Caption', 'More Detailed Caption',
12-
'OCR', 'OCR with Region',
13-
'Object Detection',
14-
'Dense Region Caption', 'Region Proposal', 'Caption to Phrase Grounding',
15-
'Referring Expression Segmentation', 'Region to Segmentation',
16-
'Open Vocabulary Detection', 'Region to Category', 'Region to Description',
12+
'OCR', 'OCR with Region'
1713
]
14+
# single_task_list = [
15+
# 'Caption', 'Detailed Caption', 'More Detailed Caption',
16+
# 'OCR', 'OCR with Region',
17+
# 'Object Detection',
18+
# 'Dense Region Caption', 'Region Proposal', 'Caption to Phrase Grounding',
19+
# 'Referring Expression Segmentation', 'Region to Segmentation',
20+
# 'Open Vocabulary Detection', 'Region to Category', 'Region to Description',
21+
# ]
1822

1923
header_markdown = """
2024
@@ -181,11 +185,12 @@ def parse_document(input_file_path, parameters, request: gr.Request):
181185

182186
document_response = response.json()
183187

184-
# print(document_response)
185-
images = document_response["images"]
186-
pil_images = [decode_base64_to_pil(base64_str) for base64_str in images.values()]
188+
images = document_response.get('images', [])
189+
190+
# Decode each base64-encoded image to a PIL image
191+
pil_images = [decode_base64_to_pil(image_dict['image']) for image_dict in images]
187192

188-
return str(document_response["markdown"]) , gr.Gallery(value=pil_images , visible=True) , str(document_response["markdown"]) , gr.JSON(value=document_response , visible=True)
193+
return str(document_response["text"]) , gr.Gallery(value=pil_images , visible=True) , str(document_response["text"]) , gr.JSON(value=document_response , visible=True)
189194

190195

191196
except Exception as e:
@@ -235,14 +240,17 @@ def process_image(input_file_path, parameters, request: gr.Request):
235240

236241

237242
image_process_response = response.json()
238-
print(image_process_response)
243+
244+
images = image_process_response.get('images', [])
245+
# Decode each base64-encoded image to a PIL image
246+
pil_images = [decode_base64_to_pil(image_dict['image']) for image_dict in images]
239247

240248
# Decode the image if present in the response
241249
# images = document_response.get('image', {})
242250
# pil_images = [decode_base64_to_pil(base64_str) for base64_str in images.values()]
243251

244-
return (gr.update(value=image_process_response["results"]),
245-
gr.Gallery(visible=False),
252+
return (gr.update(value=image_process_response["text"]),
253+
gr.Gallery(value=pil_images, visible=(len(images) != 0)),
246254
gr.JSON(value=image_process_response, visible=True))
247255

248256
except Exception as e:
@@ -283,12 +291,14 @@ def parse_image(input_file_path, parameters, request: gr.Request):
283291
document_response = response.json()
284292

285293
# Decode the image if present in the response
286-
images = document_response.get('image', {})
287-
pil_images = [decode_base64_to_pil(base64_str) for base64_str in images.values()]
294+
images = document_response.get('images', [])
295+
296+
# Decode each base64-encoded image to a PIL image
297+
pil_images = [decode_base64_to_pil(image_dict['image']) for image_dict in images]
288298

289-
return (gr.update(value=document_response["markdown"]),
299+
return (gr.update(value=document_response["text"]),
290300
gr.Gallery(value=pil_images, visible=True),
291-
gr.update(value=document_response["markdown"]),
301+
gr.update(value=document_response["text"]),
292302
gr.update(value=document_response, visible=True))
293303

294304
except Exception as e:
@@ -374,10 +384,15 @@ def parse_website(url, request: gr.Request):
374384
base64_image = result.get("screenshot", "")
375385

376386
screenshot = [decode_base64_to_pil(base64_image)] if base64_image else []
387+
388+
images = website_response.get('images', [])
389+
390+
# Decode each base64-encoded image to a PIL image
391+
pil_images = [decode_base64_to_pil(image_dict['image']) for image_dict in images]
377392

378393
return (gr.update(value=markdown, visible=True),
379394
gr.update(value=html, visible=True),
380-
gr.update(value=screenshot, visible=bool(screenshot)),
395+
gr.update(value=pil_images, visible=bool(screenshot)),
381396
gr.JSON(value=website_response , visible=True))
382397

383398
except requests.RequestException as e:
@@ -422,7 +437,7 @@ def parse_website(url, request: gr.Request):
422437
image_process_button = gr.Button("Process Image")
423438
with gr.Column(scale=200):
424439
image_process_output_text = gr.Textbox(label="Output Text")
425-
image_process_output_image = gr.Image(label="Output Image ⌛" , interactive=False)
440+
image_process_output_image = gr.Gallery(label="Output Image ⌛" , interactive=False)
426441
with gr.Accordion("JSON Output"):
427442
image_process_json = gr.JSON(label="Output JSON", visible=False)
428443
with gr.Accordion("Use API", open=True):

0 commit comments

Comments
 (0)