firefly is a function as a service framework which can be used to deploy functions as a web service. In turn, the functions can be accessed over a REST based API or RPC like client. In short, firefly puts your functions on steroids.

Machine Learning models can even be deployed over firefly.


firefly can be installed by using pip as:

$ pip install firefly-python

You can check the installation by using:

$ firefly -h

Basic Usage

Create a simple python function:


def square(n):
  return n**2

And then this function can run through firefly by the following:

$ firefly funcs.square

This function is now accessible at . An inbuilt Client is also provided to communicate with the firefly server. Example usage of the client:

>>> import firefly
>>> client = firefly.Client("")
>>> client.square(n=4)

Besides that, you can also use curl or any software through which you can do a POST request to the endpoint.

$ curl -d '{"n": 4}'

firefly supports for any number of functions. You can pass multiple functions as:

$ firefly funcs.square funcs.cube

The functions square and cube can be accessed at and respectively.


firefly also supports token-based authentication. You will need to pass a token through the CLI or the config file.

$ # CLI Usage
$ firefly --token abcd1234 funcs.square

The token now needs to be passed with each request.

>>> import firefly
>>> client = firefly.Client("", auth_token="abcd1234")
>>> client.square(n=4)

If you are using anything other than inbuilt-client, the Authorization HTTP header needs to be set in the POST request.

$ curl -d '{"n": 4}' -H "Authorization: Token abcd1234"

Using a config file

firefly can also take a configuration file with the following schema:

# config.yml

version: 1.0
token: "abcd1234"
    path: "/square"
    function: "funcs.square"
    path: "/cube"
    function: "funcs.cube"

You can specify the configuration file as:

$ firefly -c config.yml

Deploying a ML model

Machine Learning models can also be deployed by using firefly. You need to wrap the prediction logic as a function. For example, if you have a directory as follows:

$ ls classifier.pkl

where classifier.pkl is a joblib dump of a SVM Classifier model.

from sklearn.externals import joblib

clf = joblib.load('classifier.pkl')

def predict(a):
    predicted = clf.predict(a)    # predicted is 1x1 numpy array
    return int(predicted[0])

Invoke firefly as:

$ firefly model.predict

Now, you can access this by:

>>> import firefly
>>> client = firefly.Client("")
>>> client.predict(a=[5, 8])

You can use any model provided the function returns a JSON friendly data type.

Firefly with gunicorn

firefly applications can also be deployed using gunicorn . The arguments that are passed to firefly via CLI can be set as environment variables.

$ gunicorn --preload firefly.main:app -e FIREFLY_FUNCTIONS="funcs.square" -e FIREFLY_TOKEN="abcd1234"
[2017-07-19 14:47:57 +0530] [29601] [INFO] Starting gunicorn 19.7.1
[2017-07-19 14:47:57 +0530] [29601] [INFO] Listening at: (29601)
[2017-07-19 14:47:57 +0530] [29601] [INFO] Using worker: sync
[2017-07-19 14:47:57 +0530] [29604] [INFO] Booting worker with pid: 29604

If you want to deploy multiple functions, pass them as a comma-seperated list.

$ gunicorn --preload -e FIREFLY_FUNCTIONS="funcs.square,funcs.cube" -e FIREFLY_TOKEN="abcd1234"

Deployment on Heroku

firefly functions are deploying on any cloud platform. This section shows how you can deploy ML models to Heroku . There are two important files apart from your model code that you will need to have in your application root directory - Procfile and requirements.txt. Procfile lets Heroku know what sort of process you want to run and what command it should run. requirements.txt specifies dependencies of your code.

# requirements.txt

This Procfile tells Heroku to run firefly serving the predict function inside the model script.

# Procfile
web: gunicorn --preload firefly.main:app -e FIREFLY_FUNCTIONS="model.predict"
$ ls classifier.pkl requirements.txt Procfile

Now that everything is setup on your machine, we can deploy the application to Heroku.

$ git add .

$ git commit -m "Added a Procfile."

$ heroku login
Enter your Heroku credentials.

$ heroku create
Creating intense-falls-9163... done, stack is cedar |
Git remote heroku added

$ git push heroku master
-----> Python app detected
-----> Launching... done, v7 deployed to Heroku

For more information about deploying python applications to Heroku, go here .