mlbench123 commited on
Commit
57e986c
·
verified ·
1 Parent(s): 9920573

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -22
app.py CHANGED
@@ -5,6 +5,7 @@ import json
5
  from pathlib import Path
6
  import gradio as gr
7
  from openai import OpenAI
 
8
 
9
  API_KEY = "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"
10
  MODEL = "gpt-5.1"
@@ -76,12 +77,12 @@ def prompt():
76
  )
77
 
78
 
 
 
 
 
79
 
80
- def extract_image(img):
81
- """Process image input"""
82
- ext = "png" # always PNG internally from Gradio
83
-
84
- b64 = base64.b64encode(img).decode()
85
 
86
  content = [
87
  {"type": "text", "text": prompt()},
@@ -96,14 +97,12 @@ def extract_image(img):
96
  messages=[{"role": "user", "content": content}]
97
  )
98
 
99
- text = r.choices[0].message.content
100
- s = text.find("{")
101
- e = text.rfind("}")
102
- return text[s:e+1]
103
 
104
 
105
  def extract_pdf(file):
106
- """Process PDF input"""
107
  path = Path(file.name)
108
  fid = upload_pdf(path)
109
 
@@ -117,18 +116,16 @@ def extract_pdf(file):
117
  messages=[{"role": "user", "content": content}]
118
  )
119
 
120
- text = r.choices[0].message.content
121
- s = text.find("{")
122
- e = text.rfind("}")
123
- return text[s:e+1]
124
 
125
 
126
- def process(image_input, pdf_input):
127
- if image_input is not None:
128
- return extract_image(image_input)
129
 
130
- if pdf_input is not None:
131
- return extract_pdf(pdf_input)
132
 
133
  return "{}"
134
 
@@ -137,11 +134,10 @@ with gr.Blocks() as demo:
137
  gr.Markdown("# **Logistics OCR Data Extractor (GPT-5.1)**")
138
 
139
  with gr.Row():
140
- image_input = gr.Image(type="bytes", label="Upload Image")
141
  pdf_input = gr.File(type="file", label="Upload PDF")
142
 
143
  output = gr.JSON(label="Extracted JSON")
144
-
145
  submit = gr.Button("Submit")
146
 
147
  submit.click(
@@ -156,7 +152,7 @@ with gr.Blocks() as demo:
156
  ["IMG_0002.jpg", None]
157
  ],
158
  inputs=[image_input, pdf_input],
159
- label="Sample Images",
160
  )
161
 
162
  demo.launch(share=True)
 
5
  from pathlib import Path
6
  import gradio as gr
7
  from openai import OpenAI
8
+ from PIL import Image
9
 
10
  API_KEY = "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"
11
  MODEL = "gpt-5.1"
 
77
  )
78
 
79
 
80
+ def extract_image(path):
81
+ """Process image via filepath"""
82
+ img_bytes = Path(path).read_bytes()
83
+ ext = Path(path).suffix.replace(".", "").lower()
84
 
85
+ b64 = base64.b64encode(img_bytes).decode()
 
 
 
 
86
 
87
  content = [
88
  {"type": "text", "text": prompt()},
 
97
  messages=[{"role": "user", "content": content}]
98
  )
99
 
100
+ t = r.choices[0].message.content
101
+ return t[t.find("{"): t.rfind("}") + 1]
 
 
102
 
103
 
104
  def extract_pdf(file):
105
+ """Process PDF"""
106
  path = Path(file.name)
107
  fid = upload_pdf(path)
108
 
 
116
  messages=[{"role": "user", "content": content}]
117
  )
118
 
119
+ t = r.choices[0].message.content
120
+ return t[t.find("{"): t.rfind("}") + 1]
 
 
121
 
122
 
123
+ def process(image_path, pdf_file):
124
+ if image_path:
125
+ return extract_image(image_path)
126
 
127
+ if pdf_file:
128
+ return extract_pdf(pdf_file)
129
 
130
  return "{}"
131
 
 
134
  gr.Markdown("# **Logistics OCR Data Extractor (GPT-5.1)**")
135
 
136
  with gr.Row():
137
+ image_input = gr.Image(type="filepath", label="Upload Image")
138
  pdf_input = gr.File(type="file", label="Upload PDF")
139
 
140
  output = gr.JSON(label="Extracted JSON")
 
141
  submit = gr.Button("Submit")
142
 
143
  submit.click(
 
152
  ["IMG_0002.jpg", None]
153
  ],
154
  inputs=[image_input, pdf_input],
155
+ label="Sample Images"
156
  )
157
 
158
  demo.launch(share=True)