devfire commited on
Commit
74e90af
·
verified ·
1 Parent(s): 990ca55

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -5
app.py CHANGED
@@ -1,5 +1,8 @@
1
  import os
2
  import streamlit as st
 
 
 
3
  from groq import Groq
4
 
5
  # Set up the Groq API Key
@@ -87,16 +90,37 @@ def generate_chatbot_response(user_message):
87
  response = chat_completion.choices[0].message.content
88
  return response
89
 
90
- # User input for conversation (now placed at the bottom)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
  st.markdown("### 💬 Chat with me")
92
- user_input = st.chat_input("Ask me a subject-related question:")
 
 
 
 
93
 
94
- # Handle user input and display conversation
95
  if user_input:
96
  chatbot_response = generate_chatbot_response(user_input)
97
-
98
- # Save the conversation history
99
  st.session_state.conversation_history.append(("User: " + user_input, "Chatbot: " + chatbot_response))
 
 
100
 
101
  # Display chat history
102
  st.markdown("---")
 
1
  import os
2
  import streamlit as st
3
+ import speech_recognition as sr
4
+ from gtts import gTTS
5
+ import tempfile
6
  from groq import Groq
7
 
8
  # Set up the Groq API Key
 
90
  response = chat_completion.choices[0].message.content
91
  return response
92
 
93
+ # Voice input function
94
+ def voice_input():
95
+ recognizer = sr.Recognizer()
96
+ with sr.Microphone() as source:
97
+ st.info("Listening...")
98
+ audio = recognizer.listen(source)
99
+ try:
100
+ return recognizer.recognize_google(audio)
101
+ except sr.UnknownValueError:
102
+ return "Sorry, I could not understand the audio."
103
+
104
+ # Text-to-Speech output
105
+ def text_to_speech(text):
106
+ tts = gTTS(text)
107
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_audio:
108
+ tts.save(temp_audio.name)
109
+ return temp_audio.name
110
+
111
+ # User input for conversation
112
  st.markdown("### 💬 Chat with me")
113
+ if st.button("🎤 Speak Now"):
114
+ user_input = voice_input()
115
+ st.text(f"You said: {user_input}")
116
+ else:
117
+ user_input = st.chat_input("Ask me a subject-related question:")
118
 
 
119
  if user_input:
120
  chatbot_response = generate_chatbot_response(user_input)
 
 
121
  st.session_state.conversation_history.append(("User: " + user_input, "Chatbot: " + chatbot_response))
122
+ audio_file = text_to_speech(chatbot_response)
123
+ st.audio(audio_file, format='audio/mp3')
124
 
125
  # Display chat history
126
  st.markdown("---")