Skip to content

Commit 5400341

Browse files
authored
Merge pull request #175 from PatrickFarley/pafarley-snippets
Pafarley snippets
2 parents 7e2fe30 + 55b7727 commit 5400341

File tree

1 file changed

+46
-2
lines changed
  • documentation-samples/quickstarts/ContentModerator

1 file changed

+46
-2
lines changed

documentation-samples/quickstarts/ContentModerator/Program.cs

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// <snippet_using>
12
using Microsoft.Azure.CognitiveServices.ContentModerator;
23
using Microsoft.Azure.CognitiveServices.ContentModerator.Models;
34
using Newtonsoft.Json;
@@ -6,6 +7,7 @@
67
using System.IO;
78
using System.Text;
89
using System.Threading;
10+
// </snippet_using>
911

1012
/*
1113
* Content Moderator SDK Quickstart
@@ -43,26 +45,35 @@ namespace ContentModeratorQuickstart
4345
class Program
4446
{
4547
// AUTHENTICATION - ALL EXAMPLES
48+
// <snippet_creds>
4649
// Your Content Moderator subscription key is found in your Azure portal resource on the 'Keys' page. Add to your environment variables.
4750
private static readonly string SubscriptionKey = Environment.GetEnvironmentVariable("CONTENT_MODERATOR_SUBSCRIPTION_KEY");
48-
// Base endpoint with your specific region, add this to your environment variables. Found on 'Overview page' in Azure resource. For example: https://westus.api.cognitive.microsoft.com
51+
// Base endpoint URL. Add this to your environment variables. Found on 'Overview' page in Azure resource. For example: https://westus.api.cognitive.microsoft.com
4952
private static readonly string Endpoint = Environment.GetEnvironmentVariable("CONTENT_MODERATOR_ENDPOINT");
53+
// </snippet_creds>
5054

55+
// <snippet_image_vars>
5156
// IMAGE MODERATION
5257
//The name of the file that contains the image URLs to evaluate.
5358
private static readonly string ImageUrlFile = "ImageFiles.txt";
5459
// The name of the file to contain the output from the evaluation.
5560
private static string ImageOutputFile = "ImageModerationOutput.json";
61+
// </snippet_image_vars>
5662

63+
// <snippet_text_vars>
5764
// TEXT MODERATION
5865
// Name of the file that contains text
5966
private static readonly string TextFile = "TextFile.txt";
6067
// The name of the file to contain the output from the evaluation.
6168
private static string TextOutputFile = "TextModerationOutput.txt";
69+
// </snippet_text_vars>
6270

6371
// CREATE HUMAN REVIEWS FOR IMAGES
72+
// <snippet_review_urls>
6473
// The list of URLs of the images to create review jobs for.
6574
private static readonly string[] IMAGE_URLS_FOR_REVIEW = new string[] { "https://moderatorsampleimages.blob.core.windows.net/samples/sample5.png" };
75+
// </snippet_review_urls>
76+
// <snippet_review_vars>
6677
// The name of the team to assign the review to. Must be the team name used to create your Content Moderator website account.
6778
// If you do not yet have an account, follow this: https://docs.microsoft.com/en-us/azure/cognitive-services/content-moderator/quick-start
6879
// Select the gear symbol (settings)-->Credentials to retrieve it. Your team name is the Id associated with your subscription.
@@ -71,23 +82,34 @@ class Program
7182
// For example: https://westus.api.cognitive.microsoft.com/contentmoderator/review/v1.0
7283
// As reviewers complete reviews, results are sent using an HTTP POST request.
7384
private static readonly string ReviewsEndpoint = Environment.GetEnvironmentVariable("CONTENT_MODERATOR_REVIEWS_ENDPOINT");
85+
// </snippet_review_vars>
7486

7587
static void Main(string[] args)
7688
{
7789
// CLIENTS - each API call needs its own client
90+
// <snippet_client>
7891
// Create an image review client
7992
ContentModeratorClient clientImage = Authenticate(SubscriptionKey, Endpoint);
8093
// Create a text review client
8194
ContentModeratorClient clientText = Authenticate(SubscriptionKey, Endpoint);
8295
// Create a human reviews client
8396
ContentModeratorClient clientReviews = Authenticate(SubscriptionKey, Endpoint);
97+
// </snippet_client>
8498

99+
// <snippet_imagemod_call>
85100
// Moderate images from list of image URLs
86101
ModerateImages(clientImage, ImageUrlFile, ImageOutputFile);
102+
// </snippet_imagemod_call>
103+
104+
// <snippet_textmod_call>
87105
// Moderate text from text in a file
88106
ModerateText(clientText, TextFile, TextOutputFile);
107+
// </snippet_textmod_call>
108+
109+
// <snippet_review_call>
89110
// Create image reviews for human reviewers
90111
CreateReviews(clientReviews, IMAGE_URLS_FOR_REVIEW, TEAM_NAME, ReviewsEndpoint);
112+
// </snippet_review_call>
91113

92114
Console.WriteLine();
93115
Console.WriteLine("End of the quickstart.");
@@ -108,6 +130,7 @@ public static ContentModeratorClient Authenticate(string key, string endpoint)
108130
return client;
109131
}
110132

133+
// <snippet_imagemod_iterate>
111134
/*
112135
* IMAGE MODERATION
113136
* This example moderates images from URLs.
@@ -133,6 +156,8 @@ public static void ModerateImages(ContentModeratorClient client, string urlFile,
133156
{
134157
Console.WriteLine("Evaluating {0}...", Path.GetFileName(line));
135158
var imageUrl = new BodyModel("URL", line.Trim());
159+
// </snippet_imagemod_iterate>
160+
// <snippet_imagemod_analyze>
136161
var imageData = new EvaluationData
137162
{
138163
ImageUrl = imageUrl.Value,
@@ -158,6 +183,8 @@ public static void ModerateImages(ContentModeratorClient client, string urlFile,
158183
}
159184
}
160185
}
186+
// </snippet_imagemod_analyze>
187+
// <snippet_imagemod_save>
161188
// Save the moderation results to a file.
162189
using (StreamWriter outputWriter = new StreamWriter(outputFile, false))
163190
{
@@ -172,7 +199,9 @@ public static void ModerateImages(ContentModeratorClient client, string urlFile,
172199
Console.WriteLine();
173200
}
174201
}
202+
// </snippet_imagemod_save>
175203

204+
// <snippet_dataclass>
176205
// Contains the image moderation results for an image,
177206
// including text and face detection results.
178207
public class EvaluationData
@@ -189,10 +218,12 @@ public class EvaluationData
189218
// The face detection results;
190219
public FoundFaces FaceDetection;
191220
}
221+
// </snippet_dataclass>
192222
/*
193223
* END - IMAGE MODERATION
194224
*/
195225

