public string Query(string Question)
        {
            if (channel != null)
            {
                if(channel.State == Grpc.Core.ChannelState.Shutdown ||
                    channel.State == Grpc.Core.ChannelState.TransientFailure)
                {
                    channel = null;                   
                }
            }
            if (channel == null) Init();

            var query = new QueryInput
            {
                Text = new TextInput
                {
                    Text = Question,
                    LanguageCode = "en-us"
                }
            };

            DetectIntentResponse dialogFlow =null;
            try
            {
                dialogFlow = client.DetectIntent(
                               new SessionName(AgentID, sessionId),
                               query
                           );
            }
            catch (Exception ex)
            {
                NetworkError.Message = ex.Message;
                NetworkError.StackTrace = ex.ToString();
                return "I am unable to access the internet network. Can you please make sure your network is online?";
            }
           

            dialogFlowString = dialogFlow.ToString();
            Response.CreateResponseObject(dialogFlowString);
             
            QueryResult = dialogFlow.QueryResult.ToString();
            FulfillmentText = dialogFlow.QueryResult.FulfillmentText;

            if (FulfillmentText.Trim() == "") FulfillmentText = "I am sorry, I do not have a comment at this moment. Can you try again?";

            if (IsChannelKeepAlive == false)
            {
                channel.ShutdownAsync();                
            }

            return FulfillmentText;

        }
Exemple #2
0
        public static string DetectIntent(string text)
        {
            var query = new QueryInput
            {
                Text = new TextInput
                {
                    Text         = text,
                    LanguageCode = "ru"
                }
            };

            var sessionId = Guid.NewGuid().ToString();
            var agent     = "smalltalkbot-atvxnh";
            var creds     = IntentDetector.Creds;
            var channel   = new Grpc.Core.Channel(SessionsClient.DefaultEndpoint.Host,
                                                  creds.ToChannelCredentials());

            var client = SessionsClient.Create(channel);

            var dialogFlow = client.DetectIntent(
                new SessionName(agent, sessionId),
                query
                );

            channel.ShutdownAsync();
            return(dialogFlow.QueryResult.FulfillmentText);
        }
Exemple #3
0
        protected void Page_Load(object sender, EventArgs e)
        {
            var query = new QueryInput
            {
                Text = new TextInput
                {
                    Text         = "cc",
                    LanguageCode = "en-us"
                }
            };

            var sessionId = "SomeUniqueId";
            var agent     = "test5";
            var creds     = GoogleCredential.FromJson(@"D:/test5.json");
            var channel   = new Grpc.Core.Channel(SessionsClient.DefaultEndpoint.Host,
                                                  creds.ToChannelCredentials());

            var client = SessionsClient.Create(channel);

            var dialogFlow = client.DetectIntent(
                new SessionName(agent, sessionId),
                query
                );

            channel.ShutdownAsync();
        }
        public async Task <IActionResult> sendtext(string text, string sessionId)
        {
            var result = new RequestResult();

            try
            {
                var query = new QueryInput {
                    Text = new TextInput {
                        Text = text, LanguageCode = "ru-ru"
                    }
                };

                var agent   = "small-talk-1-cdboby";
                var creds   = GoogleCredential.FromFile($"{_env.WebRootPath}\\DFCredits.json");
                var channel = new Grpc.Core.Channel(SessionsClient.DefaultEndpoint.Host, creds.ToChannelCredentials());

                var client     = SessionsClient.Create(channel);
                var dialogFlow = await client.DetectIntentAsync(new SessionName(agent, sessionId), query);

                await channel.ShutdownAsync();

                result.IsSuccess = true;
                result.Data      = string.IsNullOrEmpty(dialogFlow?.QueryResult?.FulfillmentText) ? GetRandomDontKnow() : dialogFlow.QueryResult.FulfillmentText;
            }
            catch (Exception err)
            {
                result.IsSuccess = false;
                result.Data      = "Упс... что-то пошло не так...";
                result.Error     = $"Ошбика: {err.Message}";
            }

            return(new JsonResult(result));
        }
Exemple #5
0
        public string Query(string Question)
        {
            if (channel != null)
            {
                if (channel.State == Grpc.Core.ChannelState.Shutdown ||
                    channel.State == Grpc.Core.ChannelState.TransientFailure)
                {
                    channel = null;
                }
            }
            if (channel == null)
            {
                Init();
            }

            var query = new QueryInput
            {
                Text = new TextInput
                {
                    Text         = Question,
                    LanguageCode = "en-us"
                }
            };

            var dialogFlow = client.DetectIntent(
                new SessionName(AgentID, sessionId),
                query
                );

            dialogFlowString = dialogFlow.ToString();

            QueryResult     = dialogFlow.QueryResult.ToString();
            FulfillmentText = dialogFlow.QueryResult.FulfillmentText;

            if (IsChannelKeepAlive == false)
            {
                channel.ShutdownAsync();
            }

            return(FulfillmentText);
        }
        public async Task <ModerationResponse> AnalyzeImage(byte[] imageBytes)
        {
            var channel = new Grpc.Core.Channel(ImageAnnotatorClient.DefaultEndpoint.Host, Credentials.ToChannelCredentials());

            try
            {
                var client = ImageAnnotatorClient.Create(channel);

                var image = Image.FromBytes(imageBytes);

                var response = await client.DetectSafeSearchAsync(image);

                var moderationResponse = new ModerationResponse();

                moderationResponse.ModerationScores = new[] {
                    new ModerationScore()
                    {
                        Category = $"Adult", Score = ConvertLikelyhood(response.Adult)
                    },
                    new ModerationScore()
                    {
                        Category = $"Medical", Score = ConvertLikelyhood(response.Medical)
                    },
                    new ModerationScore()
                    {
                        Category = $"Spoof", Score = ConvertLikelyhood(response.Spoof)
                    },
                    new ModerationScore()
                    {
                        Category = $"Violent", Score = ConvertLikelyhood(response.Violence)
                    }
                };

                moderationResponse.Pass = !moderationResponse.ModerationScores.Any(s => s.Score > .40F);

                return(moderationResponse);
            }
            catch (Exception ex)
            {
                return(new ModerationResponse()
                {
                    Pass = false,
                    ModerationScores = new[] { new ModerationScore()
                                               {
                                                   Category = $"ServerError:{ex.Message}", Score = 100
                                               } }
                });
            }
            finally
            {
                await channel.ShutdownAsync();
            }
        }
Exemple #7
0
        static async Task Main(string[] args)
        {
            Grpc.Core.Channel channel = new Grpc.Core.Channel("127.0.0.1:30051", Grpc.Core.ChannelCredentials.Insecure);

            var client = new Calculator.CalculatorClient(channel);

            var request = new IntegerMessage {
                Value = 56, Message = "Hello Grpc World"
            };

            Console.WriteLine($"Request: Value = {request.Value}, Message = {request.Message}");
            var reply = client.AddOne(request);

            Console.WriteLine($"Reply: Value = {reply.Value}, Message = {reply.Message}");

            await channel.ShutdownAsync();

            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
        private void PingButton_Click(object sender, RoutedEventArgs e)
        {
            var uri = "127.0.0.1:50051";

            var channel = new Grpc.Core.Channel(uri, Grpc.Core.ChannelCredentials.Insecure);

            var client = new ClassLibrary1.Protos.Ping.PingClient(channel);

            try
            {
                var pingResult = client.Ping(new Google.Protobuf.WellKnownTypes.Empty());
                PingLabel.Content = pingResult.Result;
            }
            catch (Exception ex)
            {
                PingLabel.Content = ex.Message;
            }


            channel.ShutdownAsync().Wait();
        }