Пример #1
0
        public static bool Create(string subscriptionKey, string hostURI, string name, string description,
                                  string local, Guid projectId, Guid modelId, bool wait = true)
        {
            var properties = new Dictionary <string, string>();

            properties.Add("PortalAPIVersion", "3");

            System.Net.Http.HttpResponseMessage response;


            var endpointDefinition = EndpointDefinition.Create(
                name,
                description,
                local,
                new Identity(projectId),
                new List <Identity> {
                new Identity(modelId)
            },
                properties);
            var jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(endpointDefinition);

            response = APIHelper.Submit(subscriptionKey, hostURI + API_V3.VoiceEndpoints_Create, jsonString);

            if (response.StatusCode != HttpStatusCode.Accepted)
            {
                APIHelper.PrintErrorMessage(response);
                return(false);
            }

            Console.WriteLine("endpoint created: " + response.Headers.Location.ToString());

            return(true);
        }
Пример #2
0
        public static bool Create(string subscriptionKey, string hostURI, string name, string description, string gender, string locale)
        {
            var properties = new Dictionary <string, string>();

            properties.Add("Gender", gender.Substring(0, 1).ToUpper() + gender.Substring(1));

            var projectDefinition = ProjectDefinition.Create(
                name,
                name,
                description,
                locale,
                properties,
                "TextToSpeech");

            var jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(projectDefinition);

            var response = APIHelper.Submit(subscriptionKey, hostURI + API_V3.VoiceProject_Create, jsonString);


            if (response.StatusCode != HttpStatusCode.Accepted && response.StatusCode != HttpStatusCode.Created)
            {
                APIHelper.PrintErrorMessage(response);
                return(false);
            }

            System.Console.WriteLine(response.Headers.Location);
            return(true);
        }
Пример #3
0
        private static bool UploadDataset(string subscriptionKey, string hostURI, DatasetDefinition datasetDefinition, string wavePath, string scriptPath)
        {
            string waveName   = Path.GetFileName(wavePath);
            string scriptName = Path.GetFileName(scriptPath);

            using (FileStream fsscript = new FileStream(scriptPath, FileMode.Open))
                using (FileStream fswave = new FileStream(wavePath, FileMode.Open))
                    using (var client = new HttpClient())
                        using (var content = new MultipartFormDataContent())
                        {
                            client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);

                            content.Add(new StringContent(datasetDefinition.Name), "name");

                            if (datasetDefinition.Description != null)
                            {
                                content.Add(new StringContent(datasetDefinition.Description), "description");
                            }

                            if (datasetDefinition.ProjectId != null)
                            {
                                content.Add(new StringContent(datasetDefinition.ProjectId), "projectId");
                            }

                            content.Add(new StringContent(datasetDefinition.DataImportKind), "dataImportKind");
                            content.Add(new StringContent(datasetDefinition.Locale), "locale");

                            if (datasetDefinition.Properties != null)
                            {
                                content.Add(new StringContent(JsonConvert.SerializeObject(datasetDefinition.Properties)), "properties");
                            }

                            var transcriptionContent = new StreamContent(fsscript);
                            transcriptionContent.Headers.Add("Content-Disposition", $@"form-data; name=""transcriptions""; filename=""{scriptName}""");
                            transcriptionContent.Headers.Add("Content-Type", "text/plain");
                            transcriptionContent.Headers.Add("Content-Length", $"{fsscript.Length}");
                            content.Add(transcriptionContent, "transcriptions", scriptName);

                            var wavesContent = new StreamContent(fswave);
                            wavesContent.Headers.Add("Content-Disposition", $@"form-data; name=""audiodata""; filename=""{waveName}""");
                            wavesContent.Headers.Add("Content-Type", "application/x-zip-compressed");
                            wavesContent.Headers.Add("Content-Length", $"{fswave.Length}");
                            content.Add(wavesContent, "audiodata", waveName);

                            string url      = string.Format(CultureInfo.InvariantCulture, hostURI + API_V3.VoiceDatasets_Upload);
                            var    response = client.PostAsync(url, content).Result;

                            if (response.StatusCode != HttpStatusCode.Accepted)
                            {
                                APIHelper.PrintErrorMessage(response);
                                return(false);
                            }

                            System.Console.WriteLine(response.Headers.Location);
                            return(true);
                        }
        }
