Spaces:
Running
Running
Fix upload 500 error, preload embeddings, add error handling
Browse files- backend/api.py +29 -16
backend/api.py
CHANGED
|
@@ -21,6 +21,8 @@ def get_vectorstore():
|
|
| 21 |
return load_documents(embedding_model=get_embeddings())
|
| 22 |
|
| 23 |
|
|
|
|
|
|
|
| 24 |
BASE_DIR = Path("/app")
|
| 25 |
upload_dir = BASE_DIR / "uploads"
|
| 26 |
upload_dir.mkdir(parents=True, exist_ok=True)
|
|
@@ -48,6 +50,12 @@ system_stats = {
|
|
| 48 |
"start_time": datetime.now().isoformat()
|
| 49 |
}
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
# Info about API
|
| 52 |
@app.get("/")
|
| 53 |
async def root():
|
|
@@ -104,28 +112,33 @@ async def get_stats():
|
|
| 104 |
# This Endpoint upload Pdf and store into VectorDatabase
|
| 105 |
@app.post("/upload")
|
| 106 |
async def upload_file(file: UploadFile = File(...)):
|
| 107 |
-
|
| 108 |
-
|
|
|
|
| 109 |
|
| 110 |
-
|
| 111 |
|
| 112 |
-
|
| 113 |
-
|
| 114 |
|
| 115 |
-
|
| 116 |
|
| 117 |
-
|
| 118 |
-
|
| 119 |
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
-
return {
|
| 126 |
-
"message": "PDF uploaded and indexed successfully",
|
| 127 |
-
"chunks_created": len(chunked_docs)
|
| 128 |
-
}
|
| 129 |
|
| 130 |
from pydantic import BaseModel
|
| 131 |
|
|
|
|
| 21 |
return load_documents(embedding_model=get_embeddings())
|
| 22 |
|
| 23 |
|
| 24 |
+
|
| 25 |
+
|
| 26 |
BASE_DIR = Path("/app")
|
| 27 |
upload_dir = BASE_DIR / "uploads"
|
| 28 |
upload_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
| 50 |
"start_time": datetime.now().isoformat()
|
| 51 |
}
|
| 52 |
|
| 53 |
+
@app.on_event("startup")
|
| 54 |
+
def startup_event():
|
| 55 |
+
print("🔄 Preloading embedding model...")
|
| 56 |
+
get_embeddings()
|
| 57 |
+
print("✅ Embedding model loaded")
|
| 58 |
+
|
| 59 |
# Info about API
|
| 60 |
@app.get("/")
|
| 61 |
async def root():
|
|
|
|
| 112 |
# This Endpoint upload Pdf and store into VectorDatabase
|
| 113 |
@app.post("/upload")
|
| 114 |
async def upload_file(file: UploadFile = File(...)):
|
| 115 |
+
try:
|
| 116 |
+
if not file.filename.endswith(".pdf"):
|
| 117 |
+
raise HTTPException(status_code=400, detail="Only PDF files are supported")
|
| 118 |
|
| 119 |
+
file_path = upload_dir / file.filename
|
| 120 |
|
| 121 |
+
with open(file_path, "wb") as f:
|
| 122 |
+
shutil.copyfileobj(file.file, f)
|
| 123 |
|
| 124 |
+
chunked_docs = get_chunked_docs(file_path)
|
| 125 |
|
| 126 |
+
if not chunked_docs:
|
| 127 |
+
raise HTTPException(status_code=500, detail="No content extracted from PDF")
|
| 128 |
|
| 129 |
+
store_documents(chunked_docs, get_embeddings())
|
| 130 |
+
|
| 131 |
+
system_stats["total_uploads"] += 1
|
| 132 |
+
|
| 133 |
+
return {
|
| 134 |
+
"message": "PDF uploaded and indexed successfully",
|
| 135 |
+
"chunks_created": len(chunked_docs)
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
except Exception as e:
|
| 139 |
+
print("❌ UPLOAD ERROR:", str(e)) # <-- shows in HF logs
|
| 140 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
from pydantic import BaseModel
|
| 144 |
|