Bey007 commited on
Commit
62d4ef4
·
verified ·
1 Parent(s): 5e480b6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -11
app.py CHANGED
@@ -40,24 +40,27 @@ if user_input:
40
  sentiment = sentiment_analysis(user_input)[0]
41
 
42
  # Generate empathetic response with increased length and more context
43
- response = conversational_bot(user_input, max_length=300, num_return_sequences=1, temperature=0.9, top_k=50)[0]['generated_text']
44
 
45
- # Ensure response does not repeat what the user said, and is supportive
46
- if user_input.lower() in response.lower():
47
- response = "I understand how you're feeling. You're not alone in this. I'm here to listen and help."
48
-
49
- # Check if the response is too similar to the previous one, if so, generate a new one
50
- if response == st.session_state.previous_response:
51
- response = conversational_bot(user_input, max_length=300, num_return_sequences=1, temperature=0.9, top_k=50)[0]['generated_text']
 
 
 
52
 
53
  # Store the response for future comparison
54
- st.session_state.previous_response = response
55
 
56
  # Display response
57
- st.text_area("Bot's Response:", response, height=250)
58
 
59
  # Text-to-speech output
60
- tts = gTTS(response, lang='en')
61
  audio_file = "response.mp3"
62
  tts.save(audio_file)
63
  st.audio(audio_file, format="audio/mp3")
 
40
  sentiment = sentiment_analysis(user_input)[0]
41
 
42
  # Generate empathetic response with increased length and more context
43
+ response = conversational_bot(user_input, max_length=300, num_return_sequences=3, temperature=0.9, top_k=50)
44
 
45
+ # Choose the response that does not repeat what the user said
46
+ best_response = ""
47
+ for generated in response:
48
+ if user_input.lower() not in generated['generated_text'].lower():
49
+ best_response = generated['generated_text']
50
+ break
51
+
52
+ # If no diverse response was found, generate a default supportive message
53
+ if not best_response:
54
+ best_response = "I understand how you're feeling. You're not alone in this. I'm here to listen and help."
55
 
56
  # Store the response for future comparison
57
+ st.session_state.previous_response = best_response
58
 
59
  # Display response
60
+ st.text_area("Bot's Response:", best_response, height=250)
61
 
62
  # Text-to-speech output
63
+ tts = gTTS(best_response, lang='en')
64
  audio_file = "response.mp3"
65
  tts.save(audio_file)
66
  st.audio(audio_file, format="audio/mp3")