Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -51,4 +51,84 @@ iface = gr.Interface(
|
|
| 51 |
)
|
| 52 |
|
| 53 |
# Launch the Gradio app
|
| 54 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
)
|
| 52 |
|
| 53 |
# Launch the Gradio app
|
| 54 |
+
iface.launch()
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
# Import libraries
|
| 59 |
+
import whisper
|
| 60 |
+
import os
|
| 61 |
+
from gtts import gTTS
|
| 62 |
+
import gradio as gr
|
| 63 |
+
from groq import Groq
|
| 64 |
+
|
| 65 |
+
# Load Whisper model for transcription
|
| 66 |
+
model = whisper.load_model("base")
|
| 67 |
+
|
| 68 |
+
# Set up Groq API client (ensure GROQ_API_KEY is set in your environment)
|
| 69 |
+
GROQ_API_KEY = "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"
|
| 70 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 71 |
+
|
| 72 |
+
# Function to get the LLM response from Groq
|
| 73 |
+
def get_llm_response(user_input):
|
| 74 |
+
try:
|
| 75 |
+
chat_completion = client.chat.completions.create(
|
| 76 |
+
messages=[{"role": "user", "content": user_input}],
|
| 77 |
+
model="llama3-8b-8192" # Replace with your desired model
|
| 78 |
+
)
|
| 79 |
+
return chat_completion.choices[0].message.content
|
| 80 |
+
except Exception as e:
|
| 81 |
+
return f"Error: {e}"
|
| 82 |
+
|
| 83 |
+
# Function to convert text to speech using gTTS
|
| 84 |
+
def text_to_speech(text, output_audio="output_audio.mp3"):
|
| 85 |
+
try:
|
| 86 |
+
tts = gTTS(text)
|
| 87 |
+
tts.save(output_audio)
|
| 88 |
+
return output_audio
|
| 89 |
+
except Exception as e:
|
| 90 |
+
return f"Error generating audio: {e}"
|
| 91 |
+
|
| 92 |
+
# Main chatbot function to handle audio input and output
|
| 93 |
+
def chatbot(audio):
|
| 94 |
+
# Step 1: Transcribe the audio using Whisper
|
| 95 |
+
try:
|
| 96 |
+
result = model.transcribe(audio)
|
| 97 |
+
user_text = result["text"]
|
| 98 |
+
except Exception as e:
|
| 99 |
+
return f"Error in transcription: {e}", None
|
| 100 |
+
|
| 101 |
+
# Step 2: Get LLM response from Groq
|
| 102 |
+
response_text = get_llm_response(user_text)
|
| 103 |
+
|
| 104 |
+
# Step 3: Convert the response text to speech
|
| 105 |
+
output_audio = text_to_speech(response_text)
|
| 106 |
+
|
| 107 |
+
return response_text, output_audio
|
| 108 |
+
|
| 109 |
+
# Gradio interface for dual input (microphone and file upload)
|
| 110 |
+
with gr.Blocks() as demo:
|
| 111 |
+
gr.Markdown("# Voice-to-Voice Chatbot")
|
| 112 |
+
|
| 113 |
+
# Input section
|
| 114 |
+
with gr.Row():
|
| 115 |
+
microphone_input = gr.Audio(source="microphone", type="filepath", label="Speak into the microphone")
|
| 116 |
+
file_upload_input = gr.Audio(source="upload", type="filepath", label="Upload an audio file")
|
| 117 |
+
|
| 118 |
+
# Output section
|
| 119 |
+
chatbot_response_text = gr.Textbox(label="Chatbot Response")
|
| 120 |
+
chatbot_response_audio = gr.Audio(type="filepath", label="Response Audio")
|
| 121 |
+
|
| 122 |
+
# Buttons to process each input
|
| 123 |
+
mic_button = gr.Button("Process Microphone Input")
|
| 124 |
+
file_button = gr.Button("Process Uploaded Audio")
|
| 125 |
+
|
| 126 |
+
# Actions for buttons
|
| 127 |
+
mic_button.click(chatbot, inputs=microphone_input, outputs=[chatbot_response_text, chatbot_response_audio])
|
| 128 |
+
file_button.click(chatbot, inputs=file_upload_input, outputs=[chatbot_response_text, chatbot_response_audio])
|
| 129 |
+
|
| 130 |
+
# Launch the Gradio app
|
| 131 |
+
demo.launch()
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
|