Пример #4
0
        private static string Create(string subscriptionKey, string hostURI, BatchSynthesisDefinition batchSynthesisDefinition)
        {
            string scriptName = Path.GetFileName(batchSynthesisDefinition.InputTextPath);

            using (FileStream fsscript = new FileStream(batchSynthesisDefinition.InputTextPath, FileMode.Open))
                using (var client = new HttpClient())
                    using (var content = new MultipartFormDataContent())
                    {
                        client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);

                        content.Add(new StringContent(batchSynthesisDefinition.Name), "name");

                        if (!string.IsNullOrEmpty(batchSynthesisDefinition.OutputFormat))
                        {
                            content.Add(new StringContent(batchSynthesisDefinition.OutputFormat), "outputformat");
                        }

                        if (batchSynthesisDefinition.Description != null)
                        {
                            content.Add(new StringContent(batchSynthesisDefinition.Description), "description");
                        }

                        content.Add(new StringContent(JsonConvert.SerializeObject(batchSynthesisDefinition.Models)), "models");
                        content.Add(new StringContent(batchSynthesisDefinition.Locale), "locale");

                        if (batchSynthesisDefinition.Properties != null)
                        {
                            content.Add(new StringContent(JsonConvert.SerializeObject(batchSynthesisDefinition.Properties)), "properties");
                        }

                        var scriptContent = new StreamContent(fsscript);
                        scriptContent.Headers.Add("Content-Disposition", $@"form-data; name=""script""; filename=""{scriptName}""");
                        scriptContent.Headers.Add("Content-Type", "text/plain");
                        scriptContent.Headers.Add("Content-Length", $"{fsscript.Length}");
                        content.Add(scriptContent, "script", scriptName);

                        string url      = hostURI + API_V3.VoiceSynthesis_Create;
                        var    response = client.PostAsync(url, content).Result;

                        if (response.StatusCode != HttpStatusCode.Accepted)
                        {
                            APIHelper.PrintErrorMessage(response);
                            return(null);
                        }

                        var returnUrl = GetLocationFromPostResponse(response);
                        if (returnUrl != null)
                        {
                            return(new Guid(returnUrl.ToString().Split('/').LastOrDefault()).ToString());
                        }
                        return(null);
                    }
        }
Пример #5
0
        public static bool Create(string subscriptionKey, string hostURI, string name, string description,
                                  Guid projectId, string gender, string locale, List <Identity> dataset, bool isNeuralTTS, bool isMixlingual,
                                  IDictionary <string, string> neuralProperties)
        {
            var properties = new Dictionary <string, string>();

            properties.Add("Gender", gender.Substring(0, 1).ToUpper() + gender.Substring(1));

            if (isMixlingual)
            {
                locale = "zh-CN";
                properties.Add("IsMixLingual", "true");
            }
            else
            {
                properties.Add("IsMixLingual", "false");
            }

            if (isNeuralTTS)
            {
                properties.Add("VoiceModelKind", "NeuralTts");
                properties.Add("PortalAPIVersion", "3");
                properties.Add("Purpose", "Realtime");

                foreach (var neuralProperty in neuralProperties)
                {
                    properties.Add(neuralProperty.Key, neuralProperty.Value);
                }
            }

            var modelDefinition = ModelDefinition.Create(
                name,
                description,
                properties,
                locale,
                "CustomVoice",
                null,
                dataset,
                new Identity(projectId));

            var jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(modelDefinition);

            var response = APIHelper.Submit(subscriptionKey, hostURI + API_V3.VoiceModels_Create, jsonString);

            if (response.StatusCode != HttpStatusCode.Accepted)
            {
                APIHelper.PrintErrorMessage(response);
                return(false);
            }

            System.Console.WriteLine(response.Headers.Location);
            return(true);
        }
Пример #6
0
        public static bool DeleteById(string subscriptionKey, string hostURI, string endpointId)
        {
            string url      = string.Format(CultureInfo.InvariantCulture, hostURI + API_V3.VoiceEndpoints_DeleteById, endpointId);
            var    response = APIHelper.Delete(subscriptionKey, url);

            if (response.StatusCode != HttpStatusCode.NoContent)
            {
                APIHelper.PrintErrorMessage(response);
                return(false);
            }

            return(true);
        }
Пример #7
0
        public static bool Create(string subscriptionKey, string hostURI, string name, string description,
                                  Guid projectId, string gender, string locale, List <Identity> dataset, bool isNeuralTTS, bool isMixlingual,
                                  IDictionary <string, string> neuralProperties)
        {
            var properties = new Dictionary <string, string>();

            properties.Add("Gender", gender.Substring(0, 1).ToUpper() + gender.Substring(1));

            if (isMixlingual)
            {
                locale = "zh-CN";
                properties.Add("IsMixLingual", "true");
            }
            else
            {
                properties.Add("IsMixLingual", "false");
            }

            if (isNeuralTTS)
            {
                properties.Add("VoiceModelKind", "NeuralTts");
                foreach (var neuralProperty in neuralProperties)
                {
                    properties.Add(neuralProperty.Key, neuralProperty.Value);
                }
            }

            var modelDefinition = ModelDefinition.Create(
                name,
                description,
                properties,
                locale,
                "CustomVoice",
                null,
                dataset,
                new Identity(projectId));
            var response = APIHelper.Submit <ModelDefinition>(subscriptionKey, hostURI + API_V3.VoiceModels_Create, modelDefinition);

            if (response.StatusCode != HttpStatusCode.Accepted)
            {
                APIHelper.PrintErrorMessage(response);
                return(false);
            }
            return(true);
        }
