Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,364 +1,375 @@
|
|
| 1 |
-
"""
|
| 2 |
-
FastAPI Backend for Multi-PDF RAG System with Per-User Storage
|
| 3 |
-
===============================================================
|
| 4 |
-
Secure multi-user API with:
|
| 5 |
-
- API key authentication
|
| 6 |
-
- Per-user storage isolation
|
| 7 |
-
- PDF upload and management
|
| 8 |
-
- RAG-based question answering
|
| 9 |
-
- HF persistent storage
|
| 10 |
-
"""
|
| 11 |
-
|
| 12 |
-
import os
|
| 13 |
-
import asyncio
|
| 14 |
-
from typing import List, Optional, Dict
|
| 15 |
-
from fastapi import FastAPI, UploadFile, File, HTTPException, Depends
|
| 16 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 17 |
-
from pydantic import BaseModel
|
| 18 |
-
from threading import Lock
|
| 19 |
-
from rag_engine import RAGEngine
|
| 20 |
-
from hf_storage import create_hf_storage_manager
|
| 21 |
-
from auth import get_current_user
|
| 22 |
-
from
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
#
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
# ============================================
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
self.
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
# ============================================
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
# ============================================
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
print("
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
#
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
"""
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
)
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
)
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
FastAPI Backend for Multi-PDF RAG System with Per-User Storage
|
| 3 |
+
===============================================================
|
| 4 |
+
Secure multi-user API with:
|
| 5 |
+
- API key authentication
|
| 6 |
+
- Per-user storage isolation
|
| 7 |
+
- PDF upload and management
|
| 8 |
+
- RAG-based question answering
|
| 9 |
+
- HF persistent storage
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
import os
|
| 13 |
+
import asyncio
|
| 14 |
+
from typing import List, Optional, Dict
|
| 15 |
+
from fastapi import FastAPI, UploadFile, File, HTTPException, Depends
|
| 16 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 17 |
+
from pydantic import BaseModel
|
| 18 |
+
from threading import Lock
|
| 19 |
+
from rag_engine import RAGEngine
|
| 20 |
+
from hf_storage import create_hf_storage_manager
|
| 21 |
+
from auth import get_current_user
|
| 22 |
+
from user_management import create_hf_user_manager
|
| 23 |
+
from dotenv import load_dotenv
|
| 24 |
+
|
| 25 |
+
# ============================================
|
| 26 |
+
# CONFIGURATION
|
| 27 |
+
# ============================================
|
| 28 |
+
|
| 29 |
+
load_dotenv()
|
| 30 |
+
|
| 31 |
+
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
|
| 32 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 33 |
+
HF_REPO = os.environ.get("HF_REPO", "Hamza4100/multi-pdf-storage")
|
| 34 |
+
|
| 35 |
+
if not GEMINI_API_KEY:
|
| 36 |
+
raise RuntimeError("β GEMINI_API_KEY not set")
|
| 37 |
+
|
| 38 |
+
hf_storage = create_hf_storage_manager(hf_token=HF_TOKEN, hf_repo=HF_REPO)
|
| 39 |
+
hf_user_manager = create_hf_user_manager(hf_token=HF_TOKEN, hf_repo=HF_REPO)
|
| 40 |
+
|
| 41 |
+
app = FastAPI(
|
| 42 |
+
title="Multi-PDF RAG System",
|
| 43 |
+
description="Secure multi-user RAG API with persistent storage",
|
| 44 |
+
version="2.0.0"
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
app.add_middleware(
|
| 48 |
+
CORSMiddleware,
|
| 49 |
+
allow_origins=["*"],
|
| 50 |
+
allow_credentials=True,
|
| 51 |
+
allow_methods=["*"],
|
| 52 |
+
allow_headers=["*"],
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
# ============================================
|
| 56 |
+
# PER-USER RAG ENGINE MANAGER
|
| 57 |
+
# ============================================
|
| 58 |
+
|
| 59 |
+
class UserRAGManager:
|
| 60 |
+
"""Manages per-user RAG engine instances with lazy loading."""
|
| 61 |
+
|
| 62 |
+
def __init__(self):
|
| 63 |
+
self.engines: Dict[str, RAGEngine] = {}
|
| 64 |
+
self.locks: Dict[str, Lock] = {}
|
| 65 |
+
self.global_lock = Lock()
|
| 66 |
+
|
| 67 |
+
def get_user_lock(self, user_id: str) -> Lock:
|
| 68 |
+
"""Get or create lock for user."""
|
| 69 |
+
with self.global_lock:
|
| 70 |
+
if user_id not in self.locks:
|
| 71 |
+
self.locks[user_id] = Lock()
|
| 72 |
+
return self.locks[user_id]
|
| 73 |
+
|
| 74 |
+
async def get_engine(self, user_id: str) -> RAGEngine:
|
| 75 |
+
"""Get or create RAG engine for user (lazy loading)."""
|
| 76 |
+
if user_id in self.engines:
|
| 77 |
+
return self.engines[user_id]
|
| 78 |
+
|
| 79 |
+
user_lock = self.get_user_lock(user_id)
|
| 80 |
+
|
| 81 |
+
with user_lock:
|
| 82 |
+
if user_id in self.engines:
|
| 83 |
+
return self.engines[user_id]
|
| 84 |
+
|
| 85 |
+
print(f"π§ Initializing RAG for user {user_id}...")
|
| 86 |
+
|
| 87 |
+
# Sync from HF
|
| 88 |
+
await asyncio.to_thread(hf_storage.sync_storage_from_hf, user_id)
|
| 89 |
+
|
| 90 |
+
# User-specific paths
|
| 91 |
+
base_dir = os.path.dirname(os.path.abspath(__file__))
|
| 92 |
+
user_storage_dir = os.path.join(base_dir, "users", user_id, "storage")
|
| 93 |
+
|
| 94 |
+
# Initialize engine
|
| 95 |
+
engine = await asyncio.to_thread(
|
| 96 |
+
RAGEngine,
|
| 97 |
+
gemini_api_key=GEMINI_API_KEY,
|
| 98 |
+
storage_dir=user_storage_dir
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
self.engines[user_id] = engine
|
| 102 |
+
print(f"β
RAG ready for user {user_id}")
|
| 103 |
+
|
| 104 |
+
return engine
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
rag_manager = UserRAGManager()
|
| 108 |
+
|
| 109 |
+
# ============================================
|
| 110 |
+
# MODELS
|
| 111 |
+
# ============================================
|
| 112 |
+
|
| 113 |
+
class UploadResponse(BaseModel):
|
| 114 |
+
document_id: str
|
| 115 |
+
filename: str
|
| 116 |
+
status: str
|
| 117 |
+
message: str
|
| 118 |
+
pages: Optional[int] = None
|
| 119 |
+
chunks: Optional[int] = None
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
class QueryRequest(BaseModel):
|
| 123 |
+
question: str
|
| 124 |
+
top_k: Optional[int] = 5
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
class QueryResponse(BaseModel):
|
| 128 |
+
answer: str
|
| 129 |
+
sources: List[dict]
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
class DocumentInfo(BaseModel):
|
| 133 |
+
doc_id: str
|
| 134 |
+
filename: str
|
| 135 |
+
upload_timestamp: str
|
| 136 |
+
num_chunks: int
|
| 137 |
+
num_pages: int
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
class StatsResponse(BaseModel):
|
| 141 |
+
total_documents: int
|
| 142 |
+
total_chunks: int
|
| 143 |
+
index_size: int
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
class DeleteResponse(BaseModel):
|
| 147 |
+
status: str
|
| 148 |
+
message: str
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
# ============================================
|
| 152 |
+
# STARTUP
|
| 153 |
+
# ============================================
|
| 154 |
+
|
| 155 |
+
@app.on_event("startup")
|
| 156 |
+
async def startup_event():
|
| 157 |
+
print("π Multi-PDF RAG System v2.0")
|
| 158 |
+
print(f"π¦ HF Storage: {'Enabled' if hf_storage.enabled else 'Disabled'}")
|
| 159 |
+
|
| 160 |
+
# Load and display user count from HF
|
| 161 |
+
if hf_user_manager.enabled:
|
| 162 |
+
users_count = len(hf_user_manager.get_all_users())
|
| 163 |
+
print(f"π₯ Loaded {users_count} user(s) from HF repository")
|
| 164 |
+
print(f"β
User management: Enabled (HF-based)")
|
| 165 |
+
else:
|
| 166 |
+
print(f"β οΈ User management: Disabled (check HF_TOKEN and HF_REPO)")
|
| 167 |
+
|
| 168 |
+
print("β
Server ready (per-user lazy loading)")
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
# ============================================
|
| 172 |
+
# ENDPOINTS
|
| 173 |
+
# ============================================
|
| 174 |
+
|
| 175 |
+
@app.get("/health")
|
| 176 |
+
async def health_check():
|
| 177 |
+
"""Health check (no auth required)."""
|
| 178 |
+
return {"status": "ok"}
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
@app.post("/upload", response_model=UploadResponse)
|
| 182 |
+
async def upload_pdf(
|
| 183 |
+
file: UploadFile = File(...),
|
| 184 |
+
user_id: str = Depends(get_current_user)
|
| 185 |
+
):
|
| 186 |
+
"""
|
| 187 |
+
Upload PDF for authenticated user.
|
| 188 |
+
Requires: X-API-KEY header
|
| 189 |
+
"""
|
| 190 |
+
# Validate PDF
|
| 191 |
+
if not file.filename.lower().endswith('.pdf'):
|
| 192 |
+
raise HTTPException(400, "Only PDF files allowed")
|
| 193 |
+
|
| 194 |
+
if file.content_type and file.content_type not in ['application/pdf']:
|
| 195 |
+
raise HTTPException(400, "Invalid MIME type")
|
| 196 |
+
|
| 197 |
+
# Read content
|
| 198 |
+
content = await file.read()
|
| 199 |
+
|
| 200 |
+
# Size limit (10MB)
|
| 201 |
+
if len(content) > 10 * 1024 * 1024:
|
| 202 |
+
raise HTTPException(413, "File too large (max 10MB)")
|
| 203 |
+
|
| 204 |
+
try:
|
| 205 |
+
engine = await rag_manager.get_engine(user_id)
|
| 206 |
+
user_lock = rag_manager.get_user_lock(user_id)
|
| 207 |
+
|
| 208 |
+
with user_lock:
|
| 209 |
+
result = await asyncio.to_thread(
|
| 210 |
+
engine.upload_document,
|
| 211 |
+
filename=file.filename,
|
| 212 |
+
file_content=content,
|
| 213 |
+
action="auto"
|
| 214 |
+
)
|
| 215 |
+
|
| 216 |
+
if result["status"] == "success":
|
| 217 |
+
# Save PDF to user's uploaded_pdfs folder
|
| 218 |
+
base_dir = os.path.dirname(os.path.abspath(__file__))
|
| 219 |
+
user_pdfs_dir = os.path.join(base_dir, "users", user_id, "uploaded_pdfs")
|
| 220 |
+
os.makedirs(user_pdfs_dir, exist_ok=True)
|
| 221 |
+
|
| 222 |
+
pdf_path = os.path.join(user_pdfs_dir, file.filename)
|
| 223 |
+
with open(pdf_path, "wb") as f:
|
| 224 |
+
f.write(content)
|
| 225 |
+
|
| 226 |
+
await asyncio.to_thread(
|
| 227 |
+
hf_storage.push_storage_to_hf,
|
| 228 |
+
user_id,
|
| 229 |
+
f"Upload {file.filename}"
|
| 230 |
+
)
|
| 231 |
+
|
| 232 |
+
print(f"β
Upload success for user {user_id}: {file.filename}")
|
| 233 |
+
|
| 234 |
+
return UploadResponse(
|
| 235 |
+
document_id=result.get("doc_id", ""),
|
| 236 |
+
filename=file.filename,
|
| 237 |
+
status="success",
|
| 238 |
+
message="Uploaded successfully",
|
| 239 |
+
pages=result.get("pages"),
|
| 240 |
+
chunks=result.get("chunks")
|
| 241 |
+
)
|
| 242 |
+
else:
|
| 243 |
+
raise HTTPException(400, result.get("message", "Upload failed"))
|
| 244 |
+
|
| 245 |
+
except HTTPException:
|
| 246 |
+
raise
|
| 247 |
+
except Exception as e:
|
| 248 |
+
print(f"β Upload error (user {user_id}): {e}")
|
| 249 |
+
raise HTTPException(500, "Upload failed")
|
| 250 |
+
|
| 251 |
+
|
| 252 |
+
@app.post("/query", response_model=QueryResponse)
|
| 253 |
+
async def query_documents(
|
| 254 |
+
request: QueryRequest,
|
| 255 |
+
user_id: str = Depends(get_current_user)
|
| 256 |
+
):
|
| 257 |
+
"""
|
| 258 |
+
Query user's documents using RAG.
|
| 259 |
+
Requires: X-API-KEY header
|
| 260 |
+
"""
|
| 261 |
+
try:
|
| 262 |
+
engine = await rag_manager.get_engine(user_id)
|
| 263 |
+
|
| 264 |
+
result = await asyncio.to_thread(
|
| 265 |
+
engine.ask,
|
| 266 |
+
query=request.question,
|
| 267 |
+
top_k=request.top_k
|
| 268 |
+
)
|
| 269 |
+
|
| 270 |
+
print(f"β
Query success for user {user_id}")
|
| 271 |
+
|
| 272 |
+
return QueryResponse(
|
| 273 |
+
answer=result["answer"],
|
| 274 |
+
sources=result.get("sources", [])
|
| 275 |
+
)
|
| 276 |
+
|
| 277 |
+
except Exception as e:
|
| 278 |
+
print(f"β Query error (user {user_id}): {e}")
|
| 279 |
+
raise HTTPException(500, "Query failed")
|
| 280 |
+
|
| 281 |
+
|
| 282 |
+
@app.get("/documents", response_model=List[DocumentInfo])
|
| 283 |
+
async def get_documents(user_id: str = Depends(get_current_user)):
|
| 284 |
+
"""
|
| 285 |
+
Get all documents for authenticated user.
|
| 286 |
+
Requires: X-API-KEY header
|
| 287 |
+
"""
|
| 288 |
+
try:
|
| 289 |
+
engine = await rag_manager.get_engine(user_id)
|
| 290 |
+
documents = await asyncio.to_thread(engine.get_all_documents)
|
| 291 |
+
|
| 292 |
+
return [
|
| 293 |
+
DocumentInfo(
|
| 294 |
+
doc_id=doc["doc_id"],
|
| 295 |
+
filename=doc["filename"],
|
| 296 |
+
upload_timestamp=doc["upload_timestamp"],
|
| 297 |
+
num_chunks=doc["num_chunks"],
|
| 298 |
+
num_pages=doc["num_pages"]
|
| 299 |
+
)
|
| 300 |
+
for doc in documents
|
| 301 |
+
]
|
| 302 |
+
|
| 303 |
+
except Exception as e:
|
| 304 |
+
print(f"β Get documents error (user {user_id}): {e}")
|
| 305 |
+
raise HTTPException(500, "Failed to retrieve documents")
|
| 306 |
+
|
| 307 |
+
|
| 308 |
+
@app.delete("/documents/{doc_id}", response_model=DeleteResponse)
|
| 309 |
+
async def delete_document(
|
| 310 |
+
doc_id: str,
|
| 311 |
+
user_id: str = Depends(get_current_user)
|
| 312 |
+
):
|
| 313 |
+
"""
|
| 314 |
+
Delete document for authenticated user.
|
| 315 |
+
Requires: X-API-KEY header
|
| 316 |
+
"""
|
| 317 |
+
try:
|
| 318 |
+
engine = await rag_manager.get_engine(user_id)
|
| 319 |
+
user_lock = rag_manager.get_user_lock(user_id)
|
| 320 |
+
|
| 321 |
+
with user_lock:
|
| 322 |
+
result = await asyncio.to_thread(engine.delete_document, doc_id)
|
| 323 |
+
|
| 324 |
+
if result["status"] == "success":
|
| 325 |
+
await asyncio.to_thread(
|
| 326 |
+
hf_storage.push_storage_to_hf,
|
| 327 |
+
user_id,
|
| 328 |
+
f"Delete {doc_id}"
|
| 329 |
+
)
|
| 330 |
+
|
| 331 |
+
print(f"β
Delete success for user {user_id}: {doc_id}")
|
| 332 |
+
|
| 333 |
+
return DeleteResponse(
|
| 334 |
+
status="success",
|
| 335 |
+
message=result["message"]
|
| 336 |
+
)
|
| 337 |
+
else:
|
| 338 |
+
raise HTTPException(404, result["message"])
|
| 339 |
+
|
| 340 |
+
except HTTPException:
|
| 341 |
+
raise
|
| 342 |
+
except Exception as e:
|
| 343 |
+
print(f"β Delete error (user {user_id}): {e}")
|
| 344 |
+
raise HTTPException(500, "Deletion failed")
|
| 345 |
+
|
| 346 |
+
|
| 347 |
+
@app.get("/stats", response_model=StatsResponse)
|
| 348 |
+
async def get_stats(user_id: str = Depends(get_current_user)):
|
| 349 |
+
"""
|
| 350 |
+
Get stats for authenticated user.
|
| 351 |
+
Requires: X-API-KEY header
|
| 352 |
+
"""
|
| 353 |
+
try:
|
| 354 |
+
engine = await rag_manager.get_engine(user_id)
|
| 355 |
+
stats = await asyncio.to_thread(engine.get_stats)
|
| 356 |
+
|
| 357 |
+
return StatsResponse(
|
| 358 |
+
total_documents=stats["total_documents"],
|
| 359 |
+
total_chunks=stats["total_chunks"],
|
| 360 |
+
index_size=stats["index_size"]
|
| 361 |
+
)
|
| 362 |
+
|
| 363 |
+
except Exception as e:
|
| 364 |
+
print(f"β Stats error (user {user_id}): {e}")
|
| 365 |
+
raise HTTPException(500, "Failed to retrieve stats")
|
| 366 |
+
|
| 367 |
+
|
| 368 |
+
# ============================================
|
| 369 |
+
# MAIN
|
| 370 |
+
# ============================================
|
| 371 |
+
|
| 372 |
+
if __name__ == "__main__":
|
| 373 |
+
import uvicorn
|
| 374 |
+
port = int(os.environ.get("PORT", 8000))
|
| 375 |
+
uvicorn.run("main:app", host="0.0.0.0", port=port, reload=True)
|