SwatGarg commited on
Commit
a5353a1
1 Parent(s): 535ba6d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -78
app.py CHANGED
@@ -101,27 +101,34 @@ def get_text():
101
  def generate_response(prompt):
102
  response = mdl.call_conversational_rag(prompt,final_chain)
103
  return response['answer']
104
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
 
106
  ## Applying the user input box
107
  with input_container:
108
- # Add a radio button to choose input mode
109
- input_mode = st.radio("Select input mode:", ["Text", "Speech"])
110
-
111
- if input_mode == "Speech":
112
- # Use the speech_to_text function to capture speech input
113
- speech_input = speech_to_text(
114
- key='my_stt',
115
- callback=speech_recognition_callback
116
- )
117
-
118
- # Check if speech input is available
119
- if 'speech_input' in st.session_state and st.session_state.speech_input:
120
- # Display the speech input
121
- st.text(f"Speech Input: {st.session_state.speech_input}")
122
 
123
- # Process the speech input as a query
124
- query = st.session_state.speech_input
125
  with st.spinner("processing....."):
126
  response = generate_response(query)
127
  st.session_state.past.append(query)
@@ -129,67 +136,6 @@ with input_container:
129
  # Detect sentiment
130
  user_sentiment = chatbot.detect_sentiment(query)
131
 
132
-
133
-
134
- # Update mood history / mood_trend
135
- chatbot.update_mood_history()
136
- mood_trend = chatbot.check_mood_trend()
137
-
138
- # Define rewards
139
- if user_sentiment in ["Positive", "Moderately Positive"]:
140
- if mood_trend == "increased":
141
- reward = +1
142
- mood_trend_symbol = " ⬆️"
143
- elif mood_trend == "unchanged":
144
- reward = +0.8
145
- mood_trend_symbol = ""
146
- else: # decreased
147
- reward = -0.2
148
- mood_trend_symbol = " ⬇️"
149
- else:
150
- if mood_trend == "increased":
151
- reward = +1
152
- mood_trend_symbol = " ⬆️"
153
- elif mood_trend == "unchanged":
154
- reward = -0.2
155
- mood_trend_symbol = ""
156
- else: # decreased
157
- reward = -1
158
- mood_trend_symbol = " ⬇️"
159
-
160
- print(
161
- f"mood_trend - sentiment - reward: {mood_trend} - {user_sentiment} - 🛑{reward}🛑"
162
- )
163
-
164
- # Update Q-values
165
- chatbot.update_q_values(
166
- user_sentiment, reward, user_sentiment
167
- )
168
-
169
- # Convert the response to speech
170
- speech_fp = text_to_speech(response)
171
- # Play the speech
172
- st.audio(speech_fp, format='audio/mp3')
173
-
174
- else:
175
- # Add a text input field for query
176
- query = st.text_input("Query: ", key="input")
177
-
178
- # Process the query if it's not empty
179
- if query:
180
- with st.spinner("typing....."):
181
- response = generate_response(query)
182
- st.session_state.past.append(query)
183
- st.session_state.generated.append(response)
184
- # Detect sentiment
185
- user_sentiment = chatbot.detect_sentiment(query)
186
-
187
-
188
- # Convert the response to speech
189
- speech_fp = text_to_speech(response)
190
- # Play the speech
191
- st.audio(speech_fp, format='audio/mp3')
192
-
193
  # Update mood history / mood_trend
194
  chatbot.update_mood_history()
195
  mood_trend = chatbot.check_mood_trend()
 
101
  def generate_response(prompt):
102
  response = mdl.call_conversational_rag(prompt,final_chain)
103
  return response['answer']
104
+
105
+ # Collect user input
106
+ # Add a radio button to choose input mode
107
+ input_mode = st.sidebar.radio("Select input mode:", ["Text", "Speech"])
108
+ user_message = None
109
+ if input_mode == "Speech":
110
+ # Use the speech_to_text function to capture speech input
111
+ speech_input = speech_to_text(key="my_stt", callback=speech_recognition_callback)
112
+ # Check if speech input is available
113
+ if "speech_input" in st.session_state and st.session_state.speech_input:
114
+ # Display the speech input
115
+ # st.text(f"Speech Input: {st.session_state.speech_input}")
116
+
117
+ # Process the speech input as a query
118
+ user_message = st.session_state.speech_input
119
+ st.session_state.speech_input = None
120
+ else:
121
+ user_message = st.chat_input("Type your message here:")
122
 
123
  ## Applying the user input box
124
  with input_container:
125
+ if user_message:
126
+ st.session_state.entered_text.append(user_message)
127
+
128
+ st.session_state.messages.append({"role": "user", "content": user_message})
129
+ with st.chat_message("user"):
130
+ st.write(user_message)
 
 
 
 
 
 
 
 
131
 
 
 
132
  with st.spinner("processing....."):
133
  response = generate_response(query)
134
  st.session_state.past.append(query)
 
136
  # Detect sentiment
137
  user_sentiment = chatbot.detect_sentiment(query)
138
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
  # Update mood history / mood_trend
140
  chatbot.update_mood_history()
141
  mood_trend = chatbot.check_mood_trend()