Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,56 +1,21 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
import
|
| 4 |
-
import tempfile
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
# Transcribe audio to text
|
| 18 |
-
try:
|
| 19 |
-
text = recognizer.recognize_google(audio_data)
|
| 20 |
-
return text
|
| 21 |
-
except sr.UnknownValueError:
|
| 22 |
-
return "Sorry, I did not understand the audio."
|
| 23 |
-
except sr.RequestError:
|
| 24 |
-
return "Sorry, there was an error with the speech recognition service."
|
| 25 |
-
|
| 26 |
-
def generate_audio_response(name):
|
| 27 |
-
# Generate personalized greeting
|
| 28 |
-
greeting = f"Hello {name}, welcome!"
|
| 29 |
-
|
| 30 |
-
# Create a temporary file to save the speech
|
| 31 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_file:
|
| 32 |
-
audio_path = temp_file.name
|
| 33 |
-
|
| 34 |
-
# Save the greeting to an audio file
|
| 35 |
-
engine.save_to_file(greeting, audio_path)
|
| 36 |
-
engine.runAndWait()
|
| 37 |
-
|
| 38 |
-
return audio_path
|
| 39 |
-
|
| 40 |
-
def greet_user(audio):
|
| 41 |
-
# Transcribe the user's name from audio input
|
| 42 |
-
name = transcribe_audio(audio)
|
| 43 |
-
|
| 44 |
-
# Generate and return the audio response
|
| 45 |
-
response_audio = generate_audio_response(name)
|
| 46 |
-
return response_audio
|
| 47 |
-
|
| 48 |
-
# Define the Gradio Interface
|
| 49 |
-
demo = gr.Interface(
|
| 50 |
-
fn=greet_user,
|
| 51 |
-
inputs=gr.inputs.Audio(source="microphone", type="filepath"),
|
| 52 |
-
outputs="audio"
|
| 53 |
)
|
| 54 |
|
| 55 |
# Launch the interface
|
| 56 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from gtts import gTTS
|
| 3 |
+
import os
|
|
|
|
| 4 |
|
| 5 |
+
def greet(name):
|
| 6 |
+
greeting_text = f"Hello, {name}! Welcome to our AI-powered voice greeting system."
|
| 7 |
+
tts = gTTS(text=greeting_text, lang='en')
|
| 8 |
+
tts.save("greeting.mp3")
|
| 9 |
+
return "greeting.mp3"
|
| 10 |
|
| 11 |
+
# Create Gradio interface
|
| 12 |
+
iface = gr.Interface(
|
| 13 |
+
fn=greet,
|
| 14 |
+
inputs="text",
|
| 15 |
+
outputs="audio",
|
| 16 |
+
title="AI Voice Greeting",
|
| 17 |
+
description="Enter your name and receive a personalized greeting as an AI-generated voice."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
)
|
| 19 |
|
| 20 |
# Launch the interface
|
| 21 |
+
iface.launch()
|