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

Labs 2,3,4, 5submission #15

Open
wants to merge 7 commits into
base: main
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
2 changes: 2 additions & 0 deletions Code/azure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const KEY = "870ab5d3821045b0ac0db452cd892cc3";

248 changes: 205 additions & 43 deletions Code/dm.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import { KEY } from "./azure.js";
const inspector = createBrowserInspector();

const azureCredentials = {
endpoint:
"https://northeurope.api.cognitive.microsoft.com/sts/v1.0/issuetoken",
endpoint: "https://northeurope.api.cognitive.microsoft.com/sts/v1.0/issuetoken",
key: KEY,
};

Expand All @@ -19,18 +18,23 @@ const settings = {
ttsDefaultVoice: "en-US-DavisNeural",
};

/* Grammar definition */
const grammar = {
vlad: { person: "Vladislav Maraev" },
aya: { person: "Nayat Astaiza Soriano" },
rasmus: { person: "Rasmus Blanck" },
toby: { person: "Tobbe Tingvall" },
monday: { day: "Monday" },
tuesday: { day: "Tuesday" },
wednesday: {day: "Wednesday"},
thursday: {day: "Thursday"},
friday:{day: "Friday"},
"9": {time: "09:00"},
"10": { time: "10:00" },
"11": { time: "11:00" },
agree: ["yes", "yeah", "yup","of course"],
disagree: ["no", "nope"],
};

/* Helper functions */
function isInGrammar(utterance) {
return utterance.toLowerCase() in grammar;
}
Expand All @@ -39,13 +43,57 @@ function getPerson(utterance) {
return (grammar[utterance.toLowerCase()] || {}).person;
}

function getResponse(utterance) {
return (grammar[utterance.toLowerCase()] || {}).response;
}

function getDay(utterance) {
return (grammar[utterance.toLowerCase()] || {}).day;
}

function getTime(utterance) {
return (grammar[utterance.toLowerCase()] || {}).time;
}

function isTheAnswerYes(utterance){
return (grammar.agree.includes(utterance.toLowerCase()));
}

