ZarinT commited on
Commit
8234d03
Β·
verified Β·
1 Parent(s): ba70895

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -22
app.py CHANGED
@@ -340,6 +340,11 @@ def clear_user_input():
340
 
341
  from datetime import datetime
342
 
 
 
 
 
 
343
  def clear_user_input():
344
  st.session_state.user_input = ""
345
 
@@ -360,23 +365,15 @@ def main():
360
  if "last_answered_question" not in st.session_state:
361
  st.session_state.last_answered_question = ""
362
 
363
- st.header("Chat with MODTRAN Documents πŸ“„")
364
 
365
- # Inject CSS for chat-style alignment
366
  st.markdown("""
367
  <style>
368
  .chat-container {
369
  display: flex;
370
  flex-direction: column;
371
- gap: 0.5rem;
372
- }
373
- .user-bubble {
374
- align-self: flex-end;
375
- background-color: rgba(30, 30, 30, 0.85);
376
- color: white;
377
- padding: 1rem;
378
- border-radius: 1rem 0.5rem 1rem 1rem;
379
- max-width: 80%;
380
  }
381
  .bot-bubble {
382
  align-self: flex-start;
@@ -386,6 +383,14 @@ def main():
386
  border-radius: 0.5rem 1rem 1rem 1rem;
387
  max-width: 80%;
388
  }
 
 
 
 
 
 
 
 
389
  .rating-line {
390
  font-weight: bold;
391
  color: #ffaa00;
@@ -402,7 +407,7 @@ def main():
402
  </style>
403
  """, unsafe_allow_html=True)
404
 
405
- # Load chatbot agent and vectorstore if not ready
406
  if not st.session_state.chat_ready:
407
  with st.spinner("Loading MODTRAN document..."):
408
  preload_modtran_document()
@@ -410,18 +415,20 @@ def main():
410
  st.session_state.chat_ready = True
411
  st.success("MODTRAN User Manual loaded successfully!")
412
 
413
- # 🟦 Chat History Output
414
  st.markdown('<div class="chat-container">', unsafe_allow_html=True)
415
 
416
  for i, exchange in enumerate(st.session_state.chat_history):
417
- st.markdown(f'<div class="user-bubble"><strong>You:</strong> {exchange["user"]}</div>', unsafe_allow_html=True)
418
  st.markdown(f'<div class="bot-bubble"><strong>MODTRAN Bot:</strong> {exchange["bot"]}</div>', unsafe_allow_html=True)
 
 
419
 
420
- # Show rating if available
421
  if "rating" in exchange:
422
  st.markdown(f'<div class="rating-line">⭐️ You rated this: {exchange["rating"]}/5</div>', unsafe_allow_html=True)
423
 
424
- # Show form for last unanswered item
425
  elif i == len(st.session_state.chat_history) - 1:
426
  with st.form(key=f"feedback_form_{i}"):
427
  rating = st.radio(
@@ -440,9 +447,7 @@ def main():
440
  "timestamp": datetime.now().isoformat()
441
  })
442
 
443
- print(f"πŸ“Œ Feedbacks collected so far: {len(st.session_state.feedback_log)}")
444
-
445
- if len(st.session_state.feedback_log) >= 5:
446
  print("πŸ“¦ Upload threshold reached β€” saving feedback to Hugging Face.")
447
  save_feedback_to_huggingface()
448
 
@@ -452,10 +457,10 @@ def main():
452
 
453
  st.markdown('</div>', unsafe_allow_html=True)
454
 
455
- # Feedback status
456
- st.markdown(f'<div class="feedback-counter">πŸ“„ Feedbacks collected: <strong>{len(st.session_state.feedback_log)} / 5</strong></div>', unsafe_allow_html=True)
457
-
458
 
 
459
  user_question = st.text_input("Ask your next question:", key="user_input")
460
 
461
  if user_question and user_question != st.session_state.last_answered_question:
@@ -466,6 +471,7 @@ def main():
466
  except Exception as e:
467
  response = f"⚠️ Something went wrong: {e}"
