ZarinT commited on
Commit
bb45e35
Β·
verified Β·
1 Parent(s): 7b633e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -14
app.py CHANGED
@@ -336,10 +336,6 @@ def clear_user_input():
336
  def main():
337
  load_environment()
338
 
339
- if "last_answered_question" not in st.session_state:
340
- st.session_state.last_answered_question = ""
341
-
342
- # Initialize session state safely
343
  if "chat_ready" not in st.session_state:
344
  st.session_state.chat_ready = False
345
  if "chat_history" not in st.session_state:
@@ -350,6 +346,8 @@ def main():
350
  st.session_state.feedback_log = []
351
  if "feedback_submitted" not in st.session_state:
352
  st.session_state.feedback_submitted = False
 
 
353
 
354
  st.header("Chat with MODTRAN Documents πŸ“„")
355
 
@@ -360,17 +358,16 @@ def main():
360
  st.session_state.chat_ready = True
361
  st.success("MODTRAN User Manual loaded successfully!")
362
 
363
- # Show entire conversation history
364
  for i, exchange in enumerate(st.session_state.chat_history):
365
  st.markdown(f"**You:** {exchange['user']}")
366
  st.markdown(f"**MODTRAN Bot:** {exchange['bot']}")
367
 
368
- # If rated, show rating
369
  if "rating" in exchange:
370
  st.markdown(f"⭐️ You rated this: {exchange['rating']}/5")
371
 
372
- # If last answer not yet rated, show rating form
373
  elif i == len(st.session_state.chat_history) - 1:
 
374
  with st.form(key=f"feedback_form_{i}"):
375
  rating = st.radio(
376
  "Rate this response:",
@@ -380,7 +377,6 @@ def main():
380
  )
381
  submitted = st.form_submit_button("Submit Rating")
382
  if submitted:
383
- # Save rating
384
  st.session_state.chat_history[i]["rating"] = rating
385
  st.session_state.feedback_log.append({
386
  "question": exchange["user"],
@@ -391,7 +387,6 @@ def main():
391
 
392
  print(f"πŸ“Œ Feedbacks collected so far: {len(st.session_state.feedback_log)}")
393
 
394
- # Upload feedbacks when 5 are collected
395
  if len(st.session_state.feedback_log) >= 5:
396
  print("πŸ“¦ Upload threshold reached β€” saving feedback to Hugging Face.")
397
  save_feedback_to_huggingface()
@@ -400,12 +395,13 @@ def main():
400
  st.session_state.feedback_submitted = True
401
  st.rerun()
402
 
403
- # Show how many feedbacks collected
404
  st.markdown(f"πŸ“ Feedbacks collected: **{len(st.session_state.feedback_log)} / 5**")
405
 
406
- # Show input box for next question
407
- user_question = st.text_input("Ask your next question:", key="user_input", on_change=clear_user_input)
408
 
 
409
  if user_question and user_question != st.session_state.last_answered_question:
410
  with st.spinner("Generating answer..."):
411
  try:
@@ -414,17 +410,21 @@ def main():
414
  except Exception as e:
415
  response = f"⚠️ Something went wrong: {e}"
416
 
417
- # Store answer
418
  st.session_state.chat_history.append({
419
  "user": user_question,
420
  "bot": response
421
  })
422
 
423
- # βœ… Mark this question as answered
424
  st.session_state.last_answered_question = user_question
425
 
 
 
 
426
  st.rerun()
427
 
 
428
  if __name__ == "__main__":
429
  load_environment()
430
  main()
 
336
  def main():
337
  load_environment()
338
 
 
 
 
 
339
  if "chat_ready" not in st.session_state:
340
  st.session_state.chat_ready = False
341
  if "chat_history" not in st.session_state:
 
346
  st.session_state.feedback_log = []
347
  if "feedback_submitted" not in st.session_state:
348
  st.session_state.feedback_submitted = False
349
+ if "last_answered_question" not in st.session_state:
350
+ st.session_state.last_answered_question = ""
351
 
352
  st.header("Chat with MODTRAN Documents πŸ“„")
353
 
 
358
  st.session_state.chat_ready = True
359
  st.success("MODTRAN User Manual loaded successfully!")
360
 
361
+ # Display entire chat history
362
  for i, exchange in enumerate(st.session_state.chat_history):
363
  st.markdown(f"**You:** {exchange['user']}")
364
  st.markdown(f"**MODTRAN Bot:** {exchange['bot']}")
365
 
 
366
  if "rating" in exchange:
367
  st.markdown(f"⭐️ You rated this: {exchange['rating']}/5")
368
 
 
369
  elif i == len(st.session_state.chat_history) - 1:
370
+ # Show rating form for latest if not rated
371
  with st.form(key=f"feedback_form_{i}"):
372
  rating = st.radio(
373
  "Rate this response:",
 
377
  )
378
  submitted = st.form_submit_button("Submit Rating")
379
  if submitted:
 
380
  st.session_state.chat_history[i]["rating"] = rating
381
  st.session_state.feedback_log.append({
382
  "question": exchange["user"],
 
387
 
388
  print(f"πŸ“Œ Feedbacks collected so far: {len(st.session_state.feedback_log)}")
389
 
 
390
  if len(st.session_state.feedback_log) >= 5:
391
  print("πŸ“¦ Upload threshold reached β€” saving feedback to Hugging Face.")
392
  save_feedback_to_huggingface()
 
395
  st.session_state.feedback_submitted = True
396
  st.rerun()
397
 
398
+ # Feedback counter
399
  st.markdown(f"πŸ“ Feedbacks collected: **{len(st.session_state.feedback_log)} / 5**")
400
 
401
+ # Input box for new question
402
+ user_question = st.text_input("Ask your next question:", key="user_input")
403
 
404
+ # Only handle if user typed something new
405
  if user_question and user_question != st.session_state.last_answered_question:
406
  with st.spinner("Generating answer..."):
407
  try:
 
410
  except Exception as e:
411
  response = f"⚠️ Something went wrong: {e}"
412
 
413
+ # Append to chat history
414
  st.session_state.chat_history.append({
415
  "user": user_question,
416
  "bot": response
417
  })
418
 
419
+ # Save the last answered question
420
  st.session_state.last_answered_question = user_question
421
 
422
+ # βœ… Clear the user input manually now
423
+ st.session_state.user_input = ""
424
+
425
  st.rerun()
426
 
427
+
428
  if __name__ == "__main__":
429
  load_environment()
430
  main()