Spaces:
Sleeping
Sleeping
Placebo Deploy commited on
Commit ·
000a857
1
Parent(s): 9b8540d
Implement dynamic zip image extraction route for mounted dataset
Browse files147 unmodified lines14814915015115215377 unmodified lines231232233234235236237238147 unmodified lines"supabase_anon_key": os.getenv("SUPABASE_ANON_KEY", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.your-anon-key-placeholder")}import sqlite3import time77 unmodified linesseen_keys.add(key)raw_img = d.metadata.get("image_path")if raw_img and not str(raw_img).startswith("http"):# Resolve against the user's requested Hugging Face datasetraw_img = f"https://huggingface.co/datasets/TrinetraLabs/Placebo_AI_DB/resolve/main/{raw_img}"unique_sources.append({"book_name": b, "page_number": p, "image_path": raw_img})# Inject metadata directly into the context so the LLM doesn't hallucinate citations147 unmodified lines14814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518677 unmodified lines264265266267268269270271272273147 unmodified lines"supabase_anon_key": os.getenv("SUPABASE_ANON_KEY", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.your-anon-key-placeholder")}import zipfileimport ioimport urllib.parsefrom fastapi.responses import Response# Dataset mount path in Hugging Face Spaces is typically /dataZIP_PATH = os.getenv("DATASET_MOUNT_PATH", "/data/data.zip")@app.get("/api/image")async def get_image(path: str):if not os.path.exists(ZIP_PATH):return Response(status_code=404, content=f"Dataset zip not found at {ZIP_PATH}. Please mount the dataset to your space.")try:with zipfile.ZipFile(ZIP_PATH, 'r') as zf:# Handle potential nested paths in the zip# If the user packed it as data/images/... we might need to search or just try the direct pathtry:content = zf.read(path)except KeyError:# Fallback: search for the filename anywhere in the zip if exact path failsfilename = path.split('/')[-1]matches = [name for name in zf.namelist() if name.endswith(filename)]if not matches:return Response(status_code=404, content="Image not found in zip archive.")content = zf.read(matches[0])ext = path.split('.')[-1].lower()media_type = "image/png" if ext == "png" else "image/jpeg" if ext in ["jpg", "jpeg"] else "application/octet-stream"return StreamingResponse(io.BytesIO(content), media_type=media_type)except Exception as e:return Response(status_code=500, content=f"Error reading zip: {str(e)}")import sqlite3import time77 unmodified linesseen_keys.add(key)raw_img = d.metadata.get("image_path")if raw_img and not str(raw_img).startswith("http"):# Resolve against our internal FastAPI Zip extraction endpointimport urllib.parseencoded_path = urllib.parse.quote(raw_img)raw_img = f"/api/image?path={encoded_path}"unique_sources.append({"book_name": b, "page_number": p, "image_path": raw_img})# Inject metadata directly into the context so the LLM doesn't hallucinate citations
src/app.py+37-2