import os import sys # Function to check and install required libraries def install_libraries(): try: import whisper import gtts import gradio as gr from groq import Groq except ImportError: print("Required libraries not found. Installing now...") os.system("pip install git+https://github.com/openai/whisper.git gtts gradio groq") # Try to import again after installation try: import whisper import gtts import gradio as gr from groq import Groq except ImportError: print("Failed to install required libraries. Please install them manually.") sys.exit(1) # Call the install function install_libraries() # Now we can safely import the libraries import whisper import gtts import gradio as gr from groq import Groq # Load Whisper model model = whisper.load_model("base") # Hard-code the Groq API key Groq_api_key = "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" # Check if the API key is set (even though it's hard-coded, we can keep this check) if Groq_api_key is None: raise ValueError("API key for Groq not found. Please set the 'GROQ_API_KEY' environment variable.") # Initialize the Groq client client = Groq(api_key=Groq_api_key) # Function to transcribe audio def transcribe_audio(audio_path): try: result = model.transcribe(audio_path) return result["text"] except Exception as e: return f"Error transcribing audio: {str(e)}" # Function to get response from Groq's API def get_groq_response(transcribed_text): try: chat_completion = client.chat.completions.create( messages=[{ "role": "user", "content": transcribed_text, }], model="llama3-8b-8192", ) return chat_completion.choices[0].message.content except Exception as e: return f"Error getting response from Groq: {str(e)}" # Function to convert text to speech def text_to_speech(text): tts = gtts.gTTS(text=text, lang='en') audio_path = "response.mp3" tts.save(audio_path) return audio_path # Gradio chatbot function def chatbot(audio): # Step 1: Transcribe the audio transcribed_text = transcribe_audio(audio) # Step 2: Get LLM response from Groq API response_text = get_groq_response(transcribed_text) # Step 3: Convert response text to speech response_audio = text_to_speech(response_text) # Return the response audio return response_audio # Create a Gradio interface iface = gr.Interface( fn=chatbot, inputs=gr.Audio(type="filepath"), # Updated to remove 'source' outputs="audio", title="Voice-to-Voice Chatbot", description="Speak to the chatbot and listen to the response!", live=True ) # Launch the interface iface.launch()