public async Task <IActionResult> OnPost() { // recover sessionID var sessionID = HttpContext.Session.GetString("SessionID"); if (string.IsNullOrEmpty(sessionID)) { HttpContext.Session.Clear(); return(RedirectToPage("Index")); } // User posted some content if (String.IsNullOrEmpty(UserInput)) { // User didn't provide any input, so nothing to do return(Page()); } else { // Check with API if content is valid // and Use API call to send this content to GPT model var context = await _storytllerAPI.AddUserContext(UserInput, sessionID); // Update input field with corrected text ScreenedContext userContext = (ScreenedContext)context["ScreenedContext"]; UserInput = userContext.CorrectedText; if (!userContext.IsBounced) { Context apiContext = (Context)context["APIContext"]; // Add new text recieved from API to text and session string newContent = HttpContext.Session.GetString("SessionText") + "\n\n" + UserInput + "\n\n" + apiContext.SessionText; // Update session variable with user input and content from API HttpContext.Session.SetString("SessionText", newContent); // Apply new content to page model StoryText = newContent; // remove user input and any errors as successfull UserInput = null; ErrorText = null; } else { // set list of terms which are considered to be offensive OffensiveTerms = userContext.OffensiveTerms.ToHashSet(); // Return back stored in session text StoryText = HttpContext.Session.GetString("SessionText"); // Add error message ErrorText = "Please enter correct text!"; } } return(Page()); }
public async Task <ScreenedContext> ModerateText(string text) { // create in-memory stream, as ContentModerator SDK accepts text only as a stream var stream = new MemoryStream(Encoding.UTF8.GetBytes(text)); // use ContentModeratorClient.TextModeration to check provided text var answer = await _client.TextModeration.ScreenTextAsync("text/plain", stream, "eng", true, false, null, false); // Content-Type: text/plain // Text: (variable) // Language: eng // Autocorrect: true // Check for PII (US only): false // ListID (custom list of words): null // Classify as adult (class1), racist (class2) or offensive (class3): false // // Up to 1024 characters long stream can be used per one request. // Free version of Azure ContentModerator allows only 1 request per second, and up to 5000 requests per month // Details: https://docs.microsoft.com/en-us/rest/api/cognitiveservices/contentmoderator/textmoderation/screentext // and https://docs.microsoft.com/en-us/azure/cognitive-services/content-moderator/client-libraries?pivots=programming-language-csharp&tabs=cli // create a new screened text object and add some values. Can be done through model itself var screen = new ScreenedContext() { OriginalText = answer.OriginalText, CorrectedText = answer.AutoCorrectedText, OffensiveTerms = new HashSet <string>(), IsBounced = false }; // Check if ContentModerator found any offensive terms if (null != answer.Terms) { // Add all of them to the HashSet (so it keeps unique terms only) foreach (var term in answer.Terms) { screen.OffensiveTerms.Add(term.Term); } screen.IsBounced = true; } return(screen); }