Skip to content

Instantly share code, notes, and snippets.

@ashwani-pandey
Created June 4, 2020 07:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ashwani-pandey/6f60de48fd454bbb96a0ac601b4496ef to your computer and use it in GitHub Desktop.
Save ashwani-pandey/6f60de48fd454bbb96a0ac601b4496ef to your computer and use it in GitHub Desktop.
AI Playground Chatbot code for recommending subject streams
const WELCOME = 0;
const FIRST = 1;
const FINISH = 2;
let currentState = WELCOME;
let details = {};
if(currentState == WELCOME){
Bot.send("Hey, there! What's your name?");
}
async function respond(inputText){
if(currentState == WELCOME){
details.name = inputText;
Bot.send(`So ${details.name}, tell me all of your interests, and I will tell you the stream that you should choose!`);
currentState = FIRST;
} else if(currentState == FIRST){
let recommendedStream = await CampK12.classify("RecommendedStreamModel", inputText);
Bot.send(`As per your interests, you should choose ${recommendedStream.toLowerCase()} as your stream`);
Bot.send("If you want to try again, press 1 else press 0");
currentState = FINISH;
}
else if(currentState == FINISH){
if(parseInt(inputText) == 1){
currentState = WELCOME;
respond(details.name);
} else {
Bot.send("Bye, bye. Talk with me again if you need to, and stay healthy :)");
}
}
}
@kuldeep7895
Copy link

Loved it. Upload Some more stuff

@AdeetyaU
Copy link

AdeetyaU commented Jul 8, 2020

The code has a few minor improvements, they are:

For Title Case:

function titleCase(recommendedStream) {
return recommendedStream.toLowerCase().split(' ').map(function(word) {
return (word.charAt(0).toUpperCase() + word.slice(1));
}).join(' ');
}

var inputText;

const WELCOME = 0;
const FIRST = 1;
const FINISH = 2;
let currentState = WELCOME;
let details = {};

if(currentState == WELCOME){
	Bot.send("Hey, there! What's your name?");
}

async function respond(inputText){

	if(currentState == WELCOME){
		details.name = inputText;
		Bot.send(`So ${details.name}, tell me all of your interests, and I will tell you the stream that you should choose!`);
		currentState = FIRST;

	} else if(currentState == FIRST){
	let recommendedStream = <click on My AI Models, select your Model, and then paste here the code that it automatically outputs>

A sample output is: let reccomendedStream = "await CampK12.classify("CareerChoice Adeetya Upadhyay", inputText);"

		Bot.send(`As per your interests, you should choose ${recommendedStream.toLowerCase()} as your stream`);
		Bot.send("If you want to try again, press 1, else press 0");
		currentState = FINISH;
	}
	else if(currentState == FINISH){
		if(parseInt(inputText) == 1){
			currentState = WELCOME;
			respond(details.name);
		} else {
			Bot.send("Thank you for using our model!");
		}
	}

}

Also, use Google Chrome only, most other browsers do not support WebGL natively

The final code for me was:

function titleCase(recommendedStream) { 
  return recommendedStream.toLowerCase().split(' ').map(function(word) { 
    return (word.charAt(0).toUpperCase() + word.slice(1)); 
  }).join(' '); 
} 

var inputText;
const WELCOME = 0;
const FIRST = 1;
const FINISH = 2;
let currentState = WELCOME;
let details = {};

if(currentState == WELCOME){
	Bot.send("Hey, there! What's your name?");
}

async function respond(inputText){

	if(currentState == WELCOME){
		details.name = titleCase(inputText);
		Bot.send(`So ${details.name}, tell me all of your interests, and I will tell you the stream that you should choose!`);
		currentState = FIRST;

	} else if(currentState == FIRST){
		let recommendedStream = await CampK12.classify("CareerChoice   Adeetya Upadhyay", inputText);
		Bot.send(`As per your interests, you should choose ${titleCase(recommendedStream)} as your stream`);
		Bot.send("If you want to try again, press 1, else press 0");
		currentState = FINISH;
	}
	else if(currentState == FINISH){
		if(parseInt(inputText) == 1){
			currentState = WELCOME;
			respond(details.name);
		} else {
			Bot.send("Thank you for using our model!");
		}
	}

}

Summary:

  1. var inputText
  2. You have to click on My AI Models, select your Model, and then paste the code that it automatically outputs, otherwise the code does not work. I have specified the location above at which it has to pasted.
  3. I prefer Title Case to capitalize the stream as well as the name, so john wick will become John Wick, which is why I have added the function at the top, no change is required!

Otherwise, great code!

PS: If you wish to try out my code and model, here they are:
https://theaiplayground.com/amazingadeetya88/model/CareerChoice---Adeetya-Upadhyay
https://theaiplayground.com/amazingadeetya88/bot/Stream-Choice-Model

@ashwani-pandey
Copy link
Author

Hi @adeetya, thanks for taking your time to write about all the improvements. It will be helpful to a lot of people.

Regarding our models (both yours and mine), it gives higher preference to my first interest than the last one. Will have to see why it's behaving like that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment