easyocr-api / app.py
theoracle's picture
Update app.py
481a15f verified
raw
history blame
917 Bytes
import gradio as gr
import easyocr
import numpy as np
from PIL import Image
import torch
import spaces
load_dotenv() # load .env file
HF_TOKEN = os.getenv("ocr_sec")
client = Client("theoracle/easyocr-api", hf_token=HF_TOKEN)
# Create dummy tensor to confirm GPU availability
zero = torch.Tensor([0]).cuda()
print("Torch is using:", zero.device) # Should be 'cuda:0'
# Declare reader outside the function (only once)
reader = easyocr.Reader(['en'], gpu=True)
@spaces.GPU # Ensures this function runs on GPU
def extract_text(image):
result = reader.readtext(np.array(image), detail=0)
text = "\n".join(result)
print("OCR complete. Text:", text)
return text
iface = gr.Interface(
fn=extract_text,
inputs=gr.Image(type="pil"),
outputs="text",
title="GPU OCR with EasyOCR",
description="Upload an image. OCR runs on GPU using EasyOCR + Hugging Face Spaces."
)
iface.launch()