const dmMachine = setup({
actions: {
/* define your actions here */
say: ({ context }, params) =>
context.ssRef.send({
type: "SPEAK",
value: {
utterance: params,
},
}),
listen: ({ context }) =>
context.ssRef.send({
type: "LISTEN",
}),
assignPerson: assign({
person: (context, event) => getPerson(event.value[0].utterance),
}),
assignDay: assign({
day: (context, event) => getDay(event.value[0].utterance),
}),
assignTime: assign({
time: (context, event) => getTime(event.value[0].utterance),
}),
assignResponse: assign({
response: (context, event)=> getResponse(event.value[0].utterance),
}),
confirmAppointment: "say",
},
}).createMachine({
context: {
count: 0,
person: "",
day: "",
time: "",
response:"",
lastActivity: Date.now(), // This is to initialize the last activity timestamp
},
id: "DM",
initial: "Prepare",
Expand All @@ -57,53 +105,167 @@ const dmMachine = setup({
}),
({ context }) => context.ssRef.send({ type: "PREPARE" }),
],
on: { ASRTTS_READY: "WaitToStart" },
on: { ASRTTS_READY: "PromptAndAsk" },
},
PromptAndAsk: {
entry: ({ context }) =>
context.ssRef.send({
type: "SPEAK",
value: {
utterance: `Let's schedule a meeting?`,
},
}),
on: { SPEAK_COMPLETE: "AskWithWhom" },
},
WaitToStart: {
AskWithWhom: {
entry: ({ context }) =>
context.ssRef.send({
type: "SPEAK",
value: {
utterance: `With whom would you want to have a meeting with?`,
},
}),
on: { SPEAK_COMPLETE: "ListenWithWhom" },
},
ListenWithWhom: {
entry: ({ context }) =>
context.ssRef.send({
type: "LISTEN",
}),

on: {
CLICK: "PromptAndAsk",
RECOGNISED: [
{
guard: ({event}) => isInGrammar(event.value[0].utterance),
target: "AskForDay",
actions: assign({
person: ({event}) => getPerson(event.value[0].utterance),
}
)},
{
target: "AskForDay"
},
],
},
},
PromptAndAsk: {
initial: "Prompt",
states: {
Prompt: {
entry: ({ context }) =>
context.ssRef.send({
type: "SPEAK",
value: {
utterance: `Hello world!`,
},
AskForDay: {
entry: ({ context }) => {
context.lastActivity = Date.now();
context.ssRef.send({
type: "SPEAK",
value: {
utterance: `Which day would you like to have the meeting?`,
},
});
},
on: { SPEAK_COMPLETE: "ListenForDay" },
},
ListenForDay: {
entry: ({ context }) => {
context.lastActivity = Date.now();
context.ssRef.send({
type: "LISTEN",
});
},
on: {
RECOGNISED: [
{
guard: ({ event }) => isInGrammar(event.value[0].utterance),
target: "AskWholeDay",
actions: assign({
day: ({ event }) => getDay(event.value[0].utterance),
}),
on: { SPEAK_COMPLETE: "Ask" },
},
Ask: {
entry: ({ context }) =>
context.ssRef.send({
type: "LISTEN",
},
{
// transition back to AskWithWhom
target: "AskWholeDay",
},
],
},
},
AskWholeDay: {
entry: ({ context }) => {
context.lastActivity = Date.now();
context.ssRef.send({
type: "SPEAK",
value: {
utterance: `Will the meeting take the whole day?`,
},
});
},
on: { SPEAK_COMPLETE: "ListenForResponse" },
},
ListenForResponse: {
entry: ({ context }) => {
context.lastActivity = Date.now();
context.ssRef.send({
type: "LISTEN",
});
},
on: {
RECOGNISED: [
{
guard: ({ event }) => isTheAnswerYes(event.value[0].utterance),
target: "Confirmation",
},
{
// transition to MeetingTime
target: "MeetingTime",
},
],
},
},
MeetingTime: {
entry: ({ context }) => {
context.lastActivity = Date.now();
context.ssRef.send({
type: "SPEAK",
value: {
utterance: `What time would you like to schedule a meeting?`,
},
});
},
on: { SPEAK_COMPLETE: "ListenForResponse2" },
},
ListenForResponse2: {
entry: ({ context }) => {
context.lastActivity = Date.now();
context.ssRef.send({
type: "LISTEN",
});
},
on: {
RECOGNISED: [
{
guard: ({ event }) => isInGrammar(event.value[0].utterance),
target: "Confirmation",
actions: assign({
time: ({ event }) => getTime(event.value[0].utterance),
}),
on: {
RECOGNISED: {
actions: ({ context, event }) =>
context.ssRef.send({
type: "SPEAK",
value: {
utterance: `You just said: ${
event.value[0].utterance
}. And it ${
isInGrammar(event.value[0].utterance) ? "is" : "is not"
} in the grammar.`,
},
}),
},
SPEAK_COMPLETE: "#DM.Done",
},
},
{
// transition to Confirmation
target: "Confirmation",
},
],
},
},
Done: {
Confirmation: {
type: "final",
on: {
CLICK: "PromptAndAsk",
RECOGNISED: {
actions: [
{
type: "say",
params: ({ context, event }) => {
const response = getResponse(event.value[0].utterance);
return response === "Yes"
? `Your appointment with ${context.person} on ${context.day} at ${context.time} has been created.`
: "Let's start over.";
},
},
],
},
},
},
},
Expand All @@ -114,7 +276,7 @@ const dmActor = createActor(dmMachine, {
}).start();

dmActor.subscribe((state) => {
/* if you want to log some parts of the state */
// Handle state changes here if needed
});

export function setupButton(element) {
Expand Down
70 changes: 70 additions & 0 deletions Code/dm3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
dm3.js
import { assign, createActor, setup } from "xstate";
import { speechstate } from "speechstate";
import { createBrowserInspector } from "@statelyai/inspect";
import { KEY } from "./azure.js";

const inspector = createBrowserInspector();

const azureCredentials = {
endpoint:
"https://northeurope.api.cognitive.microsoft.com/sts/v1.0/issuetoken",
key: KEY,
};

const settings = {
azureCredentials: azureCredentials,
asrDefaultCompleteTimeout: 0,
asrDefaultNoInputTimeout: 5000,
locale: "en-US",
ttsDefaultVoice: "en-US-DavisNeural",
speechRecognitionEndpointId: "https://m-v-lab3.cognitiveservices.azure.com/"
};
const dmMachine = setup({
actions: {
listen : ({context}) =>
context.ssRef.send({
type: "LISTEN"
}),
},
}).createMachine({
context: {
count: 0,
},
id: "DM",
initial: "Prepare",
states: {
Prepare: {
entry: [
assign({
ssRef: ({ spawn }) => spawn(speechstate, { input: settings }),
}),
({ context }) => context.ssRef.send({ type: "PREPARE" }),
],
on: { ASRTTS_READY: "Listen" },
},

Listen: {
entry : "listen",
on : {
RECOGNISED : {
actions : ({event}) => console.log(event.value[0].confidence)
}
}
}
},
});

const dmActor = createActor(dmMachine, {
inspect: inspector.inspect,
}).start();

export function setupButton(element) {
element.addEventListener("click", () => {
dmActor.send({ type: "CLICK" });
});
dmActor.getSnapshot().context.ssRef.subscribe((snapshot) => {
element.innerHTML = `${snapshot.value.AsrTtsManager.Ready}`;
});
}

Loading