Skip to content

Commit

Permalink
VoiceAssistant
Browse files Browse the repository at this point in the history
This is Voice Assistant coded using Python which can do the following: -
    1. Speak Text entered by User.
    2. Search anything on Google.
    3. Search anything on Wikipedia.
    4. Read an MS Word(docx) document.
    5. Read a book(PDF).
    6. Can be used as a Dictator.
  • Loading branch information
SohamRatnaparkhi authored May 3, 2022
1 parent 329696d commit 39c49e0
Show file tree
Hide file tree
Showing 12 changed files with 964 additions and 0 deletions.
78 changes: 78 additions & 0 deletions VoiceAssistant/DOCUMENTATION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# *DOCUMENTATION*

There are 8 files(modules) present in the main package of this project. These files are named as follows: -

1. VoiceAssistant\_main.py
1. speakListen.py
1. websiteWork.py
1. textRead.py
1. dictator.py
1. menu.py
1. speechtotext.py
1. TextToSpeech.py

A combination of all these modules makes the Voice Assistant work efficiently.

## VoiceAssistant\_main.py

This is the main file that encapsulates the other 7 files. It is advisable to run this file to avail all the benefits of the Voice Assistant.

After giving the command to run this file to your computer, you will have to say “**Hello Python**” to activate the voice assistant. After the activation, a menu will be displayed on the screen, showing all the tasks that Voice Assistant can do. This menu is displayed with the help of the print\_menu()* function present in the menu.py module.

Speaking out the respective commands of the desired task will indicate the Voice Assistant to do the following task. Once the speech is recorded, it will be converted to ` str ` by hear() or short\_hear() function of the speakListen.py module.

For termination of the program after a task is complete, the command “**close python**” should be spoken. For abrupt termination of the program, for Windows and Linux – The ctrl + c key combination should be used.

## speakListen.py

This is the module that contains the following functions: -

1. speak(text) – This function speaks out the ‘text’ provided as a parameter. The text is a string(str). They say() and runAndWait() functions of Engine class in pyttsx3 enable the assistant to speak. Microsoft ***SAPI5*** has provided the voice.
1. hear() – This function records the voice for 9 seconds using your microphone as source and converts speech to text using recognize\_google(). recognize\_google() performs speech recognition on ``audio\_data`` (an ``AudioData`` instance), using the Google Speech Recognition API.
1. long\_hear(duration\_time) – This function records voice for the ‘duration\_time’ provided with 60 seconds as the default time. It too converts speech to text in a similar fashion to hear()
1. short\_hear(duration\_time) – This functions records voice similar to hear() but for 5 seconds.
1. greet(g) - Uses the datetime library to generate current time and then greets accordingly.

## websiteWork.py

This module mainly handles this project's ‘searching on the web’ task. It uses wikipedia and webbrowser libraries to aid its tasks. Following are the functions present in this module: -

1. google\_search() – Searches the sentence spoken by the user on the web and opens the google-searched results in the default browser.
1. wiki\_search() - Searches the sentence spoken by the user on the web and opens the Wikipedia-searched results in the default browser. It also speaks out the summary of the result and asks the user whether he wants to open the website of the corresponding query.

## textRead.py

This module is mainly related to file processing and converting text to speech. Following are the functions present in this module: -

1. ms\_word – Read out the TEXT in MS Word (.docx) file provided in the location.
1. pdf\_read – Can be used to read pdf files and more specifically eBooks. It has 3 options
1. Read a single page
1. Read a range of pages
1. Read a lesson
1. Read the whole book

It can also print the index and can find out the author’s name, the title, and the total number of pages in the PDF file.

1. doubleslash(location) – Mainly intended to help Windows users, if the user copies the default path containing 1 ‘/ ’; the program doubles it so it is not considered an escape sequence.
1. print\_index(toc) - Prints out the index in proper format with the title name and page number. It takes ‘toc’ as a parameter which is a nested list with toc[i][1] - Topic name and toc[i][2] – with page number.
1. print\_n\_speak\_index(toc) - It is similar to print\_index(), but it also speaks out the index here.
1. book\_details(author, title, total\_pages) - Creates a table of book details like author name, title, and total pages. It uses table and console from rich library.

**IMPORTANT: The voice assistant asks you the location of your file to be read by it. It won’t detect the file if it is present in the OneDrive folder or any other protected or third-party folder. Also, it would give an error if the extension of the file is not provided.**

**For example; Consider a docx file ‘***abc***’ and pdf file ‘***fgh***’ present in valid directories and folders named ‘***folder\_loc’***.**

** When location is fed as ‘* **folder\_loc \abc***’ or ‘* **folder\_loc\fgh’* **it gives an error,**

** but if the location is given as** *‘folder\_loc \abc.docx’* **or ‘** *folder\_loc \fgh.pdf’***, then it won’t give an error.**

## dictator.py

This module is like the dictator function and dictates the text that we speak. So basically, it converts the speech that we speak to text. The big\_text(duration\_time) function encapsulates the long\_hear() function. So by default, it records for 60 seconds but it can record for any amount of time as specified by the user.

## menu.py

It prints out the menu which contains the tasks and their corresponding commands. The rich library is being used here.



43 changes: 43 additions & 0 deletions VoiceAssistant/GUIDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# *GUIDE*

<br>
You can run this voice assistant through an .exe file as well as through the terminal. When using it as an .exe file, be sure to keep the .exe file in its setup.

<h3>a) For using Voice Assistant through TERMINAL WINDOW</h3>

1. All the pre-requisites should be complete to run the Voice Assistant in the terminal window.
1. Create a GitHub account if not created already.
2. The code is present in the file *Project_Basic_struct*
3. The source code can be downloaded using the following link: - <https://github.com/SohamRatnaparkhi/Voice-Assistant/tree/master/Project_Basic_struct>

This source code should be cloned using the git commands.

For more information about cloning a GitHub repository, go to the following link - <https://docs.github.com/en/repositories/creating-and-managing-repositories/cloning-a-repository>

<br>After cloning the project, to use this project do the following :- <br>
$ Run the file VoiceAssistant\_main.py in the terminal using the command:- <b> python VoiceAssistant\_main.py</b> <br>
1. For more details about running a python code follow the link: -
1. For Windows - <https://docs.python.org/3/faq/windows.html>
1. For Linux - <https://www.educative.io/edpresso/how-to-run-a-python-script-in-linux>
1. For MacOS - <https://docs.python.org/3/using/mac.html>

<h3>b) For using Voice Assistant through an EXECUTABLE (.exe) file</h3>

Download - https://github.com/SohamRatnaparkhi/Voice-Assistant/releases/tag/v1.0.0

Download the rar file.

1. Extract the folder.
2. Open VoiceAssistant folder.
3. Double-click on the file \_1\_VoiceAssistant for using it.

In case, if you don't find \_1\_VoiceAssistant in the Voice Assistant folder, just install the executable(.exe) file AND SAVE IT IN VoiceAssistant FOLDER. **It is advisable to run the (.exe) file in the VoiceAssistant folder; else the file won't run.**

<h3>Using the Voice Assistant after installation of its resources</h3>

- Saying "Hello Python" will activate the Voice Assistant.
- Then the table that will be displayed on the screen shows the tasks that Voice Assistant can do.
- Saying the respective commands of the task that is intended will enable the Voice Assistant to do those tasks.
- The README.md file of this repository has more information about the individual commands.

![Voice Assistant](https://user-images.githubusercontent.com/92905626/155857729-58a7751a-cb63-48ee-9df5-3a4ee4129a25.JPG)
40 changes: 40 additions & 0 deletions VoiceAssistant/PRE-REQUISITES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# *PRE-REQUISITES*

Following are the pre-requisites to be installed in the system to use Voice Assistant through Terminal Window.

1. Python3 should be installed in the system.
[Click to download](https://www.python.org/downloads/).

1. Following libraries and packages are to be installed. The syntax for library [installation of a library and a package](https://packaging.python.org/en/latest/tutorials/installing-packages/) is: -
1. pip install (library/package\_name) or
1. pip3 install (library/package\_name)

## *Enter the following commands to install them*: -

[pip install colorama](https://pypi.org/project/colorama/)

[](https://pypi.org/project/colorama/)[pip install rich](https://pypi.org/project/rich/)

[pip install pyttsx3](https://pypi.org/project/pyttsx3/)

[pip install DateTime](https://pypi.org/project/DateTime/)

[pip install SpeechRecognition](https://pypi.org/project/SpeechRecognition/)

[pip install docx](https://pypi.org/project/docx/)

[pip install fitz](https://pypi.org/project/fitz/)

[pip install gTTS](https://pypi.org/project/gTTS/)

[pip install playsound](https://pypi.org/project/playsound/)

[pip install pywin32](https://superuser.com/questions/609447/how-to-install-the-win32com-python-library)

[pip install wikipedia](https://pypi.org/project/wikipedia/)

[pip install webbrowser](https://docs.python.org/3/library/webbrowser.html)

## To download the source code and executable file. - [link](https://github.com/SohamRatnaparkhi/Voice-Assistant/releases/tag/v1.0.0)

To use the Voice Assistant through an executable file; download according to instructions mentioned in the installation part of [README](https://github.com/SohamRatnaparkhi/Voice-Assistant).
15 changes: 15 additions & 0 deletions VoiceAssistant/Project_Basic_struct/TextTospeech.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from gtts import gTTS
from playsound import playsound
import win32com
from win32com import client
import os

def tts():
audio = 'speech.mp3'
language = 'en'
sentence = input("Enter the text to be spoken :- ")

speaker = win32com.client.Dispatch("SAPI.SpVoice")
sp = speaker.Speak(sentence)


78 changes: 78 additions & 0 deletions VoiceAssistant/Project_Basic_struct/VoiceAssistant_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
from speakListen import *
from websiteWork import *
from textRead import *
from dictator import *
from menu import *
from speechtotext import *
from TextTospeech import *


def main():
start = 0
end = 0
if start == 0:
print("\nSay \"Hello Python\" to activate the Voice Assistant!")
start += 1
while True:

q = short_hear().lower()
if "close" in q:
greet("end")
exit(0)
if "hello python" in q:
greet("start")
print_menu()
while True:

query = hear().lower()
if "close" in query:
greet("end")
end += 1
return 0
elif "text to speech" in query:
tts()
time.sleep(4)


elif "search on google" in query or "search google" in query or "google" in query:
google_search()
time.sleep(10)

elif "search on wikipedia" in query or "search wikipedia" in query or "wikipedia" in query:
wiki_search()
time.sleep(10)

elif "word" in query:
ms_word()
time.sleep(5)

elif "book" in query:
pdf_read()
time.sleep(10)

elif "speech to text" in query:
big_text()
time.sleep(5)

else:
print("I could'nt understand what you just said!")
speak("I could'nt understand what you just said!")

print("\nDo you want to continue? if yes then say " + Fore.YELLOW + "\"YES\"" + Fore.WHITE + " else say " + Fore.YELLOW + "\"CLOSE PYTHON\"")
speak("Do you want to continue? if yes then say YES else say CLOSE PYTHON")
qry = hear().lower()
if "yes" in qry:
print_menu()
elif "close" in qry:
greet("end")
return 0
else:
speak("You didn't say a valid command. So I am continuing!")
continue

elif "close" in q:
return 0
else:
continue

main()
47 changes: 47 additions & 0 deletions VoiceAssistant/Project_Basic_struct/dictator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# from speakListen import hear
# from speakListen import long_hear
from speakListen import *

from colorama import Fore, Back, Style

def big_text():
print("By default, I will record your voice for 60 seconds.\nDo you want to change this default timing?")
speak("By default, I will record your voice for 60 seconds.\nDo you want to change this default timing?")
print(Fore.YELLOW + "Yes or No")
query = hear().lower()

duration_time = 0

if "yes" in query or "es" in query or "ye" in query or "s" in query:

print("Please enter the time(in seconds) for which I shall record your speech - ", end = '')
duration_time = int(input().strip())

print("\n")
else:
duration_time = 60
speak(f"I will record for {duration_time} seconds!")
text = long_hear(duration_time)
print("\n" + Fore.LIGHTCYAN_EX + text)

def colours():
text = "Colour"
print(Fore.BLACK + text)
print(Fore.GREEN + text)
print(Fore.YELLOW + text)
print(Fore.RED + text)
print(Fore.BLUE + text)
print(Fore.MAGENTA + text)
print(Fore.CYAN + text)
print(Fore.WHITE + text)
print(Fore.LIGHTBLACK_EX + text)
print(Fore.LIGHTRED_EX + text)
print(Fore.LIGHTGREEN_EX + text)
print(Fore.LIGHTYELLOW_EX + text)
print(Fore.LIGHTBLUE_EX + text)
print(Fore.LIGHTMAGENTA_EX + text)
print(Fore.LIGHTCYAN_EX + text)
print(Fore.LIGHTWHITE_EX + text)

#big_text()
#colours()
27 changes: 27 additions & 0 deletions VoiceAssistant/Project_Basic_struct/menu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from rich.console import Console # pip3 install Rich
from rich.table import Table
from speakListen import *


def print_menu():
"""Display a table with list of tasks and their associated commands.
"""
speak("I can do the following")
table = Table(title="\nI can do the following :- ", show_lines = True)

table.add_column("Sr. No.", style="cyan", no_wrap=True)
table.add_column("Task", style="yellow")
table.add_column("Command", justify="left", style="green")

table.add_row("1", "Speak Text entered by User", "text to speech")
table.add_row("2", "Search anything on Google", "Search on Google")
table.add_row("3", "Search anything on Wikipedia", "Search on Wikipedia")
table.add_row("4", "Read a MS Word(docx) document", "Read MS Word document")
table.add_row("5", "Convert speech to text", "Convert speech to text")
table.add_row("6", "Read a book(PDF)", "Read a book ")
table.add_row("7", "Quit the program", "Python close")

console = Console()
console.print(table)

#print_menu()
Loading

0 comments on commit 39c49e0

Please sign in to comment.