Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,52 +1,51 @@
|
|
1 |
-
# Import libraries
|
2 |
-
import whisper
|
3 |
-
import os
|
4 |
-
from gtts import gTTS
|
5 |
-
import gradio as gr
|
6 |
-
from groq import Groq
|
7 |
-
|
8 |
-
# Load Whisper model for transcription
|
9 |
-
model = whisper.load_model("base")
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
tts
|
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 |
-
iface.launch()
|
|
|
1 |
+
# Import libraries
|
2 |
+
import whisper
|
3 |
+
import os
|
4 |
+
from gtts import gTTS
|
5 |
+
import gradio as gr
|
6 |
+
from groq import Groq
|
7 |
+
|
8 |
+
# Load Whisper model for transcription
|
9 |
+
model = whisper.load_model("base")
|
10 |
+
|
11 |
+
|
12 |
+
client = Groq(api_key=Groq_api_key)
|
13 |
+
|
14 |
+
# Function to get the LLM response from Groq
|
15 |
+
def get_llm_response(user_input):
|
16 |
+
chat_completion = client.chat.completions.create(
|
17 |
+
messages=[{"role": "user", "content": user_input}],
|
18 |
+
model="llama3-8b-8192", # Replace with your desired model
|
19 |
+
)
|
20 |
+
return chat_completion.choices[0].message.content
|
21 |
+
|
22 |
+
# Function to convert text to speech using gTTS
|
23 |
+
def text_to_speech(text, output_audio="output_audio.mp3"):
|
24 |
+
tts = gTTS(text)
|
25 |
+
tts.save(output_audio)
|
26 |
+
return output_audio
|
27 |
+
|
28 |
+
# Main chatbot function to handle audio input and output
|
29 |
+
def chatbot(audio):
|
30 |
+
# Step 1: Transcribe the audio using Whisper
|
31 |
+
result = model.transcribe(audio)
|
32 |
+
user_text = result["text"]
|
33 |
+
|
34 |
+
# Step 2: Get LLM response from Groq
|
35 |
+
response_text = get_llm_response(user_text)
|
36 |
+
|
37 |
+
# Step 3: Convert the response text to speech
|
38 |
+
output_audio = text_to_speech(response_text)
|
39 |
+
|
40 |
+
return response_text, output_audio
|
41 |
+
|
42 |
+
# Gradio interface for real-time interaction
|
43 |
+
iface = gr.Interface(
|
44 |
+
fn=chatbot,
|
45 |
+
inputs=gr.Audio(type="filepath"), # Input from mic or file
|
46 |
+
outputs=[gr.Textbox(), gr.Audio(type="filepath")], # Output: response text and audio
|
47 |
+
live=True
|
48 |
+
)
|
49 |
+
|
50 |
+
# Launch the Gradio app
|
51 |
+
iface.launch()
|
|