ZarinT commited on
Commit
da482df
Β·
verified Β·
1 Parent(s): 1fb6cb1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -51
app.py CHANGED
@@ -163,6 +163,7 @@ def set_global_vectorstore(vectorstore):
163
  vectorstore_global = vectorstore
164
 
165
  kw_model = None
 
166
 
167
  def get_kw_model():
168
  global kw_model
@@ -334,16 +335,17 @@ def save_feedback_to_huggingface():
334
  def main():
335
  load_environment()
336
 
337
- if "chat_ready" not in st.session_state:
338
- st.session_state.chat_ready = False
339
- if "chat_history" not in st.session_state:
340
- st.session_state.chat_history = []
341
- if "vectorstore" not in st.session_state:
342
- st.session_state.vectorstore = None
343
- if "feedback_submitted" not in st.session_state:
344
- st.session_state.feedback_submitted = False
345
- if "last_question" not in st.session_state:
346
- st.session_state.last_question = ""
 
347
 
348
  st.header("Chat with MODTRAN Documents πŸ“„")
349
 
@@ -354,50 +356,64 @@ def main():
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
 
 
163
  vectorstore_global = vectorstore
164
 
165
  kw_model = None
166
+ reranker = None
167
 
168
  def get_kw_model():
169
  global kw_model
 
335
  def main():
336
  load_environment()
337
 
338
+ # Initialize session state
339
+ for key, default in {
340
+ "chat_ready": False,
341
+ "chat_history": [],
342
+ "vectorstore": None,
343
+ "feedback_submitted": False,
344
+ "last_question": "",
345
+ "feedback_log": []
346
+ }.items():
347
+ if key not in st.session_state:
348
+ st.session_state[key] = default
349
 
350
  st.header("Chat with MODTRAN Documents πŸ“„")
351
 
 
356
  st.session_state.chat_ready = True
357
  st.success("MODTRAN User Manual loaded successfully!")
358
 
359
+ # UI after feedback submission
360
  if st.session_state.feedback_submitted:
361
+ st.success("βœ… Thank you for your feedback!")
362
+ st.info("You may now ask another question.")
363
+ st.markdown(f"πŸ“ Feedbacks collected: **{len(st.session_state.feedback_log)} / 5**")
364
+ # Reset UI state for next question
365
+ st.session_state.user_input = ""
366
  st.session_state.last_question = ""
367
+ st.session_state.feedback_submitted = False
368
+
369
+ user_question = st.text_input("Ask your question:", key="user_input")
370
+
371
+ # Question handling logic
372
+ if (
373
+ st.session_state.chat_ready and
374
+ user_question and
375
+ user_question != st.session_state.last_question
376
+ ):
377
+ with st.spinner("Generating answer..."):
378
+ try:
379
+ set_global_vectorstore(st.session_state.vectorstore)
380
+ response = handle_user_query(user_question)
381
+ except Exception as e:
382
+ response = f"⚠️ Something went wrong: {e}"
383
+
384
+ # Show answer and feedback form
385
+ st.markdown("### Answer:")
386
+ st.write(response)
387
+
388
+ st.session_state.chat_history.append({"user": user_question, "bot": response})
389
+ st.session_state.last_question = user_question
390
+
391
+ st.markdown(f"πŸ“ Feedbacks collected: **{len(st.session_state.feedback_log)} / 5**")
392
+
393
+ with st.form(key=f"feedback_form_{len(st.session_state.chat_history)}"):
394
+ rating = st.radio(
395
+ "Rate this response:",
396
+ options=["1", "2", "3", "4", "5"],
397
+ key=f"rating_{len(st.session_state.chat_history)}",
398
+ horizontal=True
399
+ )
400
+ submitted = st.form_submit_button("Submit Rating")
401
+ if submitted:
402
+ feedback = {
403
+ "question": user_question,
404
+ "response": response,
405
+ "rating": rating,
406
+ "timestamp": datetime.datetime.now().isoformat()
407
+ }
408
+ st.session_state.feedback_log.append(feedback)
409
+
410
+ if len(st.session_state.feedback_log) >= 1:
411
+ print("πŸ“¦ Upload threshold reached β€” saving feedback to Hugging Face.")
412
  save_feedback_to_huggingface()
413
+
414
+ st.session_state.feedback_submitted = True
415
+ st.rerun()
416
+
417
 
418
 
419