Spaces:
Sleeping
Sleeping
| # Install required libraries | |
| # Import required modules | |
| import os | |
| import whisper | |
| import sounddevice as sd | |
| from scipy.io.wavfile import write | |
| from gtts import gTTS | |
| from groq import Groq | |
| import streamlit as st | |
| # Initialize the Groq client | |
| GROQ_API_KEY = "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" # Replace with your Groq API key | |
| client = Groq(api_key=GROQ_API_KEY) | |
| # Load Whisper model | |
| model = whisper.load_model("base") | |
| # Streamlit app layout | |
| st.title("Real-Time Voice-to-Voice Chatbot") | |
| st.markdown(""" | |
| ### How it works: | |
| 1. Record your voice. | |
| 2. The chatbot transcribes your voice using Whisper. | |
| 3. The transcribed text is sent to the Groq API. | |
| 4. The response is converted to audio using gTTS and played back. | |
| """) | |
| # Record audio function | |
| def record_audio(filename="input.wav", duration=5, samplerate=44100): | |
| st.info("Recording... Speak now!") | |
| audio_data = sd.rec(int(duration * samplerate), samplerate=samplerate, channels=1, dtype='int16') | |
| sd.wait() # Wait for recording to complete | |
| write(filename, samplerate, audio_data) | |
| st.success("Recording complete!") | |
| return filename | |
| # Transcribe audio function | |
| def transcribe_audio(filename="input.wav"): | |
| result = model.transcribe(filename) | |
| return result["text"] | |
| # Get response from Groq API | |
| def get_groq_response(query): | |
| try: | |
| chat_completion = client.chat.completions.create( | |
| messages=[{"role": "user", "content": query}], | |
| model="llama3-8b-8192", | |
| ) | |
| response = chat_completion.choices[0].message.content | |
| return response | |
| except Exception as e: | |
| return f"Error: {str(e)}" | |
| # Convert text to audio using gTTS | |
| def text_to_audio(text, output_file="response.mp3"): | |
| tts = gTTS(text) | |
| tts.save(output_file) | |
| return output_file | |
| # Main chatbot workflow | |
| if st.button("Start Voice Interaction"): | |
| # Step 1: Record audio | |
| audio_file = record_audio() | |
| # Step 2: Transcribe audio | |
| with st.spinner("Transcribing audio..."): | |
| user_query = transcribe_audio(audio_file) | |
| st.write(f"**You said:** {user_query}") | |
| # Step 3: Get response from Groq | |
| with st.spinner("Generating response..."): | |
| bot_response = get_groq_response(user_query) | |
| st.write(f"**Bot's response:** {bot_response}") | |
| # Step 4: Convert response to audio | |
| with st.spinner("Converting response to audio..."): | |
| response_audio = text_to_audio(bot_response) | |
| st.audio(response_audio, format="audio/mp3") | |
| st.info("Say 'exit' to stop the chatbot.") | |