abdullahzunorain commited on
Commit
1559d92
1 Parent(s): 6d0f918

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -0
app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from groq import Groq
3
+ from gtts import gTTS
4
+ import time
5
+ import gradio as gr
6
+
7
+ # Set up the Groq client using your API key
8
+ client = Groq(api_key=os.environ['GROQ_API_KEY'])
9
+
10
+ # Function to get chatbot response
11
+ def chatbot_response(user_input):
12
+ # Create a chat completion request
13
+ chat_completion = client.chat.completions.create(
14
+ messages=[{"role": "user", "content": user_input}],
15
+ model="llama3-8b-8192",
16
+ )
17
+ # Get the generated text
18
+ generated_text = chat_completion.choices[0].message.content.strip()
19
+
20
+ # Convert the response to speech
21
+ audio_file = f"response_{int(time.time())}.mp3"
22
+ tts = gTTS(text=generated_text, lang='en')
23
+ tts.save(audio_file)
24
+
25
+ return generated_text, audio_file
26
+
27
+ # Define Gradio interface
28
+ def respond(user_input):
29
+ response, audio_file = chatbot_response(user_input)
30
+ return response, audio_file
31
+
32
+ iface = gr.Interface(fn=respond,
33
+ inputs="text",
34
+ outputs=["text", "audio"],
35
+ title="Groq Chatbot",
36
+ description="Chat with an AI using Groq")
37
+
38
+ if __name__ == "__main__":
39
+ iface.launch()