Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException
|
| 2 |
+
from pydantic_models import QueryInput, QueryResponse, DocumentInfo, DeleteFileRequest
|
| 3 |
+
from langchain_utils import get_rag_chain
|
| 4 |
+
from db_utils import insert_application_logs, get_chat_history, get_all_documents, insert_document_record, delete_document_record
|
| 5 |
+
from chroma_utils import index_document_to_chroma, delete_doc_from_chroma
|
| 6 |
+
import os
|
| 7 |
+
import uuid
|
| 8 |
+
import logging
|
| 9 |
+
logging.basicConfig(filename='app.log', level=logging.INFO)
|
| 10 |
+
app = FastAPI()
|
| 11 |
+
|
| 12 |
+
@app.post("/chat", response_model=QueryResponse)
|
| 13 |
+
def chat(query_input: QueryInput):
|
| 14 |
+
session_id = query_input.session_id
|
| 15 |
+
logging.info(f"Session ID: {session_id}, User Query: {query_input.question}, Model: {query_input.model.value}")
|
| 16 |
+
if not session_id:
|
| 17 |
+
session_id = str(uuid.uuid4())
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
chat_history = get_chat_history(session_id)
|
| 22 |
+
rag_chain = get_rag_chain(query_input.model.value)
|
| 23 |
+
answer = rag_chain.invoke({
|
| 24 |
+
"input": query_input.question,
|
| 25 |
+
"chat_history": chat_history
|
| 26 |
+
})['answer']
|
| 27 |
+
|
| 28 |
+
insert_application_logs(session_id, query_input.question, answer, query_input.model.value)
|
| 29 |
+
logging.info(f"Session ID: {session_id}, AI Response: {answer}")
|
| 30 |
+
return QueryResponse(answer=answer, session_id=session_id, model=query_input.model)
|
| 31 |
+
|
| 32 |
+
from fastapi import UploadFile, File, HTTPException
|
| 33 |
+
import os
|
| 34 |
+
import shutil
|
| 35 |
+
|
| 36 |
+
@app.post("/upload-doc")
|
| 37 |
+
def upload_and_index_document(file: UploadFile = File(...)):
|
| 38 |
+
allowed_extensions = ['.pdf', '.docx', '.html']
|
| 39 |
+
file_extension = os.path.splitext(file.filename)[1].lower()
|
| 40 |
+
|
| 41 |
+
if file_extension not in allowed_extensions:
|
| 42 |
+
raise HTTPException(status_code=400, detail=f"Unsupported file type. Allowed types are: {', '.join(allowed_extensions)}")
|
| 43 |
+
|
| 44 |
+
temp_file_path = f"temp_{file.filename}"
|
| 45 |
+
|
| 46 |
+
try:
|
| 47 |
+
# Save the uploaded file to a temporary file
|
| 48 |
+
with open(temp_file_path, "wb") as buffer:
|
| 49 |
+
shutil.copyfileobj(file.file, buffer)
|
| 50 |
+
|
| 51 |
+
file_id = insert_document_record(file.filename)
|
| 52 |
+
success = index_document_to_chroma(temp_file_path, file_id)
|
| 53 |
+
|
| 54 |
+
if success:
|
| 55 |
+
return {"message": f"File {file.filename} has been successfully uploaded and indexed.", "file_id": file_id}
|
| 56 |
+
else:
|
| 57 |
+
delete_document_record(file_id)
|
| 58 |
+
raise HTTPException(status_code=500, detail=f"Failed to index {file.filename}.")
|
| 59 |
+
finally:
|
| 60 |
+
if os.path.exists(temp_file_path):
|
| 61 |
+
os.remove(temp_file_path)
|
| 62 |
+
|
| 63 |
+
@app.get("/list-docs", response_model=list[DocumentInfo])
|
| 64 |
+
def list_documents():
|
| 65 |
+
return get_all_documents()
|
| 66 |
+
|
| 67 |
+
@app.post("/delete-doc")
|
| 68 |
+
def delete_document(request: DeleteFileRequest):
|
| 69 |
+
# Delete from Chroma
|
| 70 |
+
chroma_delete_success = delete_doc_from_chroma(request.file_id)
|
| 71 |
+
|
| 72 |
+
if chroma_delete_success:
|
| 73 |
+
# If successfully deleted from Chroma, delete from our database
|
| 74 |
+
db_delete_success = delete_document_record(request.file_id)
|
| 75 |
+
if db_delete_success:
|
| 76 |
+
return {"message": f"Successfully deleted document with file_id {request.file_id} from the system."}
|
| 77 |
+
else:
|
| 78 |
+
return {"error": f"Deleted from Chroma but failed to delete document with file_id {request.file_id} from the database."}
|
| 79 |
+
else:
|
| 80 |
+
return {"error": f"Failed to delete document with file_id {request.file_id} from Chroma."}
|