VishnuRamDebyez
commited on
Commit
•
484daae
1
Parent(s):
b6cff29
Update app.py
Browse files
app.py
CHANGED
@@ -1,80 +1,18 @@
|
|
1 |
-
|
2 |
-
from
|
3 |
-
from
|
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 |
-
|
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 |
-
|
22 |
-
|
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 |
-
|
33 |
-
|
34 |
-
import shutil
|
35 |
|
36 |
-
|
37 |
-
|
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."}
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from sidebar import display_sidebar
|
3 |
+
from chat_interface import display_chat_interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
+
st.title("Langchain RAG Chatbot")
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
+
# Initialize session state variables
|
8 |
+
if "messages" not in st.session_state:
|
9 |
+
st.session_state.messages = []
|
10 |
|
11 |
+
if "session_id" not in st.session_state:
|
12 |
+
st.session_state.session_id = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
+
# Display the sidebar
|
15 |
+
display_sidebar()
|
|
|
16 |
|
17 |
+
# Display the chat interface
|
18 |
+
display_chat_interface()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|