Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement HTTP Webcast Example + Model Params When Null #270

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Deepgram.Dev.sln
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TriggerCancel", "tests\expe
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExerciseTimeout", "tests\expected_failures\websocket\exercise_timeout\ExerciseTimeout.csproj", "{8C5A02D8-E18B-4060-A567-A4AACC49B2F6}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "http", "http", "{5DFC6330-8303-4045-B186-32B396391D31}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Streaming", "examples\streaming\http\Streaming.csproj", "{61B42F6B-521C-4A07-B592-54975F4AE2E6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -261,6 +265,10 @@ Global
{8C5A02D8-E18B-4060-A567-A4AACC49B2F6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8C5A02D8-E18B-4060-A567-A4AACC49B2F6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8C5A02D8-E18B-4060-A567-A4AACC49B2F6}.Release|Any CPU.Build.0 = Release|Any CPU
{61B42F6B-521C-4A07-B592-54975F4AE2E6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{61B42F6B-521C-4A07-B592-54975F4AE2E6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{61B42F6B-521C-4A07-B592-54975F4AE2E6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{61B42F6B-521C-4A07-B592-54975F4AE2E6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -329,6 +337,8 @@ Global
{DF383769-5F1F-4692-854F-2E923F3D0F09} = {D100AACD-E5FB-45E5-980B-B19C091440B2}
{F736604C-A245-411E-BECE-028FBD98166E} = {80E7546A-4CED-4644-8900-983422CDE73C}
{8C5A02D8-E18B-4060-A567-A4AACC49B2F6} = {3AC17A9C-30BB-4298-8E5A-4FAE73189821}
{5DFC6330-8303-4045-B186-32B396391D31} = {132EEE99-6194-477A-9416-D7EF173C17FD}
{61B42F6B-521C-4A07-B592-54975F4AE2E6} = {5DFC6330-8303-4045-B186-32B396391D31}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8D4ABC6D-7126-4EE2-9303-43A954616B2A}
Expand Down
4 changes: 3 additions & 1 deletion Deepgram/Factory/HttpClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@

public static HttpClient Create(string? httpId = null)
{
if (string.IsNullOrWhiteSpace(httpId))
if (string.IsNullOrEmpty(httpId))
httpId = HTTPCLIENT_NAME;

Log.Information("HttpClientFactory.Create", $"HttpClient ID: {httpId}");

var services = new ServiceCollection();
services.AddHttpClient(httpId ?? HTTPCLIENT_NAME)
.AddTransientHttpErrorPolicy(policyBuilder =>
policyBuilder.WaitAndRetryAsync(Backoff.DecorrelatedJitterBackoffV2(TimeSpan.FromSeconds(1), 5)));
var sp = services.BuildServiceProvider();

var client = sp.GetRequiredService<IHttpClientFactory>().CreateClient(httpId);

Check warning on line 27 in Deepgram/Factory/HttpClientFactory.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'name' in 'HttpClient IHttpClientFactory.CreateClient(string name)'.

Check warning on line 27 in Deepgram/Factory/HttpClientFactory.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'name' in 'HttpClient IHttpClientFactory.CreateClient(string name)'.

Check warning on line 27 in Deepgram/Factory/HttpClientFactory.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'name' in 'HttpClient IHttpClientFactory.CreateClient(string name)'.

Check warning on line 27 in Deepgram/Factory/HttpClientFactory.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'name' in 'HttpClient IHttpClientFactory.CreateClient(string name)'.

Check warning on line 27 in Deepgram/Factory/HttpClientFactory.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'name' in 'HttpClient IHttpClientFactory.CreateClient(string name)'.

Check warning on line 27 in Deepgram/Factory/HttpClientFactory.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'name' in 'HttpClient IHttpClientFactory.CreateClient(string name)'.

Check warning on line 27 in Deepgram/Factory/HttpClientFactory.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'name' in 'HttpClient IHttpClientFactory.CreateClient(string name)'.

Check warning on line 27 in Deepgram/Factory/HttpClientFactory.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'name' in 'HttpClient IHttpClientFactory.CreateClient(string name)'.

Check warning on line 27 in Deepgram/Factory/HttpClientFactory.cs

View workflow job for this annotation

GitHub Actions / test (6.0.x)

Possible null reference argument for parameter 'name' in 'HttpClient IHttpClientFactory.CreateClient(string name)'.

Check warning on line 27 in Deepgram/Factory/HttpClientFactory.cs

View workflow job for this annotation

GitHub Actions / test (6.0.x)

Possible null reference argument for parameter 'name' in 'HttpClient IHttpClientFactory.CreateClient(string name)'.

Check warning on line 27 in Deepgram/Factory/HttpClientFactory.cs

View workflow job for this annotation

GitHub Actions / test (6.0.x)

Possible null reference argument for parameter 'name' in 'HttpClient IHttpClientFactory.CreateClient(string name)'.

Check warning on line 27 in Deepgram/Factory/HttpClientFactory.cs

View workflow job for this annotation

GitHub Actions / test (6.0.x)

Possible null reference argument for parameter 'name' in 'HttpClient IHttpClientFactory.CreateClient(string name)'.

Check warning on line 27 in Deepgram/Factory/HttpClientFactory.cs

View workflow job for this annotation

GitHub Actions / test (7.0.x)

Possible null reference argument for parameter 'name' in 'HttpClient IHttpClientFactory.CreateClient(string name)'.

Check warning on line 27 in Deepgram/Factory/HttpClientFactory.cs

View workflow job for this annotation

GitHub Actions / test (7.0.x)

Possible null reference argument for parameter 'name' in 'HttpClient IHttpClientFactory.CreateClient(string name)'.

Check warning on line 27 in Deepgram/Factory/HttpClientFactory.cs

View workflow job for this annotation

GitHub Actions / test (7.0.x)

Possible null reference argument for parameter 'name' in 'HttpClient IHttpClientFactory.CreateClient(string name)'.

Check warning on line 27 in Deepgram/Factory/HttpClientFactory.cs

View workflow job for this annotation

GitHub Actions / test (7.0.x)

Possible null reference argument for parameter 'name' in 'HttpClient IHttpClientFactory.CreateClient(string name)'.

Check warning on line 27 in Deepgram/Factory/HttpClientFactory.cs

View workflow job for this annotation

GitHub Actions / test (8.0.x)

Possible null reference argument for parameter 'name' in 'HttpClient IHttpClientFactory.CreateClient(string name)'.

Check warning on line 27 in Deepgram/Factory/HttpClientFactory.cs

View workflow job for this annotation

GitHub Actions / test (8.0.x)

Possible null reference argument for parameter 'name' in 'HttpClient IHttpClientFactory.CreateClient(string name)'.

Check warning on line 27 in Deepgram/Factory/HttpClientFactory.cs

View workflow job for this annotation

GitHub Actions / test (8.0.x)

Possible null reference argument for parameter 'name' in 'HttpClient IHttpClientFactory.CreateClient(string name)'.

Check warning on line 27 in Deepgram/Factory/HttpClientFactory.cs

View workflow job for this annotation

GitHub Actions / test (8.0.x)

Possible null reference argument for parameter 'name' in 'HttpClient IHttpClientFactory.CreateClient(string name)'.

// this is ok because we are using CancellationTokenSource with a default DefaultRESTTimeout timeout
client.Timeout = Timeout.InfiniteTimeSpan;
Expand Down
6 changes: 3 additions & 3 deletions Deepgram/Models/Live/v1/Alternative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ public record Alternative
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("transcript")]
public string Transcript { get; set; }
public string? Transcript { get; set; }
/// <summary>
/// Value between 0 and 1 indicating the model's relative confidence in this transcript.
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("confidence")]
public double Confidence { get; set; }
public double? Confidence { get; set; }

/// <summary>
/// ReadOnly List of <see cref="Word"/> objects.
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("words")]
public IReadOnlyList<Word> Words { get; set; }
public IReadOnlyList<Word>? Words { get; set; }
}
2 changes: 1 addition & 1 deletion Deepgram/Models/Live/v1/Channel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ public record Channel
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("alternatives")]
public IReadOnlyList<Alternative> Alternatives { get; set; }
public IReadOnlyList<Alternative>? Alternatives { get; set; }
}
6 changes: 3 additions & 3 deletions Deepgram/Models/Live/v1/Metadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ public record MetaData
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("request_id")]
public string RequestId { get; set; }
public string? RequestId { get; set; }

