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

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()