1
+ // <snippet_using>
1
2
using Microsoft . Azure . CognitiveServices . ContentModerator ;
2
3
using Microsoft . Azure . CognitiveServices . ContentModerator . Models ;
3
4
using Newtonsoft . Json ;
6
7
using System . IO ;
7
8
using System . Text ;
8
9
using System . Threading ;
10
+ // </snippet_using>
9
11
10
12
/*
11
13
* Content Moderator SDK Quickstart
@@ -43,26 +45,35 @@ namespace ContentModeratorQuickstart
43
45
class Program
44
46
{
45
47
// AUTHENTICATION - ALL EXAMPLES
48
+ // <snippet_creds>
46
49
// Your Content Moderator subscription key is found in your Azure portal resource on the 'Keys' page. Add to your environment variables.
47
50
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
49
52
private static readonly string Endpoint = Environment . GetEnvironmentVariable ( "CONTENT_MODERATOR_ENDPOINT" ) ;
53
+ // </snippet_creds>
50
54
55
+ // <snippet_image_vars>
51
56
// IMAGE MODERATION
52
57
//The name of the file that contains the image URLs to evaluate.
53
58
private static readonly string ImageUrlFile = "ImageFiles.txt" ;
54
59
// The name of the file to contain the output from the evaluation.
55
60
private static string ImageOutputFile = "ImageModerationOutput.json" ;
61
+ // </snippet_image_vars>
56
62
63
+ // <snippet_text_vars>
57
64
// TEXT MODERATION
58
65
// Name of the file that contains text
59
66
private static readonly string TextFile = "TextFile.txt" ;
60
67
// The name of the file to contain the output from the evaluation.
61
68
private static string TextOutputFile = "TextModerationOutput.txt" ;
69
+ // </snippet_text_vars>
62
70
63
71
// CREATE HUMAN REVIEWS FOR IMAGES
72
+ // <snippet_review_urls>
64
73
// The list of URLs of the images to create review jobs for.
65
74
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>
66
77
// The name of the team to assign the review to. Must be the team name used to create your Content Moderator website account.
67
78
// If you do not yet have an account, follow this: https://docs.microsoft.com/en-us/azure/cognitive-services/content-moderator/quick-start
68
79
// 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
71
82
// For example: https://westus.api.cognitive.microsoft.com/contentmoderator/review/v1.0
72
83
// As reviewers complete reviews, results are sent using an HTTP POST request.
73
84
private static readonly string ReviewsEndpoint = Environment . GetEnvironmentVariable ( "CONTENT_MODERATOR_REVIEWS_ENDPOINT" ) ;
85
+ // </snippet_review_vars>
74
86
75
87
static void Main ( string [ ] args )
76
88
{
77
89
// CLIENTS - each API call needs its own client
90
+ // <snippet_client>
78
91
// Create an image review client
79
92
ContentModeratorClient clientImage = Authenticate ( SubscriptionKey , Endpoint ) ;
80
93
// Create a text review client
81
94
ContentModeratorClient clientText = Authenticate ( SubscriptionKey , Endpoint ) ;
82
95
// Create a human reviews client
83
96
ContentModeratorClient clientReviews = Authenticate ( SubscriptionKey , Endpoint ) ;
97
+ // </snippet_client>
84
98
99
+ // <snippet_imagemod_call>
85
100
// Moderate images from list of image URLs
86
101
ModerateImages ( clientImage , ImageUrlFile , ImageOutputFile ) ;
102
+ // </snippet_imagemod_call>
103
+
104
+ // <snippet_textmod_call>
87
105
// Moderate text from text in a file
88
106
ModerateText ( clientText , TextFile , TextOutputFile ) ;
107
+ // </snippet_textmod_call>
108
+
109
+ // <snippet_review_call>
89
110
// Create image reviews for human reviewers
90
111
CreateReviews ( clientReviews , IMAGE_URLS_FOR_REVIEW , TEAM_NAME , ReviewsEndpoint ) ;
112
+ // </snippet_review_call>
91
113
92
114
Console . WriteLine ( ) ;
93
115
Console . WriteLine ( "End of the quickstart." ) ;
@@ -108,6 +130,7 @@ public static ContentModeratorClient Authenticate(string key, string endpoint)
108
130
return client ;
109
131
}
110
132
133
+ // <snippet_imagemod_iterate>
111
134
/*
112
135
* IMAGE MODERATION
113
136
* This example moderates images from URLs.
@@ -133,6 +156,8 @@ public static void ModerateImages(ContentModeratorClient client, string urlFile,
133
156
{
134
157
Console . WriteLine ( "Evaluating {0}..." , Path . GetFileName ( line ) ) ;
135
158
var imageUrl = new BodyModel ( "URL" , line . Trim ( ) ) ;
159
+ // </snippet_imagemod_iterate>
160
+ // <snippet_imagemod_analyze>
136
161
var imageData = new EvaluationData
137
162
{
138
163
ImageUrl = imageUrl . Value ,
@@ -158,6 +183,8 @@ public static void ModerateImages(ContentModeratorClient client, string urlFile,
158
183
}
159
184
}
160
185
}
186
+ // </snippet_imagemod_analyze>
187
+ // <snippet_imagemod_save>
161
188
// Save the moderation results to a file.
162
189
using ( StreamWriter outputWriter = new StreamWriter ( outputFile , false ) )
163
190
{
@@ -172,7 +199,9 @@ public static void ModerateImages(ContentModeratorClient client, string urlFile,
172
199
Console . WriteLine ( ) ;
173
200
}
174
201
}
202
+ // </snippet_imagemod_save>
175
203
204
+ // <snippet_dataclass>
176
205
// Contains the image moderation results for an image,
177
206
// including text and face detection results.
178
207
public class EvaluationData
@@ -189,10 +218,12 @@ public class EvaluationData
189
218
// The face detection results;
190
219
public FoundFaces FaceDetection ;
191
220
}
221
+ // </snippet_dataclass>
192
222
/*
193
223
* END - IMAGE MODERATION
194
224
*/
195
225
226
+ // <snippet_textmod>
196
227
/*
197
228
* TEXT MODERATION
198
229
* This example moderates text from file.
@@ -236,6 +267,7 @@ public static void ModerateText(ContentModeratorClient client, string inputFile,
236
267
Console . WriteLine ( "Results written to {0}" , outputFile ) ;
237
268
Console . WriteLine ( ) ;
238
269
}
270
+ // </snippet_textmod>
239
271
/*
240
272
* END - TEXT MODERATION
241
273
*/
@@ -247,7 +279,7 @@ public static void ModerateText(ContentModeratorClient client, string inputFile,
247
279
*
248
280
* Prerequisistes:
249
281
* 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.
251
283
* 3. Click the gear sign (Settings) and go to "Credentials".
252
284
* 4. Under "Whitelisted Resource Id(s)" paste your Resource ID, then select the "+" button to add it.
253
285
* 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,
257
289
* Use your Azure account with the review APIs:
258
290
* https://docs.microsoft.com/en-us/azure/cognitive-services/content-moderator/review-tool-user-guide/configure
259
291
*/
292
+ // <snippet_review_item>
260
293
// Associates the review ID (assigned by the service) to the internal.
261
294
public class ReviewItem
262
295
{
@@ -269,7 +302,9 @@ public class ReviewItem
269
302
// The ID that the service assigned to the review.
270
303
public string ReviewId ;
271
304
}
305
+ // </snippet_review_item>
272
306
307
+ // <snippet_createreview_fields>
273
308
// Create the reviews using the fixed list of images.
274
309
private static void CreateReviews ( ContentModeratorClient client , string [ ] ImageUrls , string teamName , string endpoint )
275
310
{
@@ -303,7 +338,9 @@ private static void CreateReviews(ContentModeratorClient client, string[] ImageU
303
338
304
339
// The cached review information, associating a local content ID to the created review ID for each item.
305
340
List < ReviewItem > reviewItems = new List < ReviewItem > ( ) ;
341
+ // </snippet_createreview_fields>
306
342
343
+ // <snippet_createreview_create>
307
344
using ( TextWriter outputWriter = new StreamWriter ( OutputFile , false ) )
308
345
{
309
346
writer = outputWriter ;
@@ -340,7 +377,9 @@ private static void CreateReviews(ContentModeratorClient client, string[] ImageU
340
377
}
341
378
342
379
var reviewResponse = client . Reviews . CreateReviewsWithHttpMessagesAsync ( "application/json" , teamName , requestInfo ) ;
380
+ // </snippet_createreview_create>
343
381
382
+ // <snippet_createreview_ids>
344
383
// Update the local cache to associate the created review IDs with the associated content.
345
384
var reviewIds = reviewResponse . Result . Body ;
346
385
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
359
398
WriteLine ( outputWriter , JsonConvert . SerializeObject ( reviewDetail . Result . Body , Formatting . Indented ) ) ;
360
399
Thread . Sleep ( throttleRate ) ;
361
400
}
401
+ // </snippet_createreview_ids>
362
402
403
+ // <snippet_createreview_results>
363
404
Console . WriteLine ( ) ;
364
405
Console . WriteLine ( "Perform manual reviews on the Content Moderator site." ) ;
365
406
Console . WriteLine ( "Then, press any key to continue." ) ;
@@ -391,14 +432,17 @@ private static void CreateReviews(ContentModeratorClient client, string[] ImageU
391
432
}
392
433
Console . WriteLine ( "--------------------------------------------------------------" ) ;
393
434
}
435
+ // </snippet_createreview_results>
394
436
437
+ // <snippet_writeline>
395
438
// Helper function that writes a message to the log file, and optionally to the console.
396
439
// If echo is set to true, details will be written to the console.
397
440
private static void WriteLine ( TextWriter writer , string message = null , bool echo = true )
398
441
{
399
442
writer . WriteLine ( message ?? String . Empty ) ;
400
443
if ( echo ) { Console . WriteLine ( message ?? String . Empty ) ; }
401
444
}
445
+ // </snippet_writeline>
402
446
/*
403
447
* END - CREATE HUMAN IMAGE REVIEWS
404
448
*/
0 commit comments