Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import libraries
|
2 |
+
import whisper
|
3 |
+
from gtts import gTTS
|
4 |
+
from groq import Groq
|
5 |
+
import os
|
6 |
+
import gradio as gr
|
7 |
+
|
8 |
+
# Load Whisper model
|
9 |
+
model = whisper.load_model("base")
|
10 |
+
|
11 |
+
GROQ_API_KEY = "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"
|
12 |
+
client = Groq(api_key=GROQ_API_KEY)
|
13 |
+
|
14 |
+
# Function to process audio
|
15 |
+
def process_audio(audio_path):
|
16 |
+
# Transcribe input audio
|
17 |
+
transcription = model.transcribe(audio_path)["text"]
|
18 |
+
|
19 |
+
# Interact with the GROQ model
|
20 |
+
chat_completion = client.chat.completions.create(
|
21 |
+
messages=[{"role": "user", "content": transcription}],
|
22 |
+
model="llama3-8b-8192"
|
23 |
+
)
|
24 |
+
response_text = chat_completion.choices[0].message.content
|
25 |
+
|
26 |
+
# Convert LLM response to audio
|
27 |
+
tts = gTTS(response_text)
|
28 |
+
response_audio_path = "response.mp3"
|
29 |
+
tts.save(response_audio_path)
|
30 |
+
|
31 |
+
return response_text, response_audio_path
|
32 |
+
|
33 |
+
# Gradio UI
|
34 |
+
with gr.Blocks() as demo:
|
35 |
+
gr.Markdown("### Real Voice-to-Voice Chatbot")
|
36 |
+
with gr.Row():
|
37 |
+
input_audio = gr.Audio(type="filepath", label="Record Your Voice")
|
38 |
+
output_text = gr.Textbox(label="LLM Response", lines=4)
|
39 |
+
output_audio = gr.Audio(label="Response Audio")
|
40 |
+
submit_button = gr.Button("Submit")
|
41 |
+
submit_button.click(
|
42 |
+
process_audio,
|
43 |
+
inputs=[input_audio],
|
44 |
+
outputs=[output_text, output_audio],
|
45 |
+
)
|
46 |
+
|
47 |
+
demo.launch()
|