mojakob commited on
Commit
e73fa5c
·
verified ·
1 Parent(s): 886ae8c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -72
app.py CHANGED
@@ -408,6 +408,18 @@ def process_feedback(ease, clarity, engagement, satisfaction, cognitive_load, us
408
  chat_history[-1] = (user_input, response)
409
  return chat_history, user_state
410
 
 
 
 
 
 
 
 
 
 
 
 
 
411
 
412
  #############################################################################
413
  # Chat Flow
@@ -671,99 +683,95 @@ def new_chat_input(chat_history, user_state, user_input, *args, **kwargs):
671
  gr.update(visible=False),
672
  gr.update(visible=False)
673
  )
674
-
675
  elif user_state["stage"] == "emotional_relevance":
676
- emotional_relevance = standardize_response("Please extract the user's rating on a scale from 1 to 10.", user_input)
677
- user_state["user_responses"].append(
678
- {'user_state["stage"]': 'emotional_relevance', 'response': emotional_relevance})
679
- if emotional_relevance not in ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']:
680
- response = "Sorry, I couldn't understand your response. Could you please rephrase or specify your choice?"
681
- chat_history[-1] = (user_input, response)
682
  return (
683
- chat_history, # Updated chat history
684
- user_state, # Updated user state
685
- gr.update(visible=True, value=""), # Textbox
686
- gr.update(visible=False), # Slider
687
- gr.update(visible=False) # Confirm button
 
 
 
 
 
 
 
 
 
 
688
  )
689
 
690
- # Store emotional relevance rating
691
- user_state["standardized_data"][f'Emotional Relevance'] = int(emotional_relevance)
692
  user_state["stage"] = "positive_valence"
693
- # Positive Valence
694
- response = f"""How much joy, excitement, or satisfaction did this scenario evoke in you? (1 = None, 10 = A lot)"""
695
- chat_history[-1] = (user_input, response)
696
  return (
697
- chat_history, # Updated chat history
698
- user_state, # Updated user state
699
- gr.update(visible=True, value=""), # Textbox
700
- gr.update(visible=False), # Slider
701
- gr.update(visible=False) # Confirm button
702
- )
703
 
704
  elif user_state["stage"] == "positive_valence":
705
- positive_valence = standardize_response("Please extract the user's rating on a scale from 1 to 10.", user_input)
706
- user_state["user_responses"].append(
707
- {'user_state["stage"]': 'positive_valence', 'response': positive_valence})
708
- if positive_valence not in ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']:
709
- response = "Sorry, I couldn't understand your response. Could you please rephrase or specify your choice?"
710
- chat_history[-1] = (user_input, response)
711
  return (
712
- chat_history, # Updated chat history
713
- user_state, # Updated user state
714
- gr.update(visible=True, value=""), # Textbox
715
- gr.update(visible=False), # Slider
716
- gr.update(visible=False) # Confirm button
717
  )
718
-
719
- # Store positive valence rating
720
- user_state["standardized_data"][f'Positive Valence'] = int(positive_valence)
721
  user_state["stage"] = "negative_valence"
722
-
723
- # Negative Valence
724
- response = f"""How much discomfort or tension did you feel while imagining this scenario? (1 = None, 10 = A lot)"""
725
- chat_history[-1] = (user_input, response)
726
  return (
727
- chat_history, # Updated chat history
728
- user_state, # Updated user state
729
- gr.update(visible=True, value=""), # Textbox
730
- gr.update(visible=False), # Slider
731
- gr.update(visible=False) # Confirm button
732
  )
733
 
734
  elif user_state["stage"] == "negative_valence":
735
- negative_valence = standardize_response("Please extract the user's rating on a scale from 1 to 10.", user_input)
736
- user_state["user_responses"].append(
737
- {'user_state["stage"]': 'negative_valence', 'response': negative_valence})
738
- if negative_valence not in ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']:
739
- response = "Sorry, I couldn't understand your response. Could you please rephrase or specify your choice?"
740
- chat_history[-1] = (user_input, response)
741
  return (
742
- chat_history, # Updated chat history
743
- user_state, # Updated user state
744
- gr.update(visible=True, value=""), # Textbox
745
- gr.update(visible=False), # Slider
746
- gr.update(visible=False) # Confirm button
747
  )
748
 
749
- # Store negative valence rating
750
- user_state["standardized_data"][f'Negative Valence'] = int(negative_valence)
751
-
752
- # Move to next stage
753
  user_state["stage"] = "internal_external"
754
- user_state['options_text'] = generate_internal_external_options(chat_history, user_state["standardized_data"][f'Vignette'], user_state)
755
  response = (
756
- f"Please read the scenario again and use the slider below to answer the next few questions:\n"
757
- f"\n{user_state['vignette']}\n"
758
- "\nImagine this situation is happening to you personally. \nFrom 1 to 10, which of the following statements do you think is more likely (1 = Definitely Option A, 10 = Definitely Option B):\n"
759
- f"\n{user_state['options_text']}")
760
- chat_history[-1] = (user_input, response)
 
