Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import easyocr
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import torch
|
| 6 |
+
import spaces
|
| 7 |
+
|
| 8 |
+
# Create dummy tensor to confirm GPU availability
|
| 9 |
+
zero = torch.Tensor([0]).cuda()
|
| 10 |
+
print("Torch is using:", zero.device) # Should be 'cuda:0'
|
| 11 |
+
|
| 12 |
+
# Declare reader outside the function (only once)
|
| 13 |
+
reader = easyocr.Reader(['en'], gpu=True)
|
| 14 |
+
|
| 15 |
+
@spaces.GPU # Ensures this function runs on GPU
|
| 16 |
+
def extract_text(image):
|
| 17 |
+
result = reader.readtext(np.array(image), detail=0)
|
| 18 |
+
text = "\n".join(result)
|
| 19 |
+
print("OCR complete. Text:", text)
|
| 20 |
+
return text
|
| 21 |
+
|
| 22 |
+
iface = gr.Interface(
|
| 23 |
+
fn=extract_text,
|
| 24 |
+
inputs=gr.Image(type="pil"),
|
| 25 |
+
outputs="text",
|
| 26 |
+
title="GPU OCR with EasyOCR",
|
| 27 |
+
description="Upload an image. OCR runs on GPU using EasyOCR + Hugging Face Spaces."
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
iface.launch()
|