-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.py
99 lines (78 loc) · 3.58 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Sep 22 00:16:11 2019
@author: wumeiqi
"""
#%% Information of appliccant
YOB = 1997 # Year of Birth
Income = 1000 # Annnual Income
address='Cornell University, USA' # Address
Apl_Year = 2019 # Year of Application
gender = 0 # 0 male, 1 female
#%% Import packages
import urllib3, requests, json
import urllib.request, urllib.parse, urllib.error
import json
import ssl
import json
#%% Using google place API to extract the longitude and lagitude of address
def get_LonLat(address):
# ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode =ssl.CERT_NONE
api_key = ''
serviceurl = 'https://maps.googleapis.com/maps/api/place/textsearch/json?'
# address='Cornell University, USA'
parms = dict()
parms['query'] = address
parms['key'] = api_key
url = serviceurl+urllib.parse.urlencode(parms)
# print('Retrieving', url)
uh = urllib.request.urlopen(url, context=ctx)
data = uh.read().decode() #### data is a string
# print('Retrieved', len(data), 'characters', data[:20].replace('\n', ' '))
try:
js = json.loads(data) #### js is a dict
except:
print(data) # print in case unicode causes an error
if 'status' not in js or (js['status']!='OK' and js['status']!='ZERO_RESULTS'):
print('==== Failure To Retrieve ====')
print(data)
js = json.loads(str(data))
lon = js['results'][0]['geometry']['location']['lng']
lat = js['results'][0]['geometry']['location']['lat']
# print('Longitude : ', lon)
# print('Latitude : ', lat)
return([lon, lat])
#%% Call IBM API
apikey= ""
# Request iam_token
url = "https://iam.bluemix.net/oidc/token"
headers = { "Content-Type" : "application/x-www-form-urlencoded" }
data = "apikey=" + apikey + "&grant_type=urn:ibm:params:oauth:grant-type:apikey"
IBM_cloud_IAM_uid = "bx"
IBM_cloud_IAM_pwd = "bx"
response = requests.post( url, headers=headers, data=data, auth=( IBM_cloud_IAM_uid, IBM_cloud_IAM_pwd ) )
iam_token = response.json()["access_token"]
# instance_id
ml_instance_id = "9d0fcc7a-39a9-4874-a1fb-cdc5df9b3901"
# Type in the values to be scored here:
# ["YOB", "Income", "Lon", "Lat", "Lon_lat", "Apl_Year", "gender"]
location = get_LonLat(address)
array_of_values_to_be_scored = [YOB, Income, location[0], location[1],
str(location[0])+', '+str(location[1]), Apl_Year, gender]
# NOTE: generate iam_token and retrieve ml_instance_id based on provided documentation
header = {'Content-Type': 'application/json', 'Authorization': 'Bearer ' + iam_token, 'ML-Instance-ID': ml_instance_id}
# NOTE: manually define and pass the array(s) of values to be scored in the next line
#payload_scoring = {"input_data": [{"fields": ["YOB", "Income", "Lon", "Lat", "Lon_lat", "Apl_Year", "gender"],
# "values": [array_of_values_to_be_scored, another_array_of_values_to_be_scored]}]}
payload_scoring = {"input_data": [{"fields": ["YOB", "Income", "Lon", "Lat", "Lon_lat", "Apl_Year", "gender"],
"values": [array_of_values_to_be_scored]}]}
response_scoring = requests.post('https://us-south.ml.cloud.ibm.com/v4/deployments/700e82e2-463e-4c2d-90fe-44acf4a2fbf6/predictions',
json=payload_scoring, headers=header)
print("Scoring response")
scoring = json.loads(response_scoring.text)
print('The possibility of acceptance is: ')
print(scoring['predictions'][0]['values'][0][1][1])