/// <summary>
/// Model UUID
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("model_uuid")]
public string ModelUUID { get; set; }
public string? ModelUUID { get; set; }

/// <summary>
/// IReadonlyDictionary of <see cref="ModelInfo"/>
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("model_info")]
public ModelInfo ModelInfo { get; set; }
public ModelInfo? ModelInfo { get; set; }

/// <summary>
/// Deepgram’s Extra Metadata feature allows you to attach arbitrary key-value pairs to your API requests that are attached to the API response for usage in downstream processing.
Expand Down
6 changes: 3 additions & 3 deletions Deepgram/Models/Live/v1/ModelInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ public record ModelInfo
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("arch")]
public string Arch { get; set; }
public string? Arch { get; set; }

/// <summary>
/// Name of the model
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("name")]
public string Name { get; set; }
public string? Name { get; set; }

/// <summary>
/// Version of the model
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("version")]
public string Version { get; set; }
public string? Version { get; set; }
}
16 changes: 8 additions & 8 deletions Deepgram/Models/Live/v1/ResultResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,57 +10,57 @@ public record ResultResponse
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("channel")]
public Channel Channel { get; set; }
public Channel? Channel { get; set; }

/// <summary>
/// Channel index.
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("channel_index")]
public IReadOnlyList<int> ChannelIndex { get; set; }
public IReadOnlyList<int>? ChannelIndex { get; set; }

