import speech_recognition as sr # type: ignore import os import pygame # type: ignore import gradio as gr from gtts import gTTS # type: ignore from pydub import AudioSegment from pydub.playback import play # Dog sound files (Ensure these files exist) dog_sounds = { "sit": r'C:\Users\User\Downloads\dog_sit.mp3', "come": r'C:\Users\User\Downloads\dog_come.mp3', "fetch": r'C:\Users\User\Downloads\dog_fetch.mp3', "treat": r'C:\Users\User\Downloads\dog_treat.mp3', "play": r'C:\Users\User\Downloads\dog_play.mp3', "bark": r'C:\Users\User\Downloads\dog_bark.mp3' } # Initialize speech recognizer recognizer = sr.Recognizer() pygame.mixer.init() def recognize_speech(): """Recognizes speech from the microphone input.""" with sr.Microphone() as source: try: audio = recognizer.listen(source, timeout=5) command = recognizer.recognize_google(audio) return command.lower() except sr.UnknownValueError: return "Sorry, I could not understand your speech." except sr.RequestError: return "Sorry, the speech service is unavailable." except sr.WaitTimeoutError: return "No speech detected. Please try again." def dog_response(command): """Plays the corresponding dog sound based on the recognized command.""" if command: for key in dog_sounds: if key in command: play_dog_sound(dog_sounds[key]) return f"Playing sound for {key}" play_dog_sound(dog_sounds["bark"]) return "No specific dog command recognized. Playing default bark sound." return "No command to process." def play_dog_sound(sound_file): """Plays an audio file using Pygame.""" if os.path.exists(sound_file): pygame.mixer.music.load(sound_file) pygame.mixer.music.play() while pygame.mixer.music.get_busy(): continue else: return f"Error: Sound file '{sound_file}' not found." def make_dog_response(command): """Generates a playful dog response using text-to-speech.""" try: tts = gTTS(text=f"Woof! I heard you say {command}", lang='en') response_file = "dog_response.mp3" tts.save(response_file) pygame.mixer.music.load(response_file) pygame.mixer.music.play() while pygame.mixer.music.get_busy(): continue os.remove(response_file) return f"Generated response: Woof! I heard you say {command}" except Exception as e: return f"Error generating speech: {e}" def process_command(): command = recognize_speech() response = dog_response(command) tts_response = make_dog_response(command) return command, response, tts_response iface = gr.Interface( fn=process_command, inputs=[], outputs=["text", "text", "text"], title="🐶 Dog Command Recognition 🐶", description="🎤 Speak a command and let the dog respond! 🐕\n\nTry commands like 'sit', 'come', 'fetch', 'treat', 'play', or anything else!", theme="default", live=True, css=""" body { background-color: #f0f8ff; } .output-text { color: #ff4500; font-size: 18px; font-weight: bold; } .interface-title { color: #008080; font-size: 24px; font-weight: bold; } .interface-description { color: #2f4f4f; font-size: 16px; } """ ) if __name__ == "__main__": iface.launch()