File size: 2,272 Bytes
6bd516d
 
 
 
 
 
 
e956cf2
6bd516d
ae54262
 
 
6bd516d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import gradio as gr
import os
import whisper
from gtts import gTTS
from groq import Groq

# Set up Groq API client
client = Groq(api_key="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")

# Load Whisper model
model = whisper.load_model("base")

def chatbot(audio):
    # Transcribe the audio input using Whisper
    transcription = model.transcribe(audio)
    user_input = transcription["text"]

    # Generate a response using Llama 8B via Groq API
    chat_completion = client.chat.completions.create(
        messages=[
            {
                "role": "user",
                "content": user_input,
            }
        ],
        model="llama3-8b-8192",
    )
    response_text = chat_completion.choices[0].message.content

    # Convert the response text to speech using gTTS
    tts = gTTS(text=response_text, lang='en')
    response_audio_path = "response.mp3"
    tts.save(response_audio_path)

    return response_text, response_audio_path

# Create a custom interface
#css = ".gradio-container {background: rgb(0, 166, 228)}"
def build_interface():
    with gr.Blocks() as demo:
        gr.Markdown(
            """
            <h1 style="text-align: center; color: #7C25BE;">Voice to Voice Chatbot</h1>
            <h3 style="text-align: center;">Powered by OpenAI Whisper, Llama 8B, and gTTS</h3>
            <p style="text-align: center;">Talk to the AI-powered chatbot and get responses in real-time. Start by recording your voice.</p>
            """
        )
        with gr.Row():
            with gr.Column(scale=1):
                audio_input = gr.Audio(type="filepath", label="Record or Upload Your Voice")
                submit_button = gr.Button("Submit")
            with gr.Column(scale=2):
                chatbot_output_text = gr.Textbox(label="Chatbot Response")
                chatbot_output_audio = gr.Audio(label="Audio Response")

        submit_button.click(chatbot, inputs=audio_input, outputs=[chatbot_output_text, chatbot_output_audio])

        gr.Markdown(
            """
            <p style="text-align: center; color: #888;">Developed by Shahid Hussain</p>
            """
        )
    return demo
    
# Launch the interface
if __name__ == "__main__":
    interface = build_interface()
    interface.launch()