Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
# Initialize TrOCR pipeline for specialized OCR
|
| 6 |
+
ocr_pipeline = pipeline("image-to-text", model="microsoft/trocr-base-printed")
|
| 7 |
+
|
| 8 |
+
def extract_text_from_image(image):
|
| 9 |
+
if image is None:
|
| 10 |
+
return "No image provided. Please upload an image file."
|
| 11 |
+
|
| 12 |
+
try:
|
| 13 |
+
# Convert Gradio image to PIL Image
|
| 14 |
+
pil_image = Image.fromarray(image)
|
| 15 |
+
|
| 16 |
+
# Extract text from image using TrOCR
|
| 17 |
+
result = ocr_pipeline(pil_image)
|
| 18 |
+
|
| 19 |
+
# Return the extracted text
|
| 20 |
+
return result[0]['generated_text']
|
| 21 |
+
except Exception as e:
|
| 22 |
+
return f"Error during text extraction: {str(e)}"
|
| 23 |
+
|
| 24 |
+
# Gradio interface
|
| 25 |
+
with gr.Blocks(title="Image Text Extractor") as demo:
|
| 26 |
+
gr.Markdown("# 📷 Image Text Extractor")
|
| 27 |
+
gr.Markdown("Extract text from images using Microsoft's TrOCR model")
|
| 28 |
+
|
| 29 |
+
with gr.Row():
|
| 30 |
+
with gr.Column():
|
| 31 |
+
image_input = gr.Image(
|
| 32 |
+
type="numpy",
|
| 33 |
+
label="Upload Image"
|
| 34 |
+
)
|
| 35 |
+
extract_btn = gr.Button("Extract Text", variant="primary")
|
| 36 |
+
|
| 37 |
+
with gr.Column():
|
| 38 |
+
text_output = gr.Textbox(
|
| 39 |
+
lines=10,
|
| 40 |
+
label="Extracted Text",
|
| 41 |
+
interactive=False
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
extract_btn.click(
|
| 45 |
+
extract_text_from_image,
|
| 46 |
+
inputs=image_input,
|
| 47 |
+
outputs=text_output
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
gr.Examples(
|
| 51 |
+
examples=[
|
| 52 |
+
["example1.jpg"],
|
| 53 |
+
["example2.png"]
|
| 54 |
+
],
|
| 55 |
+
inputs=[image_input],
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
gr.Markdown("### About This Model")
|
| 59 |
+
gr.Markdown("- **Model**: [microsoft/trocr-base-printed](https://huggingface.co/microsoft/trocr-base-printed)")
|
| 60 |
+
gr.Markdown("- **Task**: Optical Character Recognition (OCR)")
|
| 61 |
+
gr.Markdown("- **Architecture**: Transformer-based OCR (TrOCR)")
|
| 62 |
+
gr.Markdown("- **Capabilities**: Specialized for printed text extraction")
|
| 63 |
+
gr.Markdown("- **Note**: First processing may take 15-25 seconds (model loading)")
|
| 64 |
+
gr.Markdown("- **Supported Formats**: JPG, PNG, JPEG")
|
| 65 |
+
|
| 66 |
+
if __name__ == "__main__":
|
| 67 |
+
demo.launch()
|