468
 
 
469
  st.session_state.chat_history.append({
470
  "user": user_question,
471
  "bot": response
@@ -474,6 +480,7 @@ def main():
474
  st.session_state.last_answered_question = user_question
475
  st.rerun()
476
 
 
477
  if __name__ == "__main__":
478
  load_environment()
479
  main()
 
340
 
341
  from datetime import datetime
342
 
343
+ def clear_user_input():
344
+ st.session_state.user_input = ""
345
+
346
+ from datetime import datetime
347
+
348
  def clear_user_input():
349
  st.session_state.user_input = ""
350
 
 
365
  if "last_answered_question" not in st.session_state:
366
  st.session_state.last_answered_question = ""
367
 
368
+ st.header("Chat with MODTRAN Document πŸ“„")
369
 
370
+ # Inject custom CSS for chat bubbles
371
  st.markdown("""
372
  <style>
373
  .chat-container {
374
  display: flex;
375
  flex-direction: column;
376
+ gap: 0.75rem;
 
 
 
 
 
 
 
 
377
  }
378
  .bot-bubble {
379
  align-self: flex-start;
 
383
  border-radius: 0.5rem 1rem 1rem 1rem;
384
  max-width: 80%;
385
  }
386
+ .user-bubble {
387
+ align-self: flex-end;
388
+ background-color: rgba(0, 0, 0, 0.75);
389
+ color: white;
390
+ padding: 1rem;
391
+ border-radius: 1rem 0.5rem 1rem 1rem;
392
+ max-width: 80%;
393
+ }
394
  .rating-line {
395
  font-weight: bold;
396
  color: #ffaa00;
 
407
  </style>
408
  """, unsafe_allow_html=True)
409
 
410
+ # Load vectorstore and chatbot agent
411
  if not st.session_state.chat_ready:
412
  with st.spinner("Loading MODTRAN document..."):
413
  preload_modtran_document()
 
415
  st.session_state.chat_ready = True
416
  st.success("MODTRAN User Manual loaded successfully!")
417
 
418
+ # Render all previous Q&A in chat format
419
  st.markdown('<div class="chat-container">', unsafe_allow_html=True)
420
 
421
  for i, exchange in enumerate(st.session_state.chat_history):
422
+ # MODTRAN Bot's answer (left side)
423
  st.markdown(f'<div class="bot-bubble"><strong>MODTRAN Bot:</strong> {exchange["bot"]}</div>', unsafe_allow_html=True)
424
+ # User's question (right side)
425
+ st.markdown(f'<div class="user-bubble"><strong>You:</strong> {exchange["user"]}</div>', unsafe_allow_html=True)
426
 
427
+ # If already rated
428
  if "rating" in exchange:
429
  st.markdown(f'<div class="rating-line">⭐️ You rated this: {exchange["rating"]}/5</div>', unsafe_allow_html=True)
430
 
431
+ # If not rated yet and it's the last message, show form
432
  elif i == len(st.session_state.chat_history) - 1:
433
  with st.form(key=f"feedback_form_{i}"):
434
  rating = st.radio(
 
447
  "timestamp": datetime.now().isoformat()
448
  })
449
 
450
+ if len(st.session_state.feedback_log) >= 2:
 
 
451
  print("πŸ“¦ Upload threshold reached β€” saving feedback to Hugging Face.")
452
  save_feedback_to_huggingface()
453
 
 
457
 
458
  st.markdown('</div>', unsafe_allow_html=True)
459
 
460
+ # Show current feedback progress
461
+ #st.markdown(f'<div class="feedback-counter">πŸ“„ Feedbacks collected: <strong>{len(st.session_state.feedback_log)} / 5</strong></div>', unsafe_allow_html=True)
 
462
 
463
+ # Input for next question
464
  user_question = st.text_input("Ask your next question:", key="user_input")
465
 
466
  if user_question and user_question != st.session_state.last_answered_question:
 
471
  except Exception as e:
472
  response = f"⚠️ Something went wrong: {e}"
473
 
474
+ # Save new Q&A
475
  st.session_state.chat_history.append({
476
  "user": user_question,
477
  "bot": response
 
480
  st.session_state.last_answered_question = user_question
481
  st.rerun()
482
 
483
+
484
  if __name__ == "__main__":
485
  load_environment()
486
  main()