コード例 #1
0
ファイル: FormGptTest.cs プロジェクト: Pandinosaurus/MyCaffe
        private void dowork()
        {
            PythonInterop py      = null;
            Output        stdout1 = null;

            try
            {
                WaitHandle[] rgWait = new WaitHandle[] { m_evtCancel, m_evtReady };

                while (WaitHandle.WaitAny(rgWait) > 0)
                {
                    m_evtRunning.Set();
                    Tuple <string, string, string> arg = m_arg;
                    string strPythonPath = arg.Item1;
                    string strGpu        = arg.Item2;
                    string strInput      = arg.Item3;

                    string strPy;
                    KeyValuePair <string, object>[] rgArg;

                    if (py == null)
                    {
                        py                = new PythonInterop(strPythonPath);
                        stdout1           = new Output();
                        stdout1.OnOutput += stdout_OnOutput;

                        strPy = "import sys" + Environment.NewLine +
                                "sys.stdout = sys.stderr = output" + Environment.NewLine +
                                "res = 1";

                        rgArg = new KeyValuePair <string, object>[]
                        {
                            new KeyValuePair <string, object>("output", stdout1)
                        };

                        py.RunPythonCodeAndReturn(strPy, "res", rgArg);
                    }

                    strPy = "import os" + Environment.NewLine +
                            "os.environ['CUDA_VISIBLE_DEVICES'] = strGpu" + Environment.NewLine +
                            "from transformers import pipeline" + Environment.NewLine +
                            "generator = pipeline(task = 'text-generation', max_length=500)" + Environment.NewLine +
                            "res = generator(strInput)";

                    rgArg = new KeyValuePair <string, object>[]
                    {
                        new KeyValuePair <string, object>("strGpu", strGpu),
                        new KeyValuePair <string, object>("strInput", strInput)
                    };

                    Invoke(new fnOutput(output), "Running GPT2 transformer from https://huggingface.co...");

                    object obj = py.RunPythonCodeAndReturn(strPy, "res", rgArg);

                    Invoke(new fnOutput(output), "-----------------------------");

                    string   strResult = "";
                    object[] rgRes     = obj as object[];
                    if (rgRes != null)
                    {
                        foreach (object obj1 in rgRes)
                        {
                            string strJson = obj1.ToString();
                            strResult += strJson;
                            strResult += Environment.NewLine;
                        }
                    }
                    else
                    {
                        string strJson = obj.ToString();
                        strResult = strJson;
                    }

                    Invoke(new fnOutput(output), "RESULT:" + Environment.NewLine + strResult);
                    m_evtRunning.Reset();
                }
            }
            catch (Exception excpt)
            {
                Invoke(new fnOutput(output), "ERROR: " + excpt.Message);
            }
            finally
            {
                m_evtRunning.Reset();
                if (py != null)
                {
                    py.Dispose();
                    py = null;
                }

                m_evtDone.Set();
            }
        }