Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -46,7 +46,7 @@ def process_pdf(pdf_path, page_num=1):
|
|
| 46 |
|
| 47 |
def process_input(file_input, temperature, page_num):
|
| 48 |
if file_input is None:
|
| 49 |
-
yield "Please upload an image or PDF first.", "", "", None
|
| 50 |
return
|
| 51 |
|
| 52 |
image_to_process = None
|
|
@@ -59,18 +59,18 @@ def process_input(file_input, temperature, page_num):
|
|
| 59 |
image_to_process, total_pages, actual_page = process_pdf(file_path, int(page_num))
|
| 60 |
page_info = f"Processing page {actual_page} of {total_pages}"
|
| 61 |
except Exception as e:
|
| 62 |
-
yield f"Error processing PDF: {str(e)}", "", "", None
|
| 63 |
return
|
| 64 |
else:
|
| 65 |
try:
|
| 66 |
image_to_process = Image.open(file_path)
|
| 67 |
page_info = "Processing image"
|
| 68 |
except Exception as e:
|
| 69 |
-
yield f"Error opening image: {str(e)}", "", "", None
|
| 70 |
return
|
| 71 |
|
| 72 |
content = [
|
| 73 |
-
{"type": "text", "text": "
|
| 74 |
{
|
| 75 |
"type": "image_url",
|
| 76 |
"image_url": {"url": f"data:image/png;base64,{image_to_base64(image_to_process)}"}
|
|
@@ -113,16 +113,34 @@ def process_input(file_input, temperature, page_num):
|
|
| 113 |
if content_delta:
|
| 114 |
accumulated_response += content_delta
|
| 115 |
if first_chunk:
|
| 116 |
-
yield accumulated_response, accumulated_response, page_info, image_to_process
|
| 117 |
first_chunk = False
|
| 118 |
else:
|
| 119 |
-
yield accumulated_response, accumulated_response, page_info, gr.update()
|
| 120 |
except json.JSONDecodeError:
|
| 121 |
continue
|
| 122 |
|
| 123 |
except Exception as e:
|
| 124 |
error_msg = f"Error: {str(e)}"
|
| 125 |
-
yield error_msg, error_msg, page_info, image_to_process
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
|
| 128 |
with gr.Blocks(title="π Image/PDF OCR", theme=gr.themes.Soft()) as demo:
|
|
@@ -193,7 +211,13 @@ with gr.Blocks(title="π Image/PDF OCR", theme=gr.themes.Soft()) as demo:
|
|
| 193 |
submit_btn.click(
|
| 194 |
fn=process_input,
|
| 195 |
inputs=[file_input, temperature, num_pages],
|
| 196 |
-
outputs=[output_text, raw_output, page_info, rendered_image]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
)
|
| 198 |
|
| 199 |
clear_btn.click(
|
|
|
|
| 46 |
|
| 47 |
def process_input(file_input, temperature, page_num):
|
| 48 |
if file_input is None:
|
| 49 |
+
yield "Please upload an image or PDF first.", "", "", None, gr.update()
|
| 50 |
return
|
| 51 |
|
| 52 |
image_to_process = None
|
|
|
|
| 59 |
image_to_process, total_pages, actual_page = process_pdf(file_path, int(page_num))
|
| 60 |
page_info = f"Processing page {actual_page} of {total_pages}"
|
| 61 |
except Exception as e:
|
| 62 |
+
yield f"Error processing PDF: {str(e)}", "", "", None, gr.update()
|
| 63 |
return
|
| 64 |
else:
|
| 65 |
try:
|
| 66 |
image_to_process = Image.open(file_path)
|
| 67 |
page_info = "Processing image"
|
| 68 |
except Exception as e:
|
| 69 |
+
yield f"Error opening image: {str(e)}", "", "", None, gr.update()
|
| 70 |
return
|
| 71 |
|
| 72 |
content = [
|
| 73 |
+
{"type": "text", "text": ""},
|
| 74 |
{
|
| 75 |
"type": "image_url",
|
| 76 |
"image_url": {"url": f"data:image/png;base64,{image_to_base64(image_to_process)}"}
|
|
|
|
| 113 |
if content_delta:
|
| 114 |
accumulated_response += content_delta
|
| 115 |
if first_chunk:
|
| 116 |
+
yield accumulated_response, accumulated_response, page_info, image_to_process, gr.update()
|
| 117 |
first_chunk = False
|
| 118 |
else:
|
| 119 |
+
yield accumulated_response, accumulated_response, page_info, gr.update(), gr.update()
|
| 120 |
except json.JSONDecodeError:
|
| 121 |
continue
|
| 122 |
|
| 123 |
except Exception as e:
|
| 124 |
error_msg = f"Error: {str(e)}"
|
| 125 |
+
yield error_msg, error_msg, page_info, image_to_process, gr.update()
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
def update_slider(file_input):
|
| 129 |
+
if file_input is None:
|
| 130 |
+
return gr.update(maximum=20, value=1)
|
| 131 |
+
|
| 132 |
+
file_path = file_input if isinstance(file_input, str) else file_input.name
|
| 133 |
+
|
| 134 |
+
if file_path.lower().endswith('.pdf'):
|
| 135 |
+
try:
|
| 136 |
+
pdf = pdfium.PdfDocument(file_path)
|
| 137 |
+
total_pages = len(pdf)
|
| 138 |
+
pdf.close()
|
| 139 |
+
return gr.update(maximum=total_pages, value=1)
|
| 140 |
+
except:
|
| 141 |
+
return gr.update(maximum=20, value=1)
|
| 142 |
+
else:
|
| 143 |
+
return gr.update(maximum=1, value=1)
|
| 144 |
|
| 145 |
|
| 146 |
with gr.Blocks(title="π Image/PDF OCR", theme=gr.themes.Soft()) as demo:
|
|
|
|
| 211 |
submit_btn.click(
|
| 212 |
fn=process_input,
|
| 213 |
inputs=[file_input, temperature, num_pages],
|
| 214 |
+
outputs=[output_text, raw_output, page_info, rendered_image, num_pages]
|
| 215 |
+
)
|
| 216 |
+
|
| 217 |
+
file_input.change(
|
| 218 |
+
fn=update_slider,
|
| 219 |
+
inputs=[file_input],
|
| 220 |
+
outputs=[num_pages]
|
| 221 |
)
|
| 222 |
|
| 223 |
clear_btn.click(
|