Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,38 @@
|
|
| 1 |
-
|
| 2 |
-
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
-
RUN python -c "from paddleocr import PaddleOCR; PaddleOCR(use_angle_cls=False, lang='en', use_gpu=False)"
|
| 25 |
-
|
| 26 |
-
# 6. Copy App Code
|
| 27 |
-
COPY . .
|
| 28 |
-
|
| 29 |
-
# 7. Start the Server
|
| 30 |
-
CMD ["python", "app.py"]
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from paddleocr import PaddleOCR
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
|
| 6 |
+
# Initialize OCR (use_gpu=False is critical for free Spaces)
|
| 7 |
+
# This will load the models we pre-downloaded in the Dockerfile
|
| 8 |
+
ocr = PaddleOCR(use_angle_cls=True, lang='en', use_gpu=False)
|
| 9 |
|
| 10 |
+
def run_ocr(image):
|
| 11 |
+
if image is None:
|
| 12 |
+
return "Error: No image provided"
|
| 13 |
+
|
| 14 |
+
try:
|
| 15 |
+
# PaddleOCR expects a numpy array
|
| 16 |
+
result = ocr.ocr(image, cls=True)
|
| 17 |
+
|
| 18 |
+
txts = []
|
| 19 |
+
if result and result[0]:
|
| 20 |
+
# Extract just the text strings
|
| 21 |
+
txts = [line[1][0] for line in result[0]]
|
| 22 |
+
|
| 23 |
+
return "\n".join(txts)
|
| 24 |
+
except Exception as e:
|
| 25 |
+
return f"Error during OCR: {str(e)}"
|
| 26 |
|
| 27 |
+
# Define the interface
|
| 28 |
+
demo = gr.Interface(
|
| 29 |
+
fn=run_ocr,
|
| 30 |
+
inputs=gr.Image(type="numpy", label="Upload Screenshot"),
|
| 31 |
+
outputs=gr.Textbox(label="Detected Text", lines=10),
|
| 32 |
+
title="PaddleOCR API",
|
| 33 |
+
description="Upload Mobile Legends screenshots to extract text."
|
| 34 |
+
)
|
| 35 |
|
| 36 |
+
if __name__ == "__main__":
|
| 37 |
+
# CRITICAL FIX: server_name="0.0.0.0" allows Hugging Face to see your app
|
| 38 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|