100% found this document useful (1 vote)
473 views15 pages

Python Voice Assistant Project: Alexa

This Python code creates a voice-controlled virtual assistant named Alexa. Alexa uses speech recognition and text-to-speech to understand voice commands and respond verbally or via text. It can perform tasks like playing music, telling the time, searching Wikipedia, telling jokes, and opening web browsers and programs. The code runs continuously, allowing multiple commands until the user says "thank you" to exit. Overall, the program demonstrates using Python libraries to build an interactive voice assistant for hands-free tasks.

Uploaded by

Sakshi Sonavane
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
473 views15 pages

Python Voice Assistant Project: Alexa

This Python code creates a voice-controlled virtual assistant named Alexa. Alexa uses speech recognition and text-to-speech to understand voice commands and respond verbally or via text. It can perform tasks like playing music, telling the time, searching Wikipedia, telling jokes, and opening web browsers and programs. The code runs continuously, allowing multiple commands until the user says "thank you" to exit. Overall, the program demonstrates using Python libraries to build an interactive voice assistant for hands-free tasks.

Uploaded by

Sakshi Sonavane
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
  • Abstract
  • System Specifications
  • Introduction to VS Code
  • Implementation
  • Interactions
  • Source Code
  • Output
  • Conclusion
  • Bibliography

ALEXA

CONTENTS

SL. NO. PARTICULARS PAGE NO.


1 Abstratct 2
2 System Specifications 3
3 Introduction to VS Code 4
4 Implementation 5
5 Interaction 6
6 Source Code 7
7 Output 11
8 Conclusion 14
9 Bibliography 15

1
ALEXA

ABSTRACT

This Python code creates a virtual assistant named Alexa, which is capable of
performing various tasks through voice commands. The program uses libraries such as
speech_recognition, pyttsx3, pywhatkit, datetime, wikipedia, pyjokes, subprocess, and
webbrowser to recognize and execute commands.
The program begins by initializing the speech recognition module and the text-to-
speech engine. It then listens for user input through the microphone and uses the Google
Speech Recognition API to convert the speech into text. The user's commands are then
interpreted and executed through various functions.
The program's features include playing songs on YouTube, telling the current time,
searching for information on Wikipedia, telling jokes, opening web browsers, and launching
programs. It also includes responses to common questions, such as "Who made you?" and
"How are you?".
The program runs continuously in a while loop, allowing users to give multiple
commands. The loop ends when the user says "thank you", indicating that they are done using
the assistant.
Overall, this program demonstrates the use of Python libraries to build a voice-
controlled virtual assistant that can perform various tasks, making it a useful tool for those
who prefer voice-based interactions.

2
ALEXA

SYSTEM SPECIFICATIONS

 Software Requirements :
 Language : PYTHON
 Integrated Development Environment (IDE) : VS CODE

 Hardware Requirements :
 Graphics System
 Intel core i5 with 16 GB RAM

3
ALEXA

INTRODUCTION TO VS CODE

Visual Studio Code (VS Code) is a free and open-source code editor
developed by Microsoft. It is a lightweight yet powerful tool that is widely used by
developers for building web applications, desktop applications, and other software projects.

VS Code features a streamlined user interface and a wide range of


extensions that allow developers to customize their workflow and enhance their productivity.
It supports various programming languages, including JavaScript, TypeScript, Python, C#,
and many others.

Some of the key features of VS Code include:


1. IntelliSense: VS Code's IntelliSense feature provides intelligent code completion,
syntax highlighting, and code snippets that help developers write code more quickly
and accurately.
2. Debugging: VS Code supports debugging for a wide range of programming
languages, allowing developers to quickly identify and fix issues in their code.
3. Source control: VS Code integrates with popular source control systems such as Git,
making it easy to manage code changes and collaborate with other developers.
4. Extensions: VS Code has a large library of extensions that allow developers to
customize their workflow and add new functionality to the editor.
5. Terminal integration: VS Code includes an integrated terminal that allows developers
to run commands and execute scripts without leaving the editor.

