Professional Documents
Culture Documents
import warnings
import pyttsx3
import speech_recognition as sr
from gtts import gTTS
import playsound
import os
import calendar
import random
import wikipedia
import webbrowser
import ctypes
import winshell
import pygame
import keyboard
import subprocess
import pyjokes
import smtplib
import requests
import json
import datetime
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from twilio.rest import Client
import wolframalpha
import time
warnings.filterwarnings("ignore")
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[2].id)
engine.setProperty('rate', 168)
def talk(audio):
engine.say(audio)
engine.runAndWait()
def rec_audio():
recog = sr.Recognizer()
try:
data = recog.recognize_google(audio)
print("You said: " + data)
except sr.UnknownValueError:
pass
except sr.RequestError as ex:
print("Request Error from Google Speech Recognition" + ex)
return data
def call(text):
action_call = "assistant" or "Assistant"
text = text.lower()
if action_call in text:
return True
return False
def today_date():
now = datetime.datetime.now()
date_now = datetime.datetime.today()
week_now = calendar.day_name[date_now.weekday()]
month_now = now.month
day_now = now.day
months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
]
ordinals = [
"1st",
"2nd",
"3rd",
"4th",
"5th",
"6th",
"7th",
"8th",
"9th",
"10th",
"11th",
"12th",
"13th",
"14th",
"15th",
"16th",
"17th",
"18th",
"19th",
"20th",
"21st",
"22nd",
"23rd",
"24th",
"25th",
"26th",
"27th",
"28th",
"29th",
"30th",
"31st",
]
def say_hello(text):
greet = ["hi", "hey", "hola", "greetings", "sup", "hello", "howdy", "what's
good", "hey there", "ahoy"]
return ""
# def wiki_person(text):
# list_wiki = text.split()
# for i in range(0, len(list_wiki)):
# if i + 3 <= len(list_wiki) - 1 and list_wiki[i].lower() == "who" and list_wiki[i
+ 1].lower() == "is":
# return list_wiki[i + 2] + " " + list_wiki[i + 3]
def note(text):
date = datetime.datetime.now()
file_name = str(date).replace(":", "-") + "-note.txt"
with open(file_name, "w") as f:
f.write(text)
subprocess.Popen(["notepad.exe", file_name])
server.login("pranavkunadharaju@gmail.com", "PraKarMadAbh4!")
server.sendmail("pranavkunadharaju@gmail.com", to, content)
server.close()
# If modifying these scopes, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/calendar.readonly']
def google_calendar():
"""Shows basic usage of the Google Calendar API.
Prints the start and name of the next 10 events on the user's calendar.
"""
creds = None
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump((creds, token))
return service
if not events:
print('No upcoming events found.')
for event in events:
start = event['start'].get('dateTime', event['start'].get('date'))
events_today = (event['summary'])
start_time = str(start.split("T")[1].split("-")[0])
if int(start_time.split(':')[0]) < 12:
start_time = start_time + "am"
else:
start_time = str(int(start_time.split(":")[0]) - 12)
start_time = start_time + "pm"
talk(f'{events_today} at {start_time}')
# try:
# service = google_calendar()
# calendar_events(10, service)
# except:
# talk("Could not connect to the local internet connection. Please try again
later.")
# exit()
while True:
try:
text = rec_audio()
if call(text):
talk(say_hello(text))
elif "your name" in text or "who are you" in text or "define yourself"
in text:
print(" " + "I am Levi, your assistant!")
talk("I am Levi, your assistant")
elif "why do you exist" in text or "why are you a thing" in text:
print(" " + "Let's just say... it's a secret")
talk("Let's just say, it's a secret")
else:
talk("Sorry, this application or site is currently
unavailable.")
while True:
if keyboard.read_key() == "p":
print(" " + "Paused")
pygame.mixer.music.pause()
else:
ind = text.lower().split().index("play")
search = text.split()[ind + 1:]
print(" " + "Showing Results For " + str(search) + " On
Youtube")
talk("Showing Results For " + str(search) + " On Youtube")
webbrowser.open(
"https://www.youtube.com/results?search_query=" +
"+".join(search)
)
elif "search" in text:
ind = text.lower().split().index("search")
search = text.split()[ind + 1:]
print(" " + "Here's What I Found For " + str(search))
talk("Here's What I Found For " + str(search))
webbrowser.open(
"https://www.google.com/search?q=" + "+".join(search)
)
news = json.loads(response.text)
print(str(new["description"]), "\n")
talk(str(new["description"]))
engine.runAndWait()
elif "send message" in text or "send a message" in text:
account_sid = "ACe0d63b8ee765213241267d8411d641c7"
auth_token = "c51c81628617e6d9d7108ca2ef55b5ec"
client = Client(account_sid, auth_token)
print(message.sid)
print(" " + "Message sent successfully!")
talk("Message sent successfully")
elif "bad" in text or "upset" in text or " ok" in text or "okay" in text or
"not well" in text or "sad" in text:
print(" " + "Oh no, is there anything I can do to help?")
talk("Oh no, is there anything I can do to help?")
except:
talk("Sorry, I don't know that")