ProgrammingBeginner Tutorial | Jarvis Chatbot - Python
Posted:

ProgrammingBeginner Tutorial | Jarvis Chatbot - PythonPosted:

SiDev
  • Summer 2023
Status: Offline
Joined: Dec 13, 20203Year Member
Posts: 287
Reputation Power: 567
Status: Offline
Joined: Dec 13, 20203Year Member
Posts: 287
Reputation Power: 567
Hey TTG,

My name is SiDev.
I am a recent Computer Science college graduate and I enjoy making discord bots and programming in my free time. I recently came across my old AI class project and figured I would turn it into a tutorial for you guys.

This tutorial will NOT show you how to get your python environment ready. If you need help doing that, I recommend following this tutorial first.




Step 1: Importing APIs/Libraries

import wolframalpha
import wikipedia
import PySimpleGUI as sg
import pyttsx3


This section of code allows our script to access wolframalpha and wikipedia to answer the questions we ask it later on. PySimpleGUI allows us to have a GUI for our script to interact with while it is running.





Step 2: Connecting to APIs/GUI setup

#connection to WolfRamAlphaAPI
client = wolframalpha.Client("J7RHHV-6LQA8GK9X2")

#initializing GUI layout / window / theme / Text-to-Speech Engine
sg.theme('Reds')
layout =[[sg.Text('Please enter your question.'), sg.InputText()],[sg.Button('Find Answer'), sg.Button('Cancel')]]
window = sg.Window('Jarvis | AI Final Project', layout)
engine = pyttsx3.init()


This section allows our connection to Wolframalpha to get our data. It also defines the settings for GUI such as size, theme, and text displayed.





Step 3: Loop Setup

#loop for window
while True:
    event, values = window.read()

    #if client wants to exit window
    if event in (None, 'Cancel'):
        break

    #when answer is typed / inputted
    try:
        wiki_res = wikipedia.summary(values[0], sentences=2)
        wolfram_res = next(client.query(values[0]).results).text
        engine.say(wolfram_res)
        sg.PopupNonBlocking("Short Answer: "+wolfram_res,"Summary Answer: "+wiki_res)


This section of the code allows for us to ask multiple questions to our bot in succession. It also defines how the bot will display the answer for each question via the popup window.





Step 4: Error Handling

 #error handling
    except wikipedia.exceptions.DisambiguationError:
        wolfram_res = next(client.query(values[0]).results).text
        engine.say(wolfram_res)
        sg.PopupNonBlocking(wolfram_res)
    except wikipedia.exceptions.PageError:
        wolfram_res = next(client.query(values[0]).results).text
        engine.say(wolfram_res)
        sg.PopupNonBlocking(wolfram_res)
    except:
        wiki_res = wikipedia.summary(values[0], sentences=2)
        engine.say(wiki_res)
        sg.PopupNonBlocking(wiki_res)


This section of the code helps with errors that may get thrown when the script comes across old/archived pages or broken links, etc.





Step 5: Finishing Touches


    engine.runAndWait()

    print (values[0])

window.close()


This last little block of code completes our chatbot. See below for complete code.





Full Finished Script

#importing APIs
import wolframalpha
import wikipedia
import PySimpleGUI as sg
import pyttsx3

#connection to WolfRamAlphaAPI
client = wolframalpha.Client("J7RHHV-6LQA8GK9X2")

#initializing GUI layout / window / theme / Text-to-Speech Engine
sg.theme('Reds')
layout =[[sg.Text('Please enter your question.'), sg.InputText()],[sg.Button('Find Answer'), sg.Button('Cancel')]]
window = sg.Window('Jarvis | AI Final Project', layout)
engine = pyttsx3.init()


#loop for window
while True:
    event, values = window.read()

    #if client wants to exit window
    if event in (None, 'Cancel'):
        break

    #when answer is typed / inputted
    try:
        wiki_res = wikipedia.summary(values[0], sentences=2)
        wolfram_res = next(client.query(values[0]).results).text
        engine.say(wolfram_res)
        sg.PopupNonBlocking("Short Answer: "+wolfram_res,"Summary Answer: "+wiki_res)

    #error handling
    except wikipedia.exceptions.DisambiguationError:
        wolfram_res = next(client.query(values[0]).results).text
        engine.say(wolfram_res)
        sg.PopupNonBlocking(wolfram_res)
    except wikipedia.exceptions.PageError:
        wolfram_res = next(client.query(values[0]).results).text
        engine.say(wolfram_res)
        sg.PopupNonBlocking(wolfram_res)
    except:
        wiki_res = wikipedia.summary(values[0], sentences=2)
        engine.say(wiki_res)
        sg.PopupNonBlocking(wiki_res)

    engine.runAndWait()

    print (values[0])

window.close()


Images


Last edited by SiDev ; edited 1 time in total

The following 1 user thanked SiDev for this useful post:

Chosen (02-15-2023)
#2. Posted:
Chosen
  • Athlete
Status: Offline
Joined: Oct 28, 201013Year Member
Posts: 10,370
Reputation Power: 62736
Motto: Discord: trustedseller
Motto: Discord: trustedseller
Status: Offline
Joined: Oct 28, 201013Year Member
Posts: 10,370
Reputation Power: 62736
Motto: Discord: trustedseller
Very nice tutorial SiDev! Keep them coming
#3. Posted:
SiDev
  • Summer 2023
Status: Offline
Joined: Dec 13, 20203Year Member
Posts: 287
Reputation Power: 567
Status: Offline
Joined: Dec 13, 20203Year Member
Posts: 287
Reputation Power: 567
Chosen wrote Very nice tutorial SiDev! Keep them coming


Appreciate it boss. Will gladly keep 'em coming!
Users browsing this topic: None
Jump to:


RECENT POSTS

HOT TOPICS