-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamlit_app.py
More file actions
102 lines (80 loc) · 3.66 KB
/
streamlit_app.py
File metadata and controls
102 lines (80 loc) · 3.66 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# streamlit_app.py
import streamlit as st
from rag_system import RAGSystem
from db_operations import *
import os
import getpass
import tempfile
# Initialize session state
if 'rag_system' not in st.session_state:
st.session_state.rag_system = None
if 'db_manager' not in st.session_state:
st.session_state.db_manager = None
if 'user_id' not in st.session_state:
st.session_state.user_id = None
def initialize_systems():
openai_api_key = st.secrets["OPENAI_API_KEY"]
qdrant_url = st.secrets["QDRANT_URL"]
collection_name = "my_documents"
db_url = st.secrets["DATABASE_URL"]
rag_system = RAGSystem(openai_api_key, qdrant_url, collection_name)
db_manager = DatabaseManager(db_url)
st.session_state.file_uploaded = False
return rag_system, db_manager
st.title("Packet Copilot")
# Initialize systems if not already done
if st.session_state.rag_system is None or st.session_state.db_manager is None:
st.session_state.rag_system, st.session_state.db_manager = initialize_systems()
# User authentication
username = st.text_input("Enter your username:")
if username:
st.session_state.user_id = st.session_state.db_manager.get_or_create_user(username)
st.session_state.username = username
if st.session_state.user_id:
# File Upload Section
st.subheader("Upload Document")
uploaded_file = st.file_uploader("Choose a file", type=['txt', 'pdf', 'docx', 'etl', 'pcap', 'zip'])
if uploaded_file is not None and st.session_state.file_uploaded == False:
# Create a temporary file
with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(uploaded_file.name)[1]) as tmp_file:
tmp_file.write(uploaded_file.getvalue())
tmp_file_path = tmp_file.name
try:
# Process and add the document to the RAG system
print("Adding document to the knowledge base...\n")
st.session_state.rag_system.add_documents(tmp_file_path)
st.success(f"File {uploaded_file.name} has been successfully added to the knowledge base.")
st.session_state.file_uploaded = True
except Exception as e:
st.error(f"An error occurred while processing the file: {str(e)}")
finally:
# Remove the temporary file
os.unlink(tmp_file_path)
# Chat interface
st.subheader("Chat")
# Load chat history
chat_history = st.session_state.db_manager.get_chat_history(st.session_state.user_id)
for message in chat_history:
with st.chat_message(message["role"]):
st.write(message["content"])
if prompt := st.chat_input("What is your question?"):
# Add user message to database
st.session_state.db_manager.add_message(st.session_state.user_id, "user", prompt)
with st.chat_message("user"):
st.write(prompt)
# Get response from RAG system
response = st.session_state.rag_system.query(prompt)
# Display assistant response
with st.chat_message("assistant"):
st.write(response["answer"])
# Add assistant response to database
st.session_state.db_manager.add_message(st.session_state.user_id, "assistant", response["answer"])
# Sidebar for system information and controls
st.sidebar.title("System Info")
st.sidebar.info("This chatbot uses a Retrieval-Augmented Generation (RAG) system to provide informed answers based on a document collection.")
if st.sidebar.button("Clear Chat History"):
# In a real application, you might want to add a confirmation dialog
# and actually delete the history from the database
st.rerun()
else:
st.info("Please enter a username to start chatting.")