Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,50 @@
|
|
| 1 |
-
# Install dependencies
|
| 2 |
-
|
| 3 |
-
|
| 4 |
import os
|
| 5 |
import gradio as gr
|
| 6 |
import whisper
|
| 7 |
-
import ffmpeg
|
| 8 |
from groq import Groq
|
| 9 |
from gtts import gTTS
|
|
|
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# Load Whisper model
|
| 17 |
whisper_model = whisper.load_model("base")
|
| 18 |
|
| 19 |
-
# ============ FUNCTIONS ============
|
| 20 |
def transcribe_and_chat(audio_file):
|
| 21 |
if audio_file is None:
|
| 22 |
return "No audio received", "Error: Please record again.", None
|
| 23 |
-
try:
|
| 24 |
-
# Convert audio to wav (Whisper requirement)
|
| 25 |
-
wav_file = "converted.wav"
|
| 26 |
-
(
|
| 27 |
-
ffmpeg
|
| 28 |
-
.input(audio_file)
|
| 29 |
-
.output(wav_file, format="wav")
|
| 30 |
-
.overwrite_output()
|
| 31 |
-
.run(quiet=True)
|
| 32 |
-
)
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
|
|
|
| 36 |
user_text = result.get("text", "").strip()
|
| 37 |
if not user_text:
|
| 38 |
return "Could not transcribe", "Error: Empty transcription", None
|
| 39 |
|
| 40 |
-
# Query Groq
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
bot_text = chat_completion.choices[0].message.content
|
| 47 |
-
except Exception as e:
|
| 48 |
-
return user_text, f"LLM Error: {str(e)}", None
|
| 49 |
|
| 50 |
-
#
|
| 51 |
tts = gTTS(bot_text)
|
| 52 |
-
|
| 53 |
-
|
|
|
|
| 54 |
|
| 55 |
return user_text, bot_text, reply_audio
|
| 56 |
|
| 57 |
except Exception as e:
|
| 58 |
return "Transcription error", str(e), None
|
| 59 |
|
| 60 |
-
#
|
| 61 |
with gr.Blocks() as demo:
|
| 62 |
gr.Markdown("## ποΈ Real-time Voice-to-Voice Chatbot (Whisper + Groq + gTTS)")
|
| 63 |
|
|
@@ -73,4 +60,5 @@ with gr.Blocks() as demo:
|
|
| 73 |
outputs=[user_text_out, bot_text_out, audio_out]
|
| 74 |
)
|
| 75 |
|
| 76 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import whisper
|
|
|
|
| 4 |
from groq import Groq
|
| 5 |
from gtts import gTTS
|
| 6 |
+
import tempfile
|
| 7 |
|
| 8 |
+
# Load Groq API key from Hugging Face secrets
|
| 9 |
+
GROQ_API_KEY = os.environ.get("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")
|
| 10 |
+
if not GROQ_API_KEY:
|
| 11 |
+
raise ValueError("β Missing GROQ_API_KEY. Please set it in Hugging Face β Settings β Secrets")
|
| 12 |
+
|
| 13 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 14 |
|
| 15 |
# Load Whisper model
|
| 16 |
whisper_model = whisper.load_model("base")
|
| 17 |
|
|
|
|
| 18 |
def transcribe_and_chat(audio_file):
|
| 19 |
if audio_file is None:
|
| 20 |
return "No audio received", "Error: Please record again.", None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
try:
|
| 23 |
+
# 1. Transcribe
|
| 24 |
+
result = whisper_model.transcribe(audio_file)
|
| 25 |
user_text = result.get("text", "").strip()
|
| 26 |
if not user_text:
|
| 27 |
return "Could not transcribe", "Error: Empty transcription", None
|
| 28 |
|
| 29 |
+
# 2. Query Groq
|
| 30 |
+
chat_completion = client.chat.completions.create(
|
| 31 |
+
messages=[{"role": "user", "content": user_text}],
|
| 32 |
+
model="llama-3.1-8b-instant" # β
stable model
|
| 33 |
+
)
|
| 34 |
+
bot_text = chat_completion.choices[0].message.content
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
# 3. TTS
|
| 37 |
tts = gTTS(bot_text)
|
| 38 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp:
|
| 39 |
+
tts.save(tmp.name)
|
| 40 |
+
reply_audio = tmp.name
|
| 41 |
|
| 42 |
return user_text, bot_text, reply_audio
|
| 43 |
|
| 44 |
except Exception as e:
|
| 45 |
return "Transcription error", str(e), None
|
| 46 |
|
| 47 |
+
# Gradio UI
|
| 48 |
with gr.Blocks() as demo:
|
| 49 |
gr.Markdown("## ποΈ Real-time Voice-to-Voice Chatbot (Whisper + Groq + gTTS)")
|
| 50 |
|
|
|
|
| 60 |
outputs=[user_text_out, bot_text_out, audio_out]
|
| 61 |
)
|
| 62 |
|
| 63 |
+
demo.launch()
|
| 64 |
+
|