Example Hello pip

Defining Requirements via pip

pip is a great package manager for Python. It allows you to manage all of your package dependencies on per project basis. In the example, we're going to show you how you can use pip to include a particular version of a Python package in a Yhat deployment.

Including FuzzyWuzzy

For our example we're going to show how to use FuzzyWuzzy to build a string matching API with Yhat.

The main problem we're solving is that FuzzyWuzzy isn't already installed on Yhat (we automatically intall some core libraries like pandas, numpy, etc.) . Following a few short steps with pip, you can have FuzzyWuzzy automatically installed in your deployment environment.

Create a requirements file:

$ touch reqs.txt

Add any libraries to the file:

$ echo "fuzzywuzzy==0.2" >> reqs.txt

Read the file into your Python session.

reqs = open("reqs.txt").read()

Including requirements

Add a REQUIREMENTS variable to to your YhatModel instance and you're ready to go! Yhat will automatically discover any package imports, aliases, or class references such as from fuzzywuzzy import fuzz.

from yhat import Yhat, YhatModel , preprocess
from fuzzywuzzy import fuzz

class FuzzyMatcher(YhatModel):

    @preprocess(in_type=dict, out_type=dict) 
    def execute(self, data):
        data["string1"] = data.get("string1", "")
        data["string2"] = data.get("string2", "")
        ratio = fuzz.ratio(data["string1"], data["string2"])
        return {"ratio": ratio}

Deploy your model

testcase = {
    "string1": "peter piper pickeld peppers",   
    "string2": "peter piper picked a peck of peppers"
print FuzzyMatcher().execute(testcase)

yh = Yhat("YOUR_USERNAME", "YOUR_APIKEY", "http://cloud.yhathq.com/")
yh.deploy ("FuzzyMatcher", FuzzyMatcher, globals())


That that's it! Your deployment environment will automatically install FuzzyWuzzy. You can log into the admin console and check out your model . You can try out your model using the following code (in your python session):

print yh.predict("FuzzyMatcher", testcase)
# {u'ratio': 83, u'yhat_id': u'57fe76e4-2e88-463f-bd9f-5cf63838f731'}