Spaces:
Runtime error
Runtime error
Update main.py
Browse files12345678910111213141516171819202122232425262728293032 unmodified lines6364656667686970717273747578 unmodified lines15415515615715815954 unmodified lines21421521621721821922016 unmodified lines23723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432512 unmodified lines338339340341342343344345346127 unmodified lines474475476477478479480481# -*- coding: utf-8 -*-"""HF Space - main.py de substitution pour tests Qdrant / indexation minimaleEndpoints:- GET / → redirige vers UI_PATH (défaut: /ui)- GET /ui (UI_PATH) → UI Gradio- GET /health → healthcheck- GET /api → infos service- POST /wipe?project_id=XXX → supprime la collection Qdrant- POST /index → lance un job d'indexation- GET /status/{job_id} → état + logs du job- GET /collections/{proj}/count → count points dans Qdrant- POST /query → recherche sémantiqueENV attendues :- QDRANT_URL, QDRANT_API_KEY (requis pour upsert)- COLLECTION_PREFIX (défaut "proj_")- EMB_PROVIDER ("hf" par défaut, "dummy" sinon)- HF_EMBED_MODEL (défaut "BAAI/bge-m3")- HUGGINGFACEHUB_API_TOKEN (si EMB_PROVIDER=hf)- LOG_LEVEL (défaut DEBUG)- PORT (fourni par HF, défaut 7860)- UI_PATH (défaut "/ui")Dépendances suggérées :fastapi>=0.111, uvicorn>=0.30, httpx>=0.27, pydantic>=2.7, gradio>=4.43, numpy>=2.0"""from __future__ import annotations32 unmodified linesHF_EMBED_MODEL = os.getenv("HF_EMBED_MODEL", "BAAI/bge-m3")HF_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN", "")UI_PATH = os.getenv("UI_PATH", "/ui") # UI montée ici par défautif not QDRANT_URL or not QDRANT_API_KEY:LOG.warning("QDRANT_URL / QDRANT_API_KEY non fournis : l'upsert échouera.")if EMB_PROVIDER == "hf" and not HF_TOKEN:LOG.warning("EMB_PROVIDER=hf sans HUGGINGFACEHUB_API_TOKEN. Utilise EMB_PROVIDER=dummy pour tester sans token.")# ------------------------------------------------------------------------------# Schémas Pydantic78 unmodified linesi = jreturn resasync def ensure_collection(client: httpx.AsyncClient, coll: str, vector_size: int) -> None:"""Crée la collection Qdrant (distance=Cosine), ou la recrée si dim mismatch."""url = f"{QDRANT_URL}/collections/{coll}"54 unmodified linespayload = {"inputs": texts, "options": {"wait_for_model": True}}r = await client.post(url, headers=headers, json=payload, timeout=120)if r.status_code != 200:raise HTTPException(status_code=502, detail=f"HF Inference error: {r.text}")data = r.json()embeddings: List[List[float]] = []if isinstance(data, list):16 unmodified linesreturn outasync def embed_texts(client: httpx.AsyncClient, texts: List[str]) -> List[List[float]]:if EMB_PROVIDER == "hf":return await embed_hf(client, texts)return embed_dummy(texts, dim=128)# ------------------------------------------------------------------------------# Pipeline d'indexation# ------------------------------------------------------------------------------async def run_index_job(job: JobState, req: IndexRequest) -> None:job.stage = "embedding"job.total_files = len(req.files)job.log(f"Index start project={req.project_id} files={len(req.files)} chunk_size={req.chunk_size} overlap={req.overlap} batch_size={req.batch_size} store_text={req.store_text}")# Dédup global par hash du texte de fichierfile_hashes = [hash8(f.text) for f in req.files]uniq = len(set(file_hashes))if uniq != len(file_hashes):job.log(f"Attention: {len(file_hashes)-uniq} fichier(s) ont un texte identique (hash dupliqué).")# Chunkingrecords: List[Dict[str, Any]] = []for f in req.files:chunks = chunk_text(f.text, req.chunk_size, req.overlap)if not chunks:job.log(f"{f.path}: 0 chunk (trop court ou vide)")for idx, (start, end, ch) in enumerate(chunks):payload = {"path": f.path, "chunk": idx, "start": start, "end": end}if req.store_text:payload["text"] = chrecords.append({"payload": payload, "raw": ch})job.total_chunks = len(records)job.log(f"Total chunks = {job.total_chunks}")if job.total_chunks == 0:job.stage = "failed"job.errors.append("Aucun chunk à indexer.")job.finished_at = time.time()returnasync with httpx.AsyncClient(timeout=120) as client:# Warmup dimwarmup_vec = (await embed_texts(client, [records[0]["raw"]]))[0]vec_dim = len(warmup_vec)job.log(f"Warmup embeddings dim={vec_dim} provider={EMB_PROVIDER}")# Collection Qdrantcoll = f"{COLLECTION_PREFIX}{req.project_id}"await ensure_collection(client, coll, vector_size=vec_dim)job.stage = "upserting"batch_points: List[Dict[str, Any]] = []async def flush_batch():nonlocal batch_pointsif not batch_points:return 0added = await qdrant_upsert(client, coll, batch_points)job.upserted += addedjob.log(f"+{added} points upsert (total={job.upserted})")batch_points = []return addedEMB_BATCH = max(8, min(64, req.batch_size * 2))i = 0while i < len(records):sub = records[i : i + EMB_BATCH]texts = [r["raw"] for r in sub]vecs = await embed_texts(client, texts)if len(vecs) != len(sub):raise HTTPException(status_code=500, detail="Embedding batch size mismatch")job.embedded += len(vecs)for r, v in zip(sub, vecs):point = {"id": str(uuid.uuid4()), "vector": v, "payload": r["payload"]}batch_points.append(point)if len(batch_points) >= req.batch_size:await flush_batch()i += EMB_BATCHawait flush_batch()job.stage = "done"job.finished_at = time.time()job.log("Index job terminé.")# ------------------------------------------------------------------------------# FastAPI app + endpoints12 unmodified lines@fastapi_app.get("/api")async def api_info():return {"ok": True, "service": "remote-indexer-min", "qdrant": bool(QDRANT_URL), "emb_provider": EMB_PROVIDER, "ui_path": UI_PATH}# Redirige "/" → UI_PATH (ex.: /ui). Ça évite tout conflit avec la route racine.@fastapi_app.get("/")async def root_redirect():return RedirectResponse(url=UI_PATH, status_code=307)127 unmodified linesgr.Markdown("## 🔬 Remote Indexer — Tests sans console\n""Wipe → Index 2 docs → Status → Count → Query\n"f"- **Embeddings**: `{EMB_PROVIDER}` (model: `{HF_EMBED_MODEL}`)\n"f"- **Qdrant**: `{'OK' if QDRANT_URL else 'ABSENT'}`\n""Astuce: si pas de token HF, mets `EMB_PROVIDER=dummy`.")with gr.Row():project_tb = gr.Textbox(label="Project ID", value="DEEPWEB")jobid_tb = gr.Textbox(label="Job ID (pour Status)", value="", interactive=True)123456789101112131415161718192021222324252627282932 unmodified lines626364656667686970717273747576777878 unmodified lines15715815916016116216316416554 unmodified lines22022122222322422522622722816 unmodified lines24524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035112 unmodified lines364365366367368369370371372373374375376377378379380381382383384385386387388389390391392127 unmodified lines520521522523524525526527528# -*- coding: utf-8 -*-"""HF Space - main.py de substitution pour tests Qdrant / indexation minimale (robuste)Endpoints:- GET / → redirige vers UI_PATH (défaut: /ui)- GET /ui (UI_PATH) → UI Gradio- GET /health → healthcheck- GET /api → infos service- GET /debug/env → aperçu config (sans secrets)- POST /wipe?project_id=XXX → supprime la collection Qdrant- POST /index → lance un job d'indexation- GET /status/{job_id} → état + logs du job- GET /collections/{proj}/count → count points dans Qdrant- POST /query → recherche sémantiqueENV:- QDRANT_URL, QDRANT_API_KEY (requis pour upsert)- COLLECTION_PREFIX (défaut "proj_")- EMB_PROVIDER ("hf" par défaut, "dummy" sinon)- HF_EMBED_MODEL (défaut "BAAI/bge-m3")- HUGGINGFACEHUB_API_TOKEN (si EMB_PROVIDER=hf)- EMB_FALLBACK_TO_DUMMY (true/false) → si vrai, bascule dummy si HF indisponible- LOG_LEVEL (défaut DEBUG)- PORT (fourni par HF, défaut 7860)- UI_PATH (défaut "/ui")"""from __future__ import annotations32 unmodified linesHF_EMBED_MODEL = os.getenv("HF_EMBED_MODEL", "BAAI/bge-m3")HF_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN", "")EMB_FALLBACK_TO_DUMMY = os.getenv("EMB_FALLBACK_TO_DUMMY", "false").lower() in ("1","true","yes","on")UI_PATH = os.getenv("UI_PATH", "/ui") # UI montée ici par défautif not QDRANT_URL or not QDRANT_API_KEY:LOG.warning("QDRANT_URL / QDRANT_API_KEY non fournis : l'upsert échouera.")if EMB_PROVIDER == "hf" and not HF_TOKEN:LOG.warning("EMB_PROVIDER=hf sans HUGGINGFACEHUB_API_TOKEN. ""→ soit définis le token, soit mets EMB_PROVIDER=dummy, ""soit active EMB_FALLBACK_TO_DUMMY=true.")# ------------------------------------------------------------------------------# Schémas Pydantic78 unmodified linesi = jreturn res# ------------------------------------------------------------------------------# Qdrant helpers# ------------------------------------------------------------------------------async def ensure_collection(client: httpx.AsyncClient, coll: str, vector_size: int) -> None:"""Crée la collection Qdrant (distance=Cosine), ou la recrée si dim mismatch."""url = f"{QDRANT_URL}/collections/{coll}"54 unmodified linespayload = {"inputs": texts, "options": {"wait_for_model": True}}r = await client.post(url, headers=headers, json=payload, timeout=120)if r.status_code != 200:detail = r.textLOG.error(f"HF Inference error {r.status_code}: {detail[:400]}")raise HTTPException(status_code=502, detail=f"HF Inference error {r.status_code}: {detail}")data = r.json()embeddings: List[List[float]] = []if isinstance(data, list):16 unmodified linesreturn outasync def embed_texts(client: httpx.AsyncClient, texts: List[str]) -> List[List[float]]:# Fallback optionnel si HF indisponibleif EMB_PROVIDER == "hf":try:return await embed_hf(client, texts)except Exception as e:if EMB_FALLBACK_TO_DUMMY:LOG.warning(f"Fallback embeddings → dummy (cause: {e})")return embed_dummy(texts, dim=128)raisereturn embed_dummy(texts, dim=128)# ------------------------------------------------------------------------------# Pipeline d'indexation (robuste)# ------------------------------------------------------------------------------async def run_index_job(job: JobState, req: IndexRequest) -> None:try:job.stage = "embedding"job.total_files = len(req.files)job.log(f"Index start project={req.project_id} files={len(req.files)} "f"chunk_size={req.chunk_size} overlap={req.overlap} batch_size={req.batch_size} store_text={req.store_text} "f"provider={EMB_PROVIDER} model={HF_EMBED_MODEL}")# Dédup global par hash du texte de fichierfile_hashes = [hash8(f.text) for f in req.files]uniq = len(set(file_hashes))if uniq != len(file_hashes):job.log(f"Attention: {len(file_hashes)-uniq} fichier(s) ont un texte identique (hash dupliqué).")# Chunkingrecords: List[Dict[str, Any]] = []for f in req.files:chunks = chunk_text(f.text, req.chunk_size, req.overlap)if not chunks:job.log(f"{f.path}: 0 chunk (trop court ou vide)")for idx, (start, end, ch) in enumerate(chunks):payload = {"path": f.path, "chunk": idx, "start": start, "end": end}if req.store_text:payload["text"] = chrecords.append({"payload": payload, "raw": ch})job.total_chunks = len(records)job.log(f"Total chunks = {job.total_chunks}")if job.total_chunks == 0:job.stage = "failed"job.errors.append("Aucun chunk à indexer.")job.finished_at = time.time()returnasync with httpx.AsyncClient(timeout=120) as client:# Warmup dimwarmup_vec = (await embed_texts(client, [records[0]["raw"]]))[0]vec_dim = len(warmup_vec)job.log(f"Warmup embeddings dim={vec_dim}")# Collection Qdrantcoll = f"{COLLECTION_PREFIX}{req.project_id}"await ensure_collection(client, coll, vector_size=vec_dim)job.log(f"Collection prête: {coll} (dim={vec_dim})")job.stage = "upserting"batch_points: List[Dict[str, Any]] = []async def flush_batch():nonlocal batch_pointsif not batch_points:return 0added = await qdrant_upsert(client, coll, batch_points)job.upserted += addedjob.log(f"+{added} points upsert (total={job.upserted})")batch_points = []return addedEMB_BATCH = max(8, min(64, req.batch_size * 2))i = 0while i < len(records):sub = records[i : i + EMB_BATCH]texts = [r["raw"] for r in sub]vecs = await embed_texts(client, texts)if len(vecs) != len(sub):raise HTTPException(status_code=500, detail="Embedding batch size mismatch")job.embedded += len(vecs)for r, v in zip(sub, vecs):point = {"id": str(uuid.uuid4()), "vector": v, "payload": r["payload"]}batch_points.append(point)if len(batch_points) >= req.batch_size:await flush_batch()i += EMB_BATCHawait flush_batch()job.stage = "done"job.finished_at = time.time()job.log("Index job terminé.")except Exception as e:job.stage = "failed"job.errors.append(str(e))job.finished_at = time.time()job.log(f"❌ Exception: {e}")# ------------------------------------------------------------------------------# FastAPI app + endpoints12 unmodified lines@fastapi_app.get("/api")async def api_info():return {"ok": True,"service": "remote-indexer-min","qdrant": bool(QDRANT_URL),"emb_provider": EMB_PROVIDER,"hf_model": HF_EMBED_MODEL,"ui_path": UI_PATH,"fallback_to_dummy": EMB_FALLBACK_TO_DUMMY,}@fastapi_app.get("/debug/env")async def debug_env():return {"qdrant_url_set": bool(QDRANT_URL),"qdrant_key_set": bool(QDRANT_API_KEY),"emb_provider": EMB_PROVIDER,"hf_model": HF_EMBED_MODEL,"hf_token_set": bool(HF_TOKEN),"fallback_to_dummy": EMB_FALLBACK_TO_DUMMY,"collection_prefix": COLLECTION_PREFIX,}# Redirige "/" → UI_PATH (ex.: /ui).@fastapi_app.get("/")async def root_redirect():return RedirectResponse(url=UI_PATH, status_code=307)127 unmodified linesgr.Markdown("## 🔬 Remote Indexer — Tests sans console\n""Wipe → Index 2 docs → Status → Count → Query\n"f"- **Embeddings**: `{EMB_PROVIDER}` (model: `{HF_EMBED_MODEL}`)\n"f"- **Token HF présent**: `{'oui' if bool(HF_TOKEN) else 'non'}` — "f"**Fallback dummy**: `{'on' if EMB_FALLBACK_TO_DUMMY else 'off'}`\n"f"- **Qdrant**: `{'OK' if QDRANT_URL else 'ABSENT'}`")with gr.Row():project_tb = gr.Textbox(label="Project ID", value="DEEPWEB")jobid_tb = gr.Textbox(label="Job ID (pour Status)", value="", interactive=True)
main.py+134-87