Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import whisper
|
3 |
+
from groq import Groq
|
4 |
+
from gtts import gTTS
|
5 |
+
import tempfile
|
6 |
+
import IPython.display as ipd
|
7 |
+
import gradio as gr
|
8 |
+
|
9 |
+
# Step 1: Set up Whisper for transcription
|
10 |
+
model = whisper.load_model("base")
|
11 |
+
|
12 |
+
# Function to transcribe audio using Whisper
|
13 |
+
def transcribe_audio(audio_file):
|
14 |
+
result = model.transcribe(audio_file)
|
15 |
+
return result["text"]
|
16 |
+
|
17 |
+
GROOQ_API_KEY = "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"
|
18 |
+
Client = Grooq(api_key=GROOQ_API_KEY)
|
19 |
+
# Function to get a response from the Groq LLM (Llama 3)
|
20 |
+
def get_groq_response(text):
|
21 |
+
chat_completion = client.chat.completions.create(
|
22 |
+
messages=[{"role": "user", "content": text}],
|
23 |
+
model="llama3-8b-8192", # Use any other model if you prefer
|
24 |
+
stream=False
|
25 |
+
)
|
26 |
+
return chat_completion.choices[0].message.content
|
27 |
+
|
28 |
+
# Step 3: Convert text response from LLM to speech using GTTS
|
29 |
+
def text_to_speech(text):
|
30 |
+
tts = gTTS(text)
|
31 |
+
|
32 |
+
# Save the audio to a temporary file
|
33 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False)
|
34 |
+
tts.save(temp_file.name)
|
35 |
+
|
36 |
+
# Play the audio in Colab (testing)
|
37 |
+
ipd.Audio(temp_file.name)
|
38 |
+
|
39 |
+
return temp_file.name # Return file path for further use
|
40 |
+
|
41 |
+
# Step 4: Integrate everything into a Gradio interface
|
42 |
+
def voice_chatbot(audio_input):
|
43 |
+
# Step 1: Transcribe the audio using Whisper
|
44 |
+
transcription = transcribe_audio(audio_input)
|
45 |
+
|
46 |
+
# Step 2: Get response from Groq API using the transcription
|
47 |
+
response = get_groq_response(transcription)
|
48 |
+
|
49 |
+
# Step 3: Convert the response text to speech using GTTS
|
50 |
+
audio_response = text_to_speech(response)
|
51 |
+
|
52 |
+
# Return the audio response (Gradio will play it)
|
53 |
+
return audio_response
|
54 |
+
|
55 |
+
# Step 5: Create the Gradio interface (microphone input and audio output)
|
56 |
+
iface = gr.Interface(fn=voice_chatbot,
|
57 |
+
inputs=gr.Audio(type="filepath"), # No source="microphone" argument
|
58 |
+
outputs=gr.Audio(),
|
59 |
+
live=True)
|
60 |
+
|
61 |
+
# Launch the Gradio interface
|
62 |
+
iface.launch()
|