Рет қаралды 8
"Discover how to create a voice-controlled Wikipedia assistant using Python! Ask questions like 'Who is Albert Einstein?' or 'What is Python programming?' and get instant answers."
FULL CODE.
import wikipedia
import pyttsx3
import speech_recognition as sr
Initialize text-to-speech engine
engine = pyttsx3.init()
def speak(text):
"""Convert text to speech."""
engine.say(text)
engine.runAndWait()
def fetch_wikipedia_summary():
"""Fetch Wikipedia summary based on user input."""
while True:
try:
query = input("What would you like to know? (Type 'exit' to quit) ").strip()
if query.lower() == 'exit':
print("Goodbye!")
speak("Goodbye!")
break
Ask if the user wants a detailed summary
detailed = input("Do you want a detailed summary? (yes/no) ").strip().lower()
if detailed == 'yes':
summary = wikipedia.summary(query, sentences=5)
else:
summary = wikipedia.summary(query, sentences=2)
print("
Here’s what I found:
")
print(summary)
speak(summary)
except wikipedia.exceptions.DisambiguationError as e:
print(f"Your query is too broad! Be more specific. Suggestions: {e.options[:5]}")
speak("Your query is too broad. Try to be more specific.")
except wikipedia.exceptions.PageError:
print("Sorry, no page found for your query!")
speak("Sorry, no page found for your query!")
except Exception as e:
print(f"An error occurred: {e}")
speak("Sorry, an error occurred. Please try again.")
def fetch_wikipedia_summary_voice():
"""Fetch Wikipedia summary using voice input."""
recognizer = sr.Recognizer()
with sr.Microphone() as source:
print("Listening... Speak your query.")
speak("I am listening. Please say your query.")
try:
audio = recognizer.listen(source)
query = recognizer.recognize_google(audio)
print(f"You said: {query}")
speak(f"You said: {query}")
Fetch summary
summary = wikipedia.summary(query, sentences=2)
print("
Here’s what I found:
")
print(summary)
speak(summary)
except wikipedia.exceptions.DisambiguationError as e:
print(f"Your query is too broad! Suggestions: {e.options[:5]}")
speak("Your query is too broad. Try to be more specific.")
except wikipedia.exceptions.PageError:
print("Sorry, no page found for your query!")
speak("Sorry, no page found for your query!")
except sr.UnknownValueError:
print("Sorry, I didn’t catch that.")
speak("Sorry, I didn’t catch that.")
except sr.RequestError:
print("There seems to be an issue with the speech recognition service.")
speak("There seems to be an issue with the speech recognition service.")
except Exception as e:
print(f"An error occurred: {e}")
speak("Sorry, an error occurred. Please try again.")
Main Programex
def main():
print("Welcome to the Wikipedia Fetcher!")
speak("Welcome to the Wikipedia Fetcher!")
print("Choose a mode:")
print("1. Text Input")
print("2. Voice Input")
print("3. Exit")
while True:
choice = input("Enter your choice (1/2/3): ").strip()
if choice == '1':
fetch_wikipedia_summary()
elif choice == '2':
fetch_wikipedia_summary_voice()
elif choice == '3':
print("Goodbye!")
speak("Goodbye!")
break
else:
print("Invalid choice. Please try again.")
speak("Invalid choice. Please try again.")
if _name_ == "__main__":
main()
#PythonProjects
#VoiceAssistant
#WikipediaAPI
#PythonProgramming
#LearnPython
#AIProjects
#CodingTutorial
#TechExplained
#ProgrammingMadeEasy
#PythonForBeginners