Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from gtts import gTTS
|
| 3 |
+
from groq import Groq
|
| 4 |
+
import whisper
|
| 5 |
+
import tempfile
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# Initialize Groq client
|
| 9 |
+
api="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"
|
| 10 |
+
client = Groq(api_key=api)
|
| 11 |
+
|
| 12 |
+
# Load the Whisper model locally
|
| 13 |
+
whisper_model = whisper.load_model("base") # Options: "tiny", "base", "small", "medium", "large"
|
| 14 |
+
|
| 15 |
+
# Function to handle transcription, LLM response, and audio synthesis
|
| 16 |
+
def voice_to_voice(audio_file):
|
| 17 |
+
try:
|
| 18 |
+
# 1. Transcribe the audio using the local Whisper model
|
| 19 |
+
result = whisper_model.transcribe(audio_file)
|
| 20 |
+
user_input = result["text"]
|
| 21 |
+
|
| 22 |
+
# 2. Interact with the LLM via Groq API
|
| 23 |
+
chat_completion = client.chat.completions.create(
|
| 24 |
+
messages=[
|
| 25 |
+
{
|
| 26 |
+
"role": "user",
|
| 27 |
+
"content": user_input,
|
| 28 |
+
}
|
| 29 |
+
],
|
| 30 |
+
model="llama3-8b-8192",
|
| 31 |
+
stream=False,
|
| 32 |
+
)
|
| 33 |
+
response_text = chat_completion.choices[0].message.content
|
| 34 |
+
|
| 35 |
+
# 3. Convert text response to speech using GTTS
|
| 36 |
+
tts = gTTS(response_text)
|
| 37 |
+
temp_audio_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 38 |
+
tts.save(temp_audio_file.name)
|
| 39 |
+
|
| 40 |
+
return temp_audio_file.name, response_text
|
| 41 |
+
|
| 42 |
+
except Exception as e:
|
| 43 |
+
return None, f"Error: {str(e)}"
|
| 44 |
+
|
| 45 |
+
# Build Gradio interface
|
| 46 |
+
with gr.Blocks() as demo:
|
| 47 |
+
gr.Markdown("## Real-Time Voice-to-Voice Chatbot")
|
| 48 |
+
audio_input = gr.Audio(type="filepath", label="Speak Something")
|
| 49 |
+
audio_output = gr.Audio(label="Bot Response")
|
| 50 |
+
text_output = gr.Textbox(label="Transcription & Response")
|
| 51 |
+
|
| 52 |
+
btn = gr.Button("Process")
|
| 53 |
+
btn.click(voice_to_voice, inputs=audio_input, outputs=[audio_output, text_output])
|
| 54 |
+
|
| 55 |
+
# Launch the interface
|
| 56 |
+
demo.launch()
|