ad
ad
Topview AI logo

Major Project: Voice Assistant Project in Python Programming ? | Step by step Tutorial ?‍?

Education


Introduction

In this tutorial, we will build a fully functional voice assistant using Python. This assistant will listen to commands, play videos, retrieve information, read the news, tell jokes, share interesting facts, and provide real-time weather updates. We will achieve these functionalities through various libraries, including pyttsx3 for text-to-speech, speech_recognition for speech-to-text, selenium for web automation, and APIs for news and weather information.

Getting Started with Text-to-Speech

To make our assistant speak, we need the pyttsx3 library, which can be installed using pip:

pip install pyttsx3

Once installed, we can start by importing the library and initializing the text-to-speech engine.

import pyttsx3

engine = pyttsx3.init()
engine.say("Hello, World! My name is Nova.")
engine.runAndWait()

In this example, the assistant greets the user. We can modify the voice properties, including rate and voice type, to personalize our assistant's voice.

Converting Speech to Text

To allow our assistant to understand spoken commands, we will utilize the speech_recognition library, which also requires installation:

pip install SpeechRecognition

Next, we'll create a recognizer instance that listens to the microphone input and converts speech to text.

import speech_recognition as sr

r = sr.Recognizer()
with sr.Microphone() as source:
    audio = r.listen(source)

text = r.recognize_google(audio)
print(text)

This code listens to the user's speech and outputs the recognized text.

Enhancing Functionality

We can now build upon our assistant’s base functionality by adding several features. For instance, we can respond to user commands about playing music on YouTube or retrieving information from Wikipedia using Selenium.

Selenium for Web Automation

For web automation, let’s install the selenium package:

pip install selenium

Here’s a snippet to search YouTube for a given query.

from selenium import webdriver

class Music:
    def __init__(self, query):
        self.query = query
        self.driver = webdriver.Chrome('path_to_chromedriver')

    def play(self):
        self.driver.get(f"https://www.youtube.com/results?search_query=(self.query)")
        # Logic to play the video would be added here

Fetching Latest News

We can fetch the latest news using the requests library. First, install it:

pip install requests

Next, we’ll set up our API and a function to retrieve the latest news:

import requests

API_KEY = 'your_api_key'
url = f"https://newsapi.org/v2/top-headlines?apiKey=(API_KEY)"

response = requests.get(url)
articles = response.json().get('articles', [])

for article in articles[:3]:
    print(article['title'])

Random Facts and Jokes

Using a random facts and a jokes API will add an enjoyable feature to our assistant:

random_fact_response = requests.get("https://some-random-fact-api")
joke_response = requests.get("https://official-joke-api.appspot.com/jokes/random")

Weather Forecast and Time

To give real-time weather updates, we can use the Open Weather Map API:

pip install requests

The code snippet below fetches and processes the weather data:

weather_response = requests.get(f"http://api.openweathermap.org/data/2.5/weather?q=Delhi&appid=(API_KEY)")
weather_data = weather_response.json()
temperature = weather_data['main']['temp'] - 273.15  # Convert Kelvin to Celsius
print(f"Temperature in Delhi is (temperature:.1f) degrees Celsius.")

Wrapping Up

Finally, after integrating all the functionalities, your voice assistant will be capable of providing weather updates, playing videos, retrieving information, rejoicing in jokes, and sharing interesting facts—all through voice commands. To enhance the project further, consider adding additional features or refining the existing functionalities.


Keyword

  • Voice Assistant
  • Python
  • Text-to-Speech
  • Speech Recognition
  • Selenium
  • News API
  • Weather API
  • Jokes
  • Random Facts

FAQ

Q: What libraries do I need to install for this project?
A: You need to install pyttsx3, speech_recognition, selenium, and requests.

Q: How does the voice assistant retrieve news?
A: The voice assistant retrieves news by making API requests to newsapi.org.

Q: Can the assistant play videos on YouTube?
A: Yes, it uses Selenium for web automation to search for and play videos on YouTube.

Q: How does the weather feature work?
A: The assistant gets current weather data by calling the Open Weather Map API.

Q: Is it possible to enhance the assistant?
A: Yes, you can add more features such as playing music from different platforms or providing movie reviews.