コード例 #1
0
ファイル: Form1.cs プロジェクト: Midvel/WindowsToPythonAI
        private void creditButton_Click(object sender, EventArgs e)
        {
            this.statusTextBox.Text = "";
            int    loan_amount = int.Parse(this.loanAmountTextBox.Text);
            double int_rate    = Convert.ToDouble(this.intRateTextBox.Text);
            double installment = Convert.ToDouble(this.installmentTextBox.Text);
            string grade       = this.gradeComboBox.SelectedItem.ToString();
            int    employed    = this.employedDomainUpDown.SelectedIndex;
            string owner       = this.homwOwnerComboBox.SelectedItem.ToString();
            int    income      = int.Parse(this.annualTextBox.Text);
            string purpose     = this.purposeComboBox.SelectedItem.ToString();
            int    credit_inq  = int.Parse(this.inqTextBox.Text);
            int    incidents   = int.Parse(this.pastTextBox.Text);


            var module_folder = @"D:\Workspace\VisualStudioProj\WindowsToPythonAI\python_model";
            var module_name   = "run_keras_model";
            var class_name    = "LoanModel";
            var def_name      = "predict_this";

            //Model input arguments preparation
            var methodArguments = new PythonCallerArgs();

            methodArguments.AddArg("loan_amnt", loan_amount);
            methodArguments.AddArg("int_rate", int_rate);
            methodArguments.AddArg("installment", installment);
            methodArguments.AddArg("grade", grade);
            methodArguments.AddArg("emp_length", employed);
            methodArguments.AddArg("home_ownership", owner);
            methodArguments.AddArg("annual_inc", income);
            methodArguments.AddArg("purpose", purpose);
            methodArguments.AddArg("inq_last_12m", credit_inq);
            methodArguments.AddArg("delinq_2yrs", incidents);

            methodArguments.AddMetaArg("caller", "WindowsToPythonAI");

            // Now we can create a caller object and bind it to the model
            var pyCaller = new PythonCaller(module_folder, module_name);
            Dictionary <string, string> resultJson = pyCaller.CallClassMethod(class_name, def_name, methodArguments);

            this.statusTextBox.Text = resultJson["prediction"];
        }
コード例 #2
0
        public Dictionary <string, string> CallClassMethod(string className, string method, PythonCallerArgs args)
        {
            results = "";
            errors  = "";
            string argString = args.Serialized();

            //Caller produces the command:
            // path/to/python.exe caller.py path/to/python/module python_module_name class_name method_name args_string
            //caller.py is the aggregator script, which finds the desired script (python_module_name.py), imports its contents and calls the class method
            //args_string is JSON-formatted array of the arguments for the desired script (python_module_name.py)

            psi.Arguments = $"\"{caller}\" \"{scriptPath}\" \"{scriptName}\" \"{className}\" \"{method}\" \"{argString}\"";


            var value     = System.Environment.GetEnvironmentVariable("PATH");
            var new_value = envPath + "\\:" + envPath + "\\Scripts:" + value;

            System.Environment.SetEnvironmentVariable("PATH", new_value);

            using (var process = Process.Start(psi))
            {
                errors  = process.StandardError.ReadToEnd();
                results = process.StandardOutput.ReadToEnd();
            }

            Dictionary <string, string> ob = JsonConvert.DeserializeObject <Dictionary <string, string> >(results);

            return(ob);
        }