Overall, VS Code is a powerful and flexible code editor that is suitable for a wide range of
programming tasks. Its intuitive interface and powerful features make it a popular choice
among developers.

4
ALEXA

IMPLEMENTATION

To implement the above code, follow the steps below:

1. Install the required libraries: speech_recognition, pyttsx3, pywhatkit, wikipedia,


pyjokes, subprocess
2. You can install these libraries using pip by running the following command in your
terminal:
“pip install speechrecognition pyttsx3 pywhatkit Wikipedia pyjokes ”
3. Copy the code into a Python file, for example, `[Link]`.
4. Run the `[Link]` file using a Python interpreter.
5. Once the program is running, say "Alexa" to activate the virtual assistant. You can
then give commands to Alexa, such as "play Despacito on YouTube" or "tell me a
joke".
6. Alexa will recognize and execute the command, and respond with either spoken
words or text output in the console.
7. You can continue giving commands to Alexa until you say "thank you" to end the
program.

Note: Make sure to test the program in a quiet environment and adjust the microphone
volume and settings as necessary for optimal performance. Also, note that some commands
may require an internet connection to work, such as searching on Wikipedia or opening web
browsers.

5
ALEXA

INTERACTIONS

The interaction commands in this program are voice commands that can be given to Alexa,
which are recognized by the program using the SpeechRecognition library. The following
commands are recognized by the program:

- "hi" to get a response of "Hello, How are you....?".


- "how are you" to get a response that Alexa is good and hopes the user is good too.
- "play" followed by a song name to play the song on YouTube using PyWhatKit
library.
- "time" to get the current time using datetime library.
- "who is" followed by a person's name to get a brief summary about the person from
Wikipedia using Wikipedia library.
- "date" to get a response that Alexa has a headache.
- "joke" to get a random joke using PyJokes library.
- "who made you" to get a response with the names of the developers who created the
program.
- "purpose" to get a response that the program was created for a college mini-project.
- "chrome" to open Google Chrome using the subprocess library.
- "youtube" to open YouTube in a web browser using the webbrowser library.
- "power toys" to open PowerToys using the subprocess library.
- "thank you" to get a response of "It's my pleasure, Have a Great Day".

6
ALEXA

SOURCE CODE

import speech_recognition as sr
import pyttsx3
import pywhatkit
import datetime
import wikipedia
import pyjokes
import subprocess
import webbrowser

listener = [Link]()
engine = [Link]()
voices = [Link]('voices')
[Link]('voice',voices[1].id)

def talk(text):
[Link](text)
[Link]()

def take_command():
try:
with [Link]() as source:
print('listening...')
voice = [Link](source)
command = listener.recognize_google(voice)
command = [Link]()
if 'alexa' in command:

7
ALEXA

command = [Link]('alexa', '')


print(command)
except:
pass
return command

def run_alexa():
command = take_command()
print(command)
if 'play' in command:
song = [Link]('play', '')
talk('playing' + song)
[Link](song)
elif 'time' in command:
time = [Link]().strftime('%I:%M %p')
print(time)
talk('current time is ' +time)
elif 'who is' in command:
person = [Link]('who is', '')
info = [Link](person, 1)
print(info)
talk(info)
elif 'date' in command:
talk('sorry, I have a headache but I am everywhere with you')
elif 'are you single' in command:
talk('I am in a relationship with Wifi')
elif 'joke' in command:
joke=pyjokes.get_joke()

8
ALEXA

print(joke)
talk(joke)
elif 'kiss' in command:
talk('I can but I am a Virtual Assistant')
elif 'how are you' in command:
talk('I am Good, Hope so as you are')
elif 'hi' in command:
talk('Hello, How are you....?')
elif 'who made you' in command:
talk('Sakshi Sonavane and Tejas Gupta')
elif 'purpose' in command:
talk('for their college mini project')
elif 'thank you' in command:
talk('Its my pleasure, Have a Great Day')
return False
elif 'chrome' in command:
a='opening chrome....'
[Link](a)
[Link]()
program="C:\Program Files\Google\Chrome\Application\[Link]"
[Link]([program])
[Link]([program])
elif 'youtube' in command:
a='opening youtube....'
[Link](a)
[Link]()
url = "[Link]
[Link](url)

