Skip to content

Commit

Permalink
Update Text Analytics, Custom Model usage and Storage Connector in Tr…
Browse files Browse the repository at this point in the history
…anscription Enabled Storage project (#834)

* update TA + StorageConnector

* Update StorageConnector.cs

* update code style

* Update custom model usage
  • Loading branch information
Henry van der Vegte authored Oct 13, 2020
1 parent acbf6a7 commit 66d7131
Show file tree
Hide file tree
Showing 21 changed files with 126 additions and 167 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,6 @@ private static async Task<TResponse> GetAsync<TResponse>(string path, string sub
requestMessage.Headers.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
}

log.LogInformation($"Timeout: {timeout}");

try
{
using var cts = new CancellationTokenSource();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Storage.Blobs" Version="12.6.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="4.1.1" />
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8">
<PrivateAssets>all</PrivateAssets>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// <copyright file="EntityType.cs" company="Microsoft Corporation">
// <copyright file="EntityCategory.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
// </copyright>

namespace Connector.Enums
{
public enum EntityType
public enum EntityCategory
{
Unknown,
Person,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
namespace Connector
{
using System;
using System.Collections.Generic;
using System.Text;

public static class MessageRetryHelper
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ namespace Connector

public sealed class ModelIdentity
{
private ModelIdentity(Guid id)
private ModelIdentity(string self)
{
this.Id = id;
this.Self = self;
}

public Guid Id { get; private set; }
public string Self { get; private set; }

public static ModelIdentity Create(Guid id)
public static ModelIdentity Create(string region, Guid id)
{
return new ModelIdentity(id);
return new ModelIdentity($"https://{region}.api.cognitive.microsoft.com/speechtotext/v3.0/models/{id}");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// <copyright file="TextAnalyticsScores.cs" company="Microsoft Corporation">
// <copyright file="TextAnalyticsConfidenceScores.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
// </copyright>

namespace Connector.Serializable.TextAnalytics
{
public class TextAnalyticsScores
public class TextAnalyticsConfidenceScores
{
public TextAnalyticsScores(float positive, float neutral, float negative)
public TextAnalyticsConfidenceScores(float positive, float neutral, float negative)
{
Positive = positive;
Neutral = neutral;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ namespace Connector.Serializable.TextAnalytics

public class TextAnalyticsDocument
{
public TextAnalyticsDocument(string id, IEnumerable<TextAnalyticsSentence> sentences, IEnumerable<TextAnalyticsEntity> entities)
public TextAnalyticsDocument(string id, TextAnalyticsConfidenceScores confidenceScores, IEnumerable<TextAnalyticsEntity> entities)
{
Id = id;
Sentences = sentences;
ConfidenceScores = confidenceScores;
Entities = entities;
}

public string Id { get; set; }

public IEnumerable<TextAnalyticsSentence> Sentences { get; private set; }
public TextAnalyticsConfidenceScores ConfidenceScores { get; private set; }

public IEnumerable<TextAnalyticsEntity> Entities { get; private set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,39 @@ namespace Connector.Serializable.TextAnalytics

public class TextAnalyticsEntity
{
public TextAnalyticsEntity(string text, string type, string subType, int offset, int length, float score)
public TextAnalyticsEntity(string text, string category, string subCategory, int offset, int length, float score)
{
Text = text;
TypeString = type;
SubType = subType;
CategoryString = category;
SubCategory = subCategory;
Offset = offset;
Length = length;
Score = score;
}

public string Text { get; private set; }

public string SubType { get; private set; }
public string SubCategory { get; private set; }

public int Offset { get; private set; }

public int Length { get; private set; }

public float Score { get; private set; }

public EntityType Type
public EntityCategory Category
{
get
{
if (Enum.TryParse(TypeString, out EntityType outputType))
if (Enum.TryParse(CategoryString, out EntityCategory outputType))
{
return outputType;
}

return EntityType.Unknown;
return EntityCategory.Unknown;
}
}

private string TypeString { get; set; }
private string CategoryString { get; set; }
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

namespace Connector
{
using System;
using System.Collections.Generic;
using System.Linq;

Expand All @@ -17,13 +16,13 @@ private TranscriptionDefinition(
string locale,
IEnumerable<string> contentUrls,
Dictionary<string, string> properties,
IEnumerable<ModelIdentity> models)
ModelIdentity model)
{
DisplayName = name;
Description = description;
ContentUrls = contentUrls;
Locale = locale;
Models = models;
Model = model;
Properties = properties;
}

Expand All @@ -35,7 +34,7 @@ private TranscriptionDefinition(

public string Locale { get; set; }

public IEnumerable<ModelIdentity> Models { get; set; }
public ModelIdentity Model { get; set; }

public IDictionary<string, string> Properties { get; }

Expand All @@ -45,9 +44,9 @@ public static TranscriptionDefinition Create(
string locale,
string contentUrl,
Dictionary<string, string> properties,
IEnumerable<ModelIdentity> models)
ModelIdentity model)
{
return new TranscriptionDefinition(name, description, locale, new[] { contentUrl }.ToList(), properties, models);
return new TranscriptionDefinition(name, description, locale, new[] { contentUrl }.ToList(), properties, model);
}

public static TranscriptionDefinition Create(
Expand All @@ -56,9 +55,9 @@ public static TranscriptionDefinition Create(
string locale,
IEnumerable<string> contentUrls,
Dictionary<string, string> properties,
IEnumerable<ModelIdentity> models)
ModelIdentity model)
{
return new TranscriptionDefinition(name, description, locale, contentUrls, properties, models);
return new TranscriptionDefinition(name, description, locale, contentUrls, properties, model);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ public RecognizedPhrase(string recognitionStatus, int channel, int speaker, stri
public double DurationInTicks { get; }

[JsonProperty("nBest")]
public IEnumerable<NBest> NBest { get; }
public IEnumerable<NBest> NBest { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@

namespace Connector.Serializable.TranscriptionStartedServiceBusMessage
{
using System;
using System.Collections.Generic;
using System.Text;

public class AudioFileInfo
{
public AudioFileInfo(string fileUrl, int retryCount)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
namespace Connector
{
using System;
using System.Collections;
using System.Collections.Generic;
using Connector.Serializable.TranscriptionStartedServiceBusMessage;
using Newtonsoft.Json;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ namespace Connector
using System.Threading.Tasks;
using Microsoft.Azure.ServiceBus;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

public static class ServiceBusUtilities
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace Connector
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using Azure.Storage;
using Azure.Storage.Sas;
using Microsoft.Extensions.Logging;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
Expand All @@ -18,9 +20,21 @@ public class StorageConnector
{
private CloudBlobClient CloudBlobClient;

private string AccountName;

private string AccountKey;

public StorageConnector(string storageConnectionString)
{
if (storageConnectionString == null)
{
throw new ArgumentNullException(nameof(storageConnectionString));
}

var storageAccount = CloudStorageAccount.Parse(storageConnectionString);

AccountName = GetValueFromConnectionString("AccountName", storageConnectionString);
AccountKey = GetValueFromConnectionString("AccountKey", storageConnectionString);
CloudBlobClient = storageAccount.CreateCloudBlobClient();
}

Expand Down Expand Up @@ -81,28 +95,25 @@ public static async Task<byte[]> DownloadFileFromSAS(string blobSas)
return data;
}

public async Task<string> CreateSASAsync(Uri fileUri)
public string CreateSas(Uri fileUri)
{
var containerName = GetContainerNameFromUri(fileUri);
var fileName = GetFileNameFromUri(fileUri);
var container = CloudBlobClient.GetContainerReference(containerName);

var containerPermissions = new BlobContainerPermissions
var sasBuilder = new BlobSasBuilder()
{
PublicAccess = BlobContainerPublicAccessType.Blob
BlobContainerName = container.Name,
BlobName = fileName,
Resource = "b",
};

await container.SetPermissionsAsync(containerPermissions).ConfigureAwait(false);
containerPermissions.SharedAccessPolicies.Add("mypolicy", new SharedAccessBlobPolicy()
{
SharedAccessStartTime = DateTime.UtcNow,
SharedAccessExpiryTime = DateTime.UtcNow.AddDays(1),
Permissions = SharedAccessBlobPermissions.Read
});

var sas = container.GetSharedAccessSignature(containerPermissions.SharedAccessPolicies.FirstOrDefault().Value);
var absoluteSasUri = fileUri.AbsoluteUri + sas;
sasBuilder.StartsOn = DateTime.UtcNow;
sasBuilder.ExpiresOn = DateTime.UtcNow.AddDays(1);
sasBuilder.SetPermissions(BlobContainerSasPermissions.Read);

return absoluteSasUri;
var sasToken = sasBuilder.ToSasQueryParameters(new StorageSharedKeyCredential(AccountName, AccountKey)).ToString();
return $"{container.GetBlockBlobReference(fileName).Uri}?{sasToken}";
}

public async Task WriteTextFileToBlobAsync(string content, string containerName, string fileName, ILogger log)
Expand Down Expand Up @@ -132,5 +143,20 @@ public async Task MoveFileAsync(string inputContainerName, string inputFileName
await outputBlockBlob.StartCopyAsync(inputBlockBlob).ConfigureAwait(false);
await inputBlockBlob.DeleteAsync().ConfigureAwait(false);
}

private static string GetValueFromConnectionString(string key, string connectionString)
{
var split = connectionString.Split(';');

foreach (var subConnectionString in split)
{
if (subConnectionString.StartsWith(key, StringComparison.OrdinalIgnoreCase))
{
return subConnectionString.Substring(key.Length + 1);
}
}

return string.Empty;
}
}
}
Loading

0 comments on commit 66d7131

Please sign in to comment.