safiaa02 commited on
Commit
55db6f6
Β·
verified Β·
1 Parent(s): 3108987

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -0
app.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from groq import Groq
3
+ from gtts import gTTS
4
+ import tempfile
5
+ import os
6
+
7
+ # ---------------- CONFIG ----------------
8
+ # On Hugging Face Spaces, add your Groq API key under Settings β†’ Secrets β†’ GROQ_API_KEY
9
+ client = Groq(api_key=os.getenv("GROQ_API_KEY", "YOUR_GROQ_API_KEY"))
10
+
11
+ # Store last few messages for context
12
+ memory = []
13
+
14
+ # ---------------- LLM BOT ----------------
15
+ def llm_bot(prompt):
16
+ # Use last few turns of context for continuity
17
+ chat_history = "\n".join([f"{r[0]}: {r[1]}" for r in memory[-6:]])
18
+
19
+ completion = client.chat.completions.create(
20
+ model="llama-3.1-8b-instant",
21
+ messages=[
22
+ {"role": "system", "content": "You are a friendly and helpful AI voice assistant."},
23
+ {"role": "user", "content": f"{chat_history}\nUser: {prompt}"}
24
+ ],
25
+ )
26
+ return completion.choices[0].message.content.strip()
27
+
28
+ # ---------------- MAIN FUNCTION ----------------
29
+ def speech_to_speech(audio):
30
+ if audio is None:
31
+ return "Please speak something!", None
32
+
33
+ # Step 1: Transcribe speech to text using Groq Whisper
34
+ with open(audio, "rb") as f:
35
+ transcription = client.audio.transcriptions.create(
36
+ file=("speech.wav", f.read()),
37
+ model="whisper-large-v3",
38
+ )
39
+ user_text = transcription.text.strip()
40
+
41
+ # Step 2: Generate LLM response
42
+ response = llm_bot(user_text)
43
+ memory.append(("User", user_text))
44
+ memory.append(("Bot", response))
45
+
46
+ # Step 3: Convert LLM response to speech
47
+ tts = gTTS(response)
48
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_audio:
49
+ tts.save(temp_audio.name)
50
+ audio_out = temp_audio.name
51
+
52
+ # Step 4: Return text + audio
53
+ return f"πŸ—£οΈ You said: {user_text}\n\nπŸ€– Bot: {response}", audio_out
54
+
55
+
56
+ # ---------------- GRADIO INTERFACE ----------------
57
+ with gr.Blocks(title="Groq Voice Chatbot πŸŽ™οΈ") as demo:
58
+ gr.Markdown("## 🎧 Groq-Powered Voice Assistant")
59
+ gr.Markdown("Speak naturally β€” it listens, thinks, and replies with speech using LLaMA-3.1 and Whisper on Groq!")
60
+
61
+ mic = gr.Audio(sources=["microphone"], type="filepath", label="πŸŽ™οΈ Speak Here")
62
+ output_text = gr.Textbox(label="🧠 Transcription + Bot Response")
63
+ output_audio = gr.Audio(label="πŸ”Š Bot Voice Reply")
64
+
65
+ submit_btn = gr.Button("πŸ’¬ Talk")
66
+
67
+ submit_btn.click(
68
+ fn=speech_to_speech,
69
+ inputs=[mic],
70
+ outputs=[output_text, output_audio]
71
+ )
72
+
73
+ demo.launch()