pythontech9 commited on
Commit
9bde428
·
verified ·
1 Parent(s): 4be72e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -44
app.py CHANGED
@@ -1,6 +1,7 @@
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
@@ -17,87 +18,79 @@ dog_sounds = {
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()
 
1
  import speech_recognition as sr # type: ignore
2
  import os
3
  import pygame # type: ignore
4
+ import gradio as gr
5
  from gtts import gTTS # type: ignore
6
  from pydub import AudioSegment
7
  from pydub.playback import play
 
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."""
50
  if os.path.exists(sound_file):
51
  pygame.mixer.music.load(sound_file)
52
  pygame.mixer.music.play()
53
+ while pygame.mixer.music.get_busy():
54
  continue
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()