Advertisement
z3xt5r

Untitled

Jul 24th, 2021
1,237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.17 KB | None | 0 0
  1. import requests
  2. import time
  3. import turtle
  4. from gtts import gTTS
  5. from playsound import playsound
  6. # alias
  7. import speech_recognition as sr
  8.  
  9. import os
  10.  
  11. API_KEY = "e0ee5930-b48c-11eb-9a59-3d10624dc59a187780ad-8021-4e0b-b33c-0a183832aaf0"
  12.  
  13. # Create a window
  14. window = turtle.Screen()
  15. window.setup(1200, 765)
  16. window.bgpic('background.png')
  17. window.title('ISS Smart Assistant')
  18.  
  19. # Heading Turtle
  20. heading = turtle.Turtle()
  21. heading.penup()
  22. heading.color('black')
  23. heading.hideturtle()
  24. heading.goto(-50, 260)
  25. heading.write('Hello, I am ISS!', align='left', font=('Arial', 40, 'bold'))
  26.  
  27.  
  28.  
  29. # Answer Turtle
  30.  
  31. def speech_to_text():
  32.     recognizer = sr.Recognizer()
  33.     with sr.Microphone() as source:
  34.         # red light
  35.         speech = recognizer.listen(source)
  36.         try:
  37.  
  38.             text = recognizer.recognize_google(speech)
  39.             # green light
  40.             # print the question
  41.             return text
  42.         except:
  43.             # print 'sorry I did not get you'
  44.             return 'Here is a fun fact instead: ' + fun_fact()
  45.  
  46.  
  47. def text_to_speech(text):
  48.     language_code = 'en'
  49.     speed = False
  50.  
  51.     speech = gTTS(text=text, lang=language_code, slow=speed)
  52.     speech.save('speech.mp3')
  53.     playsound('speech.mp3')
  54.     os.remove('speech.mp3')
  55.  
  56.  
  57. def classify(text=None):
  58.     if text is not None:
  59.         question = speech_to_text()
  60.     else:
  61.         question = text
  62.     question_turtle = turtle.Turtle()
  63.     # get the question, and return the type of question
  64.     if question == '' or question == None:
  65.         return 'Error'
  66.  
  67.     url = 'https://machinelearningforkids.co.uk/api/scratch/' + API_KEY + '/classify/'
  68.     parameters = {'data': question}
  69.  
  70.     response = requests.get(url, params=parameters)
  71.  
  72.     if response.status_code != 200:
  73.         return 'Error: Request unsuccessful'
  74.     else:
  75.         data = response.json()
  76.         top_match = data[0]
  77.         category = top_match['class_name']
  78.         confidence = top_match['confidence']
  79.         if confidence < 40:
  80.             category = 'Not sure'
  81.         return category
  82.  
  83.  
  84. def astronauts_in_space():
  85.     # get the names using api. like we did in level 2
  86.     # make a string, with the names, and return it
  87.     url = 'http://api.open-notify.org/astros.json'
  88.     response = requests.get(url)
  89.     data = response.json()
  90.     astronauts = []
  91.     for i in data['people']:
  92.         astronauts.append(i['name'])
  93.  
  94.     reply = 'There are people in the ISS, like '
  95.     for astronaut in astronauts:
  96.         reply = reply + astronaut + ', '
  97.     reply = reply[:-2]+'.'
  98.     return reply
  99.  
  100.  
  101. def answer(category):
  102.     # what reply should be given back to the user
  103.     reply = ''
  104.  
  105.     if category == 'Introduction':
  106.         reply = 'Hello, I am International Space Station, a large spacecraft orbiting around the Earth!'
  107.  
  108.     elif category == 'Purpose':
  109.         reply = 'I act as a space-home for scientists living in the space. I am also a part-time laboratory.'
  110.  
  111.     elif category == 'Full_form':
  112.         reply = 'My name is Station, International Space Station'
  113.  
  114.     elif category == 'Countries':
  115.         reply = 'There are a total of 16 nations involved. Some of the notable ones are : USA, Canada, Japan, UK, Russia, France and Germany'
  116.  
  117.     elif category == 'Salutation':
  118.         reply = 'Hey Human, I am ready for your questions...'
  119.  
  120.     elif category == 'Exit':
  121.         reply = 'Goodbye!'
  122.         time.sleep(4)
  123.         # turtle.bye()
  124.         return
  125.  
  126.     elif category == 'Birthday':
  127.         reply = 'I cut my cake on 20th November every year, since 1998.'
  128.  
  129.     elif category == 'Not sure':
  130.         reply = "I am actually not sure what you're trying to say"
  131.  
  132.     elif category == 'Astronauts':
  133.         reply = astronauts_in_space()
  134.  
  135.     elif category == 'FunFact':
  136.         reply = fun_fact()
  137.  
  138.     elif category == 'Visit':
  139.         reply = visit()
  140.  
  141.     elif category == 'Current_Location':
  142.         reply = current_location()
  143.  
  144.     print(reply)
  145.     # text_to_speech(reply)
  146.  
  147.  
  148. def current_location():
  149.     url = "http://api.open-notify.org/iss-now.json"
  150.     # API Call
  151.     response = requests.get(url)
  152.     # conversion to json
  153.     data = response.json()
  154.     # print(data)
  155.     latitude = data['iss_position']['latitude']
  156.     longitude = data['iss_position']['longitude']
  157.     timestamp = data['timestamp']
  158.     latitude = float(latitude)
  159.     longitude = float(longitude)
  160.  
  161.     # Access Token - LocationIQ pk.7510fb98384ba4f451338c713b61ef9f
  162.     url = 'https://us1.locationiq.com/v1/reverse.php?key=pk.7510fb98384ba4f451338c713b61ef9f'
  163.  
  164.     # API Call
  165.     # if we need to send some information for API call. it is sent via Parameters
  166.     parameters = {
  167.         'lat': latitude,
  168.         'lon': longitude,
  169.         'format': 'json'
  170.     }
  171.     response = requests.get(url, params=parameters)
  172.     data = response.json()
  173.     # print(data)
  174.     place = 'Ocean'
  175.     if response.status_code == 200:
  176.         place = data['address']['country']
  177.  
  178.     reply = f'I am orbiting 248 miles above the Earth. I am currently at {place}, at latitude:{latitude}, longitude:{longitude}'
  179.  
  180.  
  181. def visit():
  182.     from datetime import datetime
  183.     # pk.7510fb98384ba4f451338c713b61ef9f
  184.     location = input('Tell me your location : ')
  185.     url = 'https://us1.locationiq.com/v1/search.php?key=pk.7510fb98384ba4f451338c713b61ef9f'
  186.     parameters = {'q': location, 'format': 'json'}
  187.  
  188.     response = requests.get(url, params=parameters)
  189.  
  190.     if response.status_code != 200:
  191.         # something went wrong
  192.         return "I don't seem to know this place. Here is a fun fact instead: " + fun_fact()
  193.     else:
  194.         data = response.json()
  195.         latitude = float(data[0]['lat'])
  196.  
  197.         longitude = data[0]['lon']
  198.         longitude = float(longitude)
  199.  
  200.         url = 'http://api.open-notify.org/iss-pass.json'
  201.         parameters = {'lat': latitude, 'lon': longitude, 'n': 1}
  202.         response = requests.get(url, params=parameters)
  203.         data = response.json()
  204.         risetime = data['response'][0]['risetime']
  205.         passtime = datetime.fromtimestamp(risetime)
  206.         return 'I will visit you at ' + str(passtime)
  207.  
  208.  
  209. def fun_fact():
  210.     import random
  211.     funfact = [
  212.         "I take 15 orbits around the Earth and yet I don't get dizzy",
  213.         "244 people (and counting) from 19 countries have visited the International Space Station",
  214.         "The space station has been continuously occupied since November 2000",
  215.         "More than 4.5 million lines of code have been written for the ISS",
  216.         "More than 50 computers control the systems of the space station",
  217.         "Six spaceships can be connected to the space station at once"
  218.     ]
  219.     return random.choice(fun_fact)
  220.  
  221.  
  222. def activate():
  223.     r = sr.Recognizer()
  224.     with sr.Microphone() as source:
  225.         speech = r.listen(source)
  226.         try:
  227.             text = r.recognize_google(speech)
  228.             category = classify(text)
  229.             if category == 'Activate':
  230.                 category = classify()
  231.                 answer(category)
  232.             else:
  233.                 pass
  234.         except:
  235.             pass
  236.     # recursion
  237.     activate()
  238.  
  239. activate()
  240.  
  241. # 2. it will wait for a keypress,
  242. # 3. Press spacebar, it will start listening to you
  243.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement