Skip to content

Commit

Permalink
Merge pull request #3 from cmendible/docker
Browse files Browse the repository at this point in the history
.NET 8, dockerfile, global usings & formating issues
  • Loading branch information
rag2111 authored Dec 1, 2023
2 parents 9b46855 + 8c0a2e4 commit 9645b36
Show file tree
Hide file tree
Showing 22 changed files with 312 additions and 337 deletions.
2 changes: 1 addition & 1 deletion src/AIHub/AIHub.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
Expand Down
84 changes: 34 additions & 50 deletions src/AIHub/Controllers/BrandAnalyzerController.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using MVCWeb.Models;
using Azure.AI.ContentSafety;
using Azure;
using ContentSafetySampleCode;
using System;
using Azure.Storage.Blobs;
using Azure.Identity;
using Azure.AI.OpenAI;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json;

namespace MVCWeb.Controllers;

public class BrandAnalyzerController : Controller
Expand All @@ -31,10 +15,10 @@ public class BrandAnalyzerController : Controller
public BrandAnalyzerController(IConfiguration config)
{
_config = config;
Bingendpoint= _config.GetValue<string>("BrandAnalyzer:BingEndpoint");
BingsubscriptionKey= _config.GetValue<string>("BrandAnalyzer:BingKey");
AOAIendpoint= _config.GetValue<string>("BrandAnalyzer:OpenAIEndpoint");
AOAIsubscriptionKey= _config.GetValue<string>("BrandAnalyzer:OpenAISubscriptionKey");
Bingendpoint = _config.GetValue<string>("BrandAnalyzer:BingEndpoint");
BingsubscriptionKey = _config.GetValue<string>("BrandAnalyzer:BingKey");
AOAIendpoint = _config.GetValue<string>("BrandAnalyzer:OpenAIEndpoint");
AOAIsubscriptionKey = _config.GetValue<string>("BrandAnalyzer:OpenAISubscriptionKey");
model = new BrandAnalyzerModel();

}
Expand All @@ -50,31 +34,31 @@ public async Task<IActionResult> AnalyzeCompany()

model.CompanyName = HttpContext.Request.Form["companyName"];
model.Prompt = HttpContext.Request.Form["prompt"];
string input_context="";
string input_context = "";

if (CheckNullValues(model.CompanyName))
if (CheckNullValues(model.CompanyName))
{
ViewBag.Message = "You must enter a value for Company name";
return View("BrandAnalyzer");
}
string query_bing = model.CompanyName+" opiniones de usuarios";
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(Bingendpoint);

// Add an Accept header for JSON format.
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", BingsubscriptionKey);
string uri=Bingendpoint+"?q="+query_bing+"&mkt=es-ES&count=100";
// List data response.
HttpResponseMessage response = client.GetAsync(uri).Result; // Blocking call! Program will wait here until a response is received or a timeout occurs.
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();

string query_bing = model.CompanyName + " opiniones de usuarios";
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(Bingendpoint);

// Add an Accept header for JSON format.
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", BingsubscriptionKey);
string uri = Bingendpoint + "?q=" + query_bing + "&mkt=es-ES&count=100";
// List data response.
HttpResponseMessage response = client.GetAsync(uri).Result; // Blocking call! Program will wait here until a response is received or a timeout occurs.
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
// Above three lines can be replaced with new helper method below
// string responseBody = await client.GetStringAsync(uri);

response.EnsureSuccessStatusCode();
response.EnsureSuccessStatusCode();

// Parse the response as JSON
try
Expand All @@ -86,22 +70,22 @@ public async Task<IActionResult> AnalyzeCompany()
// Iterate over the news items and print them
foreach (var i in news)
{
input_context=input_context+i.name+"\n"+i.snippet+"\n"+i.url+"\n"+"-------";
input_context = input_context + i.name + "\n" + i.snippet + "\n" + i.url + "\n" + "-------";
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
client.Dispose();

client.Dispose();

try
{
OpenAIClient client_oai = new OpenAIClient(
new Uri(AOAIendpoint),
new AzureKeyCredential(AOAIsubscriptionKey));

OpenAIClient client_oai = new OpenAIClient(
new Uri(AOAIendpoint),
new AzureKeyCredential(AOAIsubscriptionKey));

// ### If streaming is not selected
Response<ChatCompletions> responseWithoutStream = await client_oai.GetChatCompletionsAsync(
Expand All @@ -121,9 +105,9 @@ public async Task<IActionResult> AnalyzeCompany()
});

ChatCompletions completions = responseWithoutStream.Value;
ChatChoice results_analisis= completions.Choices[0];
ViewBag.Message =
//"Hate severity: " + (response.Value.HateResult?.Severity ?? 0);
ChatChoice results_analisis = completions.Choices[0];
ViewBag.Message =
//"Hate severity: " + (response.Value.HateResult?.Severity ?? 0);
results_analisis.Message.Content
;
}
Expand All @@ -135,7 +119,7 @@ public async Task<IActionResult> AnalyzeCompany()
return View("BrandAnalyzer", model);
}

