Spaces:
Running
Running
Commit
·
8a90682
1
Parent(s):
a78bc48
fix: add werkzeug dependency
Browse files- app.py +10 -27
- requirements.txt +5 -4
app.py
CHANGED
@@ -45,29 +45,20 @@
|
|
45 |
# port = int(os.environ.get('PORT', 5000)) # <-- IMPORTANT
|
46 |
# app.run(host='0.0.0.0', port=port)
|
47 |
|
48 |
-
from flask import Flask, render_template, request
|
49 |
import os
|
50 |
import time
|
51 |
|
52 |
app = Flask(__name__)
|
53 |
|
54 |
-
#
|
55 |
-
UPLOAD_FOLDER = 'tmp_uploads'
|
56 |
-
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
57 |
-
|
58 |
-
# Lazy-load OCR only when needed
|
59 |
def get_ocr():
|
60 |
from paddleocr import PaddleOCR
|
61 |
return PaddleOCR(
|
62 |
lang='en',
|
63 |
use_angle_cls=False,
|
64 |
use_gpu=False,
|
65 |
-
|
66 |
-
rec_model_dir='en_PP-OCRv3_rec_infer',
|
67 |
-
cls_model_dir='ch_ppocr_mobile_v2.0_cls_infer',
|
68 |
-
enable_mkldnn=True,
|
69 |
-
rec_batch_num=1,
|
70 |
-
det_limit_side_len=320,
|
71 |
thread_num=1
|
72 |
)
|
73 |
|
@@ -79,32 +70,24 @@ def upload_file():
|
|
79 |
return render_template('index.html', error="Please select a file")
|
80 |
|
81 |
try:
|
82 |
-
#
|
83 |
-
|
|
|
84 |
return render_template('index.html', error="Max 300KB file size")
|
85 |
file.seek(0)
|
86 |
|
87 |
-
# Save file
|
88 |
-
filename = f"{int(time.time())}.jpg"
|
89 |
-
filepath = os.path.join(UPLOAD_FOLDER, filename)
|
90 |
-
file.save(filepath)
|
91 |
-
|
92 |
# Process with OCR
|
93 |
ocr = get_ocr()
|
94 |
-
result = ocr.ocr(
|
95 |
text = ' '.join([word[1][0] for line in result[0] for word in line if len(word) >= 2])
|
96 |
-
|
97 |
-
# Cleanup
|
98 |
-
os.remove(filepath)
|
99 |
return render_template('index.html', text=text)
|
100 |
|
101 |
except Exception as e:
|
102 |
-
|
103 |
-
os.remove(filepath)
|
104 |
-
return render_template('index.html', error="OCR processing failed")
|
105 |
|
106 |
return render_template('index.html')
|
107 |
|
108 |
if __name__ == '__main__':
|
109 |
port = int(os.environ.get('PORT', 5000))
|
110 |
-
app.run(host='0.0.0.0', port=port
|
|
|
45 |
# port = int(os.environ.get('PORT', 5000)) # <-- IMPORTANT
|
46 |
# app.run(host='0.0.0.0', port=port)
|
47 |
|
48 |
+
from flask import Flask, render_template, request
|
49 |
import os
|
50 |
import time
|
51 |
|
52 |
app = Flask(__name__)
|
53 |
|
54 |
+
# Lightweight OCR loader
|
|
|
|
|
|
|
|
|
55 |
def get_ocr():
|
56 |
from paddleocr import PaddleOCR
|
57 |
return PaddleOCR(
|
58 |
lang='en',
|
59 |
use_angle_cls=False,
|
60 |
use_gpu=False,
|
61 |
+
det_limit_side_len=480,
|
|
|
|
|
|
|
|
|
|
|
62 |
thread_num=1
|
63 |
)
|
64 |
|
|
|
70 |
return render_template('index.html', error="Please select a file")
|
71 |
|
72 |
try:
|
73 |
+
# Verify file size
|
74 |
+
file.seek(0, os.SEEK_END)
|
75 |
+
if file.tell() > 300000:
|
76 |
return render_template('index.html', error="Max 300KB file size")
|
77 |
file.seek(0)
|
78 |
|
|
|
|
|
|
|
|
|
|
|
79 |
# Process with OCR
|
80 |
ocr = get_ocr()
|
81 |
+
result = ocr.ocr(file.stream, cls=False)
|
82 |
text = ' '.join([word[1][0] for line in result[0] for word in line if len(word) >= 2])
|
83 |
+
|
|
|
|
|
84 |
return render_template('index.html', text=text)
|
85 |
|
86 |
except Exception as e:
|
87 |
+
return render_template('index.html', error="Processing error")
|
|
|
|
|
88 |
|
89 |
return render_template('index.html')
|
90 |
|
91 |
if __name__ == '__main__':
|
92 |
port = int(os.environ.get('PORT', 5000))
|
93 |
+
app.run(host='0.0.0.0', port=port)
|
requirements.txt
CHANGED
@@ -16,7 +16,8 @@
|
|
16 |
# click
|
17 |
# blinker
|
18 |
flask==2.2.5
|
19 |
-
paddleocr==2.
|
20 |
-
paddlepaddle==2.
|
21 |
-
opencv-python-headless==4.
|
22 |
-
numpy==1.
|
|
|
|
16 |
# click
|
17 |
# blinker
|
18 |
flask==2.2.5
|
19 |
+
paddleocr==2.7.0.3
|
20 |
+
paddlepaddle==2.5.2 # Updated to compatible version
|
21 |
+
opencv-python-headless==4.8.1.78
|
22 |
+
numpy==1.26.0
|
23 |
+
pyclipper==1.3.0.post6
|