Update app.py
Browse files
app.py
CHANGED
|
@@ -1,79 +1,4 @@
|
|
| 1 |
-
|
| 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 |
|
|
|
|
| 1 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
|
| 4 |
|