ZarinT commited on
Commit
4892445
Β·
verified Β·
1 Parent(s): 23b35f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -66
app.py CHANGED
@@ -334,20 +334,18 @@ def save_feedback_to_huggingface():
334
  def main():
335
  load_environment()
336
 
337
- # Initialize session state
338
- for key, default in {
339
- "chat_ready": False,
340
- "chat_history": [],
341
- "vectorstore": None,
342
- "feedback_submitted": False,
343
- "last_question": "",
344
- "feedback_log": []
345
- }.items():
346
- if key not in st.session_state:
347
- st.session_state[key] = default
348
  if "feedback_log" not in st.session_state:
349
  st.session_state.feedback_log = []
350
-
 
 
351
  st.header("Chat with MODTRAN Documents πŸ“„")
352
 
353
  if not st.session_state.chat_ready:
@@ -357,24 +355,53 @@ def main():
357
  st.session_state.chat_ready = True
358
  st.success("MODTRAN User Manual loaded successfully!")
359
 
360
- # UI after feedback submission
361
- if st.session_state.feedback_submitted:
362
- st.success("βœ… Thank you for your feedback!")
363
- st.info("You may now ask another question.")
364
- st.markdown(f"πŸ“ Feedbacks collected: **{len(st.session_state.feedback_log)} / 5**")
365
- # Reset UI state for next question
366
- st.session_state.user_input = ""
367
- st.session_state.last_question = ""
368
- st.session_state.feedback_submitted = False
369
-
370
- user_question = st.text_input("Ask your question:", key="user_input")
371
-
372
- # Question handling logic
373
- if (
374
- st.session_state.chat_ready and
375
- user_question and
376
- user_question != st.session_state.last_question
377
- ):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
378
  with st.spinner("Generating answer..."):
379
  try:
380
  set_global_vectorstore(st.session_state.vectorstore)
@@ -382,43 +409,15 @@ def main():
382
  except Exception as e:
383
  response = f"⚠️ Something went wrong: {e}"
384
 
385
- # Show answer and feedback form
386
- st.markdown("### Answer:")
387
- st.write(response)
388
-
389
- st.session_state.chat_history.append({"user": user_question, "bot": response})
390
- st.session_state.last_question = user_question
391
-
392
- st.markdown(f"πŸ“ Feedbacks collected: **{len(st.session_state.feedback_log)} / 5**")
393
-
394
- with st.form(key=f"feedback_form_{len(st.session_state.chat_history)}"):
395
- rating = st.radio(
396
- "Rate this response:",
397
- options=["1", "2", "3", "4", "5"],
398
- key=f"rating_{len(st.session_state.chat_history)}",
399
- horizontal=True
400
- )
401
- submitted = st.form_submit_button("Submit Rating")
402
- if submitted:
403
- feedback = {
404
- "question": user_question,
405
- "response": response,
406
- "rating": rating,
407
- "timestamp": datetime.datetime.now().isoformat()
408
- }
409
- st.session_state.feedback_log.append(feedback)
410
-
411
- if len(st.session_state.feedback_log) >= 1:
412
- print("πŸ“¦ Upload threshold reached β€” saving feedback to Hugging Face.")
413
- save_feedback_to_huggingface()
414
-
415
- st.session_state.feedback_submitted = True
416
- st.rerun()
417
-
418
-
419
-
420
-
421
 
 
 
 
422
 
423
  if __name__ == "__main__":
424
  load_environment()
 
334
  def main():
335
  load_environment()
336
 
337
+ # Initialize session state safely
338
+ if "chat_ready" not in st.session_state:
339
+ st.session_state.chat_ready = False
340
+ if "chat_history" not in st.session_state:
341
+ st.session_state.chat_history = []
342
+ if "vectorstore" not in st.session_state:
343
+ st.session_state.vectorstore = None
 
 
 
 
344
  if "feedback_log" not in st.session_state:
345
  st.session_state.feedback_log = []
346
+ if "feedback_submitted" not in st.session_state:
347
+ st.session_state.feedback_submitted = False
348
+
349
  st.header("Chat with MODTRAN Documents πŸ“„")
350
 
351
  if not st.session_state.chat_ready:
 
355
  st.session_state.chat_ready = True
356
  st.success("MODTRAN User Manual loaded successfully!")
357
 
358
+ # Show entire conversation history
359
+ for i, exchange in enumerate(st.session_state.chat_history):
360
+ st.markdown(f"**You:** {exchange['user']}")
361
+ st.markdown(f"**MODTRAN Bot:** {exchange['bot']}")
362
+
363
+ # If rated, show rating
364
+ if "rating" in exchange:
365
+ st.markdown(f"⭐️ You rated this: {exchange['rating']}/5")
366
+
367
+ # If last answer not yet rated, show rating form
368
+ elif i == len(st.session_state.chat_history) - 1:
369
+ with st.form(key=f"feedback_form_{i}"):
370
+ rating = st.radio(
371
+ "Rate this response:",
372
+ options=["1", "2", "3", "4", "5"],
373
+ key=f"rating_{i}",
374
+ horizontal=True
375
+ )
376
+ submitted = st.form_submit_button("Submit Rating")
377
+ if submitted:
378
+ # Save rating
379
+ st.session_state.chat_history[i]["rating"] = rating
380
+ st.session_state.feedback_log.append({
381
+ "question": exchange["user"],
382
+ "response": exchange["bot"],
383
+ "rating": rating,
384
+ "timestamp": datetime.now().isoformat()
385
+ })
386
+
387
+ print(f"πŸ“Œ Feedbacks collected so far: {len(st.session_state.feedback_log)}")
388
+
389
+ # Upload feedbacks when 5 are collected
390
+ if len(st.session_state.feedback_log) >= 5:
391
+ print("πŸ“¦ Upload threshold reached β€” saving feedback to Hugging Face.")
392
+ save_feedback_to_huggingface()
393
+
394
+ st.success("βœ… Thank you for your feedback!")
395
+ st.session_state.feedback_submitted = True
396
+ st.rerun()
397
+
398
+ # Show how many feedbacks collected
399
+ st.markdown(f"πŸ“ Feedbacks collected: **{len(st.session_state.feedback_log)} / 5**")
400
+
401
+ # Show input box for next question
402
+ user_question = st.text_input("Ask your next question:", key="user_input")
403
+
404
+ if user_question:
405
  with st.spinner("Generating answer..."):
406
  try:
407
  set_global_vectorstore(st.session_state.vectorstore)
 
409
  except Exception as e:
410
  response = f"⚠️ Something went wrong: {e}"
411
 
412
+ # Append new conversation
413
+ st.session_state.chat_history.append({
414
+ "user": user_question,
415
+ "bot": response
416
+ })
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
417
 
418
+ # Clear input and rerun for a clean view
419
+ st.session_state.user_input = ""
420
+ st.rerun()
421
 
422
  if __name__ == "__main__":
423
  load_environment()