Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,91 +1,39 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
import whisper
|
| 4 |
from gtts import gTTS
|
| 5 |
-
from
|
| 6 |
import tempfile
|
| 7 |
-
from streamlit_webrtc import webrtc_streamer, WebRtcMode, AudioProcessorBase, ClientSettings
|
| 8 |
-
import numpy as np
|
| 9 |
-
import wave
|
| 10 |
|
| 11 |
-
# Load
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
# Load Whisper model
|
| 15 |
-
st.write("Loading Whisper model...")
|
| 16 |
whisper_model = whisper.load_model("base")
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
raise ValueError("API key is missing. Set GROQ_API_KEY in your .env file.")
|
| 22 |
|
| 23 |
# Function to transcribe audio to text
|
| 24 |
def transcribe_audio(audio_file):
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
#
|
| 29 |
def get_llm_response(user_input):
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
def recv(self, frame):
|
| 46 |
-
self.frames.append(frame.to_ndarray().flatten())
|
| 47 |
-
return frame
|
| 48 |
-
|
| 49 |
-
def save_audio(self, path):
|
| 50 |
-
with wave.open(path, "wb") as wf:
|
| 51 |
-
wf.setnchannels(1) # Mono
|
| 52 |
-
wf.setsampwidth(2) # 16-bit samples
|
| 53 |
-
wf.setframerate(16000) # 16kHz
|
| 54 |
-
wf.writeframes(np.concatenate(self.frames).tobytes())
|
| 55 |
-
|
| 56 |
-
# Streamlit UI
|
| 57 |
-
st.title("Real-Time Voice Chatbot")
|
| 58 |
-
st.write("Interact with the chatbot using your voice.")
|
| 59 |
-
|
| 60 |
-
webrtc_ctx = webrtc_streamer(
|
| 61 |
-
key="example",
|
| 62 |
-
mode=WebRtcMode.SENDONLY,
|
| 63 |
-
audio_processor_factory=AudioProcessor,
|
| 64 |
-
client_settings=ClientSettings(
|
| 65 |
-
rtc_configuration={"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]},
|
| 66 |
-
media_stream_constraints={"audio": True, "video": False},
|
| 67 |
-
),
|
| 68 |
-
)
|
| 69 |
-
|
| 70 |
-
if webrtc_ctx.audio_processor:
|
| 71 |
-
audio_processor = webrtc_ctx.audio_processor
|
| 72 |
-
|
| 73 |
-
if st.button("Process Microphone Input"):
|
| 74 |
-
if audio_processor:
|
| 75 |
-
audio_path = tempfile.NamedTemporaryFile(suffix=".wav", delete=False).name
|
| 76 |
-
audio_processor.save_audio(audio_path)
|
| 77 |
-
|
| 78 |
-
# Step 1: Transcribe
|
| 79 |
-
with st.spinner("Transcribing audio..."):
|
| 80 |
-
user_input = transcribe_audio(audio_path)
|
| 81 |
-
st.write(f"**You said:** {user_input}")
|
| 82 |
-
|
| 83 |
-
# Step 2: Get LLM response
|
| 84 |
-
with st.spinner("Generating response..."):
|
| 85 |
-
response = get_llm_response(user_input)
|
| 86 |
-
st.write(f"**Chatbot Response:** {response}")
|
| 87 |
-
|
| 88 |
-
# Step 3: Text to speech
|
| 89 |
-
with st.spinner("Converting response to audio..."):
|
| 90 |
-
response_audio = text_to_speech(response)
|
| 91 |
-
st.audio(response_audio, format="audio/mp3")
|
|
|
|
| 1 |
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
import whisper
|
| 4 |
from gtts import gTTS
|
| 5 |
+
from groq import Groq
|
| 6 |
import tempfile
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# Load Whisper model for speech-to-text
|
| 9 |
+
print("Loading Whisper model...")
|
|
|
|
|
|
|
|
|
|
| 10 |
whisper_model = whisper.load_model("base")
|
| 11 |
|
| 12 |
+
# Initialize Groq API
|
| 13 |
+
print("Initializing Groq API...")
|
| 14 |
+
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
|
|
|
| 15 |
|
| 16 |
# Function to transcribe audio to text
|
| 17 |
def transcribe_audio(audio_file):
|
| 18 |
+
try:
|
| 19 |
+
result = whisper_model.transcribe(audio_file)
|
| 20 |
+
return result["text"]
|
| 21 |
+
except Exception as e:
|
| 22 |
+
return f"Error in transcription: {e}"
|
| 23 |
|
| 24 |
+
# Function to get response from LLM using Groq API
|
| 25 |
def get_llm_response(user_input):
|
| 26 |
+
try:
|
| 27 |
+
chat_completion = client.chat.completions.create(
|
| 28 |
+
messages=[
|
| 29 |
+
{"role": "user", "content": user_input}
|
| 30 |
+
],
|
| 31 |
+
model="llama3-8b-8192",
|
| 32 |
+
stream=False,
|
| 33 |
+
)
|
| 34 |
+
return chat_completion.choices[0].message.content
|
| 35 |
+
except Exception as e:
|
| 36 |
+
return f"Error in LLM interaction: {e}"
|
| 37 |
+
|
| 38 |
+
# Function to convert text to speech
|
| 39 |
+
def text_to_sp
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|