8000 Adding a weather microservice by saijel · Pull Request #166 · claritylab/lucida · GitHub
[go: up one dir, main page]

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lucida/Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SUBDIRS=commandcenter imagematching questionanswering calendar speechrecognition djinntonic
SUBDIRS=commandcenter imagematching questionanswering calendar speechrecognition djinntonic weather

include ../Makefile.common
include ../Makefile.common
48 changes: 25 additions & 23 deletions lucida/commandcenter/controllers/Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,31 @@

# Pre-configured services.
# The ThriftClient assumes that the following services are running.
# Host IP addresses are resolved dynamically:
# Host IP addresses are resolved dynamically:
# either set by Kubernetes or localhost.
SERVICES = {
'IMM' : Service('IMM', 8082, 'image', 'image'),
'QA' : Service('QA', 8083, 'text', 'text'),
'CA' : Service('CA', 8084, 'text', None),
'IMC' : Service('IMC', 8085, 'image', None),
'FACE' : Service('FACE', 8086, 'image', None),
'DIG' : Service('DIG', 8087, 'image', None),
'ENSEMBLE' : Service('ENSEMBLE', 9090, 'text', None)
}
SERVICES = {
'IMM' : Service('IMM', 8082, 'image', 'image'),
'QA' : Service('QA', 8083, 'text', 'text'),
'CA' : Service('CA', 8084, 'text', None),
'IMC' : Service('IMC', 8085, 'image', None),
'FACE' : Service('FACE', 8086, 'image', None),
'DIG' : Service('DIG', 8087, 'image', None),
'ENSEMBLE' : Service('ENSEMBLE', 9090, 'text', None),
'WE' : Service('WE', 8088, 'text', None),
}

