Spaces:
Runtime error
Runtime error
Update server.py
Browse files
server.py
CHANGED
|
@@ -2,12 +2,12 @@ import os
|
|
| 2 |
import shutil
|
| 3 |
import zipfile
|
| 4 |
import logging
|
|
|
|
| 5 |
from fastapi import FastAPI, File, UploadFile, Form, HTTPException
|
| 6 |
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
from fastapi.staticfiles import StaticFiles
|
| 8 |
from huggingface_hub import snapshot_download
|
| 9 |
|
| 10 |
-
# Configure Logging to see errors in HF Logs clearly
|
| 11 |
logging.basicConfig(level=logging.INFO)
|
| 12 |
logger = logging.getLogger(__name__)
|
| 13 |
|
|
@@ -31,88 +31,84 @@ def setup_environment():
|
|
| 31 |
os.makedirs(IMAGE_DIR)
|
| 32 |
|
| 33 |
try:
|
| 34 |
-
logger.info(f"
|
| 35 |
snapshot_download(repo_id=DATASET_REPO, repo_type="dataset", token=token, local_dir=".")
|
| 36 |
|
| 37 |
zip_name = "orbiitt_db.zip"
|
| 38 |
if os.path.exists(zip_name):
|
| 39 |
-
logger.info("Extracting database and images...")
|
| 40 |
with zipfile.ZipFile(zip_name, 'r') as z:
|
| 41 |
z.extractall("temp_extract")
|
| 42 |
|
| 43 |
-
# Recursive scan for DB and Images
|
| 44 |
for root, _, files in os.walk("temp_extract"):
|
| 45 |
for f in files:
|
| 46 |
src = os.path.join(root, f)
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
if f in ["orbiitt_db", "orbiitt_db.db", "orbiitt.db"] or
|
| 50 |
if not os.path.exists(DB_TARGET):
|
| 51 |
shutil.move(src, f"./{DB_TARGET}")
|
| 52 |
-
logger.info(f"
|
| 53 |
|
| 54 |
-
#
|
| 55 |
elif f.lower().endswith(('.png', '.jpg', '.jpeg', '.webp')):
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
| 59 |
|
| 60 |
shutil.rmtree("temp_extract")
|
| 61 |
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
| 63 |
except Exception as e:
|
| 64 |
-
logger.error(f"Setup
|
| 65 |
|
| 66 |
setup_environment()
|
| 67 |
|
| 68 |
-
|
| 69 |
-
if os.path.exists(IMAGE_DIR):
|
| 70 |
-
app.mount("/Productimages", StaticFiles(directory=IMAGE_DIR), name="Productimages")
|
| 71 |
|
| 72 |
-
# Engine Logic
|
| 73 |
engine = None
|
| 74 |
try:
|
| 75 |
from orbiitt_engine import OrbiittEngine
|
| 76 |
if os.path.exists(DB_TARGET):
|
| 77 |
engine = OrbiittEngine()
|
| 78 |
-
logger.info("
|
| 79 |
except Exception as e:
|
| 80 |
-
logger.error(f"
|
| 81 |
|
| 82 |
@app.get("/")
|
| 83 |
-
def
|
| 84 |
-
"""Diagnostic endpoint to verify server state."""
|
| 85 |
-
files_in_root = os.listdir(".")
|
| 86 |
imgs = os.listdir(IMAGE_DIR) if os.path.exists(IMAGE_DIR) else []
|
| 87 |
return {
|
| 88 |
-
"
|
| 89 |
-
"
|
| 90 |
-
"
|
| 91 |
-
"
|
| 92 |
-
"sample_image": imgs[0] if imgs else None,
|
| 93 |
-
"root_files": files_in_root
|
| 94 |
}
|
| 95 |
|
| 96 |
@app.post("/search")
|
| 97 |
async def search(text: str = Form(None), weight: float = Form(0.5), file: UploadFile = File(None)):
|
| 98 |
-
if not engine:
|
| 99 |
-
raise HTTPException(status_code=503, detail="Search engine not ready.")
|
| 100 |
-
|
| 101 |
t_path = None
|
| 102 |
try:
|
| 103 |
if file:
|
| 104 |
-
t_path = f"
|
| 105 |
-
with open(t_path, "wb") as
|
| 106 |
-
buffer.write(await file.read())
|
| 107 |
|
| 108 |
results = engine.search(text_query=text, image_file=t_path, text_weight=weight)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
return {"results": results}
|
| 110 |
except Exception as e:
|
| 111 |
-
logger.error(f"Search error: {e}")
|
| 112 |
return {"results": [], "error": str(e)}
|
| 113 |
finally:
|
| 114 |
-
if t_path and os.path.exists(t_path):
|
| 115 |
-
os.remove(t_path)
|
| 116 |
|
| 117 |
if __name__ == "__main__":
|
| 118 |
import uvicorn
|
|
|
|
| 2 |
import shutil
|
| 3 |
import zipfile
|
| 4 |
import logging
|
| 5 |
+
import urllib.parse
|
| 6 |
from fastapi import FastAPI, File, UploadFile, Form, HTTPException
|
| 7 |
from fastapi.middleware.cors import CORSMiddleware
|
| 8 |
from fastapi.staticfiles import StaticFiles
|
| 9 |
from huggingface_hub import snapshot_download
|
| 10 |
|
|
|
|
| 11 |
logging.basicConfig(level=logging.INFO)
|
| 12 |
logger = logging.getLogger(__name__)
|
| 13 |
|
|
|
|
| 31 |
os.makedirs(IMAGE_DIR)
|
| 32 |
|
| 33 |
try:
|
| 34 |
+
logger.info(f"Syncing from {DATASET_REPO}...")
|
| 35 |
snapshot_download(repo_id=DATASET_REPO, repo_type="dataset", token=token, local_dir=".")
|
| 36 |
|
| 37 |
zip_name = "orbiitt_db.zip"
|
| 38 |
if os.path.exists(zip_name):
|
|
|
|
| 39 |
with zipfile.ZipFile(zip_name, 'r') as z:
|
| 40 |
z.extractall("temp_extract")
|
| 41 |
|
|
|
|
| 42 |
for root, _, files in os.walk("temp_extract"):
|
| 43 |
for f in files:
|
| 44 |
src = os.path.join(root, f)
|
| 45 |
|
| 46 |
+
# 1. DATABASE FIX: Force rename to 'orbiitt.db'
|
| 47 |
+
if f in ["orbiitt_db", "orbiitt_db.db", "orbiitt.db"] or f.endswith(".db"):
|
| 48 |
if not os.path.exists(DB_TARGET):
|
| 49 |
shutil.move(src, f"./{DB_TARGET}")
|
| 50 |
+
logger.info(f"DB Found: {f}")
|
| 51 |
|
| 52 |
+
# 2. IMAGE FIX: Remove spaces and %20 from filenames
|
| 53 |
elif f.lower().endswith(('.png', '.jpg', '.jpeg', '.webp')):
|
| 54 |
+
# Unquote removes %20, replace(' ', '_') removes spaces
|
| 55 |
+
clean_name = urllib.parse.unquote(f).replace(" ", "_")
|
| 56 |
+
dest = os.path.join(IMAGE_DIR, clean_name)
|
| 57 |
+
shutil.move(src, dest)
|
| 58 |
|
| 59 |
shutil.rmtree("temp_extract")
|
| 60 |
|
| 61 |
+
# Cleanup: Rename any files left in root that should be DB
|
| 62 |
+
if os.path.exists("orbiitt_db") and not os.path.exists(DB_TARGET):
|
| 63 |
+
shutil.move("orbiitt_db", DB_TARGET)
|
| 64 |
+
|
| 65 |
except Exception as e:
|
| 66 |
+
logger.error(f"Setup Error: {e}")
|
| 67 |
|
| 68 |
setup_environment()
|
| 69 |
|
| 70 |
+
app.mount("/Productimages", StaticFiles(directory=IMAGE_DIR), name="Productimages")
|
|
|
|
|
|
|
| 71 |
|
|
|
|
| 72 |
engine = None
|
| 73 |
try:
|
| 74 |
from orbiitt_engine import OrbiittEngine
|
| 75 |
if os.path.exists(DB_TARGET):
|
| 76 |
engine = OrbiittEngine()
|
| 77 |
+
logger.info("ENGINE ONLINE")
|
| 78 |
except Exception as e:
|
| 79 |
+
logger.error(f"ENGINE ERROR: {e}")
|
| 80 |
|
| 81 |
@app.get("/")
|
| 82 |
+
def health():
|
|
|
|
|
|
|
| 83 |
imgs = os.listdir(IMAGE_DIR) if os.path.exists(IMAGE_DIR) else []
|
| 84 |
return {
|
| 85 |
+
"engine": engine is not None,
|
| 86 |
+
"db": os.path.exists(DB_TARGET),
|
| 87 |
+
"img_count": len(imgs),
|
| 88 |
+
"first_img": imgs[0] if imgs else None
|
|
|
|
|
|
|
| 89 |
}
|
| 90 |
|
| 91 |
@app.post("/search")
|
| 92 |
async def search(text: str = Form(None), weight: float = Form(0.5), file: UploadFile = File(None)):
|
| 93 |
+
if not engine: raise HTTPException(503, detail="Engine Offline")
|
|
|
|
|
|
|
| 94 |
t_path = None
|
| 95 |
try:
|
| 96 |
if file:
|
| 97 |
+
t_path = f"tmp_{file.filename}"
|
| 98 |
+
with open(t_path, "wb") as b: b.write(await file.read())
|
|
|
|
| 99 |
|
| 100 |
results = engine.search(text_query=text, image_file=t_path, text_weight=weight)
|
| 101 |
+
|
| 102 |
+
# 3. FIX RESULT PATHS: Ensure the frontend gets the "Clean" names
|
| 103 |
+
for r in results:
|
| 104 |
+
if 'url' in r:
|
| 105 |
+
r['url'] = r['url'].replace(" ", "_")
|
| 106 |
+
|
| 107 |
return {"results": results}
|
| 108 |
except Exception as e:
|
|
|
|
| 109 |
return {"results": [], "error": str(e)}
|
| 110 |
finally:
|
| 111 |
+
if t_path and os.path.exists(t_path): os.remove(t_path)
|
|
|
|
| 112 |
|
| 113 |
if __name__ == "__main__":
|
| 114 |
import uvicorn
|