-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from Chin-may02/main
Added the codes for storyteller and recipe book with a readme file
- Loading branch information
Showing
4 changed files
with
229 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import requests | ||
import speech_recognition as sr | ||
import pyttsx3 | ||
|
||
class Recipe: | ||
def __init__(self, name, ingredients, instructions): | ||
self.name = name | ||
self.ingredients = ingredients | ||
self.instructions = instructions | ||
|
||
def __str__(self): | ||
ingredients_list = "\n".join(self.ingredients) | ||
return (f"Recipe: {self.name}\n" | ||
f"Ingredients:\n{ingredients_list}\n" | ||
f"Instructions:\n{self.instructions}") | ||
|
||
class RecipeBook: | ||
def __init__(self): | ||
self.recipes = [] | ||
|
||
def add_recipe(self, recipe): | ||
self.recipes.append(recipe) | ||
|
||
def view_recipes(self): | ||
if not self.recipes: | ||
return "No recipes available." | ||
return "\n".join([f"{index + 1}. {recipe.name}" for index, recipe in enumerate(self.recipes)]) | ||
|
||
def search_recipe(self, name): | ||
for recipe in self.recipes: | ||
if recipe.name.lower() == name.lower(): | ||
return str(recipe) | ||
return "Recipe not found." | ||
|
||
def fetch_recipes(self, api_url): | ||
try: | ||
response = requests.get(api_url) | ||
response.raise_for_status() # Raise an error for bad responses | ||
recipes_data = response.json() | ||
for recipe_data in recipes_data: | ||
name = recipe_data['title'] | ||
ingredients = recipe_data['ingredients'] | ||
instructions = recipe_data['instructions'] | ||
self.add_recipe(Recipe(name, ingredients, instructions)) | ||
return "Recipes fetched successfully." | ||
except Exception as e: | ||
return f"Error fetching recipes: {e}" | ||
|
||
class VoiceAssistant: | ||
def __init__(self): | ||
self.recognizer = sr.Recognizer() | ||
self.engine = pyttsx3.init() | ||
self.recipe_book = RecipeBook() | ||
self.api_url = "https://api.example.com/recipes" # Replace with actual API URL | ||
self.recipe_book.fetch_recipes(self.api_url) | ||
|
||
def speak(self, text): | ||
self.engine.say(text) | ||
self.engine.runAndWait() | ||
|
||
def listen(self): | ||
with sr.Microphone() as source: | ||
print("Listening...") | ||
audio = self.recognizer.listen(source) | ||
try: | ||
command = self.recognizer.recognize_google(audio) | ||
print(f"You said: {command}") | ||
return command.lower() | ||
except sr.UnknownValueError: | ||
print("Sorry, I did not understand that.") | ||
return None | ||
except sr.RequestError: | ||
print("Could not request results from Google Speech Recognition service.") | ||
return None | ||
|
||
def run(self): | ||
self.speak("Welcome to the Recipe Book! You can say 'view recipes' to see the recipes, 'search for a recipe' to find a recipe, or 'exit' to quit.") | ||
while True: | ||
command = self.listen() | ||
if command: | ||
if "view recipes" in command: | ||
recipes = self.recipe_book.view_recipes() | ||
self.speak(recipes) | ||
elif "search for a recipe" in command: | ||
self.speak("What recipe do you want to search for?") | ||
recipe_name = self.listen() | ||
if recipe_name: | ||
result = self.recipe_book.search_recipe(recipe_name) | ||
self.speak(result) | ||
elif "exit" in command: | ||
self.speak("Goodbye! Have a great day!") | ||
break | ||
else: | ||
self.speak("I didn't recognize that command. Please try again.") | ||
|
||
if __name__ == "__main__": | ||
assistant = VoiceAssistant() | ||
assistant.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
Voice-Activated Recipe Book | ||
🎯 Goal | ||
The goal of this project is to create an interactive recipe book application that allows users to fetch, view, and search for recipes using voice commands. | ||
|
||
🧾 Description | ||
This project is a Voice-Activated Recipe Book where users can interact with a collection of recipes using voice input. The application fetches recipes from an external API and provides an engaging user experience through speech recognition and text-to-speech functionalities. | ||
|
||
🧮 What I had done! | ||
Implemented a Recipe class to represent individual recipes with their ingredients and instructions. | ||
Developed a RecipeBook class to manage a collection of recipes, including methods to view and search for recipes, and to fetch recipes from an API. | ||
Created a VoiceAssistant class that uses speech recognition for input and text-to-speech for output, allowing users to interact with the recipe book via voice commands. | ||
Integrated a command loop that listens for user commands and responds accordingly. | ||
📚 Libraries Needed | ||
requests - To fetch recipes from an external API. | ||
SpeechRecognition - To capture and recognize voice commands. | ||
pyttsx3 - To convert text to speech for user feedback. | ||
📢 Conclusion | ||
In conclusion, this Voice-Activated Recipe Book project showcases the integration of voice recognition and text-to-speech capabilities in a practical application. It provides an intuitive and engaging way for users to discover and explore recipes through voice commands. The code serves as a solid foundation for further enhancements, such as adding more recipes or improving the voice recognition accuracy. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
Sure! Here’s the README file without the headers that contain "N/A": | ||
|
||
Voice Assistant Storyteller | ||
🎯 Goal | ||
The goal of this project is to create a voice assistant that can tell stories based on user input. The assistant listens for commands and provides an interactive storytelling experience. | ||
|
||
🧾 Description | ||
This project is a Voice Assistant that uses speech recognition and text-to-speech capabilities to engage users in storytelling. The assistant fetches stories from an external API or scrapes a website if the API is unavailable. Users can interact with the assistant by asking it to tell a story, and they can choose from a selection of available stories. | ||
|
||
🧮 What I had done! | ||
Built a voice interface using speech_recognition for user input and pyttsx3 for output. | ||
Implemented functionality to fetch stories from an external API, with a fallback to web scraping if the API call fails. | ||
Provided default stories in case both fetching and scraping fail. | ||
Created an interactive loop where users can continuously ask the assistant to tell stories or stop the interaction. | ||
📚 Libraries Needed | ||
speech_recognition - For recognizing user voice commands. | ||
pyttsx3 - For converting text to speech to read stories aloud. | ||
requests - To make API calls for fetching stories. | ||
beautifulsoup4 - For web scraping to gather stories if needed. | ||
📢 Conclusion | ||
In conclusion, this Voice Assistant project demonstrates fundamental programming concepts such as speech recognition, text-to-speech conversion, web scraping, and user input handling in Python. It offers an engaging, interactive experience where users can listen to stories and choose what they want to hear. The project is an excellent learning opportunity for developers looking to strengthen their understanding of Python and voice interaction. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import speech_recognition as sr | ||
import pyttsx3 | ||
import requests | ||
from bs4 import BeautifulSoup | ||
|
||
class VoiceAssistant: | ||
def __init__(self): | ||
self.recognizer = sr.Recognizer() | ||
self.engine = pyttsx3.init() | ||
self.stories = self.fetch_stories() # Fetch stories from an external API or website | ||
|
||
def fetch_stories(self): | ||
api_url = "https://api.example.com/stories" # Replace with actual API URL | ||
try: | ||
response = requests.get(api_url) | ||
if response.status_code == 200: | ||
return {str(i + 1): story['text'] for i, story in enumerate(response.json())} | ||
else: | ||
print("Failed to fetch stories from API. Trying to scrape a website.") | ||
return self.scrape_stories() # Fallback to web scraping | ||
except Exception as e: | ||
print(f"An error occurred while fetching from API: {e}") | ||
return self.scrape_stories() # Fallback to web scraping | ||
|
||
def scrape_stories(self): | ||
url = "https://www.example.com/stories" # Replace with actual website URL | ||
try: | ||
response = requests.get(url) | ||
soup = BeautifulSoup(response.text, 'html.parser') | ||
|
||
stories = {} | ||
for idx, story in enumerate(soup.find_all('div', class_='story-class')): # Adjust class based on actual HTML structure | ||
stories[str(idx + 1)] = story.get_text(strip=True) | ||
return stories | ||
except Exception as e: | ||
print(f"An error occurred while scraping: {e}") | ||
return self.default_stories() # Fallback to default stories | ||
|
||
def default_stories(self): | ||
return { | ||
"1": "The Tortoise and the Hare: A story about a race between a slow tortoise and a fast hare.", | ||
"2": "The Lion and the Mouse: A tale showing that even the smallest creature can help the mightiest.", | ||
"3": "Cinderella: A story about kindness, resilience, and finding happiness.", | ||
"4": "The Three Little Pigs: A tale about three pigs who build houses to protect themselves from a wolf.", | ||
"5": "Goldilocks and the Three Bears: A curious girl explores the home of three bears." | ||
} | ||
|
||
def speak(self, text): | ||
self.engine.say(text) | ||
self.engine.runAndWait() | ||
|
||
def listen(self): | ||
with sr.Microphone() as source: | ||
print("Listening...") | ||
audio = self.recognizer.listen(source) | ||
try: | ||
command = self.recognizer.recognize_google(audio) | ||
print(f"You said: {command}") | ||
return command.lower() | ||
except sr.UnknownValueError: | ||
print("Sorry, I did not understand that.") | ||
return None | ||
except sr.RequestError: | ||
print("Could not request results from Google Speech Recognition service.") | ||
return None | ||
|
||
def tell_story(self, story_choice): | ||
story = self.stories.get(story_choice, "I don't have a story for that choice.") | ||
self.speak(story) | ||
|
||
def run(self): | ||
self.speak("Hello! I can tell you stories. Say 'tell a story' to start.") | ||
while True: | ||
command = self.listen() | ||
if command: | ||
if "tell a story" in command: | ||
self.speak("Great! Here are the stories you can choose from:") | ||
for key, value in self.stories.items(): | ||
self.speak(f"Say {key} for {value[:30]}...") # Preview story | ||
choice = self.listen() | ||
if choice in self.stories.keys(): | ||
self.tell_story(choice) | ||
else: | ||
self.speak("I didn't recognize that choice. Please try again.") | ||
elif "stop" in command: | ||
self.speak("Goodbye! Have a great day!") | ||
break | ||
|
||
if __name__ == "__main__": | ||
assistant = VoiceAssistant() | ||
assistant.run() |