-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
126 lines (111 loc) · 4.43 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using Deepgram;
using System.Text.Json;
using System.Windows;
using Deepgram.Models.PreRecorded.v1;
using System.Net.Http;
using System.Text;
using Deepgram.Models.Manage.v1;
using System.IO;
using Microsoft.Win32;
namespace DeepGram_Sample_Project
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
internal static string apiKey = "{API_KEY}";
internal static string apiUrl = "https://api.deepgram.com/v1/listen?smart_format=true&model=nova-2&language=en-US";
internal static string jsonPayload = "{\"url\":\"https://static.deepgram.com/examples/Bueller-Life-moves-pretty-fast.wav\"}";
internal static string path = String.Empty;
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.DefaultExt = ".m4a";
dialog.Filter = "Audio Files (*.wav, *.mp3, *.m4a, *.mp4, *.mp2, *.aac, *.flac, *.pcm, *.ogg, *.opus, *.webm)|*.wav;*.mp3;*.m4a;*.mp4;*.mp2;*.aac;*.flac;*.pcm;*.ogg;*.opus;*.webm";
if (true == dialog.ShowDialog())
{
path = dialog.FileName;
}
tb_fileName.Text = path;
}
internal static async Task<string> MakeApiCallForWebsiteSample()
{
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", $"Token {apiKey}");
var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync(apiUrl, content);
if (response.IsSuccessStatusCode)
{
return await response.Content.ReadAsStringAsync();
}
else
{
return $"Error: {response.StatusCode}";
}
}
}
internal static async Task<string> MakeApiCall()
{
using (HttpClient client = new HttpClient())
{
var request = new HttpRequestMessage(HttpMethod.Post, "https://api.deepgram.com/v1/listen");
client.DefaultRequestHeaders.Add("Authorization", $"Token {apiKey}");
request.Content = new StreamContent(File.OpenRead(path));
var response = await client.SendAsync(request);
// response.EnsureSuccessStatusCode();
Console.WriteLine(await response.Content.ReadAsStringAsync());
if (response.IsSuccessStatusCode)
{
return await response.Content.ReadAsStringAsync();
}
else
{
return $"Error: {response.StatusCode}";
}
}
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
// Initialize Library with default logging ("Info" level)
Library.Initialize();
// The API key we created in step 3
//var deepgramClient = new ListenRESTClient(secret2);
// Hosted sample file
//var audioUrl = "https://static.deepgram.com/examples/Bueller-Life-moves-pretty-fast.wav";
try
{
string result = Task.Run(async () => await MakeApiCall()).GetAwaiter().GetResult();
tbb_response.Text = result;
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
Library.Terminate();
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
Library.Initialize();
// The API key we created in step 3
//var deepgramClient = new ListenRESTClient(secret2);
// Hosted sample file
//var audioUrl = "https://static.deepgram.com/examples/Bueller-Life-moves-pretty-fast.wav";
try
{
string result = Task.Run(async () => await MakeApiCallForWebsiteSample()).GetAwaiter().GetResult();
tbb_response.Text = result;
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
Library.Terminate();
}
}
}