Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -298,7 +298,8 @@ def handle_user_query(query):
|
|
298 |
def save_feedback_to_huggingface():
|
299 |
try:
|
300 |
if not st.session_state.feedback_log:
|
301 |
-
|
|
|
302 |
|
303 |
feedback_df = pd.DataFrame(st.session_state.feedback_log)
|
304 |
now = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
@@ -310,7 +311,10 @@ def save_feedback_to_huggingface():
|
|
310 |
|
311 |
token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACEHUB_API_TOKEN")
|
312 |
if not token:
|
313 |
-
raise ValueError("
|
|
|
|
|
|
|
314 |
|
315 |
api = HfApi(token=token)
|
316 |
api.upload_file(
|
@@ -320,11 +324,12 @@ def save_feedback_to_huggingface():
|
|
320 |
repo_type="dataset"
|
321 |
)
|
322 |
|
|
|
323 |
st.session_state.feedback_log.clear()
|
324 |
-
print("Feedback uploaded successfully.")
|
325 |
|
326 |
except Exception as e:
|
327 |
-
print("
|
|
|
328 |
|
329 |
def main():
|
330 |
load_environment()
|
@@ -349,56 +354,51 @@ def main():
|
|
349 |
st.session_state.chat_ready = True
|
350 |
st.success("MODTRAN User Manual loaded successfully!")
|
351 |
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
st.session_state.
|
356 |
-
user_question and
|
357 |
-
not st.session_state.feedback_submitted and
|
358 |
-
user_question != st.session_state.last_question
|
359 |
-
):
|
360 |
-
with st.spinner("Generating answer..."):
|
361 |
-
try:
|
362 |
-
set_global_vectorstore(st.session_state.vectorstore)
|
363 |
-
response = handle_user_query(user_question)
|
364 |
-
except Exception as e:
|
365 |
-
response = f"β οΈ Something went wrong: {e}"
|
366 |
-
|
367 |
-
st.markdown("### Answer:")
|
368 |
-
st.write(response)
|
369 |
-
|
370 |
-
st.session_state.chat_history.append({"user": user_question, "bot": response})
|
371 |
-
st.session_state.last_question = user_question # β
Mark as answered
|
372 |
-
|
373 |
-
with st.form(key=f"feedback_form_{len(st.session_state.chat_history)}"):
|
374 |
-
rating = st.radio(
|
375 |
-
"Rate this response:",
|
376 |
-
options=["1", "2", "3", "4", "5"],
|
377 |
-
key=f"rating_{len(st.session_state.chat_history)}",
|
378 |
-
horizontal=True
|
379 |
-
)
|
380 |
-
submitted = st.form_submit_button("Submit Rating")
|
381 |
-
if submitted:
|
382 |
-
feedback = {
|
383 |
-
"question": user_question,
|
384 |
-
"response": response,
|
385 |
-
"rating": rating,
|
386 |
-
"timestamp": datetime.datetime.now().isoformat()
|
387 |
-
}
|
388 |
-
st.session_state.feedback_log.append(feedback)
|
389 |
-
|
390 |
-
if len(st.session_state.feedback_log) >= 1:
|
391 |
-
save_feedback_to_huggingface()
|
392 |
-
|
393 |
-
|
394 |
-
st.session_state.feedback_submitted = True
|
395 |
-
st.session_state.user_input = ""
|
396 |
-
st.rerun()
|
397 |
-
|
398 |
-
# Reset flag if new question is typed
|
399 |
-
if st.session_state.feedback_submitted and not user_question:
|
400 |
st.session_state.feedback_submitted = False
|
401 |
st.session_state.last_question = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
402 |
|
403 |
|
404 |
|
|
|
298 |
def save_feedback_to_huggingface():
|
299 |
try:
|
300 |
if not st.session_state.feedback_log:
|
301 |
+
print("β οΈ No feedbacks collected yet.")
|
302 |
+
return
|
303 |
|
304 |
feedback_df = pd.DataFrame(st.session_state.feedback_log)
|
305 |
now = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
|
|
311 |
|
312 |
token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACEHUB_API_TOKEN")
|
313 |
if not token:
|
314 |
+
raise ValueError("β Hugging Face token not found!")
|
315 |
+
|
316 |
+
print(f"π€ Attempting upload to repo: ZarinT/chatbot-feedback as {filename}")
|
317 |
+
print("π Feedback data:", feedback_df)
|
318 |
|
319 |
api = HfApi(token=token)
|
320 |
api.upload_file(
|
|
|
324 |
repo_type="dataset"
|
325 |
)
|
326 |
|
327 |
+
print("β
Feedback uploaded successfully.")
|
328 |
st.session_state.feedback_log.clear()
|
|
|
329 |
|
330 |
except Exception as e:
|
331 |
+
print("β Feedback upload failed:", e)
|
332 |
+
|
333 |
|
334 |
def main():
|
335 |
load_environment()
|
|
|
354 |
st.session_state.chat_ready = True
|
355 |
st.success("MODTRAN User Manual loaded successfully!")
|
356 |
|
357 |
+
# π Show different view based on whether feedback was submitted
|
358 |
+
if st.session_state.feedback_submitted:
|
359 |
+
st.success("β
Thank you for your feedback! Please ask another question.")
|
360 |
+
st.session_state.user_input = "" # Clear input field
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
361 |
st.session_state.feedback_submitted = False
|
362 |
st.session_state.last_question = ""
|
363 |
+
else:
|
364 |
+
user_question = st.text_input("Ask your question:", key="user_input")
|
365 |
+
|
366 |
+
if st.session_state.chat_ready and user_question and user_question != st.session_state.last_question:
|
367 |
+
with st.spinner("Generating answer..."):
|
368 |
+
try:
|
369 |
+
set_global_vectorstore(st.session_state.vectorstore)
|
370 |
+
response = handle_user_query(user_question)
|
371 |
+
except Exception as e:
|
372 |
+
response = f"β οΈ Something went wrong: {e}"
|
373 |
+
|
374 |
+
st.markdown("### Answer:")
|
375 |
+
st.write(response)
|
376 |
+
|
377 |
+
st.session_state.chat_history.append({"user": user_question, "bot": response})
|
378 |
+
st.session_state.last_question = user_question
|
379 |
+
|
380 |
+
with st.form(key=f"feedback_form_{len(st.session_state.chat_history)}"):
|
381 |
+
rating = st.radio(
|
382 |
+
"Rate this response:",
|
383 |
+
options=["1", "2", "3", "4", "5"],
|
384 |
+
key=f"rating_{len(st.session_state.chat_history)}",
|
385 |
+
horizontal=True
|
386 |
+
)
|
387 |
+
submitted = st.form_submit_button("Submit Rating")
|
388 |
+
if submitted:
|
389 |
+
feedback = {
|
390 |
+
"question": user_question,
|
391 |
+
"response": response,
|
392 |
+
"rating": rating,
|
393 |
+
"timestamp": datetime.datetime.now().isoformat()
|
394 |
+
}
|
395 |
+
st.session_state.feedback_log.append(feedback)
|
396 |
+
save_feedback_to_huggingface()
|
397 |
+
|
398 |
+
# After feedback submit:
|
399 |
+
st.session_state.feedback_submitted = True
|
400 |
+
st.rerun()
|
401 |
+
|
402 |
|
403 |
|
404 |
|