Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,35 +5,31 @@ from PIL import Image
|
|
| 5 |
import numpy as np
|
| 6 |
import cv2
|
| 7 |
from paddleocr import TextDetection
|
| 8 |
-
from spaces import GPU # β
|
| 9 |
|
| 10 |
MODEL_HUB_ID = "imperiusrex/Handwritten_model"
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
-
print("π Loading models on CPU...")
|
| 16 |
processor = TrOCRProcessor.from_pretrained(MODEL_HUB_ID)
|
| 17 |
-
model = VisionEncoderDecoderModel.from_pretrained(MODEL_HUB_ID)
|
|
|
|
| 18 |
model.eval()
|
| 19 |
|
| 20 |
ocr_det_model = TextDetection(model_name="PP-OCRv5_server_det")
|
| 21 |
-
print("β
Models loaded (CPU mode). GPU will be used only during inference.")
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
def recognize_handwritten_text(image_input):
|
| 25 |
if image_input is None:
|
| 26 |
return "Please upload an image."
|
| 27 |
|
| 28 |
-
# Move model to GPU here (only when needed)
|
| 29 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 30 |
-
model.to(device)
|
| 31 |
-
torch.zeros(1).to(device) # CUDA warmup
|
| 32 |
-
|
| 33 |
image_pil = Image.fromarray(image_input).convert("RGB")
|
| 34 |
|
| 35 |
-
|
| 36 |
-
detection_results = ocr_det_model.predict(np.array(image_pil), batch_size=1)
|
| 37 |
|
| 38 |
detected_polys = []
|
| 39 |
for res in detection_results:
|
|
@@ -87,10 +83,10 @@ def build_interface():
|
|
| 87 |
fn=recognize_handwritten_text,
|
| 88 |
inputs=gr.Image(type="numpy", label="Upload Handwritten Image"),
|
| 89 |
outputs="text",
|
| 90 |
-
title="
|
| 91 |
description="π· Upload a handwritten image. Uses PaddleOCR (detection) + TrOCR (recognition).",
|
| 92 |
)
|
| 93 |
|
| 94 |
-
if
|
| 95 |
iface = build_interface()
|
| 96 |
-
iface.launch(
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
import cv2
|
| 7 |
from paddleocr import TextDetection
|
| 8 |
+
from spaces import GPU # β
Required for ZeroGPU
|
| 9 |
|
| 10 |
MODEL_HUB_ID = "imperiusrex/Handwritten_model"
|
| 11 |
|
| 12 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 13 |
+
|
| 14 |
+
print("π Loading models...")
|
| 15 |
|
|
|
|
| 16 |
processor = TrOCRProcessor.from_pretrained(MODEL_HUB_ID)
|
| 17 |
+
model = VisionEncoderDecoderModel.from_pretrained(MODEL_HUB_ID)
|
| 18 |
+
model.to(device)
|
| 19 |
model.eval()
|
| 20 |
|
| 21 |
ocr_det_model = TextDetection(model_name="PP-OCRv5_server_det")
|
|
|
|
| 22 |
|
| 23 |
+
print("β
Models loaded successfully.")
|
| 24 |
+
|
| 25 |
+
@GPU # β
This tells Hugging Face this function needs the GPU (H200)
|
| 26 |
def recognize_handwritten_text(image_input):
|
| 27 |
if image_input is None:
|
| 28 |
return "Please upload an image."
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
image_pil = Image.fromarray(image_input).convert("RGB")
|
| 31 |
|
| 32 |
+
detection_results = ocr_det_model.predict(image_input, batch_size=1)
|
|
|
|
| 33 |
|
| 34 |
detected_polys = []
|
| 35 |
for res in detection_results:
|
|
|
|
| 83 |
fn=recognize_handwritten_text,
|
| 84 |
inputs=gr.Image(type="numpy", label="Upload Handwritten Image"),
|
| 85 |
outputs="text",
|
| 86 |
+
title="β Handwritten Text Recognition",
|
| 87 |
description="π· Upload a handwritten image. Uses PaddleOCR (detection) + TrOCR (recognition).",
|
| 88 |
)
|
| 89 |
|
| 90 |
+
if _name_ == "_main_":
|
| 91 |
iface = build_interface()
|
| 92 |
+
iface.launch()
|