Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import whisper
|
3 |
+
import gradio as gr
|
4 |
+
import requests
|
5 |
+
from gtts import gTTS
|
6 |
+
|
7 |
+
# Load Whisper model
|
8 |
+
model = whisper.load_model("base")
|
9 |
+
|
10 |
+
# Read Groq API Key from environment variable
|
11 |
+
GROQ_API_KEY = os.getenv("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")
|
12 |
+
client= GROQ(API_KEY=GROQ_API_KEY)
|
13 |
+
# Main function: audio β text β LLM β speech
|
14 |
+
def transcribe_and_respond(audio_file):
|
15 |
+
# 1. Transcribe audio
|
16 |
+
result = model.transcribe(audio_file)
|
17 |
+
user_text = result["text"]
|
18 |
+
|
19 |
+
# 2. Query Groq LLM
|
20 |
+
headers = {
|
21 |
+
"Content-Type": "application/json",
|
22 |
+
"Authorization": f"Bearer {GROQ_API_KEY}"
|
23 |
+
}
|
24 |
+
|
25 |
+
data = {
|
26 |
+
"model": "llama-3.3-70b-versatile",
|
27 |
+
"messages": [{"role": "user", "content": user_text}]
|
28 |
+
}
|
29 |
+
|
30 |
+
response = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=data)
|
31 |
+
|
32 |
+
if response.status_code == 200:
|
33 |
+
output_text = response.json()['choices'][0]['message']['content']
|
34 |
+
else:
|
35 |
+
output_text = f"Error from Groq API: {response.status_code} - {response.text}"
|
36 |
+
|
37 |
+
# 3. Convert to speech
|
38 |
+
tts = gTTS(text=output_text, lang='en')
|
39 |
+
tts_path = "response.mp3"
|
40 |
+
tts.save(tts_path)
|
41 |
+
|
42 |
+
return output_text, tts_path
|
43 |
+
|
44 |
+
# Gradio UI
|
45 |
+
iface = gr.Interface(
|
46 |
+
fn=transcribe_and_respond,
|
47 |
+
inputs=gr.Audio(type="filepath", label="ποΈ Speak"),
|
48 |
+
outputs=[gr.Textbox(label="π§ LLM Reply"), gr.Audio(label="π Spoken Response")],
|
49 |
+
title="Voice Chatbot with Whisper + Groq + gTTS",
|
50 |
+
description="Click to record β Get LLM reply β Hear it spoken back"
|
51 |
+
)
|
52 |
+
|
53 |
+
if __name__ == "__main__":
|
54 |
+
iface.launch()
|