Update app/main.py
Browse files- app/main.py +38 -0
app/main.py
CHANGED
|
@@ -10,7 +10,10 @@ Run: uvicorn app.main:app --host 0.0.0.0 --port 8000
|
|
| 10 |
|
| 11 |
from __future__ import annotations
|
| 12 |
|
|
|
|
| 13 |
import json
|
|
|
|
|
|
|
| 14 |
from datetime import date
|
| 15 |
from typing import List
|
| 16 |
|
|
@@ -19,6 +22,7 @@ from fastapi import FastAPI, HTTPException
|
|
| 19 |
from fastapi.responses import StreamingResponse
|
| 20 |
from pydantic import BaseModel
|
| 21 |
|
|
|
|
| 22 |
from app.graph.builder import get_graph
|
| 23 |
|
| 24 |
app = FastAPI(title="Sattva AI API")
|
|
@@ -178,5 +182,39 @@ async def chat_stream_endpoint(request: ChatRequest):
|
|
| 178 |
return StreamingResponse(event_generator(), media_type="text/event-stream")
|
| 179 |
|
| 180 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
if __name__ == "__main__":
|
| 182 |
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
|
| 10 |
|
| 11 |
from __future__ import annotations
|
| 12 |
|
| 13 |
+
import io
|
| 14 |
import json
|
| 15 |
+
import os
|
| 16 |
+
import zipfile
|
| 17 |
from datetime import date
|
| 18 |
from typing import List
|
| 19 |
|
|
|
|
| 22 |
from fastapi.responses import StreamingResponse
|
| 23 |
from pydantic import BaseModel
|
| 24 |
|
| 25 |
+
from app.config import settings
|
| 26 |
from app.graph.builder import get_graph
|
| 27 |
|
| 28 |
app = FastAPI(title="Sattva AI API")
|
|
|
|
| 182 |
return StreamingResponse(event_generator(), media_type="text/event-stream")
|
| 183 |
|
| 184 |
|
| 185 |
+
@app.get("/download_store")
|
| 186 |
+
def download_store():
|
| 187 |
+
"""Zip and download the built Chroma vector store (chroma_db_unified/).
|
| 188 |
+
|
| 189 |
+
Use this once the build is 100% done (after the `Created unified vector store
|
| 190 |
+
with N chunks` log) to pull the store off the Space and reuse it locally /
|
| 191 |
+
commit it — so you never have to rebuild from scratch.
|
| 192 |
+
|
| 193 |
+
curl -o chroma_db_unified.zip https://<space>.hf.space/download_store
|
| 194 |
+
unzip chroma_db_unified.zip
|
| 195 |
+
|
| 196 |
+
Returns 404 if the store hasn't been built yet (empty/missing directory).
|
| 197 |
+
"""
|
| 198 |
+
base = os.path.join(os.getcwd(), settings.RAG_PERSIST_DIR) # chroma_db_unified
|
| 199 |
+
if not os.path.isdir(base) or not os.listdir(base):
|
| 200 |
+
raise HTTPException(status_code=404, detail="Vector store not built yet.")
|
| 201 |
+
|
| 202 |
+
buf = io.BytesIO()
|
| 203 |
+
with zipfile.ZipFile(buf, "w", zipfile.ZIP_DEFLATED) as zf:
|
| 204 |
+
for root, _, files in os.walk(base):
|
| 205 |
+
for fname in files:
|
| 206 |
+
fpath = os.path.join(root, fname)
|
| 207 |
+
# arcname keeps the top-level `chroma_db_unified/` folder in the zip
|
| 208 |
+
arcname = os.path.relpath(fpath, os.path.dirname(base))
|
| 209 |
+
zf.write(fpath, arcname)
|
| 210 |
+
buf.seek(0)
|
| 211 |
+
|
| 212 |
+
return StreamingResponse(
|
| 213 |
+
buf,
|
| 214 |
+
media_type="application/zip",
|
| 215 |
+
headers={"Content-Disposition": f"attachment; filename={settings.RAG_PERSIST_DIR}.zip"},
|
| 216 |
+
)
|
| 217 |
+
|
| 218 |
+
|
| 219 |
if __name__ == "__main__":
|
| 220 |
uvicorn.run(app, host="0.0.0.0", port=8000)
|