rththr commited on
Commit
eec40e6
·
verified ·
1 Parent(s): ec7bbba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -26
app.py CHANGED
@@ -1,30 +1,38 @@
1
- # 1. Use Python 3.10 (Most stable for Paddle)
2
- FROM python:3.10
 
 
3
 
4
- # 2. Set working directory
5
- WORKDIR /app
 
6
 
7
- # 3. INSTALL MISSING GRAPHICS LIBRARIES (This fixes the "Starting" crash)
8
- RUN apt-get update && apt-get install -y \
9
- libgl1 \
10
- libglib2.0-0 \
11
- libsm6 \
12
- libxext6 \
13
- libxrender-dev \
14
- && rm -rf /var/lib/apt/lists/*
 
 
 
 
 
 
 
 
15
 
16
- # 4. Copy requirements and install
17
- COPY requirements.txt requirements.txt
18
- RUN pip install --no-cache-dir --upgrade pip && \
19
- pip install --no-cache-dir -r requirements.txt
 
 
 
 
20
 
21
- # 5. Pre-download the models so they don't timeout your browser
22
- # (This runs a dummy scan during build to cache the files)
23
- RUN pip install paddlepaddle==2.6.2 paddleocr==2.7.3 numpy<2.0.0
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)