-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
31 lines (27 loc) · 955 Bytes
/
main.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
"""Main module for setting up google cloud function
https://cloud.google.com/functions/docs/writing/
"""
import os
import json
from geolocalizer import Geolocalizer
def geolocalize_map(request):
"""
Args:
request (flask.Request): HTTP request object
JSON example: {"uri": "https://i.stack.imgur.com/WiDpa.jpg"}
"""
request_json = request.get_json()
if request.args and 'uri' in request.args:
uri = request.args.get('uri')
elif request_json and 'uri' in request_json:
uri = request_json['uri']
else:
return f'No uri given!'
# Environment variables when create GCF
API_KEY = os.environ.get('GEOLOCALIZATION_API_KEY', None)
if not API_KEY:
return f'No API_KEY found'
# TODO(zhouwubai): make it singleton
geolocalizer = Geolocalizer(API_KEY)
text, candidates = geolocalizer.geolocalize(uri)
return json.dumps({"text": text, "candidates": candidates})