razanalsulami commited on
Commit
9c33ad8
·
verified ·
1 Parent(s): 2c64c8a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -50
app.py CHANGED
@@ -1,56 +1,21 @@
1
  import gradio as gr
2
- import speech_recognition as sr
3
- import pyttsx3
4
- import tempfile
5
 
6
- # Initialize text-to-speech engine
7
- engine = pyttsx3.init()
 
 
 
8
 
9
- def transcribe_audio(audio_path):
10
- # Initialize recognizer
11
- recognizer = sr.Recognizer()
12
-
13
- # Use audio file to process audio
14
- with sr.AudioFile(audio_path) as source:
15
- audio_data = recognizer.record(source)
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
- demo.launch()
 
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()