Spaces:
Sleeping
Sleeping
File size: 1,117 Bytes
3a8de33 9164d6d c6111b8 3a8de33 c6111b8 9164d6d c6111b8 9164d6d c6111b8 3a8de33 9164d6d c6111b8 9164d6d c6111b8 9164d6d c6111b8 9164d6d c6111b8 9164d6d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import gradio as gr
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
from PIL import Image
import numpy as np
import requests
# Load your model from Hugging Face
processor = TrOCRProcessor.from_pretrained("DeepDiveDev/transformodocs-ocr")
model = VisionEncoderDecoderModel.from_pretrained("DeepDiveDev/transformodocs-ocr")
# Function to extract text
def extract_text(image):
if isinstance(image, np.ndarray): # Check if input is a NumPy array
image = Image.fromarray(image) # Convert NumPy array to PIL Image
else:
image = Image.open(image).convert("RGB") # Open normally if not a NumPy array
pixel_values = processor(images=image, return_tensors="pt").pixel_values
generated_ids = model.generate(pixel_values)
extracted_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
return extracted_text
# Gradio Interface
iface = gr.Interface(
fn=extract_text,
inputs="image",
outputs="text",
title="TransformoDocs - AI OCR",
description="Upload a handwritten document and get the extracted text.",
)
iface.launch()
|