# Map from input type to query classes and services needed by each class.
CLASSIFIER_DESCRIPTIONS = {
'text' : { 'class_QA' : Graph([Node('QA')]) ,
'class_CA' : Graph([Node('CA')]) },
'image' : { 'class_IMM' : Graph([Node('IMM')]),
'class_IMC' : Graph([Node('IMC')]),
'class_FACE' : Graph([Node('FACE')]),
'class_DIG' : Graph([Node('DIG')]) },
'text_image' : { 'class_QA': Graph([Node('QA')]),
'class_IMM' : Graph([Node('IMM')]),
'class_IMM_QA' : Graph([Node('IMM', [1]), Node('QA')]),
'class_IMC' : Graph([Node('IMC')]),
'class_FACE' : Graph([Node('FACE')]),
'class_DIG' : Graph([Node('DIG')]) } }
CLASSIFIER_DESCRIPTIONS = {
'text' : { 'class_QA' : Graph([Node('QA')]),
'class_CA' : Graph([Node('CA')]),
'class_WE' : Graph([Node('WE')]) },
'image' : { 'class_IMM' : Graph([Node('IMM')]),
'class_IMC' : Graph([Node('IMC')]),
'class_FACE' : Graph([Node('FACE')]),
'class_DIG' : Graph([Node('DIG')]) },
'text_image' : { 'class_QA': Graph([Node('QA')]),
'class_IMM' : Graph([Node('IMM')]),
'class_IMM_QA' : Graph([Node('IMM', [1]), Node('QA')]),
'class_IMC' : Graph([Node('IMC')]),
'class_FACE' : Graph([Node('FACE')]),
'class_DIG' : Graph([Node('DIG')]) } }
97 changes: 97 additions & 0 deletions lucida/commandcenter/data/class_WE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
San Francisco, CA
Austin, TX
San Diego, CA
Flower Mound, TX
Tucson, AZ
New York City, NY
Lousiville, KY
Denver, CO
Atlanta, GA
Memphis, TN
Chicago, IL
Orlando, FL
New Orleans, LA
Montgomery, AL
Juneau, AK
Phoenix, AZ
Little Rock, AR
Sacramento, CA
Hartford, CT
Dover, DE
Washington, DC
Tallahassee, Florida
Honolulu, HI
What's the weather in San Francisco, CA?
What's the weather in Austin, TX?
What's the weather in San Diego, CA?
What's the weather in Flower Mound, TX?
What's the weather in Tucson, AZ?
What's the weather in New York City, NY?
What's the weather in Lousiville, KY?
What's the weather in Denver, CO?
What's the weather in Atlanta, GA?
What's the weather in Memphis, TN?
What's the weather in Chicago, IL?
What's the weather in Orlando, FL?
What's the weather in New Orleans, LA?
What's the weather in Montgomery, AL?
What's the weather in Juneau, AK?
What's the weather in Phoenix, AZ?
What's the weather in Little Rock, AR?
What's the weather in Sacramento, CA?
What's the weather in Hartford, CT?
What's the weather in Dover, DE?
What's the weather in Washington, DC?
What's the weather in Tallahassee, Florida?
What's the weather like in San Francisco, CA?
What's the weather like in Austin, TX?
What's the weather like in San Diego, CA?
What's the weather like in Flower Mound, TX?
What's the weather like in Tucson, AZ?
What's the weather like in New York City, NY?
What's the weather like in Lousiville, KY?
What's the weather like in Denver, CO?
What's the weather like in Atlanta, GA?
What's the weather like in Memphis, TN?
What's the weather like in Chicago, IL?
What's the weather like in Orlando, FL?
What's the weather like in New Orleans, LA?
What's the weather like in Montgomery, AL?
What's the weather like in Juneau, AK?
What's the weather like in Phoenix, AZ?
What's the weather like in Little Rock, AR?
What's the weather like in Sacramento, CA?
What's the weather like in Hartford, CT?
What's the weather like in Dover, DE?
What's the weather like in Washington, DC?
What's the weather like in Tallahassee, Florida?
What's the weather in Utah?
What's the weather in Texas?
What's the weather in Oklahoma?
What's the weather in South America?
What's the weather in Washington?
What's the weather in New York?
What's the weather in Kentucky?
What's the weather in Colorado?
What's the weather in Georgia?
What's the weather in Tennessee?
What's the weather in Illinois?
What's the weather in Oregon?
What's the weather in Louisiana?
What's the weather in Alabama?
What's the weather in India?
What's the weather in Arizona?
What's the weather in Arkansas?
What's the weather in California?
What's the weather in Maryland?
What's the weather in New Mexico?
What's the weather in Ohio?
What's the weather in Florida?
How hot is it in San Francisco, CA?
How cold is it in Austin, TX?
How warm is it in San Diego, CA?
How cool is it in Flower Mound, TX?
How hot is it in San Francisco?
How cold is it in Austin?
How warm is it in California?
How cool is it in Texas?
2 changes: 2 additions & 0 deletions lucida/weather/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
lucidaservice
lucidatypes
21 changes: 21 additions & 0 deletions lucida/weather/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
all: thrift

thrift:
@if [ ! -d "lucidaservice" ]; then \
thrift --gen py ../lucidaservice.thrift; \
thrift --gen py ../lucidatypes.thrift; \
cd gen-py; \
mv * ..; \
cd ..; \
rmdir gen-py; \
rm __init__.py; \
fi

clean:
rm -rf lucidaservice lucidatypes

start_server:
cd server; python WeatherServer.py

start_test:
cd client; python WeatherClient.py
31 changes: 31 additions & 0 deletions lucida/weather/client/WeatherClient.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env python

import sys
sys.path.append('../')

from lucidatypes.ttypes import QueryInput, QuerySpec
from lucidaservice import LucidaService

from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol

port = 8088

LUCID = "Clinc"
query_input_data = "What's the weather in Ann Arbor, MI?"
query_input = QueryInput(type="query", data=[query_input_data])
query_spec = QuerySpec(content=[query_input])

# Initialize thrift objects
transport = TTransport.TFramedTransport(TSocket.TSocket("localhost", port))
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = LucidaService.Client(protocol)

transport.open()
print "/////....Connecting to Weather..../////"
results = client.infer(LUCID, query_spec)
print "/////Result/////"
print "%s" % results
transport.close()
91 changes: 91 additions & 0 deletions lucida/weather/server/WeatherServer.py
10000
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/usr/bin/env python

import sys
sys.path.append('../')

