File size: 1,159 Bytes
1559d92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from groq import Groq
from gtts import gTTS
import time
import gradio as gr

# Set up the Groq client using your API key
client = Groq(api_key=os.environ['GROQ_API_KEY'])

# Function to get chatbot response
def chatbot_response(user_input):
    # Create a chat completion request
    chat_completion = client.chat.completions.create(
        messages=[{"role": "user", "content": user_input}],
        model="llama3-8b-8192",
    )
    # Get the generated text
    generated_text = chat_completion.choices[0].message.content.strip()
    
    # Convert the response to speech
    audio_file = f"response_{int(time.time())}.mp3"
    tts = gTTS(text=generated_text, lang='en')
    tts.save(audio_file)

    return generated_text, audio_file

# Define Gradio interface
def respond(user_input):
    response, audio_file = chatbot_response(user_input)
    return response, audio_file

iface = gr.Interface(fn=respond,
                     inputs="text",
                     outputs=["text", "audio"],
                     title="Groq Chatbot",
                     description="Chat with an AI using Groq")

if __name__ == "__main__":
    iface.launch()