From 22da2ac82a667df4444449fe8d1fd1e639566bb2 Mon Sep 17 00:00:00 2001 From: yum Date: Thu, 9 Mar 2023 18:48:09 -0800 Subject: Begin work fixing venv setup If you don't have Python installed, venv setup will fail. Begin work fixing environment config so `pip install` uses vendored Python. --- GUI/GUI/GUI/Frame.cpp | 102 ++++++++++++++++++++++++------------------ GUI/GUI/GUI/Frame.h | 2 +- GUI/GUI/GUI/PythonWrapper.cpp | 17 ++++++- GUI/GUI/GUI/WhisperCPP.cpp | 12 ++--- 4 files changed, 80 insertions(+), 53 deletions(-) (limited to 'GUI') diff --git a/GUI/GUI/GUI/Frame.cpp b/GUI/GUI/GUI/Frame.cpp index f7bc107..eef8251 100644 --- a/GUI/GUI/GUI/Frame.cpp +++ b/GUI/GUI/GUI/Frame.cpp @@ -344,7 +344,6 @@ using ::Logging::Log; Frame::Frame() : wxFrame(nullptr, wxID_ANY, "TaSTT"), run_py_app_(false), - env_proc_(nullptr), py_app_drain_(this, ID_PY_APP_DRAIN) { app_c_ = std::make_unique(nullptr); @@ -366,6 +365,11 @@ Frame::Frame() dump_mics_ = p.get_future(); p.set_value(true); } + { + auto p = std::promise(); + env_proc_ = p.get_future(); + p.set_value(true); + } auto* main_panel = new wxPanel(this, ID_MAIN_PANEL); main_panel_ = main_panel; @@ -1626,50 +1630,61 @@ void Frame::OnNavbarWhisper(wxCommandEvent& event) void Frame::OnSetupPython(wxCommandEvent& event) { - if (env_proc_) { - if (wxProcess::Exists(env_proc_->GetPid())) { - Log(transcribe_out_, "Environment setup already running\n"); - return; - } - delete env_proc_; - env_proc_ = nullptr; - return; - } + auto status = env_proc_.wait_for(std::chrono::seconds(0)); + if (status != std::future_status::ready) { + Log(transcribe_out_, "Virtual environment setup already running\n"); + return; + } - Log(transcribe_out_, "Setting up Python virtual environment\n"); - Log(transcribe_out_, "This could take several minutes, please be " - "patient!\n"); - Log(transcribe_out_, "This will download ~5GB of dependencies.\n"); + env_proc_ = std::move(std::async(std::launch::async, [&]() { + Log(transcribe_out_, "Setting up Python virtual environment\n"); + Log(transcribe_out_, "This could take several minutes, please be " + "patient!\n"); + Log(transcribe_out_, "This will download ~5GB of dependencies.\n"); + + { + Log(transcribe_out_, " Installing pip\n"); + auto out_cb = [&](const std::string& out, const std::string& err) { + Log(transcribe_out_, "{}", out); + Log(transcribe_out_, "{}", err); + }; + if (!PythonWrapper::InstallPip(std::move(out_cb))) { + Log(transcribe_out_, "Failed to install pip!\n"); + return false; + } + } - { - std::string transcribe_out; - Log(transcribe_out_, " Installing pip\n"); - if (!PythonWrapper::InstallPip(&transcribe_out)) { - Log(transcribe_out_, "Failed to install pip: {}\n", transcribe_out); - } - } + { + Log(transcribe_out_, " DEBUG: check sys.path\n"); + auto out_cb = [&](const std::string& out, const std::string& err) { + Log(transcribe_out_, "{}", out); + Log(transcribe_out_, "{}", err); + }; + if (!PythonWrapper::InvokeWithArgs({ + "Resources/Scripts/tst.py", + }, std::move(out_cb))) { + Log(transcribe_out_, "Failed to check sys.path!\n"); + return false; + } + } - auto cb = [&](wxProcess* proc, int ret) -> void { - Log(transcribe_out_, "Environment setup completed with code {}\n", ret); - if (ret == 0) { - Log(transcribe_out_, "Python virtual environment successfully " - "set up!\n"); + Log(transcribe_out_, " Installing dependencies\n"); + auto out_cb = [&](const std::string& out, const std::string& err) { + Log(transcribe_out_, "{}", out); + Log(transcribe_out_, "{}", err); + }; + if (!PythonWrapper::InvokeWithArgs({ + "-u", // Unbuffered output + "-m pip", + "install", + "-r Resources/Scripts/requirements.txt", + }, std::move(out_cb))) { + Log(transcribe_out_, "Failed to launch environment setup thread!\n"); + return false; } - DrainAsyncOutput(proc, transcribe_out_); - return; - }; - wxProcess* p = PythonWrapper::InvokeAsyncWithArgs({ - "-u", // Unbuffered output - "-m pip", - "install", - "-r Resources/Scripts/requirements.txt", - "--no-warn-script-location", - }, std::move(cb)); - if (!p) { - Log(transcribe_out_, "Failed to launch environment setup thread!\n"); - return; - } - env_proc_ = p; + Log(transcribe_out_, "Successfully set up virtual environment!\n"); + return true; + })); } void Frame::OnDumpMics(wxCommandEvent& event) @@ -2101,8 +2116,8 @@ void Frame::OnAppStart(wxCommandEvent& event) { app_c_->Serialize(AppConfig::kConfigPath); auto out_cb = [&](const std::string& out, const std::string& err) { - Log(transcribe_out_, out); - Log(transcribe_out_, err); + Log(transcribe_out_, "{}", out); + Log(transcribe_out_, "{}", err); }; auto in_cb = [&](std::string& in) {}; auto run_cb = [&]() { @@ -2307,7 +2322,6 @@ void Frame::OnWhisperStop(wxCommandEvent& event) { } void Frame::OnAppDrain(wxTimerEvent& event) { - DrainAsyncOutput(env_proc_, transcribe_out_); Logging::kThreadLogger.Drain(); } diff --git a/GUI/GUI/GUI/Frame.h b/GUI/GUI/GUI/Frame.h index 0ecd268..98a62c4 100644 --- a/GUI/GUI/GUI/Frame.h +++ b/GUI/GUI/GUI/Frame.h @@ -90,8 +90,8 @@ private: bool run_py_app_; std::future unity_app_; std::future dump_mics_; + std::future env_proc_; - wxProcess* env_proc_; wxTimer py_app_drain_; std::unique_ptr app_c_; diff --git a/GUI/GUI/GUI/PythonWrapper.cpp b/GUI/GUI/GUI/PythonWrapper.cpp index 78947ed..54f6686 100644 --- a/GUI/GUI/GUI/PythonWrapper.cpp +++ b/GUI/GUI/GUI/PythonWrapper.cpp @@ -296,14 +296,17 @@ bool PythonWrapper::InvokeCommandWithArgs(const std::string& cmd, } // Set up PYTHONPATH + std::filesystem::path py_path = (std::filesystem::current_path() / + "Resources/Python").lexically_normal(); std::filesystem::path py_lib = (std::filesystem::current_path() / "Resources/Python/Lib").lexically_normal(); std::filesystem::path py_site_pkgs = (std::filesystem::current_path() / "Resources/Python/Lib/site-packages").lexically_normal(); std::ostringstream pypath_oss; - pypath_oss << py_lib.string(); + pypath_oss << py_path.string(); + pypath_oss << ';' << py_lib.string(); pypath_oss << ';' << py_site_pkgs.string(); - // Add updated PATH to current process's environment + out_cb("PYTHONPATH=" + pypath_oss.str() + "\n", ""); if (!SetEnvironmentVariableA("PYTHONPATH", pypath_oss.str().c_str())) { std::ostringstream err_oss; err_oss << "Error while executing python command \"" << cmd_oss.str() @@ -312,6 +315,16 @@ bool PythonWrapper::InvokeCommandWithArgs(const std::string& cmd, out_cb("", err_oss.str()); return false; } + // Set up PYTHONHOME + out_cb("PYTHONHOME=" + py_path.string() + "\n", ""); + if (!SetEnvironmentVariableA("PYTHONHOME", py_path.string().c_str())) { + std::ostringstream err_oss; + err_oss << "Error while executing python command \"" << cmd_oss.str() + << "\": Failed to set PYTHONHOME: " + << GetWin32ErrMsg() << std::endl; + out_cb("", err_oss.str()); + return false; + } } std::string cmd_str = cmd_oss.str(); diff --git a/GUI/GUI/GUI/WhisperCPP.cpp b/GUI/GUI/GUI/WhisperCPP.cpp index 2b1a03f..fbe78ae 100644 --- a/GUI/GUI/GUI/WhisperCPP.cpp +++ b/GUI/GUI/GUI/WhisperCPP.cpp @@ -153,8 +153,8 @@ bool WhisperCPP::InstallDependencies() { } auto out_cb = [&](const std::string& out, const std::string& err) -> void { - Log(out_, out); - Log(out_, err); + Log(out_, "{}", out); + Log(out_, "{}", err); }; auto in_cb = [&](std::string& in) {}; auto run_cb = [&]() -> bool { @@ -186,8 +186,8 @@ bool WhisperCPP::DownloadModel(const std::string& model_name, url_oss << model_name; Log(out_, "Model will be saved to {}\n", fs_path.lexically_normal().string()); auto out_cb = [&](const std::string& out, const std::string& err) { - Log(out_, out); - Log(out_, err); + Log(out_, "{}", out); + Log(out_, "{}", err); }; auto in_cb = [&](std::string& in) {}; auto run_cb = [&]() -> bool { @@ -263,8 +263,8 @@ void WhisperCPP::Start(const AppConfig& c) { { auto out_cb = [&](const std::string& out, const std::string& err) -> void { - Log(out_, out); - Log(out_, err); + Log(out_, "{}", out); + Log(out_, "{}", err); }; auto in_cb = [&](std::string& in) {}; auto run_cb = [&]() -> bool { -- cgit v1.2.3