From fc67d2fa12298039df3389b1ff32d756d32b8a42 Mon Sep 17 00:00:00 2001 From: ArtaXerxes <74452252+ArtaXerxess@users.noreply.github.com> Date: Sat, 12 Mar 2022 11:28:19 +0530 Subject: [PATCH] Add files via upload --- README.md | 51 ++++++++++++++++++++ file finding.py | 54 +++++++++++++++++++++ foo.py | 2 + main.py | 119 ++++++++++++++++++++++++++++++++++++++++++++++ mic testing.py | 21 ++++++++ speak tester.py | 17 +++++++ text to speech.py | 19 ++++++++ 7 files changed, 283 insertions(+) create mode 100644 README.md create mode 100644 file finding.py create mode 100644 foo.py create mode 100644 main.py create mode 100644 mic testing.py create mode 100644 speak tester.py create mode 100644 text to speech.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..38ee9c5 --- /dev/null +++ b/README.md @@ -0,0 +1,51 @@ +# __Mini project Third Year__ + +## __Voice Assistant(Backend)__ + +Python Mudules required: + + pyttsx3 module + wikipedia + speech_recognition module + +You may get error in windows regarding [pyaudio](https://pypi.org/project/PyAudio/) , to solve that error you have two options: + +__Option 1__ + +* Go to [pyaudio unofficial binaries](https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyaudio) and download any of the previous versions. + +* For more information refer the [details](http://people.csail.mit.edu/hubert/pyaudio/). + +* Open the powershell window in the diretory where you have downloaded it. + +* Then use `pip` to install the pyaudio file. + +example: +>pip install .\PyAudio‑0.2.11‑cp310‑cp310‑win_amd64.whl + + + +__Option 2__ + +* Go to terminal and enter: + +>pip install pipwin + +>pipwin install pyaudio + +* For more information about `pipwin` refer the [documentation](https://pypi.org/project/pipwin/) + + + + +___ +## Author +[Harshvardhan Singh]() +___ + + +Features: + + quick wikipedia search + file access like play music + browser access \ No newline at end of file diff --git a/file finding.py b/file finding.py new file mode 100644 index 0000000..df78218 --- /dev/null +++ b/file finding.py @@ -0,0 +1,54 @@ +import pickle,os + +class FileSearchEngine: + def __init__(self): + self.file_index = [] + self.results = [] + self.matches = 0 + self.records = 0 + def create_new_index(self, root_path): + self.file_index = [(root,files) for root,dirs,files in os.walk(root_path) if files] + with open('file_index.pkl','wb') as foo: + pickle.dump(self.file_index,foo) + foo.close() + def load_existing_index(self): + try: + with open('file_index.pkl','rb') as foo: + self.file_index = pickle.load(foo) + foo.close() + except: + self.file_index = [] + def search(self,term,search_type='contains'): + self.matches = 0 + self.records = 0 + self.results.clear() + for path, files in self.file_index: + for file in files: + self.records += 1 + if (search_type == 'contains' and term.lower() in file.lower() or + search_type == 'startswith' and file.lower().startswith(term.lower()) or + search_type == 'endswith' and file.lower().endswith(term.lower())): + result = path.replace('\\','/') + '/' + file + self.results.append(result) + self.matches += 1 + else: + continue + with open('search_results.txt','w') as f: + f.write('name of file:{}\n'.format(term)) + f.write('{} Matches Found!\n'.format(self.matches)) + for row in self.results: + f.write(row+'\n') + f.close() + +def find_the_file(file_name): + path = 'C:/Users/WiiN10pro' #C:/Users/WiiN10pro + print('Finding {}...'.format(file_name)) + Guddu= FileSearchEngine() + Guddu.create_new_index(path) + Guddu.search(file_name) + print("files containing the name {} are found, we have {} matches".format(file_name,Guddu.matches)) + os.remove('file_index.pkl') + os.startfile('search_results.txt') + + +find_the_file(str(input("Enter the name of file you want to find: "))) \ No newline at end of file diff --git a/foo.py b/foo.py new file mode 100644 index 0000000..77a5605 --- /dev/null +++ b/foo.py @@ -0,0 +1,2 @@ +import os +os.startfile("search_results.txt") \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..ac7884b --- /dev/null +++ b/main.py @@ -0,0 +1,119 @@ +__title__ = 'Voice Assistant' +__author__ = 'Harshvardhan Singh' + +import pyttsx3 +import speech_recognition +import os +import pickle +import wikipedia +import webbrowser + +class FileSearchEngine: + def __init__(self): + self.file_index = [] + self.results = [] + self.matches = 0 + self.records = 0 + def create_new_index(self, root_path): + self.file_index = [(root,files) for root,dirs,files in os.walk(root_path) if files] + with open('file_index.pkl','wb') as foo: + pickle.dump(self.file_index,foo) + foo.close() + def load_existing_index(self): + try: + with open('file_index.pkl','rb') as foo: + self.file_index = pickle.load(foo) + foo.close() + except: + self.file_index = [] + def search(self,term,search_type='contains'): + self.matches = 0 + self.records = 0 + self.results.clear() + for path, files in self.file_index: + for file in files: + self.records += 1 + if (search_type == 'contains' and term.lower() in file.lower() or + search_type == 'startswith' and file.lower().startswith(term.lower()) or + search_type == 'endswith' and file.lower().endswith(term.lower())): + result = path.replace('\\','/') + '/' + file + self.results.append(result) + self.matches += 1 + else: + continue + with open('search_results.txt','w') as f: + f.write('name of file:{}\n'.format(term)) + f.write('{} Matches Found!\n'.format(self.matches)) + for row in self.results: + f.write(row+'\n') + f.close() + +def find_the_file(file_name): + path = 'C:/Users/WiiN10pro' + print('Finding {}...'.format(file_name)) + Guddu= FileSearchEngine() + Guddu.create_new_index(path) + Guddu.search(file_name) + speak('Files containing the name {} are found.\nI have {}matches'.format(file_name,Guddu.matches)) + +speak_engine = pyttsx3.init('sapi5') +voices = speak_engine.getProperty(name = 'voices') +speak_engine.setProperty(name = 'voice',value = voices[0].id) +speak_engine.setProperty(name = 'rate' , value = 180) +def speak(speech): + speak_engine.say(speech) + speak_engine.runAndWait() +def takeCommand(): + ''' + takes input from the microphone, converts it into string + ''' + recg = speech_recognition.Recognizer() + with speech_recognition.Microphone() as mic_source: + print('Please Speak...') + print('Listening...') + recg.pause_threshold = 1 + audio = recg.listen(source=mic_source) + try: + print('Recognizing...') + query = str(recg.recognize_google(audio_data=audio,language='en-in')).lower() + print('user said: {}'.format(query)) + except: + print('Error!\nSay that again please') + return 'None' + return query + + + +def listen(): + speak('hello, how may i help you?') + query = takeCommand() + ChromePath = 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe' + if 'find' in query: + query = query.replace('find ','') + find_the_file(query) + elif 'wikipedia ' in query: + query = query.replace('wikipedia ', '') + results = wikipedia.summary(query,sentences = 2) + print(results,'\n') + speak(results) + elif 'open youtube' in query: + print('Opening YouTube...') + speak('Opening YouTube') + webbrowser.open('youtube.com') + elif 'open chrome' in query: + os.startfile(ChromePath) + elif 'play' in query: + songpath = 'C:/Users/Admin/Desktop' + foo = FileSearchEngine() + foo.create_new_index(songpath) + query = query.replace('play ','') + foo.search(query) + if foo.results is not None: + f=str(foo.results[0]).split('/')[-2] + speak('Song {}, is found in the directory named: {}'.format(query,f)) + os.startfile(foo.results[0]) + + +# Main +if __name__ == '__main__': + listen() \ No newline at end of file diff --git a/mic testing.py b/mic testing.py new file mode 100644 index 0000000..ede014c --- /dev/null +++ b/mic testing.py @@ -0,0 +1,21 @@ +import speech_recognition +def takeCommand(): + ''' + takes input from the microphone, converts it into string + ''' + recg = speech_recognition.Recognizer() + with speech_recognition.Microphone() as mic_source: + print('Please Speak...') + print('Listening...') + recg.pause_threshold = 1 + audio = recg.listen(source=mic_source) + try: + print('Recognizing...') + query = str(recg.recognize_google(audio_data=audio,language='en-in')).lower() + print('user said: {}'.format(query)) + except: + print('Error!\nSay that again please') + return 'None' + return query + +print(takeCommand()) \ No newline at end of file diff --git a/speak tester.py b/speak tester.py new file mode 100644 index 0000000..07508a9 --- /dev/null +++ b/speak tester.py @@ -0,0 +1,17 @@ +import pyttsx3 + +speak_engine = pyttsx3.init('sapi5') +voices = speak_engine.getProperty(name = 'voices') +speak_engine.setProperty(name = 'voice',value = voices[0].id) +speak_engine.setProperty(name = 'rate' , value = 130) + +def speak(speech): + speak_engine.say(speech) + speak_engine.runAndWait() + +sample='hello boss, lets get our coding freak on' + +try: + speak(sample) +except: + print('error!',Exception) diff --git a/text to speech.py b/text to speech.py new file mode 100644 index 0000000..adf6306 --- /dev/null +++ b/text to speech.py @@ -0,0 +1,19 @@ +import pyttsx3,pyperclip +speak_engine = pyttsx3.init() +voices = speak_engine.getProperty(name = 'voices') +speak_engine.setProperty(name = 'voice',value = voices[0].id) +speak_engine.setProperty(name = 'rate' , value = 160) +def speak(speech): + speak_engine.say(speech) + speak_engine.runAndWait() +def ReadOutLoud() -> None: + try: + if (1): + print('Reading...') + pyperclip.waitForNewPaste() + print('speaking...') + speak(pyperclip.paste()) + except: + print('Error!') + +ReadOutLoud() \ No newline at end of file