in src/autotrain/cli/run_app.py [0:0]
def run(self):
if self.colab:
from IPython.display import display
from autotrain.app.colab import colab_app
elements = colab_app()
display(elements)
return
if self.share:
from pyngrok import ngrok
os.system(f"fuser -n tcp -k {self.port}")
authtoken = os.environ.get("NGROK_AUTH_TOKEN", "")
if authtoken.strip() == "":
logger.info("NGROK_AUTH_TOKEN not set")
raise ValueError("NGROK_AUTH_TOKEN not set. Please set it!")
ngrok.set_auth_token(authtoken)
active_tunnels = ngrok.get_tunnels()
for tunnel in active_tunnels:
public_url = tunnel.public_url
ngrok.disconnect(public_url)
url = ngrok.connect(addr=self.port, bind_tls=True)
logger.info(f"AutoTrain Public URL: {url}")
logger.info("Please wait for the app to load...")
command = f"uvicorn autotrain.app.app:app --host {self.host} --port {self.port}"
command += f" --workers {self.workers}"
with open("autotrain.log", "w", encoding="utf-8") as log_file:
if sys.platform == "win32":
process = subprocess.Popen(
command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True, text=True, bufsize=1
)
else:
process = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=True,
text=True,
bufsize=1,
preexec_fn=os.setsid,
)
output_thread = threading.Thread(target=handle_output, args=(process.stdout, log_file))
output_thread.start()
try:
process.wait()
output_thread.join()
except KeyboardInterrupt:
logger.warning("Attempting to terminate the process...")
if sys.platform == "win32":
process.terminate()
else:
# If user cancels (Ctrl+C), terminate the subprocess
# Use os.killpg to send SIGTERM to the process group, ensuring all child processes are killed
os.killpg(os.getpgid(process.pid), signal.SIGTERM)
logger.info("Process terminated by user")