Who is speaking?
Anand Chitipothu
@anandology
• building a data science platform at
@rorodata
• advanced programming courses at
@pipalacademy
Slide 3
Slide 3 text
The problem
How to expose a function as an API for others to use?
Slide 4
Slide 4 text
Why?
• To use it in a different environment
• Loose coupling
Slide 5
Slide 5 text
Use cases
• Deploy a machine learning model
• preprocess an image
• live price check
Slide 6
Slide 6 text
Challenges
• Requires writing a web application
• What about authentication?
• How to do data validation?
• How I need write a client library too?
Slide 7
Slide 7 text
Welcome to Firefly
Deploying functions made easy!
Slide 8
Slide 8 text
Code
Write your function:
# sq.py
def square(n):
return n*n
Slide 9
Slide 9 text
Run
Start web service:
$ firefly sq.square
[INFO] Starting gunicorn 19.7.1
[INFO] Listening at: http://127.0.0.1:8000
...
Slide 10
Slide 10 text
Use
And use it with a client.
>>> from firefly.client import Client
>>> client = Client("http://127.0.0.1:8000")
>>> client.square(n=4)
16
Slide 11
Slide 11 text
Behind the scenes, it is a RESTful API.
$ curl -d '{"n": 4}' http://127.0.0.1:8000/square
16
And supports any JSON-friendly datatype.
Slide 12
Slide 12 text
More practical example
Deploying a machine learning model.
# model.py
import pickle
model = pickle.load('model.pkl')
def predict(features):
result = model.predict(features])
return int(result[0])
Slide 13
Slide 13 text
Run the server using:
$ firefly model.predict
...
And use it in the client:
>>> remote_model = Client("http://localhost:8080/")
>>> remote_model.predict(features=[5.9, 3, 5.1, 1.8]))
2
Slide 14
Slide 14 text
Authentication
Firefly has built-in support for autentication.
$ firefly --token abcd1234 sq.square
...
The client must pass the same token to autenticate it.
>>> client = Client("http://127.0.0.1:8000", auth_token="abcd1234")
>>> client.square(n=4)
16
Slide 15
Slide 15 text
Upcoming Features...
• supporting other input and output content-types in addition
to json. (for example, a function to resize an image)
• validation using type annotations
• caching support
Slide 16
Slide 16 text
It's open source!
https://github.com/rorodata/firefly
To install:
pip install firefly-python