Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Install dependencies in Colab
|
| 2 |
+
try:
|
| 3 |
+
import whisper, gradio as gr
|
| 4 |
+
from gtts import gTTS
|
| 5 |
+
from groq import Groq
|
| 6 |
+
except:
|
| 7 |
+
import whisper, gradio as gr
|
| 8 |
+
from gtts import gTTS
|
| 9 |
+
from groq import Groq
|
| 10 |
+
|
| 11 |
+
import os
|
| 12 |
+
import tempfile
|
| 13 |
+
|
| 14 |
+
# Load Whisper model
|
| 15 |
+
whisper_model = whisper.load_model("base")
|
| 16 |
+
|
| 17 |
+
# Groq API Key (replace with your actual key or set as env variable)
|
| 18 |
+
GROQ_API_KEY = "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"
|
| 19 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 20 |
+
|
| 21 |
+
# Core logic
|
| 22 |
+
def voice_chat(audio_path):
|
| 23 |
+
# Step 1: Transcribe audio
|
| 24 |
+
result = whisper_model.transcribe(audio_path)
|
| 25 |
+
user_text = result["text"]
|
| 26 |
+
|
| 27 |
+
# Step 2: Groq LLM response
|
| 28 |
+
response = client.chat.completions.create(
|
| 29 |
+
messages=[{"role": "user", "content": user_text}],
|
| 30 |
+
model="llama3-8b-8192",
|
| 31 |
+
)
|
| 32 |
+
bot_reply = response.choices[0].message.content
|
| 33 |
+
|
| 34 |
+
# Step 3: Text to speech using gTTS
|
| 35 |
+
tts = gTTS(bot_reply)
|
| 36 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as f:
|
| 37 |
+
tts.save(f.name)
|
| 38 |
+
audio_response_path = f.name
|
| 39 |
+
|
| 40 |
+
return user_text, bot_reply, audio_response_path
|
| 41 |
+
|
| 42 |
+
# Gradio interface
|
| 43 |
+
iface = gr.Interface(
|
| 44 |
+
fn=voice_chat,
|
| 45 |
+
inputs=gr.Microphone(label="π€ Speak your question", type="filepath"),
|
| 46 |
+
outputs=[
|
| 47 |
+
gr.Text(label="π Transcribed Input"),
|
| 48 |
+
gr.Text(label="π€ LLM Reply"),
|
| 49 |
+
gr.Audio(label="π Spoken Reply", type="filepath")
|
| 50 |
+
],
|
| 51 |
+
title="π£οΈ Real-Time Voice-to-Voice Chatbot (Whisper + Groq + gTTS)",
|
| 52 |
+
live=True
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
if __name__ == "__main__":
|
| 56 |
+
iface.launch()
|