Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,20 +6,18 @@ from werkzeug.utils import secure_filename
|
|
| 6 |
import time
|
| 7 |
|
| 8 |
app = Flask(__name__)
|
| 9 |
-
app.secret_key = 'your-secret-key-here'
|
| 10 |
|
| 11 |
UPLOAD_FOLDER = "/tmp/uploads"
|
| 12 |
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif', 'bmp'}
|
| 13 |
-
MAX_FILE_SIZE = 10 * 1024 * 1024
|
| 14 |
|
| 15 |
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
|
| 16 |
app.config["MAX_CONTENT_LENGTH"] = MAX_FILE_SIZE
|
| 17 |
|
| 18 |
-
# Setup logging
|
| 19 |
logging.basicConfig(level=logging.INFO)
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
reader = easyocr.Reader(['en'])
|
| 23 |
|
| 24 |
def allowed_file(filename):
|
| 25 |
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
|
@@ -49,16 +47,16 @@ def upload():
|
|
| 49 |
try:
|
| 50 |
filename = secure_filename(file.filename)
|
| 51 |
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
|
|
|
| 52 |
file.save(filepath)
|
| 53 |
|
| 54 |
-
#
|
|
|
|
| 55 |
result = reader.readtext(filepath, detail=0, paragraph=True)
|
| 56 |
|
| 57 |
-
# Clean up
|
| 58 |
os.remove(filepath)
|
| 59 |
|
| 60 |
text = "\n".join(result) if result else "No text detected"
|
| 61 |
-
|
| 62 |
processing_time = round(time.time() - start_time, 2)
|
| 63 |
logging.info(f"Processed {filename} in {processing_time}s")
|
| 64 |
|
|
|
|
| 6 |
import time
|
| 7 |
|
| 8 |
app = Flask(__name__)
|
| 9 |
+
app.secret_key = 'your-secret-key-here'
|
| 10 |
|
| 11 |
UPLOAD_FOLDER = "/tmp/uploads"
|
| 12 |
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif', 'bmp'}
|
| 13 |
+
MAX_FILE_SIZE = 10 * 1024 * 1024
|
| 14 |
|
| 15 |
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
|
| 16 |
app.config["MAX_CONTENT_LENGTH"] = MAX_FILE_SIZE
|
| 17 |
|
|
|
|
| 18 |
logging.basicConfig(level=logging.INFO)
|
| 19 |
|
| 20 |
+
# ← No global reader here anymore
|
|
|
|
| 21 |
|
| 22 |
def allowed_file(filename):
|
| 23 |
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
|
|
|
| 47 |
try:
|
| 48 |
filename = secure_filename(file.filename)
|
| 49 |
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
| 50 |
+
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
|
| 51 |
file.save(filepath)
|
| 52 |
|
| 53 |
+
# Initialize reader here, inside the request
|
| 54 |
+
reader = easyocr.Reader(['en'])
|
| 55 |
result = reader.readtext(filepath, detail=0, paragraph=True)
|
| 56 |
|
|
|
|
| 57 |
os.remove(filepath)
|
| 58 |
|
| 59 |
text = "\n".join(result) if result else "No text detected"
|
|
|
|
| 60 |
processing_time = round(time.time() - start_time, 2)
|
| 61 |
logging.info(f"Processed {filename} in {processing_time}s")
|
| 62 |
|