[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
Expand All @@ -146,7 +130,7 @@ private bool CheckNullValues(string companyName)
if (string.IsNullOrEmpty(companyName))
{
return true;
}
}
return false;
}
}
36 changes: 10 additions & 26 deletions src/AIHub/Controllers/CallCenterController.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using MVCWeb.Models;
using Azure.AI.ContentSafety;
using Azure;
using ContentSafetySampleCode;
using System;
using Azure.Storage.Blobs;
using Azure.Identity;
using Azure.AI.OpenAI;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json;

namespace MVCWeb.Controllers;

public class CallCenterController : Controller
Expand All @@ -29,8 +13,8 @@ public class CallCenterController : Controller
public CallCenterController(IConfiguration config)
{
_config = config;
endpoint= _config.GetValue<string>("CallCenter:OpenAIEndpoint");
subscriptionKey= _config.GetValue<string>("CallCenter:OpenAISubscriptionKey");
endpoint = _config.GetValue<string>("CallCenter:OpenAIEndpoint");
subscriptionKey = _config.GetValue<string>("CallCenter:OpenAISubscriptionKey");
model = new CallCenterModel();

}
Expand All @@ -53,10 +37,10 @@ public async Task<IActionResult> AnalyzeCall()
}
try
{
OpenAIClient client_oai = new OpenAIClient(
new Uri(endpoint),
new AzureKeyCredential(subscriptionKey));

OpenAIClient client_oai = new OpenAIClient(
new Uri(endpoint),
new AzureKeyCredential(subscriptionKey));

// ### If streaming is not selected
Response<ChatCompletions> responseWithoutStream = await client_oai.GetChatCompletionsAsync(
Expand All @@ -76,9 +60,9 @@ public async Task<IActionResult> AnalyzeCall()
});

ChatCompletions completions = responseWithoutStream.Value;
ChatChoice results_analisis= completions.Choices[0];
System.Console.WriteLine(results_analisis);
ViewBag.Message =
ChatChoice results_analisis = completions.Choices[0];
System.Console.WriteLine(results_analisis);
ViewBag.Message =
results_analisis.Message.Content
;
}
Expand All @@ -105,7 +89,7 @@ private bool CheckNullValues(string companyName, string prompt)
if (string.IsNullOrEmpty(prompt))
{
return true;
}
}
return false;
}
}
21 changes: 5 additions & 16 deletions src/AIHub/Controllers/ContentSafetyController.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using MVCWeb.Models;
using Azure.AI.ContentSafety;
using Azure;
using ContentSafetySampleCode;
using System;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using Azure.Identity;

namespace MVCWeb.Controllers;

public class ContentSafetyController : Controller
Expand Down Expand Up @@ -58,8 +47,8 @@ public ContentSafetyController(IConfiguration config, ILogger<HomeController> lo
sasUri = containerClient.GenerateSasUri(Azure.Storage.Sas.BlobContainerSasPermissions.Read, DateTimeOffset.UtcNow.AddHours(1));
// Obtiene una lista de blobs en el contenedor
blobs = containerClient.GetBlobs();
model = new ContentSafetyModel();
model = new ContentSafetyModel();

}

public IActionResult TextModerator()
Expand Down Expand Up @@ -115,7 +104,7 @@ public IActionResult EvaluateImage(string imageUrl)
"SelfHarm severity: " + (response.Value.SelfHarmResult?.Severity ?? 0) + "\n" +
"Sexual severity: " + (response.Value.SexualResult?.Severity ?? 0) + "\n" +
"Violence severity: " + (response.Value.ViolenceResult?.Severity ?? 0);
ViewBag.Image=imageUrl + sasUri.Query;
ViewBag.Image = imageUrl + sasUri.Query;
}
catch (RequestFailedException ex)
{
Expand All @@ -130,7 +119,7 @@ public IActionResult EvaluateImage(string imageUrl)
[HttpPost]
public IActionResult EvaluateText()
{
if(CheckNullValues(HttpContext))
if (CheckNullValues(HttpContext))
{
ViewBag.Message = "You must enter a value for each threshold";
return View("TextModerator", model);
Expand All @@ -147,7 +136,7 @@ public IActionResult EvaluateText()
model.Hate = Convert.ToInt32(HttpContext.Request.Form["hatetext"]);
model.Text = HttpContext.Request.Form["text"];
model.Approve = true;

ContentSafetyClient client = new ContentSafetyClient(new Uri(endpoint), new AzureKeyCredential(subscriptionKey));

var request = new AnalyzeTextOptions(model.Text);
Expand Down
22 changes: 0 additions & 22 deletions src/AIHub/Controllers/FormAnalyzerController.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,3 @@
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using MVCWeb.Models;
using Azure.AI.ContentSafety;
using Azure;
using ContentSafetySampleCode;
using System;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using Azure.Identity;
using Azure.AI.OpenAI;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Runtime.InteropServices;


namespace MVCWeb.Controllers;

public class FormAnalyzerController : Controller
Expand Down
14 changes: 2 additions & 12 deletions src/AIHub/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using MVCWeb.Models;
using Azure.AI.ContentSafety;
using Azure;
using ContentSafetySampleCode;
using System;
using Azure.Storage.Blobs;
using Azure.Identity;

namespace MVCWeb.Controllers;
namespace MVCWeb.Controllers;

public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly IConfiguration _config;
private readonly ContentSafetyClient _client;

private string endpoint;
private string subscriptionKey;
private string storageconnstring;
Expand Down
Loading

0 comments on commit 9645b36

Please sign in to comment.