Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,13 +5,14 @@ from gtts import gTTS
|
|
| 5 |
from groq import Groq
|
| 6 |
import tempfile
|
| 7 |
|
| 8 |
-
# Load Whisper model
|
| 9 |
-
print("Loading Whisper model...")
|
| 10 |
whisper_model = whisper.load_model("base")
|
| 11 |
|
| 12 |
# Initialize Groq API
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# Function to transcribe audio to text
|
| 17 |
def transcribe_audio(audio_file):
|
|
@@ -36,4 +37,50 @@ def get_llm_response(user_input):
|
|
| 36 |
return f"Error in LLM interaction: {e}"
|
| 37 |
|
| 38 |
# Function to convert text to speech
|
| 39 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from groq import Groq
|
| 6 |
import tempfile
|
| 7 |
|
| 8 |
+
# Load Whisper model
|
|
|
|
| 9 |
whisper_model = whisper.load_model("base")
|
| 10 |
|
| 11 |
# Initialize Groq API
|
| 12 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 13 |
+
if not GROQ_API_KEY:
|
| 14 |
+
raise ValueError("GROQ_API_KEY environment variable not set.")
|
| 15 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 16 |
|
| 17 |
# Function to transcribe audio to text
|
| 18 |
def transcribe_audio(audio_file):
|
|
|
|
| 37 |
return f"Error in LLM interaction: {e}"
|
| 38 |
|
| 39 |
# Function to convert text to speech
|
| 40 |
+
def text_to_speech(text):
|
| 41 |
+
try:
|
| 42 |
+
tts = gTTS(text)
|
| 43 |
+
temp_file = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)
|
| 44 |
+
tts.save(temp_file.name)
|
| 45 |
+
return temp_file.name
|
| 46 |
+
except Exception as e:
|
| 47 |
+
return f"Error in text-to-speech conversion: {e}"
|
| 48 |
+
|
| 49 |
+
# Main pipeline
|
| 50 |
+
def chatbot_pipeline(audio_file):
|
| 51 |
+
# Transcribe audio
|
| 52 |
+
user_input = transcribe_audio(audio_file)
|
| 53 |
+
if "Error" in user_input:
|
| 54 |
+
return user_input, "No response", None
|
| 55 |
+
|
| 56 |
+
# Get response from LLM
|
| 57 |
+
response = get_llm_response(user_input)
|
| 58 |
+
if "Error" in response:
|
| 59 |
+
return user_input, response, None
|
| 60 |
+
|
| 61 |
+
# Convert response to audio
|
| 62 |
+
response_audio = text_to_speech(response)
|
| 63 |
+
if "Error" in response_audio:
|
| 64 |
+
return user_input, response, None
|
| 65 |
+
|
| 66 |
+
return user_input, response, response_audio
|
| 67 |
+
|
| 68 |
+
# Gradio Interface
|
| 69 |
+
interface = gr.Interface(
|
| 70 |
+
fn=chatbot_pipeline,
|
| 71 |
+
inputs=gr.Audio(source="microphone", type="filepath"),
|
| 72 |
+
outputs=[
|
| 73 |
+
gr.Textbox(label="Transcribed Text"),
|
| 74 |
+
gr.Textbox(label="Chatbot Response"),
|
| 75 |
+
gr.Audio(label="Response Audio"),
|
| 76 |
+
],
|
| 77 |
+
title="Real-Time Voice-to-Voice Chatbot",
|
| 78 |
+
description=(
|
| 79 |
+
"This chatbot transcribes your voice input using Whisper, "
|
| 80 |
+
"processes it through Groq's API to generate a response, "
|
| 81 |
+
"and converts the response back to speech."
|
| 82 |
+
),
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
if __name__ == "__main__":
|
| 86 |
+
interface.launch(server_name="0.0.0.0", server_port=7860)
|