Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial Commit #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
148 changes: 146 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,146 @@
# gifty
A Demo on Using Watson Personality Insight
# Gifty

![Gifty](https://github.com/xnorax/eCommerce-with-Watson/blob/master/git_images/webshot_1.PNG)

In this journey, our server application will get 200 tweets for a specified Twitter handle. These tweets will be analyzed by [Waston Personality Insights][documentation] to understand users’ habits and preferences on an individual level. Based on the resulting analysis, suggested gifts will be presented in a Web UI.

When the reader has completed this journey, they will understand how to:
* Run an application that monitors a Twitter feed.
* Send the tweets to Watson Personality Insights for processing and analysis.
* Present the information in a Node.js web UI.

## Flow

1. User enters a Twitter handle name and click `Find!` button.
2. Tweets are pushed out by Twitter.
3. The app (app.js) processes the tweets.
4. The Watson personality Insights Service performs analysis of consumption needs.
5. The Web UI displays products that matches user consumption needs and preferences.

## Included components

* [Waston Personality Insights][documentation]: Predict personality characteristics, needs and values through written text. Understand your customers’ habits and preferences on an individual level, and at scale.
* [Cloud Foundry](http://cloudfoundry.org/): Build, deploy, and run applications on an open source cloud platform.

## Featured technologies

* [Artificial Intelligence](https://medium.com/ibm-data-science-experience): Artificial intelligence can be applied to disparate solution spaces to deliver disruptive technologies.
* [Node.js](https://nodejs.org/): An open-source JavaScript run-time environment for executing server-side JavaScript code.

# Steps

The setup is done in 3 primary steps. You will download the code, setup the application and then deploy the code to IBM Cloud.

1. [Sign up on IBM Cloud](#1-sign-up-on-ibm-cloud)
2. [Clone the repo](#2-clone-the-repo)
3. [Twitter Requirements](#3-twitter-requirements)
4. [Create Watson Personality Insights service with IBM Cloud](#4-create-watson-personality-insights-service-with-ibm-cloud)
5. [Run the application](#5-run-the-application)

### 1. Sign up on IBM Cloud

If you do not already have a IBM Cloud account, [sign up for IBM Cloud](https://ibm.biz/clouddayalfaisal).

### 2. Clone the repo

1. Click `Clone or download` button. Then, `Download ZIP`.

![Clone](https://github.com/xnorax/eCommerce-with-Watson/blob/master/git_images/clone.png)

2. Right click on the downloaded `.zip` folder and click `Extract All`.

### 3. Twitter requirements

To get Tweets from a specific handle in this application, it is required to create a Twitter account and a Twitter application.
The Twitter account will be used as the account that gets the tweets from other Twitter users.
* You can create an account on [Twitter](https://twitter.com/signup) or use an existing account.
* Once you have the Twitter account created and verified, log in to [Twitter Dev](https://apps.twitter.com/) and create an application.
* Select the Keys and Access Tokens tab and copy your `API Key` and `API Secret`.
* In `app.js` file, paste your keys to `consumer_key` and `consumer_secret`:
```
var Twithelper = new Twit({
consumer_key: 'Your key',
consumer_secret: 'Your secret',
app_only_auth: true
})
```

![Twitter app](https://github.com/xnorax/eCommerce-with-Watson/blob/master/git_images/twitter.gif)

### 4. Create Watson Personality Insights service with IBM Cloud

1. Log into [IBM Cloud](http://bluemix.net/) with your account.
2. Create `Personality Insights` service.
- From the top bar menu, click `Catalog`.
- On the left menu, select `Watson`.
- Select `Personality Insights`.
- Click `Create`.
3. Once the application is created, go into the application and select `Service credentials`.
4. Click `View credentials` to copy your username and password into the code in `app.js`:

```
var personality_insights = watson.personality_insights({
username: 'Your username',
password: 'Your password',
version_date:'2016-10-19',
version: 'v3'
});
```

![Watson service](https://github.com/xnorax/eCommerce-with-Watson/blob/master/git_images/bluemix.gif)

### 5. Run the application

1. Open the `manifest.yml` file and change the `name` AND `host` value to a unique application name.

2. Download and install the [Bluemix CLI](https://console.bluemix.net/docs/cli/reference/bluemix_cli/get_started.html#getting-started) tool.

3. Now open `Command Prompt` app and go to the directory of `eCommerce with Watson` folder in your computer.

```
cd C:\Users\%username%\Downloads\eCommerce-with-Watson-master
```

4. Then, connect to IBM Cloud and follow the prompts to log in.

```
bx login
```
5. Specify your targeted organization and space. You can find it in IBM Cloud Dashboard.

![Organization and Space](https://github.com/xnorax/eCommerce-with-Watson/blob/master/git_images/org.PNG)

```
bx target -o nora.alnashwan -s dev
```

6. Push the app to IBM Cloud.

```
bx app push
```

7. The application should now be running on IBM Cloud.
![Running](https://github.com/xnorax/eCommerce-with-Watson/blob/master/git_images/running.PNG)

Congrats! :smile: Now you can access the application from the route specified in your IBM Cloud Dashboard.

![App](https://github.com/xnorax/eCommerce-with-Watson/blob/master/git_images/app.PNG)


[documentation]: https://console.bluemix.net/docs/services/personality-insights/getting-started.html

## Contributors

Thanks goes to these wonderful people:

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore -->
| [<img src="https://avatars0.githubusercontent.com/u/17964781?s=460&v=4" width="100px;"/><br /><sub><b>Nora AlNashwan</b></sub>](https://twitter.com/xnorax)<br />[💻](#Contributors "Code") [📖](#Contributors "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/18343619?s=400&v=4" width="100px;"/><br /><sub><b>Rackad AlRasheed</b></sub>](https://twitter.com/rackadra)<br />[🤔](#Contributors "Ideas & Planning") [🎨](Contributors "Design")
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
This project follows the [all-contributors][all-contributors] specification.
Contributions of any kind are welcome!

[emojis]: https://github.com/kentcdodds/all-contributors#emoji-key
[all-contributors]: https://github.com/kentcdodds/all-contributors

131 changes: 131 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@

// define required packages
const watson = require('watson-developer-cloud'),
Twit = require('twit'),
express = require('express'),
fs = require('fs'),
cfenv = require('cfenv'),
http = require('http');

// create a new express server
var app = express();

app.set('view engine','ejs');
app.use(express.static(__dirname));

// To parse the HTML form input
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));

// define personality insights
var personality_insights = watson.personality_insights({
username: 'Your username',
password: 'Your password',
version_date:'2016-10-19',
version: 'v3'
});

// define twitter helper
var Twithelper = new Twit({
consumer_key: 'Your key',
consumer_secret: 'Your secret',
app_only_auth: true
})

const clothesTitles = ['Big & Tall Men\'s Quality Long Sleeve Pocket T-Shirts by Cotton Traders','RAISEVERN Unisex Casual 3d Pattern Printed Short Sleeve T-Shirts Top Tees','Men\'s Cotton Pique Polo Shirt','Burberry men\'s short sleeve shirt t-shirt thornaby blu'];
const clothesPrices = ['$25.00','$13.99','$12.00','$79.99'];

const booksTitles = ['Entertainment Weekly','A Short History of Nearly Everything','How to Turn $100 into $1,000,000','Etched in Sand: A True Story'];
const booksPrices = ['$17.00','$9.99','$7.98','$9.03'];

const healthTitle = 'Fitness Tracker, MoreFit Slim Touch Screen Activity Health Tracker Wearable Pedometer Smart Wristband';
const healthPrice = '$21.84';

const envTitle = 'Kalalou Recycled Flip Flop Large Rectangle Mat';
const envPrice = '$53.99';
/******************** Main Functions ******************/

// if you run the server locally it will run in localhost:port
// otherwise it will run on cloud foundry port
var appEnv = cfenv.getAppEnv();
http.Server(app).listen(appEnv.port, function() {
console.log("server starting on " + appEnv.url);
});

app.post('/search', function(req, res){

var titles = [];
var subtitles = [];
var prices = [];
var images = [];

Twithelper.get('statuses/user_timeline', { 
screen_name: req.body.name,
count: 200
 } , function(err, data) {
if (err) console.log('T Error:', err);
var buffer = "";
for (var i = 0; i < data.length; i++) {
buffer = buffer.concat(data[i].text);
}

personality_insights.profile({
text: buffer,
consumption_preferences: true,
language: 'en'
}, function(error, response) {
if (error)
console.log('PI Error:', error);
else {
var json = JSON.stringify(response, null, 2);
var obj = JSON.parse(json);

for (var i=0;i<obj.consumption_preferences.length;i++){

var category = obj.consumption_preferences[i];
var category_id = category.consumption_preference_category_id;

if (category_id.indexOf('shopping')!=-1){

for(var j=2;j<6;j++){
var preference = category.consumption_preferences[j];
if (preference.score=='1'){
titles.push(clothesTitles[j-2]); subtitles.push(preference.name);
prices.push(clothesPrices[j-2]); images.push('/images/c'+(j-2)+'.jpg');
}
}// for shopping category

} else if (category_id.indexOf('reading')!=-1) {

for(var j=1;j<category.consumption_preferences.length;j++){
var preference = category.consumption_preferences[j];
if (preference.score=='1'){
titles.push(booksTitles[j-1]); subtitles.push(preference.name.split(' ').splice(-2).join(' ').slice(0,-1));
prices.push(booksPrices[j-1]); images.push('/images/b'+(j-1)+'.jpg');
}
}// for book category

} else if (category_id.indexOf('health')!=-1){

if (category.consumption_preferences[2].score=='1'){
titles.push(healthTitle); subtitles.push(category.consumption_preferences[2].name);
prices.push(healthPrice); images.push('/images/h0.jpg');}

} else if (category_id.indexOf('environmental')!=-1){

if (category.consumption_preferences[0].score=='1'){
titles.push(envTitle); subtitles.push(category.consumption_preferences[0].name);
prices.push(envPrice); images.push('/images/e0.jpg');
}
}

}// for consumption_preferences
var data = {name:req.body.name,titles:titles,subtitles:subtitles,prices:prices,images:images};
res.render('gifts',{data:data});
}//else

}); //personalityInsights.profile

});//Twithelper.get

});//end of post function
1 change: 1 addition & 0 deletions assets/css/style.css

Large diffs are not rendered by default.

Loading