Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,7 @@ import os
|
|
4 |
from datetime import *
|
5 |
import subprocess # Added missing import
|
6 |
from datetime import datetime
|
|
|
7 |
from calendar_rag import (
|
8 |
create_default_config,
|
9 |
AcademicCalendarRAG,
|
@@ -172,27 +173,6 @@ def initialize_pipeline():
|
|
172 |
st.error(f"Error initializing pipeline: {str(e)}")
|
173 |
return None
|
174 |
|
175 |
-
def save_feedback_to_json():
|
176 |
-
"""Save feedback data to a JSON file"""
|
177 |
-
try:
|
178 |
-
# Get current directory
|
179 |
-
current_dir = os.path.dirname(os.path.abspath(__file__))
|
180 |
-
file_path = os.path.join(current_dir, 'feedback_data.json')
|
181 |
-
|
182 |
-
# Save the file
|
183 |
-
with open(file_path, "w", encoding="utf-8") as f:
|
184 |
-
json.dump(
|
185 |
-
st.session_state.feedback_data,
|
186 |
-
f,
|
187 |
-
ensure_ascii=False,
|
188 |
-
indent=2
|
189 |
-
)
|
190 |
-
return file_path
|
191 |
-
|
192 |
-
except Exception as e:
|
193 |
-
st.error(f"Error saving feedback: {str(e)}")
|
194 |
-
return None
|
195 |
-
|
196 |
def add_feedback(query: str, answer: str, is_correct: bool):
|
197 |
"""Add feedback entry to session state and save to JSON and GitHub"""
|
198 |
feedback_entry = {
|
@@ -203,27 +183,6 @@ def add_feedback(query: str, answer: str, is_correct: bool):
|
|
203 |
}
|
204 |
st.session_state.feedback_data.append(feedback_entry)
|
205 |
|
206 |
-
file_path = save_feedback_to_json()
|
207 |
-
if file_path:
|
208 |
-
if save_feedback_to_repo(file_path):
|
209 |
-
st.success("Feedback saved successfully")
|
210 |
-
else:
|
211 |
-
st.warning("Feedback saved locally but not pushed to repository")
|
212 |
-
|
213 |
-
def evaluate_answer(query: str, answer: str):
|
214 |
-
"""Display evaluation buttons for the answer"""
|
215 |
-
col1, col2, col3 = st.columns([1, 1, 4])
|
216 |
-
|
217 |
-
with col1:
|
218 |
-
if st.button("✅ Correct", key=f"correct_{len(st.session_state.chat_history)}"):
|
219 |
-
add_feedback(query, answer, True)
|
220 |
-
st.success("Feedback recorded")
|
221 |
-
|
222 |
-
with col2:
|
223 |
-
if st.button("❌ Incorrect", key=f"incorrect_{len(st.session_state.chat_history)}"):
|
224 |
-
add_feedback(query, answer, False)
|
225 |
-
st.error("Feedback recorded")
|
226 |
-
|
227 |
def display_chat_history():
|
228 |
"""Display chat history with enhanced styling"""
|
229 |
for i, (role, message) in enumerate(st.session_state.chat_history):
|
@@ -251,6 +210,77 @@ def add_to_history(role: str, message: str):
|
|
251 |
"""Add message to chat history"""
|
252 |
st.session_state.chat_history.append((role, message))
|
253 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
254 |
def main():
|
255 |
# Header with animation
|
256 |
st.markdown("""
|
|
|
4 |
from datetime import *
|
5 |
import subprocess # Added missing import
|
6 |
from datetime import datetime
|
7 |
+
from huggingface_hub import HfApi
|
8 |
from calendar_rag import (
|
9 |
create_default_config,
|
10 |
AcademicCalendarRAG,
|
|
|
173 |
st.error(f"Error initializing pipeline: {str(e)}")
|
174 |
return None
|
175 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
176 |
def add_feedback(query: str, answer: str, is_correct: bool):
|
177 |
"""Add feedback entry to session state and save to JSON and GitHub"""
|
178 |
feedback_entry = {
|
|
|
183 |
}
|
184 |
st.session_state.feedback_data.append(feedback_entry)
|
185 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
186 |
def display_chat_history():
|
187 |
"""Display chat history with enhanced styling"""
|
188 |
for i, (role, message) in enumerate(st.session_state.chat_history):
|
|
|
210 |
"""Add message to chat history"""
|
211 |
st.session_state.chat_history.append((role, message))
|
212 |
|
213 |
+
def load_qa_history():
|
214 |
+
"""Load QA history from local JSON file"""
|
215 |
+
try:
|
216 |
+
history_file = Path("qa_history.json")
|
217 |
+
if history_file.exists():
|
218 |
+
with open(history_file, "r", encoding="utf-8") as f:
|
219 |
+
return json.load(f)
|
220 |
+
return []
|
221 |
+
except Exception as e:
|
222 |
+
st.error(f"Error loading QA history: {str(e)}")
|
223 |
+
return []
|
224 |
+
|
225 |
+
def save_qa_history(history_data):
|
226 |
+
"""Save QA history to local JSON file and push to Hugging Face"""
|
227 |
+
try:
|
228 |
+
# Save locally
|
229 |
+
with open("qa_history.json", "w", encoding="utf-8") as f:
|
230 |
+
json.dump(history_data, f, ensure_ascii=False, indent=2)
|
231 |
+
|
232 |
+
# Push to Hugging Face
|
233 |
+
api = HfApi()
|
234 |
+
api.upload_file(
|
235 |
+
path_or_fileobj="qa_history.json",
|
236 |
+
path_in_repo="qa_history.json",
|
237 |
+
repo_id="JirasakJo/Questions_Graduate_Studies_Calendar_2024",
|
238 |
+
repo_type="space"
|
239 |
+
)
|
240 |
+
except Exception as e:
|
241 |
+
st.error(f"Error saving QA history: {str(e)}")
|
242 |
+
|
243 |
+
def add_to_qa_history(query: str, answer: str):
|
244 |
+
"""Add new QA pair to history"""
|
245 |
+
history_entry = {
|
246 |
+
"timestamp": (datetime.now() + timedelta(hours=7)).isoformat(),
|
247 |
+
"query": query,
|
248 |
+
"answer": answer
|
249 |
+
}
|
250 |
+
|
251 |
+
# Load existing history
|
252 |
+
history_data = load_qa_history()
|
253 |
+
|
254 |
+
# Add new entry
|
255 |
+
history_data.append(history_entry)
|
256 |
+
|
257 |
+
# Save updated history
|
258 |
+
save_qa_history(history_data)
|
259 |
+
return history_data
|
260 |
+
|
261 |
+
# Modify the main code to include these changes
|
262 |
+
def add_to_history(role: str, message: str):
|
263 |
+
"""Add message to chat history and save if it's a complete QA pair"""
|
264 |
+
st.session_state.chat_history.append((role, message))
|
265 |
+
|
266 |
+
# If this is an assistant response, save the QA pair
|
267 |
+
if role == "assistant" and len(st.session_state.chat_history) >= 2:
|
268 |
+
# Get the corresponding user query (previous message)
|
269 |
+
user_query = st.session_state.chat_history[-2][1]
|
270 |
+
add_to_qa_history(user_query, message)
|
271 |
+
|
272 |
+
# In the main function, load history at startup
|
273 |
+
if 'qa_history_loaded' not in st.session_state:
|
274 |
+
st.session_state.qa_history_loaded = True
|
275 |
+
loaded_history = load_qa_history()
|
276 |
+
# You might want to display or process the loaded history here
|
277 |
+
|
278 |
+
# Modify the clear history button to also clear the saved history
|
279 |
+
if clear_history:
|
280 |
+
st.session_state.chat_history = []
|
281 |
+
save_qa_history([]) # Save empty history
|
282 |
+
st.rerun()
|
283 |
+
|
284 |
def main():
|
285 |
# Header with animation
|
286 |
st.markdown("""
|