Skip to content

Commit 2513257

Browse files
committed
update c# qss
1 parent 38f5fce commit 2513257

File tree

2 files changed

+78
-54
lines changed

2 files changed

+78
-54
lines changed

articles/ai-services/content-safety/includes/quickstarts/csharp-quickstart-image.md

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ ms.author: pafarley
1515
## Prerequisites
1616

1717
* An Azure subscription - [Create one for free](https://azure.microsoft.com/free/cognitive-services/)
18-
* The [Visual Studio IDE](https://visualstudio.microsoft.com/vs/) with workload .NET desktop development enabled. Or if you don't plan on using Visual Studio IDE, you need [.NET 6.0](https://dotnet.microsoft.com/download/dotnet-core) SDK or above installed.
18+
* The [Visual Studio IDE](https://visualstudio.microsoft.com/vs/) with workload .NET desktop development enabled. Or if you don't plan on using Visual Studio IDE, you need the current version of [.NET Core](https://dotnet.microsoft.com/download/dotnet-core).
1919
* [.NET Runtime](https://dotnet.microsoft.com/download/dotnet/) installed.
2020
* Once you have your Azure subscription, <a href="https://aka.ms/acs-create" title="Create a Content Safety resource" target="_blank">create a Content Safety resource </a> in the Azure portal to get your key and endpoint. Enter a unique name for your resource, select the subscription you entered on the application form, select a resource group, supported region, and supported pricing tier. Then select **Create**.
2121
* The resource takes a few minutes to deploy. After it finishes, Select **go to resource**. In the left pane, under **Resource Management**, select **Subscription Key and Endpoint**. The endpoint and either of the keys are used to call APIs.
@@ -73,37 +73,52 @@ dotnet add package Azure.AI.ContentSafety --prerelease
7373
From the project directory, open the *Program.cs* file that was created previously. Paste in the following code:
7474

7575
```csharp
76-
// retrieve the endpoint and key from the environment variables created earlier
77-
string endpoint = Environment.GetEnvironmentVariable("CONTENT_SAFETY_ENDPOINT");
78-
string key = Environment.GetEnvironmentVariable("CONTENT_SAFETY_KEY");
76+
using System;
77+
using Azure.AI.ContentSafety;
7978

80-
ContentSafetyClient client = new ContentSafetyClient(new Uri(endpoint), new AzureKeyCredential(key));
81-
82-
string datapath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Samples", "sample_data", "image.jpg");
83-
ImageData image = new ImageData() { Content = BinaryData.FromBytes(File.ReadAllBytes(datapath)) };
84-
85-
var request = new AnalyzeImageOptions(image);
86-
87-
Response<AnalyzeImageResult> response;
88-
try
89-
{
90-
response = client.AnalyzeImage(request);
91-
}
92-
catch (RequestFailedException ex)
79+
namespace Azure.AI.ContentSafety.Dotnet.Sample
9380
{
94-
Console.WriteLine("Analyze image failed.\nStatus code: {0}, Error code: {1}, Error message: {2}", ex.Status, ex.ErrorCode, ex.Message);
95-
throw;
81+
class ContentSafetySampleAnalyzeImage
82+
{
83+
public static void AnalyzeImage()
84+
{
85+
// retrieve the endpoint and key from the environment variables created earlier
86+
string endpoint = Environment.GetEnvironmentVariable("CONTENT_SAFETY_ENDPOINT");
87+
string key = Environment.GetEnvironmentVariable("CONTENT_SAFETY_KEY");
88+
89+
ContentSafetyClient client = new ContentSafetyClient(new Uri(endpoint), new AzureKeyCredential(key));
90+
91+
// Example: analyze image
92+
93+
string imagePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Samples", "sample_data", "image.jpg");
94+
ImageData image = new ImageData() { Content = BinaryData.FromBytes(File.ReadAllBytes(imagePath)) };
95+
96+
var request = new AnalyzeImageOptions(image);
97+
98+
Response<AnalyzeImageResult> response;
99+
try
100+
{
101+
response = client.AnalyzeImage(request);
102+
}
103+
catch (RequestFailedException ex)
104+
{
105+
Console.WriteLine("Analyze image failed.\nStatus code: {0}, Error code: {1}, Error message: {2}", ex.Status, ex.ErrorCode, ex.Message);
106+
throw;
107+
}
108+
109+
Console.WriteLine("Hate severity: {0}", response.Value.HateResult?.Severity ?? 0);
110+
Console.WriteLine("SelfHarm severity: {0}", response.Value.SelfHarmResult?.Severity ?? 0);
111+
Console.WriteLine("Sexual severity: {0}", response.Value.SexualResult?.Severity ?? 0);
112+
Console.WriteLine("Violence severity: {0}", response.Value.ViolenceResult?.Severity ?? 0);
113+
}
114+
static void Main()
115+
{
116+
AnalyzeImage();
117+
}
118+
}
96119
}
97-
98-
Console.WriteLine("Hate severity: {0}", response.Value.HateResult?.Severity ?? 0);
99-
Console.WriteLine("SelfHarm severity: {0}", response.Value.SelfHarmResult?.Severity ?? 0);
100-
Console.WriteLine("Sexual severity: {0}", response.Value.SexualResult?.Severity ?? 0);
101-
Console.WriteLine("Violence severity: {0}", response.Value.ViolenceResult?.Severity ?? 0);
102120
```
103121

104-
105-
Then, build and run the application. You should see output similar to the one shown here.
106-
107122
#### [Visual Studio IDE](#tab/visual-studio)
108123

109124
Build and run the application by selecting **Start Debugging** from the **Debug** menu at the top of the IDE window (or press **F5**).

articles/ai-services/content-safety/includes/quickstarts/csharp-quickstart-text.md

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ ms.author: pafarley
1515
## Prerequisites
1616

1717
* An Azure subscription - [Create one for free](https://azure.microsoft.com/free/cognitive-services/)
18-
* The [Visual Studio IDE](https://visualstudio.microsoft.com/vs/) with workload .NET desktop development enabled. Or if you don't plan on using Visual Studio IDE, you need [.NET 6.0](https://dotnet.microsoft.com/download/dotnet-core) SDK or above installed.
19-
* [.NET Runtime](https://dotnet.microsoft.com/download/dotnet/) installed.
18+
* The [Visual Studio IDE](https://visualstudio.microsoft.com/vs/) with workload .NET desktop development enabled. Or if you don't plan on using Visual Studio IDE, you need the current version of [.NET Core](https://dotnet.microsoft.com/download/dotnet-core).
2019
* Once you have your Azure subscription, <a href="https://aka.ms/acs-create" title="Create a Content Safety resource" target="_blank">create a Content Safety resource </a> in the Azure portal to get your key and endpoint. Enter a unique name for your resource, select the subscription you entered on the application form, select a resource group, supported region, and supported pricing tier. Then select **Create**.
2120
* The resource takes a few minutes to deploy. After it finishes, Select **go to resource**. In the left pane, under **Resource Management**, select **Subscription Key and Endpoint**. The endpoint and either of the keys are used to call APIs.
2221

@@ -73,36 +72,46 @@ dotnet add package Azure.AI.ContentSafety --prerelease
7372
From the project directory, open the *Program.cs* file that was created previously. Paste in the following code:
7473

7574
```csharp
76-
// retrieve the endpoint and key from the environment variables created earlier
77-
string endpoint = Environment.GetEnvironmentVariable("CONTENT_SAFETY_ENDPOINT");
78-
string key = Environment.GetEnvironmentVariable("CONTENT_SAFETY_KEY");
75+
using System;
76+
using Azure.AI.ContentSafety;
7977

80-
ContentSafetyClient client = new ContentSafetyClient(new Uri(endpoint), new AzureKeyCredential(key));
81-
82-
string datapath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Samples", "sample_data", "text.txt");
83-
string text = File.ReadAllText(datapath);
84-
85-
var request = new AnalyzeTextOptions(text);
86-
87-
Response<AnalyzeTextResult> response;
88-
try
78+
namespace Azure.AI.ContentSafety.Dotnet.Sample
8979
{
90-
response = client.AnalyzeText(request);
80+
class ContentSafetySampleAnalyzeText
81+
{
82+
public static void AnalyzeText()
83+
{
84+
// retrieve the endpoint and key from the environment variables created earlier
85+
string endpoint = Environment.GetEnvironmentVariable("CONTENT_SAFETY_ENDPOINT");
86+
string key = Environment.GetEnvironmentVariable("CONTENT_SAFETY_KEY");
87+
88+
ContentSafetyClient client = new ContentSafetyClient(new Uri(endpoint), new AzureKeyCredential(key));
89+
90+
string text = "Your input text";
91+
92+
var request = new AnalyzeTextOptions(text);
93+
94+
Response<AnalyzeTextResult> response;
95+
try
96+
{
97+
response = client.AnalyzeText(request);
98+
}
99+
catch (RequestFailedException ex)
100+
{
101+
Console.WriteLine("Analyze text failed.\nStatus code: {0}, Error code: {1}, Error message: {2}", ex.Status, ex.ErrorCode, ex.Message);
102+
throw;
103+
}
104+
105+
Console.WriteLine("Hate severity: {0}", response.Value.HateResult?.Severity ?? 0);
106+
Console.WriteLine("SelfHarm severity: {0}", response.Value.SelfHarmResult?.Severity ?? 0);
107+
Console.WriteLine("Sexual severity: {0}", response.Value.SexualResult?.Severity ?? 0);
108+
Console.WriteLine("Violence severity: {0}", response.Value.ViolenceResult?.Severity ?? 0);
109+
}
110+
}
91111
}
92-
catch (RequestFailedException ex)
93-
{
94-
Console.WriteLine("Analyze text failed.\nStatus code: {0}, Error code: {1}, Error message: {2}", ex.Status, ex.ErrorCode, ex.Message);
95-
throw;
96-
}
97-
98-
Console.WriteLine("Hate severity: {0}", response.Value.HateResult?.Severity ?? 0);
99-
Console.WriteLine("SelfHarm severity: {0}", response.Value.SelfHarmResult?.Severity ?? 0);
100-
Console.WriteLine("Sexual severity: {0}", response.Value.SexualResult?.Severity ?? 0);
101-
Console.WriteLine("Violence severity: {0}", response.Value.ViolenceResult?.Severity ?? 0);
102112
```
103113

104-
105-
Then, build and run the application. You should see output similar to the one shown here.
114+
Replace `"Your input text"` with the text content you'd like to use.
106115

107116
#### [Visual Studio IDE](#tab/visual-studio)
108117

0 commit comments

Comments
 (0)