internal VisionClient(CognitiveServicesConfig config, HttpClient httpClient)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            if (string.IsNullOrWhiteSpace(config.AzureComputerVisionApiServiceUriBase))
            {
                throw new ArgumentNullException("AzureComputerVisionApiServiceUriBase");
            }

            if (string.IsNullOrWhiteSpace(config.AzureComputerVisionApiSubscriptionKey))
            {
                throw new ArgumentNullException("AzureComputerVisionApiSubscriptionKey");
            }

            if (httpClient == null)
            {
                throw new ArgumentNullException("httpClient");
            }

            this.serviceUriBase         = config.AzureComputerVisionApiServiceUriBase;
            this.subscriptionKey        = config.AzureComputerVisionApiSubscriptionKey;
            this.httpClient             = httpClient;
            this.httpClient.BaseAddress = new Uri(this.serviceUriBase, UriKind.Absolute);
            this.DecorateHeaders(this.httpClient, this.subscriptionKey);
        }
Beispiel #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TranslateClient" /> class.
        /// </summary>
        /// <param name="logger">The logger.</param>
        /// <param name="cognitiveServiceConfig">The cognitive service configuration.</param>
        public TranslateClient(CognitiveServicesConfig cognitiveServiceConfig)
        {
            _cognitiveServiceConfig = cognitiveServiceConfig;

            // In Core 2.1, we can use what is described in: https://docs.microsoft.com/en-gb/aspnet/core/fundamentals/http-requests?view=aspnetcore-2.1
            // Otherwise, maybe use RestSharp?
            _httpClient = new HttpClient
            {
                BaseAddress = new Uri(Constants.TranslatorApiBasePath)
            };
        }
        /// <summary>
        /// Checks whether the Cognitive Services config has the required settings.
        /// </summary>
        /// <param name="config">The Cognitive Services config object.</param>
        /// <returns>Returns true if the config has the required settings. Otherwise, false.</returns>
        private bool ModuleHasRequiredSettings(CognitiveServicesConfig config)
        {
            if (string.IsNullOrWhiteSpace(config.AzureComputerVisionApiServiceUriBase))
            {
                return(false);
            }

            if (string.IsNullOrWhiteSpace(config.AzureComputerVisionApiSubscriptionKey))
            {
                return(false);
            }

            return(true);
        }
        public static IServiceCollection AddCognitiveServicesTranslator(this IServiceCollection services, IConfiguration configuration)
        {
            var cognitiveServiceConfig = new CognitiveServicesConfig();

            configuration.GetSection(CognitiveServicesConfig.SectionName).Bind(cognitiveServiceConfig);
            if (cognitiveServiceConfig == null)
            {
                throw new System.Exception($"The configuration must have the {CognitiveServicesConfig.SectionName} " +
                                           $"section. Please read the doc.");
            }

            services.AddSingleton(cognitiveServiceConfig);
            services.AddScoped <ITranslateClient, TranslateClient>(); // Maybe should be a singleton instead?

            return(services);
        }
Beispiel #5
0
        public static async Task <Skillset> GetSkillsetFromFile(string name, CognitiveServicesConfig cognitiveServicesConfig)
        {
            using (var reader = new StreamReader($"PipelineJson/{name}.json"))
            {
                var json = await reader.ReadToEndAsync();

                var serializerSettings = new JsonSerializerSettings {
                    TypeNameHandling = TypeNameHandling.All
                };
                serializerSettings.Converters.Add(new PolymorphicDeserializeJsonConverter <Skill>("@odata.type"));

                var skillset = JsonConvert.DeserializeObject <Skillset>(json, serializerSettings);
                skillset.Name              = name;
                skillset.Description       = "Cognitive skills collection";
                skillset.CognitiveServices = new CognitiveServicesByKey(cognitiveServicesConfig.Key, cognitiveServicesConfig.ResourceId);

                return(skillset);
            }
        }
 public CognitiveServicesController(CognitiveServicesConfig config)
 {
     this.textAnalize = new Client(config.AylienAppId, config.AylienAppKey);
 }
        /// <summary>
        /// Checks whether the Cognitive Services config has the required settings.
        /// </summary>
        /// <returns>Returns true if the config has the required settings. Otherwise, false.</returns>
        private bool ModuleHasRequiredSettings()
        {
            CognitiveServicesConfig config = Config.Get <CognitiveServicesConfig>();

            return(this.ModuleHasRequiredSettings(config));
        }
Beispiel #8
0
        public static async Task <Skillset> GetSkillset(ISearchServiceClient serviceClient, string skillsetName, CognitiveServicesConfig cognitiveServicesConfig)
        {
            if (await serviceClient.Skillsets.ExistsAsync(skillsetName))
            {
                // Retrieve the skillset from the Search Service.
                var skillset = await serviceClient.Skillsets.GetAsync(skillsetName);

                return(skillset);
            }
            else
            {
                // Retrieve the default skillset from a JSON file.
                return(await GetSkillsetFromFile(skillsetName, cognitiveServicesConfig));
            }
        }
 public static async Task <Skillset> GetBaseSkillset(string name, CognitiveServicesConfig cognitiveServicesConfig)
 {
     return(await Task.FromResult(new Skillset
     {
         Name = name,
         Description = "Cognitive skills collection",
         CognitiveServices = new CognitiveServicesByKey(cognitiveServicesConfig.Key, cognitiveServicesConfig.ResourceId),
         Skills = new List <Skill>
         {
             new EntityRecognitionSkill
             {
                 Description = "Entity recognition skill",
                 Context = "/document",
                 Categories = new List <EntityCategory>
                 {
                     EntityCategory.Person,
                     EntityCategory.Quantity,
                     EntityCategory.Organization,
                     EntityCategory.Location,
                     EntityCategory.Datetime,
                     EntityCategory.Url,
                     EntityCategory.Email
                 },
                 DefaultLanguageCode = "en",
                 Inputs = new List <InputFieldMappingEntry>
                 {
                     new InputFieldMappingEntry("text", "/document/text")
                 },
                 Outputs = new List <OutputFieldMappingEntry> {
                     new OutputFieldMappingEntry("persons", "people"),
                     new OutputFieldMappingEntry("organizations", "organizations"),
                     new OutputFieldMappingEntry("locations", "locations")
                 }
             },
             new KeyPhraseExtractionSkill
             {
                 Context = "/document",
                 Description = "Key phrase extraction skill",
                 DefaultLanguageCode = "en",
                 Inputs = new List <InputFieldMappingEntry>
                 {
                     new InputFieldMappingEntry("text", "/document/text")
                 },
                 Outputs = new List <OutputFieldMappingEntry> {
                     new OutputFieldMappingEntry("keyPhrases", "keyphrases")
                 }
             },
             new LanguageDetectionSkill
             {
                 Context = "/document",
                 Description = "Language detection skill",
                 Inputs = new List <InputFieldMappingEntry>
                 {
                     new InputFieldMappingEntry("text", "/document/text")
                 },
                 Outputs = new List <OutputFieldMappingEntry> {
                     new OutputFieldMappingEntry("languageCode", "language")
                 }
             }
         }
     }));
 }