Ammar-khan commited on
Commit
fb39d60
·
verified ·
1 Parent(s): a4ba564

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from groq import Groq
4
+ import whisper
5
+ from gtts import gTTS
6
+ import tempfile
7
+
8
+ # Set up Groq API key
9
+ os.environ['GROQ_API_KEY'] = 'GROQ_API-KEY'
10
+ groq_client = Groq(api_key=os.environ.get('GROQ_API_KEY'))
11
+
12
+ # Load Whisper model
13
+ whisper_model = whisper.load_model("base")
14
+
15
+ def process_audio(audio_file):
16
+ # Transcribe audio using Whisper
17
+ result = whisper_model.transcribe(audio_file)
18
+ user_text = result['text']
19
+
20
+ # Generate response using Llama 8b model with Groq API
21
+ chat_completion = groq_client.chat.completions.create(
22
+ messages=[
23
+ {
24
+ "role": "user",
25
+ "content": user_text,
26
+ }
27
+ ],
28
+ model="llama3-8b-8192",
29
+ )
30
+ response_text = chat_completion.choices[0].message.content
31
+
32
+ # Convert response text to speech using gTTS
33
+ tts = gTTS(text=response_text, lang='en')
34
+ audio_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp3')
35
+ tts.save(audio_file.name)
36
+
37
+ return response_text, audio_file.name
38
+
39
+ # Create Gradio interface
40
+ iface = gr.Interface(
41
+ fn=process_audio,
42
+ inputs=gr.Audio(type="filepath"),
43
+ outputs=[gr.Textbox(label="Response"), gr.Audio(label="Response Audio")],
44
+ live=True
45
+ )
46
+
47
+ iface.launch()