-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
86 lines (73 loc) · 2.53 KB
/
Copy pathapi.py
File metadata and controls
86 lines (73 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from fastapi import FastAPI, UploadFile, File, HTTPException
from fastapi.responses import JSONResponse
import shutil
import os
from pathlib import Path
from fastapi import Form
from src.processors import process_and_store
import uuid
from src.generate_answer_qwen import generate_answer
app = FastAPI(title="Document QA API", version="1.0")
file_registry = {}
# Creating upload directory if it doesn't exist
UPLOAD_DIR = Path("uploads")
UPLOAD_DIR.mkdir(exist_ok=True)
@app.get("/")
def root():
return {"message": "Document QA API is running"}
@app.get("/health")
def health_check():
return {"status": "healthy"}
@app.post("/upload")
async def upload_file(file: UploadFile = File(...)):
"""
Upload and process a document (PDF, PPT, HTML, TXT)
"""
try:
# Generate unique ID
file_id = str(uuid.uuid4())
file_ext = os.path.splitext(file.filename)[1].lower()
# Check if supported
supported = ['.pdf', '.pptx', '.ppt', '.html', '.htm', '.txt']
if file_ext not in supported:
raise HTTPException(
status_code=400,
detail=f"Unsupported file type. Supported: {supported}"
)
# Save file
file_path = UPLOAD_DIR / f"{file_id}{file_ext}"
with open(file_path, "wb") as buffer:
shutil.copyfileobj(file.file, buffer)
# Get file size safely
file_size = os.path.getsize(file_path)
# Process the file
collection_name = f"doc_{file_id}"
process_and_store(str(file_path), collection_name)
# Register
file_registry[file_id] = {
"filename": file.filename,
"path": str(file_path),
"collection": collection_name
}
return {
"file_id": file_id,
"filename": file.filename,
"file_type": file_ext,
"size": file_path.stat().st_size,
"chunks_stored": "Check console for count",
"message": f"File uploaded and processed successfully. Format: {file_ext}"
}
except Exception as e:
print(f"ERROR: {str(e)}") # This will show in console
raise HTTPException(status_code=500, detail=str(e))
@app.post("/ask")
def ask_question(question: str, filename: str = None):
"""
Ask a question about uploaded document(s)
"""
return {
"question": question,
"filename": filename,
"answer": "Answer generation will be implemented next",
"status": "pending"
}