Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -3,24 +3,25 @@
|
|
| 3 |
HF Space - main.py de substitution pour tests Qdrant / indexation minimale
|
| 4 |
|
| 5 |
Endpoints:
|
| 6 |
-
-
|
| 7 |
-
-
|
| 8 |
-
- GET /
|
| 9 |
-
- GET /
|
| 10 |
-
- POST /
|
| 11 |
-
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
ENV attendues :
|
| 16 |
-
- QDRANT_URL
|
| 17 |
-
-
|
| 18 |
-
-
|
| 19 |
-
-
|
| 20 |
-
- HF_EMBED_MODEL : "BAAI/bge-m3" par défaut
|
| 21 |
- HUGGINGFACEHUB_API_TOKEN (si EMB_PROVIDER=hf)
|
| 22 |
-
- LOG_LEVEL
|
| 23 |
-
- PORT
|
|
|
|
| 24 |
|
| 25 |
Dépendances suggérées :
|
| 26 |
fastapi>=0.111, uvicorn>=0.30, httpx>=0.27, pydantic>=2.7, gradio>=4.43, numpy>=2.0
|
|
@@ -41,6 +42,7 @@ import uvicorn
|
|
| 41 |
from pydantic import BaseModel, Field, ValidationError
|
| 42 |
from fastapi import FastAPI, HTTPException, Query
|
| 43 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 44 |
import gradio as gr
|
| 45 |
|
| 46 |
# ------------------------------------------------------------------------------
|
|
@@ -61,6 +63,8 @@ EMB_PROVIDER = os.getenv("EMB_PROVIDER", "hf").lower() # "hf" | "dummy"
|
|
| 61 |
HF_EMBED_MODEL = os.getenv("HF_EMBED_MODEL", "BAAI/bge-m3")
|
| 62 |
HF_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN", "")
|
| 63 |
|
|
|
|
|
|
|
| 64 |
if not QDRANT_URL or not QDRANT_API_KEY:
|
| 65 |
LOG.warning("QDRANT_URL / QDRANT_API_KEY non fournis : l'upsert échouera.")
|
| 66 |
|
|
@@ -332,9 +336,14 @@ fastapi_app.add_middleware(
|
|
| 332 |
async def health():
|
| 333 |
return {"status": "ok"}
|
| 334 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
@fastapi_app.get("/")
|
| 336 |
-
async def
|
| 337 |
-
return
|
| 338 |
|
| 339 |
@fastapi_app.post("/wipe")
|
| 340 |
async def wipe(project_id: str = Query(..., min_length=1)):
|
|
@@ -491,11 +500,10 @@ with gr.Blocks(title="Remote Indexer - Minimal Test", analytics_enabled=False) a
|
|
| 491 |
count_btn.click(ui_count, inputs=[project_tb], outputs=[out_log])
|
| 492 |
query_btn.click(ui_query, inputs=[project_tb, query_tb, topk], outputs=[query_out])
|
| 493 |
|
| 494 |
-
# Monte l'UI Gradio sur la FastAPI
|
| 495 |
-
app = gr.mount_gradio_app(fastapi_app, ui, path=
|
| 496 |
|
| 497 |
if __name__ == "__main__":
|
| 498 |
-
# Démarre Uvicorn pour les Spaces Docker (CMD: python -u /app/main.py)
|
| 499 |
port = int(os.getenv("PORT", "7860"))
|
| 500 |
-
LOG.info(f"Démarrage Uvicorn sur 0.0.0.0:{port}")
|
| 501 |
uvicorn.run(app, host="0.0.0.0", port=port)
|
|
|
|
| 3 |
HF Space - main.py de substitution pour tests Qdrant / indexation minimale
|
| 4 |
|
| 5 |
Endpoints:
|
| 6 |
+
- GET / → redirige vers UI_PATH (défaut: /ui)
|
| 7 |
+
- GET /ui (UI_PATH) → UI Gradio
|
| 8 |
+
- GET /health → healthcheck
|
| 9 |
+
- GET /api → infos service
|
| 10 |
+
- POST /wipe?project_id=XXX → supprime la collection Qdrant
|
| 11 |
+
- POST /index → lance un job d'indexation
|
| 12 |
+
- GET /status/{job_id} → état + logs du job
|
| 13 |
+
- GET /collections/{proj}/count → count points dans Qdrant
|
| 14 |
+
- POST /query → recherche sémantique
|
| 15 |
|
| 16 |
ENV attendues :
|
| 17 |
+
- QDRANT_URL, QDRANT_API_KEY (requis pour upsert)
|
| 18 |
+
- COLLECTION_PREFIX (défaut "proj_")
|
| 19 |
+
- EMB_PROVIDER ("hf" par défaut, "dummy" sinon)
|
| 20 |
+
- HF_EMBED_MODEL (défaut "BAAI/bge-m3")
|
|
|
|
| 21 |
- HUGGINGFACEHUB_API_TOKEN (si EMB_PROVIDER=hf)
|
| 22 |
+
- LOG_LEVEL (défaut DEBUG)
|
| 23 |
+
- PORT (fourni par HF, défaut 7860)
|
| 24 |
+
- UI_PATH (défaut "/ui")
|
| 25 |
|
| 26 |
Dépendances suggérées :
|
| 27 |
fastapi>=0.111, uvicorn>=0.30, httpx>=0.27, pydantic>=2.7, gradio>=4.43, numpy>=2.0
|
|
|
|
| 42 |
from pydantic import BaseModel, Field, ValidationError
|
| 43 |
from fastapi import FastAPI, HTTPException, Query
|
| 44 |
from fastapi.middleware.cors import CORSMiddleware
|
| 45 |
+
from fastapi.responses import RedirectResponse, JSONResponse
|
| 46 |
import gradio as gr
|
| 47 |
|
| 48 |
# ------------------------------------------------------------------------------
|
|
|
|
| 63 |
HF_EMBED_MODEL = os.getenv("HF_EMBED_MODEL", "BAAI/bge-m3")
|
| 64 |
HF_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN", "")
|
| 65 |
|
| 66 |
+
UI_PATH = os.getenv("UI_PATH", "/ui") # UI montée ici par défaut
|
| 67 |
+
|
| 68 |
if not QDRANT_URL or not QDRANT_API_KEY:
|
| 69 |
LOG.warning("QDRANT_URL / QDRANT_API_KEY non fournis : l'upsert échouera.")
|
| 70 |
|
|
|
|
| 336 |
async def health():
|
| 337 |
return {"status": "ok"}
|
| 338 |
|
| 339 |
+
@fastapi_app.get("/api")
|
| 340 |
+
async def api_info():
|
| 341 |
+
return {"ok": True, "service": "remote-indexer-min", "qdrant": bool(QDRANT_URL), "emb_provider": EMB_PROVIDER, "ui_path": UI_PATH}
|
| 342 |
+
|
| 343 |
+
# Redirige "/" → UI_PATH (ex.: /ui). Ça évite tout conflit avec la route racine.
|
| 344 |
@fastapi_app.get("/")
|
| 345 |
+
async def root_redirect():
|
| 346 |
+
return RedirectResponse(url=UI_PATH, status_code=307)
|
| 347 |
|
| 348 |
@fastapi_app.post("/wipe")
|
| 349 |
async def wipe(project_id: str = Query(..., min_length=1)):
|
|
|
|
| 500 |
count_btn.click(ui_count, inputs=[project_tb], outputs=[out_log])
|
| 501 |
query_btn.click(ui_query, inputs=[project_tb, query_tb, topk], outputs=[query_out])
|
| 502 |
|
| 503 |
+
# Monte l'UI Gradio sur la FastAPI au chemin UI_PATH
|
| 504 |
+
app = gr.mount_gradio_app(fastapi_app, ui, path=UI_PATH)
|
| 505 |
|
| 506 |
if __name__ == "__main__":
|
|
|
|
| 507 |
port = int(os.getenv("PORT", "7860"))
|
| 508 |
+
LOG.info(f"Démarrage Uvicorn sur 0.0.0.0:{port} (UI_PATH={UI_PATH})")
|
| 509 |
uvicorn.run(app, host="0.0.0.0", port=port)
|