226+
// <snippet_textmod>
196227
/*
197228
* TEXT MODERATION
198229
* This example moderates text from file.
@@ -236,6 +267,7 @@ public static void ModerateText(ContentModeratorClient client, string inputFile,
236267
Console.WriteLine("Results written to {0}", outputFile);
237268
Console.WriteLine();
238269
}
270+
// </snippet_textmod>
239271
/*
240272
* END - TEXT MODERATION
241273
*/
@@ -247,7 +279,7 @@ public static void ModerateText(ContentModeratorClient client, string inputFile,
247279
*
248280
* Prerequisistes:
249281
* 1. In your Content Moderator resource go to Resource Management -> Properties, then copy your Resource ID.
250-
* 2. Go to the Content Moderator website, https://{YOUR REGION}.contentmoderator.cognitive.microsoft.com.
282+
* 2. Go to the Content Moderator website.
251283
* 3. Click the gear sign (Settings) and go to "Credentials".
252284
* 4. Under "Whitelisted Resource Id(s)" paste your Resource ID, then select the "+" button to add it.
253285
* This enables the website to receive programmatic reviews from your Content Moderator resource in Azure.
@@ -257,6 +289,7 @@ public static void ModerateText(ContentModeratorClient client, string inputFile,
257289
* Use your Azure account with the review APIs:
258290
* https://docs.microsoft.com/en-us/azure/cognitive-services/content-moderator/review-tool-user-guide/configure
259291
*/
292+
// <snippet_review_item>
260293
// Associates the review ID (assigned by the service) to the internal.
261294
public class ReviewItem
262295
{
@@ -269,7 +302,9 @@ public class ReviewItem
269302
// The ID that the service assigned to the review.
270303
public string ReviewId;
271304
}
305+
// </snippet_review_item>
272306

307+
// <snippet_createreview_fields>
273308
// Create the reviews using the fixed list of images.
274309
private static void CreateReviews(ContentModeratorClient client, string[] ImageUrls, string teamName, string endpoint)
275310
{
@@ -303,7 +338,9 @@ private static void CreateReviews(ContentModeratorClient client, string[] ImageU
303338

304339
// The cached review information, associating a local content ID to the created review ID for each item.
305340
List<ReviewItem> reviewItems = new List<ReviewItem>();
341+
// </snippet_createreview_fields>
306342

343+
// <snippet_createreview_create>
307344
using (TextWriter outputWriter = new StreamWriter(OutputFile, false))
308345
{
309346
writer = outputWriter;
@@ -340,7 +377,9 @@ private static void CreateReviews(ContentModeratorClient client, string[] ImageU
340377
}
341378

342379
var reviewResponse = client.Reviews.CreateReviewsWithHttpMessagesAsync("application/json", teamName, requestInfo);
380+
// </snippet_createreview_create>
343381

382+
// <snippet_createreview_ids>
344383
// Update the local cache to associate the created review IDs with the associated content.
345384
var reviewIds = reviewResponse.Result.Body;
346385
for (int i = 0; i < reviewIds.Count; i++) { reviewItems[i].ReviewId = reviewIds[i]; }
@@ -359,7 +398,9 @@ private static void CreateReviews(ContentModeratorClient client, string[] ImageU
359398
WriteLine(outputWriter, JsonConvert.SerializeObject(reviewDetail.Result.Body, Formatting.Indented));
360399
Thread.Sleep(throttleRate);
361400
}
401+
// </snippet_createreview_ids>
362402

403+
// <snippet_createreview_results>
363404
Console.WriteLine();
364405
Console.WriteLine("Perform manual reviews on the Content Moderator site.");
365406
Console.WriteLine("Then, press any key to continue.");
@@ -391,14 +432,17 @@ private static void CreateReviews(ContentModeratorClient client, string[] ImageU
391432
}
392433
Console.WriteLine("--------------------------------------------------------------");
393434
}
435+
// </snippet_createreview_results>
394436

437+
// <snippet_writeline>
395438
// Helper function that writes a message to the log file, and optionally to the console.
396439
// If echo is set to true, details will be written to the console.
397440
private static void WriteLine(TextWriter writer, string message = null, bool echo = true)
398441
{
399442
writer.WriteLine(message ?? String.Empty);
400443
if (echo) { Console.WriteLine(message ?? String.Empty); }
401444
}
445+
// </snippet_writeline>
402446
/*
403447
* END - CREATE HUMAN IMAGE REVIEWS
404448
*/

0 commit comments

Comments
 (0)