import os import tempfile import gradio as gr from groq import Groq import whisper from gtts import gTTS client = Groq(api_key=os.getenv("GROQ_API_KEY")) whisper_model = whisper.load_model("small") def app2_speech_chat(): def speech_to_speech(audio_file): try: result = whisper_model.transcribe(audio_file) user_text = result["text"] chat_completion = client.chat.completions.create( messages=[{"role": "user", "content": user_text}], model="llama-3.3-70b-versatile" ) bot_reply = chat_completion.choices[0].message.content tts = gTTS(bot_reply) temp_audio_path = tempfile.mktemp(suffix=".mp3") tts.save(temp_audio_path) return user_text, bot_reply, temp_audio_path except Exception as e: return f"Error: {str(e)}", "", None interface = gr.Interface( fn=speech_to_speech, inputs=gr.Audio(sources=["microphone", "upload"], type="filepath"), outputs=[ gr.Textbox(label="Transcribed Text"), gr.Textbox(label="Groq Reply"), gr.Audio(label="Reply Audio") ], title="🎤 Speech-to-Speech ChatBot", description="Speak or upload audio → Whisper transcribes → Groq replies → gTTS speaks the answer." ) return interface