Skip to content

Instantly share code, notes, and snippets.

@avseng
Last active January 16, 2022 14:51
Show Gist options
  • Save avseng/bdc9e29a442911f9e43297a7d588db5d to your computer and use it in GitHub Desktop.
Save avseng/bdc9e29a442911f9e43297a7d588db5d to your computer and use it in GitHub Desktop.
AI Voice Assistance using Python
Install bellow libraries before running the VoiceAssistance.py
pip install speechrecognition
pip install gTTS
pip install wheel
pip install pipwin
pipwin install pyaudio
python3 -m pip install pyobj
pip install weathercom
pip install playsound
import speech_recognition as sr
from gtts import gTTS
import playsound
import os
import weathercom
import json
r = sr.Recognizer()
def voice_command_processor(ask=False):
with sr.Microphone() as source:
if(ask):
audio_playback(ask)
audio = r.listen(source,phrase_time_limit=4)
text = ''
try:
text=r.recognize_google(audio)
except sr.UnknownValueError as e:
print(e)
except sr.RequestError as e:
print("service is down")
return text.lower()
def audio_playback(text):
filename = "test.mp3"
tts = gTTS(text=text, lang='en-us')
tts.save(filename)
playsound.playsound(filename)
os.remove(filename)
def execute_voice_command(text):
if "what are you" in text:
audio_playback("i am a i voice assistance system")
if "what is the today's weather" in text:
city = voice_command_processor("which city")
humidity, temp, phrase = weatherReport(city)
audio_playback("currently in " + city + " temperature is " + str(temp)
+ " degree celsius, " + "humidity is " + str(humidity) + " percent and sky is " + phrase)
print("currently in " + city + " temperature is " + str(temp)
+ "degree celsius, " + "humidity is " + str(humidity) + " percent and sky is " + phrase)
def weatherReport(city):
weatherDetails = weathercom.getCityWeatherDetails(city)
humidity = json.loads(weatherDetails)["vt1observation"]["humidity"]
temp = json.loads(weatherDetails)["vt1observation"]["temperature"]
phrase = json.loads(weatherDetails)["vt1observation"]["phrase"]
return humidity, temp, phrase
while True:
command = voice_command_processor()
print(command)
execute_voice_command(command)
@SHANKAR1704
Copy link

HI, Sir I Copy & Paste Your Program on My Terminal .But It Shows a Error In this program. i don't know how to solve it .please, verify it

@ipple92
Copy link

ipple92 commented Jul 4, 2021

Bro!! am face this Error "NameError: name 'text' is not defined" ..... plz help

@jaybeckham292
Copy link

You know you'd have better SR (Speech Recognition) doing something like 'pip install nemo_toolkit[all]' and quartznet = nemo_asr.models.ASRModel.from_pretrained(model_name="QuartzNet15x5Base-En") just to use a simple example instead of getting further in-depth. Also, I'm on Discord if you need help with something :) unicorns#7402

So please do let me know if something errors out here, ^ inferencing the model shouldn't be bad on a CPU, it's about 5-20 seconds on a CPU using my machine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment