Spaces:
Running
Running
Update backend/app/main.py
Browse files- backend/app/main.py +21 -10
backend/app/main.py
CHANGED
|
@@ -14,6 +14,15 @@ from app.config import EMBEDDING_MODEL
|
|
| 14 |
# =========================
|
| 15 |
|
| 16 |
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# =========================
|
| 19 |
# CORS (REQUIRED FOR FRONTEND)
|
|
@@ -52,29 +61,31 @@ def chunk_text(text, size=500):
|
|
| 52 |
|
| 53 |
@app.post("/upload")
|
| 54 |
async def upload_document(file: UploadFile = File(...)):
|
| 55 |
-
global chat_history
|
| 56 |
|
| 57 |
# reset chat history for new document
|
| 58 |
chat_history = []
|
| 59 |
|
| 60 |
-
# save uploaded file
|
| 61 |
file_path = os.path.join(UPLOAD_DIR, file.filename)
|
|
|
|
| 62 |
with open(file_path, "wb") as f:
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
# load & process document
|
| 66 |
text = load_document(file_path)
|
| 67 |
chunks = chunk_text(text)
|
| 68 |
|
|
|
|
|
|
|
|
|
|
| 69 |
# build vector index
|
| 70 |
vector_store.build(chunks)
|
| 71 |
|
| 72 |
-
# logs for debugging
|
| 73 |
-
print("✅ DOCUMENT INDEXED")
|
| 74 |
-
print(f"✅ Document name: {file.filename}")
|
| 75 |
-
print(f"✅ Total chunks: {len(chunks)}")
|
| 76 |
-
print(f"✅ Index exists? {vector_store.index is not None}")
|
| 77 |
-
|
| 78 |
return {
|
| 79 |
"message": "Document uploaded and indexed successfully",
|
| 80 |
"document_name": file.filename
|
|
@@ -117,4 +128,4 @@ async def chat(request: ChatRequest):
|
|
| 117 |
|
| 118 |
return {
|
| 119 |
"answer": answer
|
| 120 |
-
}
|
|
|
|
| 14 |
# =========================
|
| 15 |
|
| 16 |
app = FastAPI()
|
| 17 |
+
@app.get("/")
|
| 18 |
+
def home():
|
| 19 |
+
return {
|
| 20 |
+
"message": "✅ DocAI Backend is running successfully!",
|
| 21 |
+
"endpoints": {
|
| 22 |
+
"upload": "/upload",
|
| 23 |
+
"chat": "/chat"
|
| 24 |
+
}
|
| 25 |
+
}
|
| 26 |
|
| 27 |
# =========================
|
| 28 |
# CORS (REQUIRED FOR FRONTEND)
|
|
|
|
| 61 |
|
| 62 |
@app.post("/upload")
|
| 63 |
async def upload_document(file: UploadFile = File(...)):
|
| 64 |
+
global chat_history, vector_store
|
| 65 |
|
| 66 |
# reset chat history for new document
|
| 67 |
chat_history = []
|
| 68 |
|
| 69 |
+
# save uploaded file (mobile friendly)
|
| 70 |
file_path = os.path.join(UPLOAD_DIR, file.filename)
|
| 71 |
+
|
| 72 |
with open(file_path, "wb") as f:
|
| 73 |
+
while True:
|
| 74 |
+
chunk = await file.read(1024 * 1024)
|
| 75 |
+
if not chunk:
|
| 76 |
+
break
|
| 77 |
+
f.write(chunk)
|
| 78 |
|
| 79 |
# load & process document
|
| 80 |
text = load_document(file_path)
|
| 81 |
chunks = chunk_text(text)
|
| 82 |
|
| 83 |
+
# reset vector store for new document
|
| 84 |
+
vector_store = VectorStore(EMBEDDING_MODEL)
|
| 85 |
+
|
| 86 |
# build vector index
|
| 87 |
vector_store.build(chunks)
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
return {
|
| 90 |
"message": "Document uploaded and indexed successfully",
|
| 91 |
"document_name": file.filename
|
|
|
|
| 128 |
|
| 129 |
return {
|
| 130 |
"answer": answer
|
| 131 |
+
}
|