Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -8,42 +8,43 @@ from pydub.playback import play
|
|
8 |
|
9 |
# Dog sound files (Ensure these files exist)
|
10 |
dog_sounds = {
|
11 |
-
"sit":
|
12 |
-
"come":
|
13 |
-
"fetch":
|
14 |
-
"treat":
|
15 |
-
"play":
|
16 |
-
"bark":
|
17 |
}
|
18 |
|
19 |
# Initialize speech recognizer
|
20 |
recognizer = sr.Recognizer()
|
|
|
21 |
pygame.mixer.init()
|
22 |
|
23 |
-
def recognize_speech():
|
24 |
-
"""Recognizes speech from
|
25 |
-
|
26 |
-
|
27 |
-
audio = recognizer.
|
28 |
command = recognizer.recognize_google(audio)
|
29 |
return command.lower()
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
|
37 |
def dog_response(command):
|
38 |
-
"""Plays the corresponding dog sound
|
39 |
if command:
|
40 |
for key in dog_sounds:
|
41 |
if key in command:
|
42 |
play_dog_sound(dog_sounds[key])
|
43 |
-
return f"Playing sound for {key}"
|
44 |
play_dog_sound(dog_sounds["bark"])
|
45 |
-
return "No specific dog command recognized. Playing default bark sound."
|
46 |
-
return "No command to process."
|
47 |
|
48 |
def play_dog_sound(sound_file):
|
49 |
"""Plays an audio file using Pygame."""
|
@@ -55,42 +56,34 @@ def play_dog_sound(sound_file):
|
|
55 |
else:
|
56 |
return f"Error: Sound file '{sound_file}' not found."
|
57 |
|
58 |
-
def
|
59 |
-
"""Generates a
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
pygame.mixer.music.load(response_file)
|
65 |
-
pygame.mixer.music.play()
|
66 |
-
while pygame.mixer.music.get_busy():
|
67 |
-
continue
|
68 |
-
os.remove(response_file)
|
69 |
-
return f"Generated response: Woof! I heard you say {command}"
|
70 |
-
except Exception as e:
|
71 |
-
return f"Error generating speech: {e}"
|
72 |
|
73 |
-
def process_command():
|
74 |
-
command = recognize_speech()
|
75 |
-
|
76 |
-
|
77 |
-
return command, response, tts_response
|
78 |
|
|
|
79 |
iface = gr.Interface(
|
80 |
fn=process_command,
|
81 |
-
inputs=[],
|
82 |
-
outputs=["text", "text", "
|
83 |
title="πΆ Dog Command Recognition πΆ",
|
84 |
-
description="π€ Speak a command and let the dog respond! π\n\nTry commands like 'sit', 'come', 'fetch', 'treat', 'play'
|
85 |
theme="default",
|
86 |
live=True,
|
87 |
css="""
|
88 |
-
body { background-color: #
|
89 |
-
.output-text { color: #ff4500; font-size:
|
90 |
-
.interface-title { color: #008080; font-size:
|
91 |
-
.interface-description { color: #2f4f4f; font-size:
|
92 |
"""
|
93 |
)
|
94 |
|
95 |
if __name__ == "__main__":
|
96 |
-
iface.launch()
|
|
|
8 |
|
9 |
# Dog sound files (Ensure these files exist)
|
10 |
dog_sounds = {
|
11 |
+
"sit": "dog_sit.mp3",
|
12 |
+
"come": "dog_come.mp3",
|
13 |
+
"fetch": "dog_fetch.mp3",
|
14 |
+
"treat": "dog_treat.mp3",
|
15 |
+
"play": "dog_play.mp3",
|
16 |
+
"bark": "dog_bark.mp3"
|
17 |
}
|
18 |
|
19 |
# Initialize speech recognizer
|
20 |
recognizer = sr.Recognizer()
|
21 |
+
os.environ["SDL_AUDIODRIVER"] = "dummy" # Prevents pygame audio errors in headless mode
|
22 |
pygame.mixer.init()
|
23 |
|
24 |
+
def recognize_speech(audio_file):
|
25 |
+
"""Recognizes speech from an uploaded audio file."""
|
26 |
+
try:
|
27 |
+
with sr.AudioFile(audio_file) as source:
|
28 |
+
audio = recognizer.record(source)
|
29 |
command = recognizer.recognize_google(audio)
|
30 |
return command.lower()
|
31 |
+
except sr.UnknownValueError:
|
32 |
+
return "Sorry, I could not understand your speech."
|
33 |
+
except sr.RequestError:
|
34 |
+
return "Sorry, the speech service is unavailable."
|
35 |
+
except Exception as e:
|
36 |
+
return f"Error: {str(e)}"
|
37 |
|
38 |
def dog_response(command):
|
39 |
+
"""Plays the corresponding dog sound and generates a speech response."""
|
40 |
if command:
|
41 |
for key in dog_sounds:
|
42 |
if key in command:
|
43 |
play_dog_sound(dog_sounds[key])
|
44 |
+
return key, f"Playing sound for {key}", generate_speech(f"Woof! I heard you say {key}")
|
45 |
play_dog_sound(dog_sounds["bark"])
|
46 |
+
return "bark", "No specific dog command recognized. Playing default bark sound.", generate_speech("Woof! I didn't recognize that, so I'll just bark!")
|
47 |
+
return "unknown", "No command to process.", None
|
48 |
|
49 |
def play_dog_sound(sound_file):
|
50 |
"""Plays an audio file using Pygame."""
|
|
|
56 |
else:
|
57 |
return f"Error: Sound file '{sound_file}' not found."
|
58 |
|
59 |
+
def generate_speech(text):
|
60 |
+
"""Generates a TTS response and returns the file path."""
|
61 |
+
speech_file = "dog_response.mp3"
|
62 |
+
tts = gTTS(text=text, lang="en")
|
63 |
+
tts.save(speech_file)
|
64 |
+
return speech_file # Return audio file for Gradio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
+
def process_command(audio_file):
|
67 |
+
command = recognize_speech(audio_file)
|
68 |
+
keyword, response_text, speech_file = dog_response(command)
|
69 |
+
return command, response_text, speech_file
|
|
|
70 |
|
71 |
+
# Gradio UI
|
72 |
iface = gr.Interface(
|
73 |
fn=process_command,
|
74 |
+
inputs=gr.Audio(sources=["microphone"], type="filepath"),
|
75 |
+
outputs=["text", "text", "audio"], # Added audio output
|
76 |
title="πΆ Dog Command Recognition πΆ",
|
77 |
+
description="π€ Speak a command and let the dog respond! π\n\nTry commands like 'sit', 'come', 'fetch', 'treat', 'play'",
|
78 |
theme="default",
|
79 |
live=True,
|
80 |
css="""
|
81 |
+
body { background-color: #f8f9fa; text-align: center; }
|
82 |
+
.output-text { color: #ff4500; font-size: 20px; font-weight: bold; }
|
83 |
+
.interface-title { color: #008080; font-size: 26px; font-weight: bold; }
|
84 |
+
.interface-description { color: #2f4f4f; font-size: 18px; }
|
85 |
"""
|
86 |
)
|
87 |
|
88 |
if __name__ == "__main__":
|
89 |
+
iface.launch(share=True)
|