761
  return (
762
- chat_history, # Updated chat history
763
- user_state, # Updated user state
764
- gr.update(visible=False), # Textbox
765
- gr.update(visible=True, value=1), # Slider
766
- gr.update(visible=True) # Confirm button
767
  )
768
 
769
  elif user_state["stage"] == "vignette_regeneration":
 
408
  chat_history[-1] = (user_input, response)
409
  return chat_history, user_state
410
 
411
+ def validate_numeric_input(user_input, min_val=1, max_val=10):
412
+ """
413
+ Attempts to convert user_input to an integer and ensures it's between min_val and max_val.
414
+ Returns (value, None) if valid, or (None, error_message) if invalid.
415
+ """
416
+ try:
417
+ value = int(user_input.strip())
418
+ if value < min_val or value > max_val:
419
+ return None, f"Please provide a number between {min_val} and {max_val}."
420
+ return value, None
421
+ except ValueError:
422
+ return None, f"Please provide a number between {min_val} and {max_val}."
423
 
424
  #############################################################################
425
  # Chat Flow
 
683
  gr.update(visible=False),
684
  gr.update(visible=False)
685
  )
686
+
687
  elif user_state["stage"] == "emotional_relevance":
688
+ try:
689
+ emotional_relevance_int = int(user_input.strip())
690
+ except ValueError:
691
+ response = "Please provide a number between 1 and 10."
692
+ chat_history[-1] = (user_input, response)
 
693
  return (
694
+ chat_history,
695
+ user_state,
696
+ gr.update(visible=True, value=""),
697
+ gr.update(visible=False),
698
+ gr.update(visible=False)
699
+ )
700
+ if emotional_relevance_int not in range(1, 11):
701
+ response = "Please provide a number between 1 and 10."
702
+ chat_history[-1] = (user_input, response)
703
+ return (
704
+ chat_history,
705
+ user_state,
706
+ gr.update(visible=True, value=""),
707
+ gr.update(visible=False),
708
+ gr.update(visible=False)
709
  )
710
 
711
+ user_state["standardized_data"]["Emotional Relevance"] = emotional_relevance_int
 
712
  user_state["stage"] = "positive_valence"
713
+ response = "How much joy, excitement, or satisfaction did this scenario evoke in you? (1 = None, 10 = A lot)"
714
+ chat_history[-1] = (user_input, response)
 
715
  return (
716
+ chat_history,
717
+ user_state,
718
+ gr.update(visible=True, value=""),
719
+ gr.update(visible=False),
720
+ gr.update(visible=False)
 
721
 
722
  elif user_state["stage"] == "positive_valence":
723
+ positive_valence, error = validate_numeric_input(user_input, 1, 10)
724
+ if error:
725
+ response = error
726
+ chat_history[-1] = (user_input, response)
 
 
727
  return (
728
+ chat_history,
729
+ user_state,
730
+ gr.update(visible=True, value=""),
731
+ gr.update(visible=False),
732
+ gr.update(visible=False)
733
  )
734
+
735
+ user_state["standardized_data"]['Positive Valence'] = positive_valence
 
736
  user_state["stage"] = "negative_valence"
737
+ response = "How much discomfort or tension did you feel while imagining this scenario? (1 = None, 10 = A lot)"
738
+ chat_history[-1] = (user_input, response)
 
 
739
  return (
740
+ chat_history,
741
+ user_state,
742
+ gr.update(visible=True, value=""),
743
+ gr.update(visible=False),
744
+ gr.update(visible=False)
745
  )
746
 
747
  elif user_state["stage"] == "negative_valence":
748
+ negative_valence, error = validate_numeric_input(user_input, 1, 10)
749
+ if error:
750
+ response = error
751
+ chat_history[-1] = (user_input, response)
 
 
752
  return (
753
+ chat_history,
754
+ user_state,
755
+ gr.update(visible=True, value=""),
756
+ gr.update(visible=False),
757
+ gr.update(visible=False)
758
  )
759
 
760
+ user_state["standardized_data"]['Negative Valence'] = negative_valence
 
 
 
761
  user_state["stage"] = "internal_external"
 
762
  response = (
763
+ f"Please read the scenario again and use the slider below to answer the next few questions:\n"
764
+ f"{user_state['vignette']}\n"
765
+ "\nImagine this situation is happening to you personally. From 1 to 10, which of the following statements do you think is more likely (1 = Definitely Option A, 10 = Definitely Option B):\n"
766
+ f"{user_state['options_text']}"
767
+ )
768
+ chat_history[-1] = (user_input, response)
769
  return (
770
+ chat_history,
771
+ user_state,
772
+ gr.update(visible=False),
773
+ gr.update(visible=True),
774
+ gr.update(visible=True)
775
  )
776
 
777
  elif user_state["stage"] == "vignette_regeneration":