Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import time
- import turtle
- from gtts import gTTS
- from playsound import playsound
- # alias
- import speech_recognition as sr
- import os
- API_KEY = "e0ee5930-b48c-11eb-9a59-3d10624dc59a187780ad-8021-4e0b-b33c-0a183832aaf0"
- # Create a window
- window = turtle.Screen()
- window.setup(1200, 765)
- window.bgpic('background.png')
- window.title('ISS Smart Assistant')
- # Heading Turtle
- heading = turtle.Turtle()
- heading.penup()
- heading.color('black')
- heading.hideturtle()
- heading.goto(-50, 260)
- heading.write('Hello, I am ISS!', align='left', font=('Arial', 40, 'bold'))
- # Answer Turtle
- def speech_to_text():
- recognizer = sr.Recognizer()
- with sr.Microphone() as source:
- # red light
- speech = recognizer.listen(source)
- try:
- text = recognizer.recognize_google(speech)
- # green light
- # print the question
- return text
- except:
- # print 'sorry I did not get you'
- return 'Here is a fun fact instead: ' + fun_fact()
- def text_to_speech(text):
- language_code = 'en'
- speed = False
- speech = gTTS(text=text, lang=language_code, slow=speed)
- speech.save('speech.mp3')
- playsound('speech.mp3')
- os.remove('speech.mp3')
- def classify(text=None):
- if text is not None:
- question = speech_to_text()
- else:
- question = text
- question_turtle = turtle.Turtle()
- # get the question, and return the type of question
- if question == '' or question == None:
- return 'Error'
- url = 'https://machinelearningforkids.co.uk/api/scratch/' + API_KEY + '/classify/'
- parameters = {'data': question}
- response = requests.get(url, params=parameters)
- if response.status_code != 200:
- return 'Error: Request unsuccessful'
- else:
- data = response.json()
- top_match = data[0]
- category = top_match['class_name']
- confidence = top_match['confidence']
- if confidence < 40:
- category = 'Not sure'
- return category
- def astronauts_in_space():
- # get the names using api. like we did in level 2
- # make a string, with the names, and return it
- url = 'http://api.open-notify.org/astros.json'
- response = requests.get(url)
- data = response.json()
- astronauts = []
- for i in data['people']:
- astronauts.append(i['name'])
- reply = 'There are people in the ISS, like '
- for astronaut in astronauts:
- reply = reply + astronaut + ', '
- reply = reply[:-2]+'.'
- return reply
- def answer(category):
- # what reply should be given back to the user
- reply = ''
- if category == 'Introduction':
- reply = 'Hello, I am International Space Station, a large spacecraft orbiting around the Earth!'
- elif category == 'Purpose':
- reply = 'I act as a space-home for scientists living in the space. I am also a part-time laboratory.'
- elif category == 'Full_form':
- reply = 'My name is Station, International Space Station'
- elif category == 'Countries':
- reply = 'There are a total of 16 nations involved. Some of the notable ones are : USA, Canada, Japan, UK, Russia, France and Germany'
- elif category == 'Salutation':
- reply = 'Hey Human, I am ready for your questions...'
- elif category == 'Exit':
- reply = 'Goodbye!'
- time.sleep(4)
- # turtle.bye()
- return
- elif category == 'Birthday':
- reply = 'I cut my cake on 20th November every year, since 1998.'
- elif category == 'Not sure':
- reply = "I am actually not sure what you're trying to say"
- elif category == 'Astronauts':
- reply = astronauts_in_space()
- elif category == 'FunFact':
- reply = fun_fact()
- elif category == 'Visit':
- reply = visit()
- elif category == 'Current_Location':
- reply = current_location()
- print(reply)
- # text_to_speech(reply)
- def current_location():
- url = "http://api.open-notify.org/iss-now.json"
- # API Call
- response = requests.get(url)
- # conversion to json
- data = response.json()
- # print(data)
- latitude = data['iss_position']['latitude']
- longitude = data['iss_position']['longitude']
- timestamp = data['timestamp']
- latitude = float(latitude)
- longitude = float(longitude)
- # Access Token - LocationIQ pk.7510fb98384ba4f451338c713b61ef9f
- url = 'https://us1.locationiq.com/v1/reverse.php?key=pk.7510fb98384ba4f451338c713b61ef9f'
- # API Call
- # if we need to send some information for API call. it is sent via Parameters
- parameters = {
- 'lat': latitude,
- 'lon': longitude,
- 'format': 'json'
- }
- response = requests.get(url, params=parameters)
- data = response.json()
- # print(data)
- place = 'Ocean'
- if response.status_code == 200:
- place = data['address']['country']
- reply = f'I am orbiting 248 miles above the Earth. I am currently at {place}, at latitude:{latitude}, longitude:{longitude}'
- def visit():
- from datetime import datetime
- # pk.7510fb98384ba4f451338c713b61ef9f
- location = input('Tell me your location : ')
- url = 'https://us1.locationiq.com/v1/search.php?key=pk.7510fb98384ba4f451338c713b61ef9f'
- parameters = {'q': location, 'format': 'json'}
- response = requests.get(url, params=parameters)
- if response.status_code != 200:
- # something went wrong
- return "I don't seem to know this place. Here is a fun fact instead: " + fun_fact()
- else:
- data = response.json()
- latitude = float(data[0]['lat'])
- longitude = data[0]['lon']
- longitude = float(longitude)
- url = 'http://api.open-notify.org/iss-pass.json'
- parameters = {'lat': latitude, 'lon': longitude, 'n': 1}
- response = requests.get(url, params=parameters)
- data = response.json()
- risetime = data['response'][0]['risetime']
- passtime = datetime.fromtimestamp(risetime)
- return 'I will visit you at ' + str(passtime)
- def fun_fact():
- import random
- funfact = [
- "I take 15 orbits around the Earth and yet I don't get dizzy",
- "244 people (and counting) from 19 countries have visited the International Space Station",
- "The space station has been continuously occupied since November 2000",
- "More than 4.5 million lines of code have been written for the ISS",
- "More than 50 computers control the systems of the space station",
- "Six spaceships can be connected to the space station at once"
- ]
- return random.choice(fun_fact)
- def activate():
- r = sr.Recognizer()
- with sr.Microphone() as source:
- speech = r.listen(source)
- try:
- text = r.recognize_google(speech)
- category = classify(text)
- if category == 'Activate':
- category = classify()
- answer(category)
- else:
- pass
- except:
- pass
- # recursion
- activate()
- activate()
- # 2. it will wait for a keypress,
- # 3. Press spacebar, it will start listening to you
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement