Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -65,11 +65,12 @@ def process_single_page(pdf_path, page_number):
|
|
| 65 |
|
| 66 |
def process_input(file_input, temperature, page_number):
|
| 67 |
if file_input is None:
|
| 68 |
-
yield "Please upload an image or PDF first.", "", ""
|
| 69 |
return
|
| 70 |
|
| 71 |
images_to_process = []
|
| 72 |
page_info = ""
|
|
|
|
| 73 |
|
| 74 |
file_path = file_input if isinstance(file_input, str) else file_input.name
|
| 75 |
|
|
@@ -78,30 +79,34 @@ def process_input(file_input, temperature, page_number):
|
|
| 78 |
if page_number > 0:
|
| 79 |
img, total_pages = process_single_page(file_path, page_number)
|
| 80 |
if img is None:
|
| 81 |
-
yield f"Error: Page {page_number} does not exist. PDF has {total_pages} pages.", "", ""
|
| 82 |
return
|
| 83 |
images_to_process = [img]
|
|
|
|
| 84 |
page_info = f"Processing page {page_number} of {total_pages}"
|
| 85 |
else:
|
| 86 |
-
|
| 87 |
-
if
|
| 88 |
-
yield "Error: Could not
|
| 89 |
return
|
| 90 |
-
|
|
|
|
|
|
|
| 91 |
except Exception as e:
|
| 92 |
-
yield f"Error processing PDF: {str(e)}", "", ""
|
| 93 |
return
|
| 94 |
else:
|
| 95 |
try:
|
| 96 |
img = Image.open(file_path)
|
| 97 |
images_to_process = [img]
|
|
|
|
| 98 |
except Exception as e:
|
| 99 |
-
yield f"Error opening image: {str(e)}", "", ""
|
| 100 |
return
|
| 101 |
|
| 102 |
for img in images_to_process:
|
| 103 |
if not isinstance(img, Image.Image):
|
| 104 |
-
yield "Error: Invalid image format.", "", ""
|
| 105 |
return
|
| 106 |
|
| 107 |
content = [{"type": "text", "text": ""}]
|
|
@@ -114,7 +119,7 @@ def process_input(file_input, temperature, page_number):
|
|
| 114 |
"image_url": {"url": f"data:image/png;base64,{b64_image}"}
|
| 115 |
})
|
| 116 |
except Exception as e:
|
| 117 |
-
yield f"Error encoding image: {str(e)}", "", ""
|
| 118 |
return
|
| 119 |
|
| 120 |
payload = {
|
|
@@ -156,13 +161,13 @@ def process_input(file_input, temperature, page_number):
|
|
| 156 |
content_delta = delta.get('content', '')
|
| 157 |
if content_delta:
|
| 158 |
accumulated_response += content_delta
|
| 159 |
-
yield accumulated_response, accumulated_response, page_info
|
| 160 |
except json.JSONDecodeError:
|
| 161 |
continue
|
| 162 |
|
| 163 |
except Exception as e:
|
| 164 |
error_msg = f"Error: {str(e)}"
|
| 165 |
-
yield error_msg, error_msg, page_info
|
| 166 |
|
| 167 |
|
| 168 |
with gr.Blocks(title="π Image/PDF OCR", theme=gr.themes.Soft()) as demo:
|
|
@@ -185,8 +190,14 @@ with gr.Blocks(title="π Image/PDF OCR", theme=gr.themes.Soft()) as demo:
|
|
| 185 |
file_types=[".pdf", ".png", ".jpg", ".jpeg"],
|
| 186 |
type="filepath"
|
| 187 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
page_number = gr.Number(
|
| 189 |
-
label="PDF: Page Number (0 = first
|
| 190 |
value=0,
|
| 191 |
minimum=0,
|
| 192 |
step=1,
|
|
@@ -197,7 +208,7 @@ with gr.Blocks(title="π Image/PDF OCR", theme=gr.themes.Soft()) as demo:
|
|
| 197 |
value="",
|
| 198 |
interactive=False
|
| 199 |
)
|
| 200 |
-
gr.Markdown("*Upload an image (PNG/JPG) or PDF. For PDF: 0 =
|
| 201 |
temperature = gr.Slider(
|
| 202 |
minimum=0.1,
|
| 203 |
maximum=1.0,
|
|
@@ -228,12 +239,12 @@ with gr.Blocks(title="π Image/PDF OCR", theme=gr.themes.Soft()) as demo:
|
|
| 228 |
submit_btn.click(
|
| 229 |
fn=process_input,
|
| 230 |
inputs=[file_input, temperature, page_number],
|
| 231 |
-
outputs=[output_text, raw_output, page_info]
|
| 232 |
)
|
| 233 |
|
| 234 |
clear_btn.click(
|
| 235 |
-
fn=lambda: (None, "", "", 0, ""),
|
| 236 |
-
outputs=[file_input, output_text, raw_output, page_number, page_info]
|
| 237 |
)
|
| 238 |
|
| 239 |
|
|
|
|
| 65 |
|
| 66 |
def process_input(file_input, temperature, page_number):
|
| 67 |
if file_input is None:
|
| 68 |
+
yield "Please upload an image or PDF first.", "", "", None
|
| 69 |
return
|
| 70 |
|
| 71 |
images_to_process = []
|
| 72 |
page_info = ""
|
| 73 |
+
display_image = None
|
| 74 |
|
| 75 |
file_path = file_input if isinstance(file_input, str) else file_input.name
|
| 76 |
|
|
|
|
| 79 |
if page_number > 0:
|
| 80 |
img, total_pages = process_single_page(file_path, page_number)
|
| 81 |
if img is None:
|
| 82 |
+
yield f"Error: Page {page_number} does not exist. PDF has {total_pages} pages.", "", "", None
|
| 83 |
return
|
| 84 |
images_to_process = [img]
|
| 85 |
+
display_image = img
|
| 86 |
page_info = f"Processing page {page_number} of {total_pages}"
|
| 87 |
else:
|
| 88 |
+
img, total_pages = process_single_page(file_path, 1)
|
| 89 |
+
if img is None:
|
| 90 |
+
yield f"Error: Could not read PDF.", "", "", None
|
| 91 |
return
|
| 92 |
+
images_to_process = [img]
|
| 93 |
+
display_image = img
|
| 94 |
+
page_info = f"Processing page 1 of {total_pages}"
|
| 95 |
except Exception as e:
|
| 96 |
+
yield f"Error processing PDF: {str(e)}", "", "", None
|
| 97 |
return
|
| 98 |
else:
|
| 99 |
try:
|
| 100 |
img = Image.open(file_path)
|
| 101 |
images_to_process = [img]
|
| 102 |
+
display_image = img
|
| 103 |
except Exception as e:
|
| 104 |
+
yield f"Error opening image: {str(e)}", "", "", None
|
| 105 |
return
|
| 106 |
|
| 107 |
for img in images_to_process:
|
| 108 |
if not isinstance(img, Image.Image):
|
| 109 |
+
yield "Error: Invalid image format.", "", "", None
|
| 110 |
return
|
| 111 |
|
| 112 |
content = [{"type": "text", "text": ""}]
|
|
|
|
| 119 |
"image_url": {"url": f"data:image/png;base64,{b64_image}"}
|
| 120 |
})
|
| 121 |
except Exception as e:
|
| 122 |
+
yield f"Error encoding image: {str(e)}", "", "", display_image
|
| 123 |
return
|
| 124 |
|
| 125 |
payload = {
|
|
|
|
| 161 |
content_delta = delta.get('content', '')
|
| 162 |
if content_delta:
|
| 163 |
accumulated_response += content_delta
|
| 164 |
+
yield accumulated_response, accumulated_response, page_info, display_image
|
| 165 |
except json.JSONDecodeError:
|
| 166 |
continue
|
| 167 |
|
| 168 |
except Exception as e:
|
| 169 |
error_msg = f"Error: {str(e)}"
|
| 170 |
+
yield error_msg, error_msg, page_info, display_image
|
| 171 |
|
| 172 |
|
| 173 |
with gr.Blocks(title="π Image/PDF OCR", theme=gr.themes.Soft()) as demo:
|
|
|
|
| 190 |
file_types=[".pdf", ".png", ".jpg", ".jpeg"],
|
| 191 |
type="filepath"
|
| 192 |
)
|
| 193 |
+
rendered_image = gr.Image(
|
| 194 |
+
label="π Current Page/Image",
|
| 195 |
+
type="pil",
|
| 196 |
+
height=400,
|
| 197 |
+
interactive=False
|
| 198 |
+
)
|
| 199 |
page_number = gr.Number(
|
| 200 |
+
label="PDF: Page Number (0 = auto first page, or specify 1, 2, 3...)",
|
| 201 |
value=0,
|
| 202 |
minimum=0,
|
| 203 |
step=1,
|
|
|
|
| 208 |
value="",
|
| 209 |
interactive=False
|
| 210 |
)
|
| 211 |
+
gr.Markdown("*Upload an image (PNG/JPG) or PDF. For PDF: 0 = page 1 automatically, or specify any page number*")
|
| 212 |
temperature = gr.Slider(
|
| 213 |
minimum=0.1,
|
| 214 |
maximum=1.0,
|
|
|
|
| 239 |
submit_btn.click(
|
| 240 |
fn=process_input,
|
| 241 |
inputs=[file_input, temperature, page_number],
|
| 242 |
+
outputs=[output_text, raw_output, page_info, rendered_image]
|
| 243 |
)
|
| 244 |
|
| 245 |
clear_btn.click(
|
| 246 |
+
fn=lambda: (None, "", "", 0, "", None),
|
| 247 |
+
outputs=[file_input, output_text, raw_output, page_number, page_info, rendered_image]
|
| 248 |
)
|
| 249 |
|
| 250 |
|