Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #MAKE SURE TO PUT A FILE test.txt in the same directory to be able to read it.
- #Dont forgot to do pip install pyttsx3 pywhatkit SpeechRecognition PyPDF2 pyaudio
- #Also you need to install at least 3.10.6 python in https://www.python.org/ don't worry higher version should work perfectly
- import os
- import pyttsx3
- import pywhatkit
- import speech_recognition as sr
- import PyPDF2
- engine = pyttsx3.init()
- voices = engine.getProperty('voices')
- engine.setProperty('voice', voices[1].id) # Use the appropriate index for a French voice
- def speak(text):
- engine.say(text)
- engine.runAndWait()
- def listen():
- recognizer = sr.Recognizer()
- with sr.Microphone() as source:
- print("Listening...")
- recognizer.adjust_for_ambient_noise(source)
- recognizer.pause_threshold = 1
- audio = recognizer.listen(source)
- try:
- print("Recognizing...")
- query = recognizer.recognize_google(audio, language="en-US")
- print("You said:", query)
- return query.lower()
- except sr.UnknownValueError:
- print("Sorry, I didn't understand.")
- return None
- except sr.RequestError as e:
- print(f"Unable to fetch results from Google Speech Recognition service; {e}")
- return None
- def search_in_text_file(query, file_path):
- try:
- with open(file_path, 'r', encoding='utf-8') as file:
- content = file.read()
- if query in content:
- return True
- else:
- return False
- except FileNotFoundError:
- print(f"The file {file_path} was not found.")
- return False
- except Exception as e:
- print(f"An error occurred: {e}")
- return False
- def read_text_file(file_path):
- try:
- with open(file_path, 'r', encoding='utf-8') as file:
- content = file.read()
- speak("Here is the content of the text file:")
- speak(content)
- except FileNotFoundError:
- speak(f"The file {file_path} was not found.")
- except Exception as e:
- speak(f"An error occurred: {e}")
- def run_assistant():
- speak("Hello, how can I help you?")
- while True:
- command = listen()
- if command:
- if 'what time is it' in command:
- pywhatkit.info("Current time", 1)
- elif 'open google' in command:
- speak("Opening Google...")
- pywhatkit.search("Google")
- elif 'search in the text file' in command:
- speak("What search do you want to perform in the text file?")
- query = listen()
- if query:
- if search_in_text_file(query, "test.txt"):
- speak(f"The search '{query}' was found in the text file.")
- else:
- speak(f"The search '{query}' was not found in the text file.")
- elif 'read the text file' in command:
- read_text_file("test.txt")
- elif 'stop' in command:
- speak("Stopping the bot.")
- break
- elif 'quit' in command or 'exit' in command:
- speak("Goodbye!")
- break
- else:
- speak("Sorry, I didn't understand that command. Can you please repeat?")
- if __name__ == "__main__":
- run_assistant()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement