ddxorg commited on
Commit
7c37ae2
·
1 Parent(s): f99b3ea

add preview

Browse files
Files changed (2) hide show
  1. .gradio/flagged/dataset1.csv +2 -0
  2. app.py +7 -12
.gradio/flagged/dataset1.csv ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ Input Type,Document or Image URL,Extracted Plain Text,Preview,timestamp
2
+ URL,,,[],2025-04-23 12:25:00.449275
app.py CHANGED
@@ -4,6 +4,7 @@ from io import BytesIO
4
  import gradio as gr
5
  from mistralai import Mistral, TextChunk, ImageURLChunk
6
  from PIL import Image
 
7
 
8
 
9
  def process_msg(client, target):
@@ -15,14 +16,6 @@ def process_msg(client, target):
15
  )
16
  processed_output = image_response.pages[0].markdown
17
 
18
- images=[]
19
- base64_str = image_response.pages[0].images[0].image_base64
20
- if "," in base64_str:
21
- base64_str = base64_str.split(",")[1]
22
- img_bytes = base64.b64decode(base64_str)
23
- img_pil = Image.open(BytesIO(img_bytes))
24
- images.append(img_pil)
25
-
26
  messages = [
27
  {
28
  "role": "user",
@@ -39,7 +32,7 @@ def process_msg(client, target):
39
  ]
40
  }
41
  ]
42
- return messages, images
43
 
44
  def chat_response(client, model, messages):
45
  response = client.chat.complete(
@@ -73,8 +66,10 @@ def do_ocr(input_type, url=None):
73
  #ocr = PaddleOCR(use_angle_cls=True, lang="en",use_gpu=False)
74
  #ocr_response = ocr.ocr(document_source[document_source["type"]], cls=True)[0]
75
 
76
- message, images = process_msg(client, document_source[document_source["type"]])
77
  response = chat_response(client, model, message)
 
 
78
  return response.choices[0].message.content, images
79
 
80
  custom_css = """
@@ -114,7 +109,7 @@ with gr.Blocks(
114
  img_example = gr.Button("Image", elem_classes="example-button")
115
  with gr.Column(scale=2):
116
  cleaned_output = gr.Textbox(label="Extracted Plain Text", lines=10, show_copy_button=True)
117
- image_output = gr.Gallery(label="OCR Extracted Images", columns=10, height="auto")
118
 
119
  def update_visibility(choice):
120
  return gr.update(visible=(choice == "URL")), gr.update(visible=(choice == "Upload file"))
@@ -132,7 +127,7 @@ with gr.Blocks(
132
  submit_btn.click(
133
  fn=do_ocr,
134
  inputs=[input_type, url_input],
135
- outputs=[cleaned_output, url_input]
136
  )
137
 
138
  if __name__ == "__main__":
 
4
  import gradio as gr
5
  from mistralai import Mistral, TextChunk, ImageURLChunk
6
  from PIL import Image
7
+ import requests
8
 
9
 
10
  def process_msg(client, target):
 
16
  )
17
  processed_output = image_response.pages[0].markdown
18
 
 
 
 
 
 
 
 
 
19
  messages = [
20
  {
21
  "role": "user",
 
32
  ]
33
  }
34
  ]
35
+ return messages
36
 
37
  def chat_response(client, model, messages):
38
  response = client.chat.complete(
 
66
  #ocr = PaddleOCR(use_angle_cls=True, lang="en",use_gpu=False)
67
  #ocr_response = ocr.ocr(document_source[document_source["type"]], cls=True)[0]
68
 
69
+ message = process_msg(client, document_source[document_source["type"]])
70
  response = chat_response(client, model, message)
71
+ images=[]
72
+ images.append(Image.open(BytesIO(requests.get(url).content)))
73
  return response.choices[0].message.content, images
74
 
75
  custom_css = """
 
109
  img_example = gr.Button("Image", elem_classes="example-button")
110
  with gr.Column(scale=2):
111
  cleaned_output = gr.Textbox(label="Extracted Plain Text", lines=10, show_copy_button=True)
112
+ image_output = gr.Gallery(label="Preview", height="contain")
113
 
114
  def update_visibility(choice):
115
  return gr.update(visible=(choice == "URL")), gr.update(visible=(choice == "Upload file"))
 
127
  submit_btn.click(
128
  fn=do_ocr,
129
  inputs=[input_type, url_input],
130
+ outputs=[cleaned_output, image_output]
131
  )
132
 
133
  if __name__ == "__main__":