9
ALEXA

elif 'power toys' in command:


a='opening powertoys....'

[Link](a)
[Link]()
program="C:\Program Files\PowerToys\[Link]"
[Link]([program])
else:
talk('please say it again')
return True
while True:
if not run_alexa():
break

10
ALEXA

OUTPUT

11
ALEXA

12
ALEXA

13
ALEXA
CONCLUSION

This code is a Python program that uses speech recognition to interpret voice
commands given to a virtual assistant named Alexa. The assistant can perform several tasks,
including playing songs on YouTube, telling the time, providing information on famous
people using Wikipedia, telling jokes, opening web browsers and specific programs such as
Google Chrome and PowerToys, and engaging in small talk with users.
The program uses various Python libraries such as speech_recognition, pyttsx3,
pywhatkit, wikipedia, pyjokes, subprocess, and webbrowser to enable these features.
Additionally, it uses datetime to get the current time.
This program can be used as a starting point for developing a more advanced virtual
assistant that can perform more complex tasks based on user input. It can also serve as a basic
framework for creating similar virtual assistants in other programming languages.

14
ALEXA
BIBLIOGRAPHY

Here is a possible bibliography for the code provided:


1. "Speech Recognition" - a Python library for performing speech recognition with
support for several engines, including Google Speech Recognition, Microsoft Bing
Voice Recognition, and others. Available at:
[Link]
2. "pyttsx3" - a Python library for text-to-speech conversion. Available at:
[Link]
3. "pywhatkit" - a Python library for performing several tasks with the help of various
APIs, such as playing songs on YouTube, opening web pages, etc. Available at:
[Link]
4. "datetime" - a Python module for working with dates and times. Available at:
[Link]
5. "wikipedia" - a Python library for working with the Wikipedia API. Available at:
[Link]
6. "pyjokes" - a Python library for generating random jokes. Available at:
[Link]
7. "subprocess" - a Python module for working with system processes. Available at:
[Link]
8. "webbrowser" - a Python module for opening web pages. Available at:
[Link]
In addition to these sources, the code may have also been inspired by various online tutorials,
code snippets, and examples found on websites such as Stack Overflow, GitHub, and others.
It is important to properly credit and cite any external sources used in a project report to
avoid plagiarism and give credit where it is due.

15

Common questions

Powered by AI

The Alexa virtual assistant can handle interactions such as: - **Greeting**: Responds with "Hello, How are you....?" when the user says "hi" . - **Checking well-being**: Responds that Alexa is good and wishes the user well in return to "how are you" . - **Playing music**: Plays a song on YouTube when the user says "play" followed by a song name . - **Telling time**: Tells the current time in response to "time" . - **Providing information**: Gives a short summary from Wikipedia if the command is "who is" followed by a name . - **Joking**: Tells a random joke in response to "joke" . - **Acknowledgement**: Responds with "It's my pleasure, Have a Great Day" when thanked . Each interaction is processed by recognizing the command via the microphone using the SpeechRecognition library, responding through the Pyttsx3 text-to-speech engine, and executing relevant tasks using suitable libraries such as PyWhatKit, Wikipedia, or PyJokes .

In the virtual assistant program, Python's 'subprocess' module is used to open applications like Google Chrome and PowerToys. This allows the assistant to execute system-level commands from within the Python environment, facilitating tasks that require launching external programs . The module's advantage is its ability to handle various process creation and management tasks efficiently. However, it poses risks such as security vulnerabilities if input commands are not properly sanitized or controlled, potentially allowing malicious or unintended code execution .

The Python libraries used for implementing the virtual assistant Alexa include: 1. **speech_recognition**: Used for recognizing speech from audio inputs . 2. **pyttsx3**: A text-to-speech conversion library to make Alexa respond verbally . 3. **pywhatkit**: Facilitates tasks such as playing songs on YouTube . 4. **datetime**: Provides functionality to tell the current time . 5. **wikipedia**: Used to fetch information from Wikipedia . 6. **pyjokes**: Used for generating and telling jokes . 7. **subprocess**: Manages system processes, such as opening applications . 8. **webbrowser**: Opens URLs in a web browser for web-based commands .

