Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from pix2text import Pix2Text
|
| 3 |
import logging
|
|
@@ -18,7 +98,7 @@ except Exception as e:
|
|
| 18 |
def recognize_text(image_path: str) -> str:
|
| 19 |
"""
|
| 20 |
Performs OCR on the uploaded image and safely parses the output.
|
| 21 |
-
This function includes
|
| 22 |
"""
|
| 23 |
if p2t is None:
|
| 24 |
return (
|
|
@@ -30,28 +110,64 @@ def recognize_text(image_path: str) -> str:
|
|
| 30 |
# Recognize text and formulas
|
| 31 |
result = p2t.recognize(image_path, save_formula_images=False, use_analyzer=True)
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
except Exception as e:
|
| 53 |
# Catch any unexpected errors during the recognition process
|
| 54 |
-
|
|
|
|
| 55 |
|
| 56 |
|
| 57 |
# --- Gradio Interface Setup ---
|
|
@@ -61,7 +177,7 @@ iface = gr.Interface(
|
|
| 61 |
# Use type="filepath" to send the local file path to the Python function
|
| 62 |
inputs=gr.Image(type="filepath", label="Upload Image (Text/Formula/Math)"),
|
| 63 |
# The output is a standard textbox
|
| 64 |
-
outputs=gr.Textbox(label="Extracted Text (LaTeX/Plain Text)"),
|
| 65 |
title="🔬 Pix2Text OCR Formula and Text Recognition",
|
| 66 |
description=(
|
| 67 |
"Upload an image containing text, mathematical formulas, or scientific notation. "
|
|
|
|
| 1 |
+
# import gradio as gr
|
| 2 |
+
# from pix2text import Pix2Text
|
| 3 |
+
# import logging
|
| 4 |
+
# from PIL import Image
|
| 5 |
+
|
| 6 |
+
# # Set up logging to WARNING level to suppress excessive output from model libraries
|
| 7 |
+
# logging.basicConfig(level=logging.WARNING)
|
| 8 |
+
|
| 9 |
+
# # Initialize Pix2Text model globally (expensive operation, do it once)
|
| 10 |
+
# p2t = None
|
| 11 |
+
# try:
|
| 12 |
+
# # Initialize the Pix2Text model
|
| 13 |
+
# p2t = Pix2Text()
|
| 14 |
+
# except Exception as e:
|
| 15 |
+
# print(f"Error initializing Pix2Text model: {e}. Recognition will use a fallback function.")
|
| 16 |
+
|
| 17 |
+
# # Define the main recognition function
|
| 18 |
+
# def recognize_text(image_path: str) -> str:
|
| 19 |
+
# """
|
| 20 |
+
# Performs OCR on the uploaded image and safely parses the output.
|
| 21 |
+
# This function includes the fix for the "'str' object has no attribute 'text'" error.
|
| 22 |
+
# """
|
| 23 |
+
# if p2t is None:
|
| 24 |
+
# return (
|
| 25 |
+
# "Model initialization failed at startup. Please check the logs "
|
| 26 |
+
# "to ensure all dependencies (like ONNX runtime) loaded correctly."
|
| 27 |
+
# )
|
| 28 |
+
|
| 29 |
+
# try:
|
| 30 |
+
# # Recognize text and formulas
|
| 31 |
+
# result = p2t.recognize(image_path, save_formula_images=False, use_analyzer=True)
|
| 32 |
+
|
| 33 |
+
# extracted_parts = []
|
| 34 |
+
|
| 35 |
+
# # Loop through the result list and safely extract text from P2T objects or strings
|
| 36 |
+
# for item in result:
|
| 37 |
+
# if hasattr(item, 'text'):
|
| 38 |
+
# # P2TOutput object (for formulas or structured text)
|
| 39 |
+
# extracted_parts.append(item.text)
|
| 40 |
+
# elif isinstance(item, str):
|
| 41 |
+
# # Simple text string
|
| 42 |
+
# extracted_parts.append(item)
|
| 43 |
+
# # Ignore anything else
|
| 44 |
+
|
| 45 |
+
# extracted_text = "\n\n".join(extracted_parts)
|
| 46 |
+
|
| 47 |
+
# if not extracted_text.strip():
|
| 48 |
+
# return "No recognizable text or formulas found in the image."
|
| 49 |
+
|
| 50 |
+
# return extracted_text
|
| 51 |
+
|
| 52 |
+
# except Exception as e:
|
| 53 |
+
# # Catch any unexpected errors during the recognition process
|
| 54 |
+
# return f"An unexpected error occurred during recognition: {e}"
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
# # --- Gradio Interface Setup ---
|
| 58 |
+
|
| 59 |
+
# iface = gr.Interface(
|
| 60 |
+
# fn=recognize_text,
|
| 61 |
+
# # Use type="filepath" to send the local file path to the Python function
|
| 62 |
+
# inputs=gr.Image(type="filepath", label="Upload Image (Text/Formula/Math)"),
|
| 63 |
+
# # The output is a standard textbox
|
| 64 |
+
# outputs=gr.Textbox(label="Extracted Text (LaTeX/Plain Text)"),
|
| 65 |
+
# title="🔬 Pix2Text OCR Formula and Text Recognition",
|
| 66 |
+
# description=(
|
| 67 |
+
# "Upload an image containing text, mathematical formulas, or scientific notation. "
|
| 68 |
+
# "The app converts the image content into editable text, using LaTeX for formulas."
|
| 69 |
+
# ),
|
| 70 |
+
# theme=gr.themes.Soft(),
|
| 71 |
+
# allow_flagging="never",
|
| 72 |
+
# )
|
| 73 |
+
|
| 74 |
+
# # Launch the Gradio app
|
| 75 |
+
# if __name__ == "__main__":
|
| 76 |
+
# iface.launch(show_api=False)
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
|
| 81 |
import gradio as gr
|
| 82 |
from pix2text import Pix2Text
|
| 83 |
import logging
|
|
|
|
| 98 |
def recognize_text(image_path: str) -> str:
|
| 99 |
"""
|
| 100 |
Performs OCR on the uploaded image and safely parses the output.
|
| 101 |
+
This function includes debugging to understand the result structure.
|
| 102 |
"""
|
| 103 |
if p2t is None:
|
| 104 |
return (
|
|
|
|
| 110 |
# Recognize text and formulas
|
| 111 |
result = p2t.recognize(image_path, save_formula_images=False, use_analyzer=True)
|
| 112 |
|
| 113 |
+
# DEBUG: Print the actual result structure
|
| 114 |
+
print(f"DEBUG - Result type: {type(result)}")
|
| 115 |
+
print(f"DEBUG - Result content: {result}")
|
| 116 |
+
|
| 117 |
+
# Handle different possible return types
|
| 118 |
+
if isinstance(result, str):
|
| 119 |
+
# If result is directly a string
|
| 120 |
+
return result if result.strip() else "No recognizable text or formulas found in the image."
|
| 121 |
+
|
| 122 |
+
if isinstance(result, dict):
|
| 123 |
+
# If result is a dictionary, try to extract text from common keys
|
| 124 |
+
print(f"DEBUG - Result keys: {result.keys()}")
|
| 125 |
+
possible_keys = ['text', 'content', 'result', 'output']
|
| 126 |
+
for key in possible_keys:
|
| 127 |
+
if key in result:
|
| 128 |
+
return str(result[key])
|
| 129 |
+
return f"Result is a dict but couldn't find text. Keys: {list(result.keys())}"
|
| 130 |
+
|
| 131 |
+
if isinstance(result, list):
|
| 132 |
+
# If result is a list, process each item
|
| 133 |
+
extracted_parts = []
|
| 134 |
+
|
| 135 |
+
for i, item in enumerate(result):
|
| 136 |
+
print(f"DEBUG - Item {i} type: {type(item)}")
|
| 137 |
+
print(f"DEBUG - Item {i} content: {item}")
|
| 138 |
+
|
| 139 |
+
if hasattr(item, 'text'):
|
| 140 |
+
# P2TOutput object (for formulas or structured text)
|
| 141 |
+
extracted_parts.append(item.text)
|
| 142 |
+
elif isinstance(item, str):
|
| 143 |
+
# Simple text string
|
| 144 |
+
extracted_parts.append(item)
|
| 145 |
+
elif isinstance(item, dict):
|
| 146 |
+
# Dictionary with text content
|
| 147 |
+
if 'text' in item:
|
| 148 |
+
extracted_parts.append(item['text'])
|
| 149 |
+
elif 'content' in item:
|
| 150 |
+
extracted_parts.append(item['content'])
|
| 151 |
+
else:
|
| 152 |
+
extracted_parts.append(str(item))
|
| 153 |
+
else:
|
| 154 |
+
# Try to convert to string as fallback
|
| 155 |
+
extracted_parts.append(str(item))
|
| 156 |
|
| 157 |
+
extracted_text = "\n\n".join(extracted_parts)
|
| 158 |
|
| 159 |
+
if not extracted_text.strip():
|
| 160 |
+
return "No recognizable text or formulas found in the image."
|
| 161 |
|
| 162 |
+
return extracted_text
|
| 163 |
+
|
| 164 |
+
# If none of the above, try to convert to string
|
| 165 |
+
return str(result) if result else "No recognizable text or formulas found in the image."
|
| 166 |
|
| 167 |
except Exception as e:
|
| 168 |
# Catch any unexpected errors during the recognition process
|
| 169 |
+
import traceback
|
| 170 |
+
return f"An unexpected error occurred during recognition: {e}\n\nTraceback:\n{traceback.format_exc()}"
|
| 171 |
|
| 172 |
|
| 173 |
# --- Gradio Interface Setup ---
|
|
|
|
| 177 |
# Use type="filepath" to send the local file path to the Python function
|
| 178 |
inputs=gr.Image(type="filepath", label="Upload Image (Text/Formula/Math)"),
|
| 179 |
# The output is a standard textbox
|
| 180 |
+
outputs=gr.Textbox(label="Extracted Text (LaTeX/Plain Text)", lines=10),
|
| 181 |
title="🔬 Pix2Text OCR Formula and Text Recognition",
|
| 182 |
description=(
|
| 183 |
"Upload an image containing text, mathematical formulas, or scientific notation. "
|