Skip to content

Commit

Permalink
Merge pull request #92 from from13/patch-1
Browse files Browse the repository at this point in the history
Create real_time_shopping_list.py
  • Loading branch information
suryanshsk authored Oct 9, 2024
2 parents 13d7c8e + 01122c0 commit 5bc06e9
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions real_time_shopping_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
mport speech_recognition as sr
import pyttsx3
recognizer = sr.Recognizer()
tts_engine = pyttsx3.init()
shopping_list = []

# Function to recognize speech and return text
def recognize_speech():
with sr.Microphone() as source:
print("Listening...")
audio = recognizer.listen(source)
try:
text = recognizer.recognize_google(audio)
return text.lower() # Standardize by returning lowercased input
except sr.UnknownValueError:
return "Sorry, I didn't catch that."
except sr.RequestError:
return "Service is unavailable."

# Function to speak text
def speak_text(text):
tts_engine.say(text)
tts_engine.runAndWait()

# Function to add an item to the shopping list
def add_to_shopping_list(item):
shopping_list.append(item)
speak_text(f"{item} has been added to your shopping list.")
print(f"Shopping list: {shopping_list}") # For debugging

# Function to ask for more items
def ask_for_more_items():
speak_text("Would you like to add another item?")
response = recognize_speech()
if 'yes' in response:
ask_for_item()
elif 'no' in response:
speak_text("Okay. Would you like to view your shopping list?")
response = recognize_speech()
if 'yes' in response:
view_shopping_list()
else:
speak_text("All right.")
else:
speak_text("I didn't understand that. Please say yes or no.")
ask_for_more_items()

# Function to ask the user what item to add
def ask_for_item():
speak_text("What would you like to add to your shopping list?")
item = recognize_speech()
if item:
add_to_shopping_list(item)
ask_for_more_items()

# Function to view the shopping list
def view_shopping_list():
if shopping_list:
speak_text("Your shopping list includes:")
for item in shopping_list:
speak_text(item)
else:
speak_text("Your shopping list is empty.")


def main():
speak_text("Hello, would you like to create a shopping list?")
response = recognize_speech()
if 'yes' in response:
ask_for_item()
else:
speak_text("Okay, let me know if you need help.")

if __name__ == "__main__":
main()

0 comments on commit 5bc06e9

Please sign in to comment.