The program's reliance on external APIs such as Google Speech Recognition and Wikipedia has several implications. Positively, it allows the virtual assistant to leverage powerful, well-maintained services to provide accurate speech recognition and expansive information access, reducing development complexity. However, it also introduces dependencies on third-party service availability and network connectivity, potentially limiting functionality during outages or when offline .

The while loop in the virtual assistant's program is significant because it allows the assistant to continuously listen for and process voice commands. This enables the program to execute tasks sequentially based on user input without needing to restart, thereby providing a more seamless and efficient user experience. The loop exits when the user says "thank you", signifying the end of the session .

User feedback, particularly the phrase "thank you," plays a critical role in the program's flow by signaling intent and controlling execution. It acts as a natural language trigger for ending the session, which enhances user satisfaction by allowing intuitive control over interactions. Designing programs to respond to user feedback like this aligns with human-centered design practices, promoting user engagement and simplifying task completion without requiring explicit commands to start or stop operations .

To set up the Alexa virtual assistant, users need to: 1. Install required libraries using pip . 2. Copy the provided Python code into a file such as 'alexa.py' . 3. Run the Python script using a Python interpreter . 4. Activate the assistant by saying "Alexa" and give commands . Challenges during setup might include ensuring a quiet environment for accurate voice recognition, correctly configuring microphone settings, and maintaining an internet connection for tasks like Wikipedia searches .

Using this Python-based virtual assistant as a college mini-project has several educational implications. It provides students hands-on experience in integrating multiple software libraries and managing system operations, deepening their understanding of Python's capabilities. The project fosters skills in troubleshooting and debugging complex systems, promoting critical thinking and problem-solving. Additionally, it encourages exploration of natural language processing and human-computer interaction fields, preparing students for advanced studies or careers in tech .

The hardware requirements for running the virtual assistant program include a graphics system and an Intel Core i5 CPU with 16 GB RAM . These specifications imply that the program is designed to run on machines with moderate processing power and memory, enabling efficient multitasking and quick response times without significant lag. The requirement for a graphics system suggests the potential for future upgrades involving more graphically-intensive operations .

Visual Studio Code's IntelliSense feature enhances development by providing intelligent code completion, syntax highlighting, and code snippets. This allows developers to write code more quickly and accurately, reducing the likelihood of syntactical errors and improving code quality .

ALEXA
CONTENTS
SL. NO.
PARTICULARS
PAGE NO.
1
Abstratct
2
2
System Specifications
3
3
Introduction to VS Code
4
4
Implementat
ALEXA
ABSTRACT
This  Python code creates  a virtual assistant named  Alexa,  which  is  capable of
performing various tasks t
ALEXA
SYSTEM SPECIFICATIONS
Software Requirements :

Language : PYTHON

Integrated Development Environment (IDE) : VS CODE
ALEXA
INTRODUCTION TO VS CODE
Visual  Studio  Code  (VS  Code)  is  a  free  and  open-source  code  editor
developed  by Mic
ALEXA
IMPLEMENTATION
To implement the above code, follow the steps below:
1. Install  the  required  libraries:  speech_recog
ALEXA
INTERACTIONS
The interaction commands in this program are voice commands that can be given to Alexa,
which are recogniz
ALEXA
SOURCE CODE
import speech_recognition as sr
import pyttsx3
import pywhatkit
import datetime
import wikipedia
import pyj
ALEXA
            
    command = command.replace('alexa', '')
                print(command)
    except:
        pass
    ret
ALEXA
        
print(joke)
        talk(joke)
    elif 'kiss' in command:
        talk('I can but I am a Virtual Assistant')
ALEXA
  
  elif 'power toys' in command:
        a='opening powertoys....'
      
  engine.say(a)
        engine.runAndWait()

You might also like