Skip to content

Commit 5963da0

Browse files
neoguo0601boltomli
authored andcommitted
upgrade custom voice api (#123)
* upgrade custom voice api * add readme
1 parent acf211f commit 5963da0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+3125
-1683
lines changed
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
4-
VisualStudioVersion = 15.0.26403.3
4+
VisualStudioVersion = 15.0.28307.572
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CustomVoice-API", "CustomVoice-API\CustomVoice-API.csproj", "{52AFEC63-B3EB-4A35-951E-B4BD5EEE3C49}"
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CustomVoice-API", "CustomVoice-API\CustomVoice-API.csproj", "{7025794D-3DEE-485D-B6F9-3DE255543EED}"
77
EndProject
88
Global
99
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1010
Debug|Any CPU = Debug|Any CPU
1111
Release|Any CPU = Release|Any CPU
1212
EndGlobalSection
1313
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14-
{52AFEC63-B3EB-4A35-951E-B4BD5EEE3C49}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15-
{52AFEC63-B3EB-4A35-951E-B4BD5EEE3C49}.Debug|Any CPU.Build.0 = Debug|Any CPU
16-
{52AFEC63-B3EB-4A35-951E-B4BD5EEE3C49}.Release|Any CPU.ActiveCfg = Release|Any CPU
17-
{52AFEC63-B3EB-4A35-951E-B4BD5EEE3C49}.Release|Any CPU.Build.0 = Release|Any CPU
14+
{7025794D-3DEE-485D-B6F9-3DE255543EED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{7025794D-3DEE-485D-B6F9-3DE255543EED}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{7025794D-3DEE-485D-B6F9-3DE255543EED}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{7025794D-3DEE-485D-B6F9-3DE255543EED}.Release|Any CPU.Build.0 = Release|Any CPU
1818
EndGlobalSection
1919
GlobalSection(SolutionProperties) = preSolution
2020
HideSolutionNode = FALSE
2121
EndGlobalSection
2222
GlobalSection(ExtensibilityGlobals) = postSolution
23-
SolutionGuid = {F923BF5A-21E8-4419-9CC0-63513A78457B}
23+
SolutionGuid = {5ABCFAE1-D1A3-473B-B161-6FE33EA208D5}
2424
EndGlobalSection
2525
EndGlobal
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using System.IO;
3+
using System.Net.Http;
4+
using System.Threading;
5+
using Newtonsoft.Json;
6+
7+
namespace CustomVoice_API.API
8+
{
9+
class APIHelper
10+
{
11+
public static T Get<T>(string subscriptionKey, string url)
12+
{
13+
using (var client = new HttpClient())
14+
{
15+
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
16+
var response = client.GetAsync(url, CancellationToken.None).Result;
17+
18+
using (var responseStream = response.Content.ReadAsStreamAsync().Result)
19+
using (var streamReader = new StreamReader(responseStream))
20+
{
21+
string responseJson = streamReader.ReadToEnd();
22+
var items = JsonConvert.DeserializeObject<T>(responseJson);
23+
return items;
24+
}
25+
}
26+
}
27+
28+
public static HttpResponseMessage Submit<T>(string subscriptionKey, string url, T definition)
29+
{
30+
using (var client = new HttpClient())
31+
using (var content = new MultipartFormDataContent())
32+
{
33+
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
34+
return client.PostAsJsonAsync(url, definition, CancellationToken.None).Result;
35+
}
36+
}
37+
38+
public static HttpResponseMessage Delete(string subscriptionKey, string url)
39+
{
40+
using (var client = new HttpClient())
41+
{
42+
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
43+
return client.DeleteAsync(url, CancellationToken.None).Result;
44+
}
45+
}
46+
47+
public static string GetToken(string authenticationPath, string subscriptionKey)
48+
{
49+
var ibizaStsUrl = new Uri(authenticationPath);
50+
var authentication = new Authentication(ibizaStsUrl, subscriptionKey);
51+
return authentication.RetrieveNewTokenAsync();
52+
}
53+
}
54+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
namespace CustomVoice_API.API
2+
{
3+
public class API_V3
4+
{
5+
private static string TextToSpeechBasePath_V3_beta1 => "api/texttospeech/v3.0-beta1/";
6+
private static string speechtotextBasePath_V3_beta1 => "api/speechtotext/v3.0-beta1/";
7+
8+
//Voice Project
9+
private static string VoiceProject_Base => TextToSpeechBasePath_V3_beta1 + "projects";
10+
public static string VoiceProject_Get => VoiceProject_Base + "kind/TextToSpeech";
11+
public static string VoiceProject_Create => VoiceProject_Base;
12+
public static string VoiceProject_DeleteById => VoiceProject_Base + "/{0}";
13+
14+
//Voice Datasets
15+
private static string VoiceDataset_Base => TextToSpeechBasePath_V3_beta1 + "datasets";
16+
public static string VoiceDatasets_Get => VoiceDataset_Base;
17+
public static string VoiceDatasets_GetByProjectId => VoiceDataset_Base + "/project/{0}";
18+
public static string VoiceDatasets_Upload => VoiceDataset_Base + "/upload";
19+
public static string VoiceDatasets_UploadAudioOnly => VoiceDataset_Base + "/audioonly";
20+
public static string VoiceDatasets_UploadLongAudio => VoiceDataset_Base + "/longaudio";
21+
public static string VoiceDatasets_DeleteById => VoiceDataset_Base;
22+
23+
//Voice Models
24+
private static string VoiceModels_Base => TextToSpeechBasePath_V3_beta1 + "models";
25+
public static string VoiceModels_Get => VoiceModels_Base;
26+
public static string VoiceModels_GetByProjectId => VoiceModels_Base + "project/{0}";
27+
public static string VoiceModels_Create => VoiceModels_Base;
28+
public static string VoiceModels_DeleteById => VoiceModels_Base + "/{0}";
29+
30+
//Voice Tests
31+
private static string VoiceTests_Base => TextToSpeechBasePath_V3_beta1 + "tests";
32+
public static string VoiceTests_GetByModelId => VoiceTests_Base + "/model/{0}";
33+
public static string VoiceTests_GetByProjectId => VoiceTests_Base + "/project/{0}";
34+
public static string VoiceTests_Create => VoiceTests_Base;
35+
public static string VoiceTests_DeleteById => VoiceTests_Base + "/{0}";
36+
37+
//Voice Endpoints
38+
private static string VoiceEndpoints_Base => TextToSpeechBasePath_V3_beta1 + "endpoints";
39+
public static string VoiceEndpoints_Get => VoiceEndpoints_Base;
40+
public static string VoiceEndpoints_GetByProjectId => VoiceEndpoints_Base + "/project/{0}";
41+
public static string VoiceEndpoints_Create => VoiceEndpoints_Base;
42+
public static string VoiceEndpoints_DeleteById => VoiceEndpoints_Base + "/{0}";
43+
44+
//Voice Synthesis
45+
private static string VoiceSynthesis_Base => TextToSpeechBasePath_V3_beta1 + "voicesynthesis";
46+
public static string VoiceSynthesis_Get => VoiceSynthesis_Base;
47+
public static string VoiceSynthesis_Create => VoiceSynthesis_Base;
48+
public static string VoiceSynthesis_DeleteById => VoiceSynthesis_Base + "/{0}";
49+
public static string VoiceSynthesis_GetVoice => VoiceSynthesis_Base + "/voices";
50+
}
51+
}

CustomVoice-API-Samples/CSharp/CustomVoice-API/VoiceAPI/Authentication.cs renamed to CustomVoice-API-Samples/CSharp/CustomVoice-API/API/Authentication.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
42
using System.Net;
53
using System.Net.Http;
6-
using System.Text;
7-
using System.Threading.Tasks;
84

9-
namespace ConsoleApp1.VoiceAPI
5+
namespace CustomVoice_API.API
106
{
117
class Authentication
128
{
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
using CustomVoice_API.API.DTO;
2+
using Newtonsoft.Json;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Globalization;
6+
using System.IO;
7+
using System.Net;
8+
using System.Net.Http;
9+
10+
namespace CustomVoice_API.API
11+
{
12+
class BatchSynthesis
13+
{
14+
public static IEnumerable<DTO.BatchSynthesis> Get(string subscriptionKey, string hostURI)
15+
{
16+
string url = string.Format(CultureInfo.InvariantCulture, hostURI + API_V3.VoiceSynthesis_Get);
17+
return APIHelper.Get<IEnumerable<DTO.BatchSynthesis>>(subscriptionKey, url);
18+
}
19+
20+
public static IEnumerable<DTO.Voice> Getvoices(string subscriptionKey, string hostURI)
21+
{
22+
string url = string.Format(CultureInfo.InvariantCulture, hostURI + API_V3.VoiceSynthesis_GetVoice);
23+
return APIHelper.Get<IEnumerable<DTO.Voice>>(subscriptionKey, url);
24+
}
25+
26+
public static bool DeleteById(string subscriptionKey, string hostURI, string batchSynthesisId)
27+
{
28+
string url = string.Format(CultureInfo.InvariantCulture, hostURI + API_V3.VoiceSynthesis_DeleteById, batchSynthesisId);
29+
var response = APIHelper.Delete(subscriptionKey, url);
30+
if (response.StatusCode != HttpStatusCode.NoContent)
31+
{
32+
Console.WriteLine($"Status Code: {response.StatusCode}");
33+
Console.WriteLine($"Status ReasonPhrase: {response.ReasonPhrase}");
34+
return false;
35+
}
36+
37+
return true;
38+
}
39+
40+
public static bool Create(string subscriptionKey, string hostURI, string name, string description,
41+
string inputTextPath, string locale, IEnumerable<Identity> models, string outputFormat, bool isConcatenateResult)
42+
{
43+
var properties = new Dictionary<string, string>();
44+
if (isConcatenateResult)
45+
{
46+
properties.Add("ConcatenateResult", "true");
47+
}
48+
49+
var batchSynthesisDefinition = BatchSynthesisDefinition.Create(name,
50+
description,
51+
inputTextPath,
52+
properties,
53+
locale,
54+
models,
55+
outputFormat);
56+
57+
return Create(subscriptionKey, hostURI, batchSynthesisDefinition);
58+
}
59+
60+
private static bool Create(string subscriptionKey, string hostURI, BatchSynthesisDefinition batchSynthesisDefinition)
61+
{
62+
string scriptName = Path.GetFileName(batchSynthesisDefinition.InputTextPath);
63+
64+
using (FileStream fsscript = new FileStream(batchSynthesisDefinition.InputTextPath, FileMode.Open))
65+
using (var client = new HttpClient())
66+
using (var content = new MultipartFormDataContent())
67+
{
68+
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
69+
70+
content.Add(new StringContent(batchSynthesisDefinition.Name), "name");
71+
72+
if (!string.IsNullOrEmpty(batchSynthesisDefinition.OutputFormat))
73+
{
74+
content.Add(new StringContent(batchSynthesisDefinition.OutputFormat), "outputformat");
75+
}
76+
77+
if (batchSynthesisDefinition.Description != null)
78+
{
79+
content.Add(new StringContent(batchSynthesisDefinition.Description), "description");
80+
}
81+
82+
content.Add(new StringContent(JsonConvert.SerializeObject(batchSynthesisDefinition.Models)), "models");
83+
content.Add(new StringContent(batchSynthesisDefinition.Locale), "locale");
84+
85+
if (batchSynthesisDefinition.Properties != null)
86+
{
87+
content.Add(new StringContent(JsonConvert.SerializeObject(batchSynthesisDefinition.Properties)), "properties");
88+
}
89+
90+
var scriptContent = new StreamContent(fsscript);
91+
scriptContent.Headers.Add("Content-Disposition", $@"form-data; name=""script""; filename=""{scriptName}""");
92+
scriptContent.Headers.Add("Content-Type", "text/plain");
93+
scriptContent.Headers.Add("Content-Length", $"{fsscript.Length}");
94+
content.Add(scriptContent, "script", scriptName);
95+
96+
string url = string.Format(CultureInfo.InvariantCulture, hostURI + API_V3.VoiceSynthesis_Create);
97+
var response = client.PostAsync(url, content).Result;
98+
99+
if (response.StatusCode != HttpStatusCode.Accepted)
100+
{
101+
Console.WriteLine($"Status Code: {response.StatusCode}");
102+
Console.WriteLine($"Status ReasonPhrase: {response.ReasonPhrase}");
103+
return false;
104+
}
105+
return true;
106+
}
107+
}
108+
}
109+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Globalization;
5+
6+
namespace CustomVoice_API.API.DTO
7+
{
8+
class BatchSynthesis
9+
{
10+
[JsonConstructor]
11+
private BatchSynthesis(
12+
string name,
13+
string description,
14+
IReadOnlyDictionary<string, string> properties,
15+
CultureInfo locale,
16+
Guid id,
17+
DateTime created,
18+
DateTime lastAction,
19+
OneApiState state,
20+
Uri resultsUrl,
21+
Model model,
22+
string statusMessage)
23+
{
24+
this.Name = name;
25+
this.Description = description;
26+
this.Properties = properties;
27+
this.Locale = locale.Name;
28+
this.Id = id;
29+
this.Created = created;
30+
this.LastAction = lastAction;
31+
this.State = state;
32+
this.ResultsUrl = resultsUrl;
33+
this.Model = model;
34+
this.StatusMessage = statusMessage;
35+
}
36+
37+
public string Name { get; private set; }
38+
39+
public string Description { get; private set; }
40+
41+
public IReadOnlyDictionary<string, string> Properties { get; private set; }
42+
43+
public string Locale { get; private set; }
44+
45+
public Guid Id { get; private set; }
46+
47+
public DateTime Created { get; private set; }
48+
49+
public DateTime LastAction { get; private set; }
50+
51+
public OneApiState State { get; private set; }
52+
53+
public Uri ResultsUrl { get; private set; }
54+
55+
public Model Model { get; private set; }
56+
57+
public string StatusMessage { get; private set; }
58+
59+
public static BatchSynthesis Create(
60+
Guid id,
61+
string name,
62+
string description,
63+
IReadOnlyDictionary<string, string> properties,
64+
CultureInfo locale,
65+
DateTime created,
66+
DateTime lastAction,
67+
OneApiState state,
68+
Uri resultsUrl,
69+
Model model,
70+
string statusMessage)
71+
{
72+
return new BatchSynthesis(
73+
name,
74+
description,
75+
properties,
76+
locale,
77+
id,
78+
created,
79+
lastAction,
80+
state,
81+
resultsUrl,
82+
model,
83+
statusMessage);
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)