Пример #8
0
        public static bool Update(string subscriptionKey, string hostURI, Guid modelId, string description, Guid?projectId)
        {
            if (string.IsNullOrEmpty(description) && projectId == null)
            {
                Console.WriteLine("Nothing to update");
                return(true);
            }

            string url      = string.Format(CultureInfo.InvariantCulture, hostURI + API_V3.VoiceModels_Update, modelId);
            var    payload  = UpdateDefinition.Create(null, description, projectId);
            var    response = APIHelper.Patch(subscriptionKey, url, payload);

            if (!response.IsSuccessStatusCode)
            {
                APIHelper.PrintErrorMessage(response);
                return(false);
            }

            return(true);
        }
Пример #9
0
        public static bool Create(string subscriptionKey, string hostURI, string name, string description,
                                  string local, Guid projectId, Guid modelId)
        {
            var endpointDefinition = EndpointDefinition.Create(
                name,
                description,
                local,
                new Identity(projectId),
                new List <Identity> {
                new Identity(modelId)
            },
                null);
            var response = APIHelper.Submit <EndpointDefinition>(subscriptionKey, hostURI + API_V3.VoiceEndpoints_Create, endpointDefinition);

            if (response.StatusCode != HttpStatusCode.Accepted)
            {
                APIHelper.PrintErrorMessage(response);
                return(false);
            }
            return(true);
        }
Пример #10
0
        public static bool AddToProject(string subscriptionKey, string hostURI, Guid projectId, Guid modelId)
        {
            string url         = string.Format(CultureInfo.InvariantCulture, hostURI + API_V3.VoiceModels_AddToProject, projectId);
            var    modelsToAdd = new List <Identity> {
                Identity.Create(modelId)
            };
            var response = APIHelper.Submit(subscriptionKey, url, modelsToAdd);

            if (!response.IsSuccessStatusCode)
            {
                APIHelper.PrintErrorMessage(response);
                return(false);
            }

            var content         = response.Content.ReadAsStringAsync().Result;
            var beautifyContent = JsonConvert.SerializeObject(JsonConvert.DeserializeObject(content), Formatting.Indented);

            Console.WriteLine(beautifyContent);

            return(true);
        }
Пример #11
0
        public static bool Copy(string subscriptionKey, string hostURI, Guid modelId, string targetSubscriptionKey)
        {
            string       url = string.Format(CultureInfo.InvariantCulture, hostURI + API_V3.VoiceModels_Copy, modelId);
            const string targetSubscriptionKeyName = "targetSubscriptionKey";
            var          payload = new Dictionary <string, string> {
                { targetSubscriptionKeyName, targetSubscriptionKey }
            };
            var response = APIHelper.Submit(subscriptionKey, url, payload);

            if (!response.IsSuccessStatusCode)
            {
                APIHelper.PrintErrorMessage(response);
                return(false);
            }

            var uri = APIHelper.GetLocationFromPostResponseAsync(response);

            Console.WriteLine($"Copied model: {uri}");

            return(true);
        }
Пример #12
0
        public static bool Create(string subscriptionKey, string hostURI, string name, string description, string gender, string locale)
        {
            var properties = new Dictionary <string, string>();

            properties.Add("Gender", gender.Substring(0, 1).ToUpper() + gender.Substring(1));

            var projectDefinition = ProjectDefinition.Create(
                name,
                description,
                locale,
                properties,
                "TextToSpeech");
            var response = APIHelper.Submit <ProjectDefinition>(subscriptionKey, hostURI + API_V3.VoiceProject_Create, projectDefinition);

            if (response.StatusCode != HttpStatusCode.OK)
            {
                APIHelper.PrintErrorMessage(response);
                return(false);
            }
            return(true);
        }
Пример #13
0
        public static bool Create(string subscriptionKey, string hostURI, Guid projectId, Guid modelId, string script, bool isSSML)
        {
            string TextKind = "Text";

            if (isSSML)
            {
                TextKind = "SSML";
            }

            var voiceTestDefinition = VoiceTestDefinition.Create(
                new Identity(modelId),
                script,
                TextKind,
                new Identity(projectId));

            var response = APIHelper.Submit <VoiceTestDefinition>(subscriptionKey, hostURI + API_V3.VoiceTests_Create, voiceTestDefinition);

            if (response.StatusCode != HttpStatusCode.Accepted)
            {
                APIHelper.PrintErrorMessage(response);
                return(false);
            }
            return(true);
        }