Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -8,6 +8,7 @@ import cv2
|
|
| 8 |
import numpy as np
|
| 9 |
from PIL import Image
|
| 10 |
from io import BytesIO
|
|
|
|
| 11 |
from fastapi import FastAPI, File, UploadFile, Form, Request
|
| 12 |
from fastapi.middleware.cors import CORSMiddleware
|
| 13 |
from fastapi.responses import FileResponse
|
|
@@ -27,10 +28,12 @@ app.state.limiter = limiter
|
|
| 27 |
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
|
| 28 |
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])
|
| 29 |
|
| 30 |
-
# --- PERSISTENT STORAGE ---
|
| 31 |
CACHE_DIR = os.environ.get("HF_HOME", "/app/.cache")
|
| 32 |
MODEL_DIR = os.path.join(CACHE_DIR, "easyocr_models")
|
| 33 |
-
|
|
|
|
|
|
|
| 34 |
|
| 35 |
if not os.path.exists(MODEL_DIR): os.makedirs(MODEL_DIR)
|
| 36 |
if not os.path.exists(DATA_DIR): os.makedirs(DATA_DIR)
|
|
@@ -56,8 +59,13 @@ ai_cache = load_cache(AI_CACHE_FILE)
|
|
| 56 |
|
| 57 |
# --- INIT ---
|
| 58 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
reader = easyocr.Reader(['en'], gpu=False, model_storage_directory=MODEL_DIR)
|
| 62 |
|
| 63 |
def get_server_ocr(content):
|
|
@@ -91,7 +99,7 @@ async def analyze_product(
|
|
| 91 |
extracted_text: str = Form(None), # Client OCR text
|
| 92 |
image: UploadFile = File(...)
|
| 93 |
):
|
| 94 |
-
if not client: return {"error": "Server Error: Missing
|
| 95 |
|
| 96 |
try:
|
| 97 |
# Fallback to Server OCR if client failed
|
|
|
|
| 8 |
import numpy as np
|
| 9 |
from PIL import Image
|
| 10 |
from io import BytesIO
|
| 11 |
+
from functools import lru_cache
|
| 12 |
from fastapi import FastAPI, File, UploadFile, Form, Request
|
| 13 |
from fastapi.middleware.cors import CORSMiddleware
|
| 14 |
from fastapi.responses import FileResponse
|
|
|
|
| 28 |
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
|
| 29 |
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])
|
| 30 |
|
| 31 |
+
# --- PERSISTENT STORAGE FIX ---
|
| 32 |
CACHE_DIR = os.environ.get("HF_HOME", "/app/.cache")
|
| 33 |
MODEL_DIR = os.path.join(CACHE_DIR, "easyocr_models")
|
| 34 |
+
|
| 35 |
+
# FIX: Use a local directory ('data') instead of root ('/data')
|
| 36 |
+
DATA_DIR = os.path.join(os.getcwd(), "data")
|
| 37 |
|
| 38 |
if not os.path.exists(MODEL_DIR): os.makedirs(MODEL_DIR)
|
| 39 |
if not os.path.exists(DATA_DIR): os.makedirs(DATA_DIR)
|
|
|
|
| 59 |
|
| 60 |
# --- INIT ---
|
| 61 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 62 |
+
# Safety Check: Don't crash if key is missing
|
| 63 |
+
if not GROQ_API_KEY:
|
| 64 |
+
logger.warning("⚠️ GROQ_API_KEY missing! App will fail.")
|
| 65 |
+
client = None
|
| 66 |
+
else:
|
| 67 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 68 |
+
|
| 69 |
reader = easyocr.Reader(['en'], gpu=False, model_storage_directory=MODEL_DIR)
|
| 70 |
|
| 71 |
def get_server_ocr(content):
|
|
|
|
| 99 |
extracted_text: str = Form(None), # Client OCR text
|
| 100 |
image: UploadFile = File(...)
|
| 101 |
):
|
| 102 |
+
if not client: return {"error": "Server Error: Missing GROQ_API_KEY in Settings"}
|
| 103 |
|
| 104 |
try:
|
| 105 |
# Fallback to Server OCR if client failed
|