/// <summary>
/// Duration of the result.
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("duration")]
public double Duration { get; set; }
public decimal? Duration { get; set; }

/// <summary>
/// Is the result final.
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("is_final")]
public bool IsFinal { get; set; }
public bool? IsFinal { get; set; }

/// <summary>
/// Metadata information.
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("metadata")]
public MetaData MetaData { get; set; }
public MetaData? MetaData { get; set; }

/// <summary>
/// Is the result a partial result.
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("speech_final")]
public bool SpeechFinal { get; set; }
public bool? SpeechFinal { get; set; }

/// <summary>
/// Start time of the result.
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("start")]
public decimal Start { get; set; }
public decimal? Start { get; set; }

/// <summary>
/// Result event type.
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("type")]
[JsonConverter(typeof(JsonStringEnumConverter))]
public LiveType Type { get; set; } = LiveType.Results;
public LiveType? Type { get; set; } = LiveType.Results;

// TODO: DYV is this needed???
/// <summary>
Expand Down
8 changes: 4 additions & 4 deletions Deepgram/Models/Live/v1/Word.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,28 @@ public record Word
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("word")]
public string HeardWord { get; set; }
public string? HeardWord { get; set; }

/// <summary>
/// Offset in seconds from the start of the audio to where the spoken word starts.
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("start")]
public decimal Start { get; set; }
public decimal? Start { get; set; }

/// <summary>
/// Offset in seconds from the start of the audio to where the spoken word ends.
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("end")]
public decimal End { get; set; }
public decimal? End { get; set; }

/// <summary>
/// Value between 0 and 1 indicating the model's relative confidence in this word.
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("confidence")]
public double Confidence { get; set; }
public double? Confidence { get; set; }

/// <summary>
/// Punctuated version of the word
Expand Down
2 changes: 1 addition & 1 deletion Deepgram/Models/PreRecorded/v1/Topic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ public record Topic
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("topic")]
public string Text;
public string? Text;
}

12 changes: 10 additions & 2 deletions examples/analyze/intent/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Text.Json;

using Deepgram.Models.Analyze.v1;
using Deepgram.Logger;

namespace PreRecorded
{
Expand All @@ -14,7 +15,9 @@ static async Task Main(string[] args)
{
// Initialize Library with default logging
// Normal logging is "Info" level
Library.Initialize();
// Library.Initialize();
// OR to set logging level
Library.Initialize(LogLevel.Debug);

// JSON options
JsonSerializerOptions options = new(JsonSerializerDefaults.Web)
Expand All @@ -32,14 +35,19 @@ static async Task Main(string[] args)
return;
}

// increase timeout to 60 seconds
CancellationTokenSource cancelToken = new CancellationTokenSource();
cancelToken.CancelAfter(TimeSpan.FromSeconds(120));

var audioData = File.ReadAllBytes(@"conversation.txt");
var response = await deepgramClient.AnalyzeFile(
audioData,
new AnalyzeSchema()
{
Language = "en",
Intents = true,
});
},
cancelToken);

Console.WriteLine($"\n\n{JsonSerializer.Serialize(response, options)}\n\n");
Console.ReadKey();
Expand Down
75 changes: 75 additions & 0 deletions examples/streaming/http/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved.
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT
// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved.
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT

using Deepgram.Models.Live.v1;
using Deepgram.Logger;
using System.Net.WebSockets;

namespace SampleApp
{
class Program
{
static async Task Main(string[] args)
{
// Initialize Library with default logging
Library.Initialize();

// Set "DEEPGRAM_API_KEY" environment variable to your Deepgram API Key
var liveClient = new LiveClient();

// Subscribe to the EventResponseReceived event
liveClient.Subscribe(new EventHandler<ResultResponse>((sender, e) =>
{
if (e.Channel.Alternatives[0].Transcript == "")
{
return;
}
Console.WriteLine($"Speaker: {e.Channel.Alternatives[0].Transcript}");
}));

// Start the connection
var liveSchema = new LiveSchema()
{
Model = "nova-2",
Punctuate = true,
SmartFormat = true,
};
await liveClient.Connect(liveSchema);

// get the webcast data... this is a blocking operation
try
{
var url = "http://stream.live.vc.bbcmedia.co.uk/bbc_world_service";
using (HttpClient client = new HttpClient())
{
using (Stream receiveStream = await client.GetStreamAsync(url))
{
while (liveClient.State() == WebSocketState.Open)
{
byte[] buffer = new byte[2048];
await receiveStream.ReadAsync(buffer, 0, buffer.Length);
liveClient.Send(buffer);
}
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}

// Stop the connection
await liveClient.Stop();

// Dispose the client
liveClient.Dispose();

// Teardown Library
Library.Terminate();
}
}
}
25 changes: 25 additions & 0 deletions examples/streaming/http/Streaming.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\Deepgram\Deepgram.csproj" />
</ItemGroup>

<ItemGroup>
<Using Include="Deepgram" />
</ItemGroup>

<ItemGroup>
<None Update="preamble.wav">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>


</Project>
Loading