Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import speech_recognition as sr # type: ignore
|
| 2 |
+
import os
|
| 3 |
+
import pygame # type: ignore
|
| 4 |
+
from gtts import gTTS # type: ignore
|
| 5 |
+
from pydub import AudioSegment
|
| 6 |
+
from pydub.playback import play
|
| 7 |
+
|
| 8 |
+
# Dog sound files (Ensure these files exist)
|
| 9 |
+
dog_sounds = {
|
| 10 |
+
"sit": r'C:\Users\User\Downloads\dog_sit.mp3',
|
| 11 |
+
"come": r'C:\Users\User\Downloads\dog_come.mp3',
|
| 12 |
+
"fetch": r'C:\Users\User\Downloads\dog_fetch.mp3',
|
| 13 |
+
"treat": r'C:\Users\User\Downloads\dog_treat.mp3',
|
| 14 |
+
"play": r'C:\Users\User\Downloads\dog_play.mp3',
|
| 15 |
+
"bark": r'C:\Users\User\Downloads\dog_bark.mp3'
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
# Initialize speech recognizer
|
| 19 |
+
recognizer = sr.Recognizer()
|
| 20 |
+
|
| 21 |
+
# Initialize pygame mixer
|
| 22 |
+
pygame.mixer.init()
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def recognize_speech():
|
| 26 |
+
"""Recognizes speech from the microphone input."""
|
| 27 |
+
with sr.Microphone() as source:
|
| 28 |
+
print("Listening for command...")
|
| 29 |
+
try:
|
| 30 |
+
audio = recognizer.listen(source, timeout=5)
|
| 31 |
+
command = recognizer.recognize_google(audio)
|
| 32 |
+
print(f"Human said: {command}")
|
| 33 |
+
return command.lower()
|
| 34 |
+
except sr.UnknownValueError:
|
| 35 |
+
print("Sorry, I could not understand your speech.")
|
| 36 |
+
return None
|
| 37 |
+
except sr.RequestError:
|
| 38 |
+
print("Sorry, the speech service is unavailable.")
|
| 39 |
+
return None
|
| 40 |
+
except sr.WaitTimeoutError:
|
| 41 |
+
print("No speech detected. Please try again.")
|
| 42 |
+
return None
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def dog_response(command):
|
| 46 |
+
"""Plays the corresponding dog sound based on the recognized command."""
|
| 47 |
+
if command:
|
| 48 |
+
# Check if command matches known keywords
|
| 49 |
+
for key in dog_sounds:
|
| 50 |
+
if key in command:
|
| 51 |
+
play_dog_sound(dog_sounds[key])
|
| 52 |
+
return
|
| 53 |
+
|
| 54 |
+
# Default to bark sound if command is unknown
|
| 55 |
+
print("No specific dog command recognized. Playing default bark sound.")
|
| 56 |
+
play_dog_sound(dog_sounds["bark"])
|
| 57 |
+
else:
|
| 58 |
+
print("No command to process.")
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def play_dog_sound(sound_file):
|
| 62 |
+
"""Plays an audio file using Pygame."""
|
| 63 |
+
if os.path.exists(sound_file):
|
| 64 |
+
pygame.mixer.music.load(sound_file)
|
| 65 |
+
pygame.mixer.music.play()
|
| 66 |
+
while pygame.mixer.music.get_busy(): # Wait until sound finishes playing
|
| 67 |
+
continue
|
| 68 |
+
else:
|
| 69 |
+
print(f"Error: Sound file '{sound_file}' not found.")
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def make_dog_response(text):
|
| 73 |
+
"""Generates a playful dog response using text-to-speech."""
|
| 74 |
+
try:
|
| 75 |
+
tts = gTTS(text=text, lang='en')
|
| 76 |
+
response_file = "dog_response.mp3"
|
| 77 |
+
tts.save(response_file)
|
| 78 |
+
|
| 79 |
+
pygame.mixer.music.load(response_file)
|
| 80 |
+
pygame.mixer.music.play()
|
| 81 |
+
while pygame.mixer.music.get_busy():
|
| 82 |
+
continue
|
| 83 |
+
|
| 84 |
+
os.remove(response_file) # Clean up the temporary file
|
| 85 |
+
except Exception as e:
|
| 86 |
+
print(f"Error generating speech: {e}")
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def main():
|
| 90 |
+
"""Main loop to listen for speech commands and respond accordingly."""
|
| 91 |
+
print("Say 'exit' or 'quit' to stop.")
|
| 92 |
+
while True:
|
| 93 |
+
command = recognize_speech()
|
| 94 |
+
if command in ["exit", "quit"]:
|
| 95 |
+
print("Exiting program. Goodbye!")
|
| 96 |
+
break
|
| 97 |
+
|
| 98 |
+
dog_response(command) # Process command
|
| 99 |
+
make_dog_response(f"Woof! I heard you say {command}.") # Playful dog response
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
if __name__ == "__main__":
|
| 103 |
+
main()
|