Advertisement
Manu-J

Python Chat-GPT / Read File

Jan 28th, 2024
759
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.41 KB | Source Code | 0 0
  1. #MAKE SURE TO PUT A FILE test.txt in the same directory to be able to read it.
  2. #Dont forgot to do pip install pyttsx3 pywhatkit SpeechRecognition PyPDF2 pyaudio
  3. #Also you need to install at least 3.10.6 python in https://www.python.org/ don't worry higher version should work perfectly
  4.  
  5. import os
  6. import pyttsx3
  7. import pywhatkit
  8. import speech_recognition as sr
  9. import PyPDF2
  10.  
  11. engine = pyttsx3.init()
  12. voices = engine.getProperty('voices')
  13. engine.setProperty('voice', voices[1].id)  # Use the appropriate index for a French voice
  14.  
  15. def speak(text):
  16.     engine.say(text)
  17.     engine.runAndWait()
  18.  
  19. def listen():
  20.     recognizer = sr.Recognizer()
  21.     with sr.Microphone() as source:
  22.         print("Listening...")
  23.         recognizer.adjust_for_ambient_noise(source)
  24.         recognizer.pause_threshold = 1
  25.         audio = recognizer.listen(source)
  26.         try:
  27.             print("Recognizing...")
  28.             query = recognizer.recognize_google(audio, language="en-US")
  29.             print("You said:", query)
  30.             return query.lower()
  31.         except sr.UnknownValueError:
  32.             print("Sorry, I didn't understand.")
  33.             return None
  34.         except sr.RequestError as e:
  35.             print(f"Unable to fetch results from Google Speech Recognition service; {e}")
  36.             return None
  37.  
  38. def search_in_text_file(query, file_path):
  39.     try:
  40.         with open(file_path, 'r', encoding='utf-8') as file:
  41.             content = file.read()
  42.             if query in content:
  43.                 return True
  44.             else:
  45.                 return False
  46.     except FileNotFoundError:
  47.         print(f"The file {file_path} was not found.")
  48.         return False
  49.     except Exception as e:
  50.         print(f"An error occurred: {e}")
  51.         return False
  52.  
  53. def read_text_file(file_path):
  54.     try:
  55.         with open(file_path, 'r', encoding='utf-8') as file:
  56.             content = file.read()
  57.             speak("Here is the content of the text file:")
  58.             speak(content)
  59.     except FileNotFoundError:
  60.         speak(f"The file {file_path} was not found.")
  61.     except Exception as e:
  62.         speak(f"An error occurred: {e}")
  63.  
  64. def run_assistant():
  65.     speak("Hello, how can I help you?")
  66.     while True:
  67.         command = listen()
  68.         if command:
  69.             if 'what time is it' in command:
  70.                 pywhatkit.info("Current time", 1)
  71.             elif 'open google' in command:
  72.                 speak("Opening Google...")
  73.                 pywhatkit.search("Google")
  74.             elif 'search in the text file' in command:
  75.                 speak("What search do you want to perform in the text file?")
  76.                 query = listen()
  77.                 if query:
  78.                     if search_in_text_file(query, "test.txt"):
  79.                         speak(f"The search '{query}' was found in the text file.")
  80.                     else:
  81.                         speak(f"The search '{query}' was not found in the text file.")
  82.             elif 'read the text file' in command:
  83.                 read_text_file("test.txt")
  84.             elif 'stop' in command:
  85.                 speak("Stopping the bot.")
  86.                 break
  87.             elif 'quit' in command or 'exit' in command:
  88.                 speak("Goodbye!")
  89.                 break
  90.             else:
  91.                 speak("Sorry, I didn't understand that command. Can you please repeat?")
  92.                
  93. if __name__ == "__main__":
  94.     run_assistant()
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement