Update app.py
Browse files
app.py
CHANGED
@@ -1,61 +1,61 @@
|
|
1 |
-
import os
|
2 |
-
import gradio as gr
|
3 |
-
import whisper
|
4 |
-
from groq import Groq
|
5 |
-
from gtts import gTTS
|
6 |
-
import tempfile
|
7 |
-
|
8 |
-
# Initialize Whisper model
|
9 |
-
model = whisper.load_model("base")
|
10 |
-
|
11 |
-
# Initialize Groq client
|
12 |
-
client = Groq(
|
13 |
-
api_key="
|
14 |
-
)
|
15 |
-
|
16 |
-
# Function for speech-to-text using Whisper
|
17 |
-
def transcribe_audio(audio):
|
18 |
-
# Transcribe the audio using Whisper
|
19 |
-
result = model.transcribe(audio)
|
20 |
-
return result["text"]
|
21 |
-
|
22 |
-
# Function for generating response using Llama 8B
|
23 |
-
def generate_response(transcription):
|
24 |
-
chat_completion = client.chat.completions.create(
|
25 |
-
messages=[
|
26 |
-
{
|
27 |
-
"role": "user",
|
28 |
-
"content": transcription,
|
29 |
-
}
|
30 |
-
],
|
31 |
-
model="llama3-8b-8192",
|
32 |
-
)
|
33 |
-
return chat_completion.choices[0].message.content
|
34 |
-
|
35 |
-
# Function for text-to-speech using gTTS
|
36 |
-
def text_to_speech(response):
|
37 |
-
tts = gTTS(response)
|
38 |
-
temp_audio = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)
|
39 |
-
tts.save(temp_audio.name)
|
40 |
-
return temp_audio.name
|
41 |
-
|
42 |
-
# Function that integrates the full pipeline
|
43 |
-
def chatbot(audio):
|
44 |
-
transcription = transcribe_audio(audio)
|
45 |
-
response = generate_response(transcription)
|
46 |
-
audio_output = text_to_speech(response)
|
47 |
-
return transcription, response, audio_output
|
48 |
-
|
49 |
-
# Gradio Interface
|
50 |
-
iface = gr.Interface(
|
51 |
-
fn=chatbot,
|
52 |
-
inputs=gr.Audio(type="filepath"), # Removed the 'source' argument
|
53 |
-
outputs=[
|
54 |
-
gr.Textbox(label="Transcription"),
|
55 |
-
gr.Textbox(label="Llama Response"),
|
56 |
-
gr.Audio(label="Response Audio")
|
57 |
-
],
|
58 |
-
live=True
|
59 |
-
)
|
60 |
-
|
61 |
-
iface.launch()
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
import whisper
|
4 |
+
from groq import Groq
|
5 |
+
from gtts import gTTS
|
6 |
+
import tempfile
|
7 |
+
|
8 |
+
# Initialize Whisper model
|
9 |
+
model = whisper.load_model("base")
|
10 |
+
|
11 |
+
# Initialize Groq client
|
12 |
+
client = Groq(
|
13 |
+
api_key = os.environ.get("GROQ_API_KEY"),
|
14 |
+
)
|
15 |
+
|
16 |
+
# Function for speech-to-text using Whisper
|
17 |
+
def transcribe_audio(audio):
|
18 |
+
# Transcribe the audio using Whisper
|
19 |
+
result = model.transcribe(audio)
|
20 |
+
return result["text"]
|
21 |
+
|
22 |
+
# Function for generating response using Llama 8B
|
23 |
+
def generate_response(transcription):
|
24 |
+
chat_completion = client.chat.completions.create(
|
25 |
+
messages=[
|
26 |
+
{
|
27 |
+
"role": "user",
|
28 |
+
"content": transcription,
|
29 |
+
}
|
30 |
+
],
|
31 |
+
model="llama3-8b-8192",
|
32 |
+
)
|
33 |
+
return chat_completion.choices[0].message.content
|
34 |
+
|
35 |
+
# Function for text-to-speech using gTTS
|
36 |
+
def text_to_speech(response):
|
37 |
+
tts = gTTS(response)
|
38 |
+
temp_audio = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)
|
39 |
+
tts.save(temp_audio.name)
|
40 |
+
return temp_audio.name
|
41 |
+
|
42 |
+
# Function that integrates the full pipeline
|
43 |
+
def chatbot(audio):
|
44 |
+
transcription = transcribe_audio(audio)
|
45 |
+
response = generate_response(transcription)
|
46 |
+
audio_output = text_to_speech(response)
|
47 |
+
return transcription, response, audio_output
|
48 |
+
|
49 |
+
# Gradio Interface
|
50 |
+
iface = gr.Interface(
|
51 |
+
fn=chatbot,
|
52 |
+
inputs=gr.Audio(type="filepath"), # Removed the 'source' argument
|
53 |
+
outputs=[
|
54 |
+
gr.Textbox(label="Transcription"),
|
55 |
+
gr.Textbox(label="Llama Response"),
|
56 |
+
gr.Audio(label="Response Audio")
|
57 |
+
],
|
58 |
+
live=True
|
59 |
+
)
|
60 |
+
|
61 |
+
iface.launch()
|