
Last week at the [TrailheaDX Salesforce Dev Conference][1] we launched the [DreamHouse sample application][2] to showcase the Salesforce App Cloud and numerous possible integrations. I built an integration with the open source [PredictionIO][3] Machine Learning framework. The use case for ML in DreamHouse is a real estate recommendation engine that learns based on users with similar favorites. [Check out a demo and get the source][4].

For the DreamHouse PredictionIO integration to work I needed to get the PredictionIO service running on Heroku. Since it is a Scala app everything worked great! Here are the steps to get PredictionIO up and running on Heroku.

First you will need a PredictionIO event server and app defined in the event server:

1. Deploy: [![][5]][6]
2. Create an app:
    
    ```bash
    heroku run -a <APP NAME> console app new <A PIO APP NAME>
    ```

3. List apps:
  
    ```bash
    heroku run -a <APP NAME> console app list
    ```

4. <a href="https://github.com/jamesward/pio-eventserver-heroku">Check out the source and local dev instructions for the event server</a>.

Now that you have an event server and app, load some event data:

```bash
export ACCESS_KEY=<YOUR ACCESS KEY>
export URL=http://<YOUR HEROKU APP NAME>.herokuapp.com
for i in {1..5}; do curl -i -X POST $URL/events.json?accessKey=$ACCESS_KEY -H "Content-Type: application/json" -d "{ \"event\" : \"\$set\", \"entityType\" : \"user\", \"entityId\" : \"u$i\" }"; done

for i in {1..50}; do curl -i -X POST $URL/events.json?accessKey=$ACCESS_KEY -H "Content-Type: application/json" -d "{ \"event\" : \"\$set\", \"entityType\" : \"item\", \"entityId\" : \"i$i\", \"properties\" : { \"categories\" : [\"c1\", \"c2\"] } }"; done

for j in {1..20}; do for i in {1..5}; do curl -i -X POST $URL/events.json?accessKey=$ACCESS_KEY -H "Content-Type: application/json" -d "{ \"event\" : \"view\", \"entityType\" : \"user\", \"entityId\" : \"u$i\",  \"targetEntityType\" : \"item\", \"targetEntityId\" : \"i$(( ( RANDOM % 50 )  + 1 ))\" }"; done; done
```

Check out the demo data:

```bash
http://<YOUR HEROKU APP NAME>.herokuapp.com/events.json?accessKey=<YOUR APP ACCESS KEY>&limit=-1
```

Now you need an engine that will learn from a set of training data and then be able to make predictions.  With PredictionIO you can use any algorithm you want but often <a href="https://spark.apache.org/docs/1.3.0/ml-guide.html">SparkML</a> is a great choice.  For this simple example I'm just using single-node Spark and Postgres but the underlying data source and ML engine can be anything.


This example is based on PredictionIO's <a href="https://docs.prediction.io/templates/recommendation/dase/">Recommendation Template</a> so it uses SparkML's <a href="https://spark.apache.org/docs/1.3.0/mllib-collaborative-filtering.html">Alternating Least Squares</a> (ALS) algorithm.  To deploy it on Heroku follow these steps:

1. Deploy: <a href="https://heroku.com/deploy?template=https://github.com/jamesward/pio-engine-heroku.git"><img src="https://www.herokucdn.com/deploy/button.svg" /></a>
  
2. Attach your PredictionIO Event Server's Postgres:
    
    ```bash
    heroku addons:attach <YOUR-ADDON-ID> -a <YOUR HEROKU APP NAME>
    ```
    
    Note: You can find out `<YOUR-ADDON-ID>` by running: 
    
    ```bash
    heroku addons -a <YOUR EVENT SERVER HEROKU APP NAME>
    ```

3. Train the app:
    
    ```bash
    heroku run -a <YOUR HEROKU APP NAME> train
    ```
  
4. Restart the app to load the new training data:
    
    ```bash
    heroku restart -a <YOUR HEROKU APP NAME>
    ```

5. Check the status of your engine:</p>
    ```bash
    http://<YOUR HEROKU APP NAME>.herokuapp.com
    ```

Now you can check out the recommendations for an item (must be an item that has events):


```bash
curl -H "Content-Type: application/json" -d '{ "items": ["i11"], "num": 4 }' -k http://<YOUR HEROKU APP NAME>.herokuapp.com/queries.json
```

<a href="https://github.com/jamesward/pio-engine-heroku">Check out the source and local dev instructions for this example engine</a>.

Let me know if you have any questions or problems.  Happy ML'ing!

 [1]: https://developer.salesforce.com/trailheadx
 [2]: http://dreamhouseapp.io
 [3]: http://prediction.io
 [4]: http://dreamhouseapp.io/pio
 [5]: https://www.herokucdn.com/deploy/button.svg
 [6]: https://heroku.com/deploy?template=https://github.com/jamesward/pio-eventserver-heroku.git

