pythontech9 commited on
Commit
7cbd8b4
Β·
verified Β·
1 Parent(s): eaf485c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -48
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": r'C:\Users\User\Downloads\dog_sit.mp3',
12
- "come": r'C:\Users\User\Downloads\dog_come.mp3',
13
- "fetch": r'C:\Users\User\Downloads\dog_fetch.mp3',
14
- "treat": r'C:\Users\User\Downloads\dog_treat.mp3',
15
- "play": r'C:\Users\User\Downloads\dog_play.mp3',
16
- "bark": r'C:\Users\User\Downloads\dog_bark.mp3'
17
  }
18
 
19
  # Initialize speech recognizer
20
  recognizer = sr.Recognizer()
 
21
  pygame.mixer.init()
22
 
23
- def recognize_speech():
24
- """Recognizes speech from the microphone input."""
25
- with sr.Microphone() as source:
26
- try:
27
- audio = recognizer.listen(source, timeout=5)
28
  command = recognizer.recognize_google(audio)
29
  return command.lower()
30
- except sr.UnknownValueError:
31
- return "Sorry, I could not understand your speech."
32
- except sr.RequestError:
33
- return "Sorry, the speech service is unavailable."
34
- except sr.WaitTimeoutError:
35
- return "No speech detected. Please try again."
36
 
37
  def dog_response(command):
38
- """Plays the corresponding dog sound based on the recognized command."""
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 make_dog_response(command):
59
- """Generates a playful dog response using text-to-speech."""
60
- try:
61
- tts = gTTS(text=f"Woof! I heard you say {command}", lang='en')
62
- response_file = "dog_response.mp3"
63
- tts.save(response_file)
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
- response = dog_response(command)
76
- tts_response = make_dog_response(command)
77
- return command, response, tts_response
78
 
 
79
  iface = gr.Interface(
80
  fn=process_command,
81
- inputs=[],
82
- outputs=["text", "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', or anything else!",
85
  theme="default",
86
  live=True,
87
  css="""
88
- body { background-color: #f0f8ff; }
89
- .output-text { color: #ff4500; font-size: 18px; font-weight: bold; }
90
- .interface-title { color: #008080; font-size: 24px; font-weight: bold; }
91
- .interface-description { color: #2f4f4f; font-size: 16px; }
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)