Skip to content

Commit

Permalink
This resolves #27, creates a title for suggested pictos, also adds me…
Browse files Browse the repository at this point in the history
…tadata to a board to be used with Midjourney
  • Loading branch information
aguszanoli committed Apr 13, 2024
1 parent 07d7bc5 commit b15bda1
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 5 deletions.
23 changes: 19 additions & 4 deletions run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ const engineInstance = initEngine({
contentSafetyConfiguration,
});

const prompt = "A family of 5 people";
const maxSuggestions = 5;
const prompt = "elements to tell a story about a wizard and a lizard";
const maxSuggestions = 10;
const symbolSet = "arasaac";
const language = "eng";

Expand All @@ -44,7 +44,7 @@ engineInstance.isContentSafe(prompt).then((result) => {
console.log('Is content safe?', result);
});


/*
// Get suggestions with GlobalSymbols
engineInstance
.getSuggestions({
Expand All @@ -58,8 +58,23 @@ engineInstance
"\nSuggestions -----------------------------------------------\n",
suggestions,
"length: " + suggestions.length
)
)
);
*/

engineInstance
.getFullBoard({
prompt,
maxSuggestions,
symbolSet,
language,
})
.then((board) =>
console.log(
"\nFull board \n",
board
)
);

/*
// Get suggestions with GlobalSymbol and Pictonizer images
Expand Down
78 changes: 77 additions & 1 deletion src/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ export type ContentSafetyConfiguration = {
key: string;
};

export type Board = {
boardTitle: string;
boardContent: string;
pictos: Suggestion[];
};

export function init({
openAIConfiguration,
globalSymbolsApiURL,
Expand Down Expand Up @@ -83,9 +89,11 @@ export function init({
pictonizer,
getSuggestionsAndProcessPictograms,
isContentSafe,
getFullBoard,
};
}

//Using a prompt get a list of suggested words.
async function getWordSuggestions({
prompt,
maxWords,
Expand Down Expand Up @@ -129,6 +137,7 @@ async function getWordSuggestions({
throw new Error("ERROR: Suggestion list is empty");
}

//Based on a list of words fetch the Pictogram URL from GlobalSymbols API
async function fetchPictogramsURLs({
words,
symbolSet,
Expand Down Expand Up @@ -254,6 +263,7 @@ async function processPictograms(
return suggestionsWithAIImage;
}

//Get suggested Pictograms based on a prompt.
async function getSuggestions({
prompt,
maxSuggestions = DEFAULT_MAX_SUGGESTIONS,
Expand All @@ -276,7 +286,6 @@ async function getSuggestions({
symbolSet,
language,
});

return suggestionsWithGlobalSymbolsImages;
}

Expand All @@ -303,6 +312,7 @@ const getSuggestionsAndProcessPictograms = async ({
return suggestionsWithAIImages;
};

//Use Azure ContentSafety API to check if a string is safe for all users.
async function isContentSafe(
textPrompt: string,
): Promise<boolean> {
Expand All @@ -328,3 +338,69 @@ async function isContentSafe(
}

}


//Using a list of words get a descriptive title.
async function getBoardTitle(
words: string[],
language: string
): Promise<string> {
const max_tokens = Math.round(2 * words.length + 110);
const completionRequestParams = {
model: "text-davinci-003",
prompt: `act as a speech pathologist in language ${language}
usign this list of words {${words}} create a descriptive title for a communication board.
Here are mandatory instructions for the list:
-The title is 4 words maximum.
-It is very important to not repeat words.
-Do not add any other text or characters to the title.`,
temperature: 0,
max_tokens: max_tokens,
};

const response = await globalConfiguration.openAIInstance.createCompletion(
completionRequestParams
);
const titleSuggestionsData = response.data?.choices[0]?.text;
if (titleSuggestionsData) {
return titleSuggestionsData;
} else {
return "No AI title.";
}
}


//Get a board with a title, a content desciption and Pictograms based on a prompt.
async function getFullBoard({
prompt,
maxSuggestions = DEFAULT_MAX_SUGGESTIONS,
symbolSet,
language = DEFAULT_LANGUAGE,
}: {
prompt: string;
maxSuggestions: number;
symbolSet?: string;
language: string;
}): Promise<Board> {
const words: string[] = await getWordSuggestions({
prompt,
maxWords: maxSuggestions,
language,
});
//TODO we can check here if the word suggestions are safe @rodrisanchez
const title: string = await getBoardTitle(words,language);
console.log("Title: " + title);
const suggestionsWithGlobalSymbolsImages: Suggestion[] =
await fetchPictogramsURLs({
words,
symbolSet,
language,
});
return {
boardTitle: title,
boardContent: "MISC", //TODO change here to use a different midjourney model for different each category.
pictos:suggestionsWithGlobalSymbolsImages,
};
}


0 comments on commit b15bda1

Please sign in to comment.