File size: 1,392 Bytes
faf41a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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