From 90caf68299ff7be1344dcd213b723a181824910e Mon Sep 17 00:00:00 2001 From: Raj Aryan Singh Date: Tue, 24 Oct 2023 01:50:55 +0530 Subject: [PATCH 1/2] improved the calculator function a bit --- Assistant.py | 52 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/Assistant.py b/Assistant.py index 0cdae0e..f180e28 100644 --- a/Assistant.py +++ b/Assistant.py @@ -1,6 +1,7 @@ from gtts import gTTS from playsound import playsound import random +import pyttsx3 #===============================================INITIALIZATION========================================================== initialization = "Initializing data" language = 'en' @@ -35,12 +36,23 @@ def help(): "3)nofunc\n" "4)calculator\n") def calculator(): - print("calculator") + num1 = int(input("enter first number= ")) + num2 = int(input ("enter second number= ")) + op = input("+,-,*,/ ? = ") + if op == "+": + print (num1+num2) + elif op == "-": + print(num1-num2) + elif op == "*": + print(num1*num2) + elif op == "/": + print(num1/num2) + else: + print("Please choose from the given options only (+,-,*,/)") def greet(yo): - name = raw_input("What's your name: ") - return yo + name -print greet("hi! ") + name = input("What's your name: ") + speak(yo + name) def speak(audio): @@ -63,22 +75,22 @@ def speak(audio): #================================================Asking the user for input============================================== while True: - r1 = random.randint(1, 10000000) - r2 = random.randint(1, 10000000) - task = input('What should I do for you?') - mytask = gTTS(text="Received Query "+task , lang=language , slow=False) - mytask.save(str(r1)+("task")+str(r2)+(".mp3")) - playsound(str(r1)+("task")+str(r2)+(".mp3")) -if task==("time"): - time() - elif task==("calculate"): - calculator() - elif task==("help"): - help() - elif task==("calculator"): - calculator() - else: - nofunc() + r1 = random.randint(1, 10000000) + r2 = random.randint(1, 10000000) + task = input('What should I do for you?') + mytask = gTTS(text="Received Query " + task, lang=language, slow=False) + mytask.save(str(r1) + "task" + str(r2) + ".mp3") + playsound(str(r1) + "task" + str(r2) + ".mp3") + if task == "time": + time() + elif task == "calculate": + calculator() + elif task == "help": + help() + elif task == "calculator": + calculator() + else: + nofunc() From 3b0c9e6860812d6479dd2a52d59fe0199a8afd58 Mon Sep 17 00:00:00 2001 From: Raj Aryan Singh Date: Tue, 24 Oct 2023 02:11:37 +0530 Subject: [PATCH 2/2] created a search function for web search and wikipedia fucntion for wikipedia results --- Assistant.py | 91 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 62 insertions(+), 29 deletions(-) diff --git a/Assistant.py b/Assistant.py index f180e28..692af15 100644 --- a/Assistant.py +++ b/Assistant.py @@ -2,82 +2,112 @@ from playsound import playsound import random import pyttsx3 -#===============================================INITIALIZATION========================================================== + +from langchain.tools import DuckDuckGoSearchRun +from langchain.utilities import WikipediaAPIWrapper + +# ===============================================INITIALIZATION========================================================== initialization = "Initializing data" -language = 'en' +language = "en" myobj = gTTS(text=initialization, lang=language, slow=False) myobj.save("initialization.mp3") playsound("initialization.mp3") import datetime -#==============================================Asking the user for input================================================ + +# ==============================================Asking the user for input================================================ askinit = gTTS(text="What should I do for you?", lang=language, slow=False) askinit.save("askinit.mp3") playsound("askinit.mp3") -#============================================Functions================================================================== + + +# ============================================Functions================================================================== def time(): timern = datetime.datetime.now() - mytimern= gTTS(text="Please see the screen to see the time", lang=language, slow=False) + mytimern = gTTS( + text="Please see the screen to see the time", lang=language, slow=False + ) mytimern.save("mytimern.mp3") playsound("mytimern.mp3") print(timern) + + def nofunc(): - nofuncstr = gTTS(text="Sorry we don't provide this function right now", lang=language, slow=False) + nofuncstr = gTTS( + text="Sorry we don't provide this function right now", lang=language, slow=False + ) nofuncstr.save("nofuncstr.mp3") playsound("nofuncstr.mp3") print("Sorry we don't provide this function right now") + def help(): - helpstr=gTTS(text="List of available commands are Time help calculator nofunc", lang=language, slow=False) + helpstr = gTTS( + text="List of available commands are Time help calculator nofunc", + lang=language, + slow=False, + ) helpstr.save("help.mp3") playsound("help.mp3") - print("List of all commands\n" - "1)Help\n" - "2)Time\n" - "3)nofunc\n" - "4)calculator\n") + print("List of all commands\n" "1)Help\n" "2)Time\n" "3)nofunc\n" "4)calculator\n 5)Ask Wikipedia(wikipedia)\n 5)Search Web(search)\n ") + + def calculator(): num1 = int(input("enter first number= ")) - num2 = int(input ("enter second number= ")) + num2 = int(input("enter second number= ")) op = input("+,-,*,/ ? = ") if op == "+": - print (num1+num2) + print(num1 + num2) elif op == "-": - print(num1-num2) + print(num1 - num2) elif op == "*": - print(num1*num2) + print(num1 * num2) elif op == "/": - print(num1/num2) + print(num1 / num2) else: print("Please choose from the given options only (+,-,*,/)") + +def wiki(): + query = input("Ask me Anything: ") + wikipedia = WikipediaAPIWrapper() + result = wikipedia.run(query) + print(result) + + +def search(): + query = input("Ask me Anything: ") + search = DuckDuckGoSearchRun() + result = search.run(query) + print(result) + def greet(yo): name = input("What's your name: ") - speak(yo + name) + speak(yo + name) + def speak(audio): - engine = pyttsx3.init() # getter method(gets the current value # of engine property) - voices = engine.getProperty('voices') - + voices = engine.getProperty("voices") + # setter method .[0]=male voice and # [1]=female voice in set Property. - engine.setProperty('voice', voices[0].id) - + engine.setProperty("voice", voices[0].id) + # Method for the speaking of the assistant - engine.say(audio) - + engine.say(audio) + # Blocks while processing all the currently # queued commands engine.runAndWait() -#================================================Asking the user for input============================================== +# ================================================Asking the user for input============================================== while True: r1 = random.randint(1, 10000000) r2 = random.randint(1, 10000000) - task = input('What should I do for you?') + task = input("What should I do for you?") mytask = gTTS(text="Received Query " + task, lang=language, slow=False) mytask.save(str(r1) + "task" + str(r2) + ".mp3") playsound(str(r1) + "task" + str(r2) + ".mp3") @@ -90,7 +120,10 @@ def speak(audio): help() elif task == "calculator": calculator() + elif task == "wikipedia": + wiki() + + elif task == "search": + wiki() else: nofunc() - -