File size: 1,502 Bytes
6bdb281 3384f88 |
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 |
import os
import shutil
import uvicorn
from pydantic import BaseModel
from fastapi import FastAPI, File, UploadFile, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from app.app import data_ingestion, handle_query, DATA_DIR
class Query(BaseModel):
question: str
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Change this to your frontend's URL in production
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.post("/upload")
async def upload_file(file: UploadFile = File(...)):
print(f"Received file: {file.filename}") # Log the filename
file_extension = os.path.splitext(file.filename)[1].lower()
if file_extension not in [".pdf", ".docx", ".txt"]:
raise HTTPException(status_code=400, detail="Invalid file type. Only PDF, DOCX, and TXT are allowed.")
file_path = os.path.join(DATA_DIR, file.filename)
with open(file_path, "wb") as buffer:
shutil.copyfileobj(file.file, buffer)
data_ingestion()
return {"message": "File uploaded and processed successfully"}
@app.post("/query")
async def query_document(query: Query):
if not os.listdir(DATA_DIR):
raise HTTPException(status_code=400, detail="No document has been uploaded yet.")
response = handle_query(query.question)
return {"response": response}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000) |