Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/Azure/aihub
Browse files Browse the repository at this point in the history
  • Loading branch information
heblasco committed Apr 22, 2024
2 parents 2a8a5cb + 1536e6b commit fc73208
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions src/AIHub/Controllers/VideoAnalyzerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using System.Text.Json.Serialization;
using System.Linq.Expressions;


namespace MVCWeb.Controllers;

public class VideoAnalyzerController : Controller
Expand All @@ -26,7 +25,6 @@ public class VideoAnalyzerController : Controller
private VideoAnalyzerModel model;
private HttpClient httpClient;



public VideoAnalyzerController(IConfiguration config, IHttpClientFactory clientFactory)
{
Expand Down Expand Up @@ -54,38 +52,35 @@ public class acvDocumentIdWrapper
public string? AcvDocumentId { get; set; }
}

static async Task<HttpResponseMessage> CreateVideoIndex(string visionApiEndpoint, string visionApiKey, string indexName)
async Task<HttpResponseMessage> CreateVideoIndex(string visionApiEndpoint, string visionApiKey, string indexName)
{
using var client = new HttpClient();
string url = $"{visionApiEndpoint}/retrieval/indexes/{indexName}?api-version=2023-05-01-preview";
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", visionApiKey);
httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", visionApiKey);
var data = new { features = new[] { new { name = "vision", domain = "surveillance" } } };
var content = new StringContent(JsonSerializer.Serialize(data), Encoding.UTF8, "application/json");
var response = await client.PutAsync(url, content);
var content = new StringContent(JsonSerializer.Serialize(data), Encoding.UTF8, "application/json");
var response = await httpClient.PutAsync(url, content);
return response;
}

static async Task<HttpResponseMessage> AddVideoToIndex(string visionApiEndpoint, string visionApiKey, string indexName, string videoUrl, string videoId)
async Task<HttpResponseMessage> AddVideoToIndex(string visionApiEndpoint, string visionApiKey, string indexName, string videoUrl, string videoId)
{
using var client = new HttpClient();
string url = $"{visionApiEndpoint}/retrieval/indexes/{indexName}/ingestions/my-ingestion?api-version=2023-05-01-preview";
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", visionApiKey);
httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", visionApiKey);
var data = new { videos = new[] { new { mode = "add", documentId = videoId, documentUrl = videoUrl } } };
var content = new StringContent(JsonSerializer.Serialize(data), Encoding.UTF8, "application/json");
var response = await client.PutAsync(url, content);
var response = await httpClient.PutAsync(url, content);
return response;
}

static async Task<bool> WaitForIngestionCompletion(string visionApiEndpoint, string visionApiKey, string indexName, int maxRetries = 30)
async Task<bool> WaitForIngestionCompletion(string visionApiEndpoint, string visionApiKey, string indexName, int maxRetries = 30)
{
using var client = new HttpClient();
string url = $"{visionApiEndpoint}/retrieval/indexes/{indexName}/ingestions?api-version=2023-05-01-preview";
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", visionApiKey);
httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", visionApiKey);
int retries = 0;
while (retries < maxRetries)
{
await Task.Delay(10000);
var response = await client.GetAsync(url);
var response = await httpClient.GetAsync(url);
if (response.IsSuccessStatusCode)
{
var stateData = JsonSerializer.Deserialize<dynamic>(await response.Content.ReadAsStringAsync());
Expand Down Expand Up @@ -148,7 +143,7 @@ public async Task<IActionResult> DenseCaptionVideo(string video_url, string prom
}
var payload = new
{
model = "gpt-4-vision-preview",
model = "gpt4-vision",
dataSources = new[]
{
new
Expand Down Expand Up @@ -177,7 +172,7 @@ public async Task<IActionResult> DenseCaptionVideo(string video_url, string prom
}
},
new {
role = "user",
role = "user",
content = new object[]
{
new {
Expand All @@ -202,15 +197,15 @@ public async Task<IActionResult> DenseCaptionVideo(string video_url, string prom
chatResponse.EnsureSuccessStatusCode();
var responseContent = JsonSerializer.Deserialize<JsonObject>(await chatResponse.Content.ReadAsStringAsync());
Console.WriteLine(responseContent);

model.Message = responseContent?["choices"]?[0]?["message"]?["content"]?.ToString();
model.Video = VIDEO_FILE_SAS_URL;
}
catch
{
Console.WriteLine($"Error after GPT4V: {response.StatusCode}, {response.ReasonPhrase}");
}

return View("VideoAnalyzer", model);
}

Expand Down

0 comments on commit fc73208

Please sign in to comment.