Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import whisper
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from gtts import gTTS
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Load Whisper model
|
| 7 |
+
model = whisper.load_model("base")
|
| 8 |
+
|
| 9 |
+
# Function to transcribe audio to text
|
| 10 |
+
def transcribe_audio(audio_file):
|
| 11 |
+
try:
|
| 12 |
+
audio = whisper.load_audio(audio_file)
|
| 13 |
+
audio = whisper.pad_or_trim(audio)
|
| 14 |
+
mel = whisper.log_mel_spectrogram(audio).to(model.device)
|
| 15 |
+
result = model.transcribe(mel)
|
| 16 |
+
return result["text"]
|
| 17 |
+
except Exception as e:
|
| 18 |
+
return f"Error in transcription: {e}"
|
| 19 |
+
|
| 20 |
+
# Function to generate text-to-speech
|
| 21 |
+
def generate_speech(text):
|
| 22 |
+
try:
|
| 23 |
+
tts = gTTS(text)
|
| 24 |
+
output_file = "response.mp3"
|
| 25 |
+
tts.save(output_file)
|
| 26 |
+
return output_file
|
| 27 |
+
except Exception as e:
|
| 28 |
+
return f"Error in TTS: {e}"
|
| 29 |
+
|
| 30 |
+
# Voice-to-Voice chatbot function
|
| 31 |
+
def voice_to_voice(audio_file):
|
| 32 |
+
try:
|
| 33 |
+
# Transcribe the audio input
|
| 34 |
+
transcribed_text = transcribe_audio(audio_file)
|
| 35 |
+
if "Error" in transcribed_text:
|
| 36 |
+
return transcribed_text, None
|
| 37 |
+
|
| 38 |
+
# Generate a response (mock response for now)
|
| 39 |
+
response_text = f"You said: {transcribed_text}"
|
| 40 |
+
|
| 41 |
+
# Convert response text to speech
|
| 42 |
+
audio_response = generate_speech(response_text)
|
| 43 |
+
if "Error" in audio_response:
|
| 44 |
+
return response_text, None
|
| 45 |
+
|
| 46 |
+
return response_text, audio_response
|
| 47 |
+
except Exception as e:
|
| 48 |
+
return f"Error in processing: {e}", None
|
| 49 |
+
|
| 50 |
+
# Gradio Interface
|
| 51 |
+
iface = gr.Interface(
|
| 52 |
+
fn=voice_to_voice,
|
| 53 |
+
inputs=gr.Audio(type="filepath"), # Accepts audio input
|
| 54 |
+
outputs=[
|
| 55 |
+
gr.Textbox(label="Transcription"), # Displays transcribed text
|
| 56 |
+
gr.Audio(type="filepath") # Returns audio response
|
| 57 |
+
],
|
| 58 |
+
title="Voice-to-Voice Chatbot",
|
| 59 |
+
description="Speak into the microphone, and the chatbot will respond with speech."
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
# Launch the app
|
| 63 |
+
if __name__ == "__main__":
|
| 64 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|