Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -47,7 +47,6 @@ def read_docx(file):
|
|
47 |
|
48 |
@st.cache_resource(show_spinner=False)
|
49 |
def load_data(uploaded_files):
|
50 |
-
# Create the LLM instance outside the cache
|
51 |
llm = OpenAI(model="gpt-3.5-turbo", temperature=0.5,
|
52 |
system_prompt="You are an expert on the Streamlit Python library and your job is to answer technical questions. Assume that all questions are related to the Streamlit Python library. Keep your answers technical and based on facts – do not hallucinate features.")
|
53 |
|
@@ -66,13 +65,21 @@ def load_data(uploaded_files):
|
|
66 |
|
67 |
# Function to save the conversation
|
68 |
def save_conversation():
|
|
|
|
|
|
|
69 |
with open("conversations.json", "a") as f:
|
70 |
conversation_data = {
|
71 |
"messages": st.session_state.messages,
|
72 |
"file_names": st.session_state.uploaded_file_names
|
73 |
}
|
74 |
-
|
75 |
-
|
|
|
|
|
|
|
|
|
|
|
76 |
f.write("\n")
|
77 |
|
78 |
# Function to load previous conversations
|
@@ -80,17 +87,13 @@ def load_conversations():
|
|
80 |
if os.path.exists("conversations.json"):
|
81 |
with open("conversations.json", "r") as f:
|
82 |
conversations = [json.loads(line) for line in f]
|
83 |
-
# Ensure each conversation is a dictionary
|
84 |
-
for conv in conversations:
|
85 |
-
if not isinstance(conv, dict):
|
86 |
-
print("Warning: Loaded conversation is not a dictionary:", conv)
|
87 |
return conversations
|
88 |
return []
|
89 |
|
90 |
# Function to delete selected conversations
|
91 |
-
def delete_selected_conversations(
|
92 |
conversations = load_conversations()
|
93 |
-
remaining_conversations = [conv for
|
94 |
with open("conversations.json", "w") as f:
|
95 |
for conv in remaining_conversations:
|
96 |
json.dump(conv, f)
|
@@ -158,15 +161,16 @@ if st.session_state.show_conversations:
|
|
158 |
|
159 |
if conversations:
|
160 |
selected_indices = []
|
161 |
-
for
|
162 |
if isinstance(conv, dict): # Ensure conv is a dictionary
|
163 |
-
|
|
|
164 |
for message in conv.get('messages', []): # Use get safely
|
165 |
st.sidebar.write(f"{message['role']}: {message['content']}")
|
166 |
st.sidebar.write(f"Files: {', '.join(conv.get('file_names', []))}")
|
167 |
# Checkbox for selecting conversation to delete
|
168 |
-
if st.sidebar.checkbox(f"Select Conversation {
|
169 |
-
selected_indices.append(
|
170 |
else:
|
171 |
print("Warning: Encountered a non-dictionary conversation:", conv)
|
172 |
|
|
|
47 |
|
48 |
@st.cache_resource(show_spinner=False)
|
49 |
def load_data(uploaded_files):
|
|
|
50 |
llm = OpenAI(model="gpt-3.5-turbo", temperature=0.5,
|
51 |
system_prompt="You are an expert on the Streamlit Python library and your job is to answer technical questions. Assume that all questions are related to the Streamlit Python library. Keep your answers technical and based on facts – do not hallucinate features.")
|
52 |
|
|
|
65 |
|
66 |
# Function to save the conversation
|
67 |
def save_conversation():
|
68 |
+
conversations = load_conversations() # Load existing conversations
|
69 |
+
conversation_count = len(conversations) + 1 # Determine the next conversation number
|
70 |
+
|
71 |
with open("conversations.json", "a") as f:
|
72 |
conversation_data = {
|
73 |
"messages": st.session_state.messages,
|
74 |
"file_names": st.session_state.uploaded_file_names
|
75 |
}
|
76 |
+
# Prepend conversation number to the saved data
|
77 |
+
conversation_with_number = {
|
78 |
+
"conversation_number": conversation_count,
|
79 |
+
**conversation_data
|
80 |
+
}
|
81 |
+
if isinstance(conversation_with_number, dict):
|
82 |
+
json.dump(conversation_with_number, f)
|
83 |
f.write("\n")
|
84 |
|
85 |
# Function to load previous conversations
|
|
|
87 |
if os.path.exists("conversations.json"):
|
88 |
with open("conversations.json", "r") as f:
|
89 |
conversations = [json.loads(line) for line in f]
|
|
|
|
|
|
|
|
|
90 |
return conversations
|
91 |
return []
|
92 |
|
93 |
# Function to delete selected conversations
|
94 |
+
def delete_selected_conversations(selected_conversation_numbers):
|
95 |
conversations = load_conversations()
|
96 |
+
remaining_conversations = [conv for conv in conversations if conv.get('conversation_number') not in selected_conversation_numbers]
|
97 |
with open("conversations.json", "w") as f:
|
98 |
for conv in remaining_conversations:
|
99 |
json.dump(conv, f)
|
|
|
161 |
|
162 |
if conversations:
|
163 |
selected_indices = []
|
164 |
+
for conv in conversations:
|
165 |
if isinstance(conv, dict): # Ensure conv is a dictionary
|
166 |
+
conv_number = conv.get('conversation_number', 'Unknown')
|
167 |
+
st.sidebar.write(f"Conversation {conv_number}:")
|
168 |
for message in conv.get('messages', []): # Use get safely
|
169 |
st.sidebar.write(f"{message['role']}: {message['content']}")
|
170 |
st.sidebar.write(f"Files: {', '.join(conv.get('file_names', []))}")
|
171 |
# Checkbox for selecting conversation to delete
|
172 |
+
if st.sidebar.checkbox(f"Select Conversation {conv_number} for Deletion", key=f"delete_checkbox_{conv_number}"):
|
173 |
+
selected_indices.append(conv_number)
|
174 |
else:
|
175 |
print("Warning: Encountered a non-dictionary conversation:", conv)
|
176 |
|