from lucidatypes.ttypes import QuerySpec
from lucidaservice import LucidaService

from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer

import urllib2
import json
import urllib

port = 8088
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we really need to rethink how all the ports are set, it's super messy to have them nested deep into the code. then you need to worry about setting them correctly within the CMD center as well

owm_api_key = "362537b891e6df03a316e82565fe4df3"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ew Magic Numbers, can you move this to a config and put in the README that it needs to be set or something? Should be a "secret"

wu_api_key = "ff76e8e80c802d46"

class WeatherHandler(LucidaService.Iface):
"""
Do nothing
Parameters:
- LUCID: ID of Lucida user
- spec: spec
"""
def create(self, LUCID, spec):
return

"""
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these docstrings should be inside the function header, check how we do it internally

Do nothing
Parameters:
- LUCID: ID of Lucida user
- knowledge: knowledge
"""
def learn(self, LUCID, knowledge):
return

"""
Determine the weather based off input
Parameters:
- LUCID: ID of Lucida user
- query: query
"""
def infer(self, LUCID, query):
result = "No weather found for %s" % query.content[0].data[0]
url_location = urllib.quote_plus(query.content[0].data[0])

try:
# weather from Weather Underground
f = urllib2.urlopen('http://api.wunderground.com/api/%s/conditions/q/%s.json' % (wu_api_key, url_location) )
@jhauswald jhauswald Apr 15, 2017 •
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be a global

json_string = f.read()
parsed_json = json.loads(json_string)
if 'error' not in parsed_json['response'] and 'current_observation' in parsed_json:
weather = parsed_json['current_observation']['weather']
temp = parsed_json['current_observation']['temperature_string']
city = parsed_json['current_observation']['display_location']['full']
result = "Current weather in %s is %s %s" % (city, weather, temp)
print "From Weather Underground: %s" % result
else:
# weather from Open Weather Map
f = urllib2.urlopen('http://api.openweathermap.org/data/2.5/weather?q=%s&appid=%s&units=imperial&type=like' % (url_location, owm_api_key))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

global or some config

json_string = f.read()
parsed_json = json.loads(json_string)
if 'weather' in parsed_json and 'main' in parsed_json and 'name' in parsed_json:
weather = parsed_json['weather'][0]['main']
temp = parsed_json['main']['temp']
city = parsed_json['name']
result = "Current weather in %s is %s, %s F" % (city, weather, temp)
print "From Open Weather Map: %s" % result
f.close()
except urllib2.HTTPError as err:
if err == "404":
print result

return result

# Set handler to our implementation
handler = WeatherHandler()

processor = LucidaService.Processor(handler)
transport = TSocket.TServerSocket(port=port)
tfactory = TTransport.TFramedTransportFactory()
pfactory = TBinaryProtocol.TBinaryProtocolFactory()

server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)

print "WE at port %d" % port
server.serve()
23 changes: 23 additions & 0 deletions tools/deploy/we-controller.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
apiVersion: v1
kind: ReplicationController
metadata:
labels:
name: we
name: we-controller
spec:
replicas: 1
selector:
name: we
template:
metadata:
labels:
name: we
spec:
containers:
- image: claritylab/lucida:v2.2
command: ['/bin/sh', '-c']
args: ['cd $LUCIDAROOT/weather/server/ && python WeatherServer.py']
name: we
ports:
- containerPort: 8088
name: thrift
14 changes: 14 additions & 0 deletions tools/deploy/we-service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: v1
kind: Service
metadata:
name: we
labels:
name: we
spec:
type: ClusterIP
ports:
- port: 8088
targetPort: 8088
protocol: TCP
selector:
name: we
1 change: 1 addition & 0 deletions tools/start_all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ declare -a services=(
"$(pwd)/../lucida/imagematching/opencv_imm"
"$(pwd)/../lucida/questionanswering/OpenEphyra"
"$(pwd)/../lucida/calendar"
"$(pwd)/../lucida/weather"
"$(pwd)/../lucida/djinntonic/dig"
"$(pwd)/../lucida/djinntonic/face"
"$(pwd)/../lucida/djinntonic/imc")
Expand Down
0