From f793438d41eea3686e7b2122c052a9a6ed0dda42 Mon Sep 17 00:00:00 2001 From: arkohut <39525455+arkohut@users.noreply.github.com> Date: Sat, 12 Oct 2024 23:17:00 +0800 Subject: [PATCH] feat(cli): add ps control cmds --- memos/commands.py | 83 ++++++++++++++++++++++++++++--- memos/plugins/ocr/main.py | 2 +- memos/plugins/ocr/temp_ppocr.yaml | 40 +++++++++++++++ 3 files changed, 117 insertions(+), 8 deletions(-) create mode 100644 memos/plugins/ocr/temp_ppocr.yaml diff --git a/memos/commands.py b/memos/commands.py index e6e446a..50ad7e6 100644 --- a/memos/commands.py +++ b/memos/commands.py @@ -20,6 +20,8 @@ import platform from .cmds.plugin import plugin_app, bind from .cmds.library import lib_app, scan, index, watch import psutil +import signal +from tabulate import tabulate app = typer.Typer(context_settings={"help_option_names": ["-h", "--help"]}) @@ -249,7 +251,7 @@ start /B "" "{pythonw_path}" -m memos.commands watch > "{log_dir / 'watch.log'}" content = f"""@echo off start /B "" "{pythonw_path}" -m memos.commands record > "{log_dir / 'record.log'}" 2>&1 start /B "" "{pythonw_path}" -m memos.commands serve > "{log_dir / 'serve.log'}" 2>&1 -timeout /t 5 /nobreak >nul +timeout /t 10 /nobreak >nul start /B "" "{pythonw_path}" -m memos.commands watch > "{log_dir / 'watch.log'}" 2>&1 """ @@ -445,6 +447,7 @@ def enable(): def ps(): """Show the status of Memos processes""" services = ["serve", "watch", "record"] + table_data = [] for service in services: processes = [p for p in psutil.process_iter(['pid', 'name', 'cmdline', 'create_time']) @@ -456,13 +459,79 @@ def ps(): for process in processes: create_time = datetime.fromtimestamp(process.info['create_time']).strftime('%Y-%m-%d %H:%M:%S') running_time = str(timedelta(seconds=int(time.time() - process.info['create_time']))) - typer.echo(f"{service.capitalize()} is running:") - typer.echo(f" PID: {process.info['pid']}") - typer.echo(f" Started at: {create_time}") - typer.echo(f" Running for: {running_time}") + table_data.append([ + service.capitalize(), + "Running", + process.info['pid'], + create_time, + running_time + ]) else: - typer.echo(f"{service.capitalize()} is not running") - typer.echo("---") + table_data.append([service.capitalize(), "Not Running", "-", "-", "-"]) + + headers = ["Name", "Status", "PID", "Started At", "Running For"] + typer.echo(tabulate(table_data, headers=headers, tablefmt="plain")) + + +@app.command() +def stop(): + """Stop all running Memos processes""" + services = ["serve", "watch", "record"] + stopped = False + + for service in services: + processes = [p for p in psutil.process_iter(['pid', 'name', 'cmdline']) + if 'python' in p.info['name'].lower() and + 'memos.commands' in p.info['cmdline'] and + service in p.info['cmdline']] + + for process in processes: + try: + os.kill(process.info['pid'], signal.SIGTERM) + typer.echo(f"Stopped {service} process (PID: {process.info['pid']})") + stopped = True + except ProcessLookupError: + typer.echo(f"Process {service} (PID: {process.info['pid']}) not found") + except PermissionError: + typer.echo(f"Permission denied to stop {service} process (PID: {process.info['pid']})") + + if not stopped: + typer.echo("No running Memos processes found") + else: + typer.echo("All Memos processes have been stopped") + + +@app.command() +def start(): + """Start all Memos processes""" + memos_dir = get_memos_dir() + + if is_windows(): + bat_path = memos_dir / "launch.bat" + if not bat_path.exists(): + typer.echo("Launch script not found. Please run 'memos enable' first.") + return + + try: + subprocess.Popen([str(bat_path)], shell=True, creationflags=subprocess.CREATE_NEW_CONSOLE) + typer.echo("Started Memos processes. Check the logs for more information.") + except Exception as e: + typer.echo(f"Failed to start Memos processes: {str(e)}") + + elif is_macos(): + launch_sh_path = memos_dir / "launch.sh" + if not launch_sh_path.exists(): + typer.echo("Launch script not found. Please run 'memos enable' first.") + return + + try: + subprocess.Popen(["bash", str(launch_sh_path)], start_new_session=True) + typer.echo("Started Memos processes. Check the logs for more information.") + except Exception as e: + typer.echo(f"Failed to start Memos processes: {str(e)}") + + else: + typer.echo("Unsupported operating system.") if __name__ == "__main__": diff --git a/memos/plugins/ocr/main.py b/memos/plugins/ocr/main.py index 690a139..46df093 100644 --- a/memos/plugins/ocr/main.py +++ b/memos/plugins/ocr/main.py @@ -7,7 +7,7 @@ import json import base64 from PIL import Image import numpy as np -from rapidocr_onnxruntime import RapidOCR +from rapidocr_openvino import RapidOCR from concurrent.futures import ThreadPoolExecutor from functools import partial import yaml diff --git a/memos/plugins/ocr/temp_ppocr.yaml b/memos/plugins/ocr/temp_ppocr.yaml new file mode 100644 index 0000000..ae088f7 --- /dev/null +++ b/memos/plugins/ocr/temp_ppocr.yaml @@ -0,0 +1,40 @@ +Cls: + cls_batch_num: 6 + cls_image_shape: + - 3 + - 48 + - 192 + cls_thresh: 0.9 + label_list: + - '0' + - '180' + model_path: C:\Users\arkoh\projects\memos\memos\plugins\ocr\models\ch_ppocr_mobile_v2.0_cls_train.onnx + use_cuda: false +Det: + box_thresh: 0.3 + limit_side_len: 1500 + limit_type: min + max_candidates: 1000 + model_path: C:\Users\arkoh\projects\memos\memos\plugins\ocr\models\ch_PP-OCRv4_det_infer.onnx + score_mode: fast + thresh: 0.3 + unclip_ratio: 1.6 + use_cuda: false + use_dilation: true +Global: + min_height: 30 + print_verbose: false + text_score: 0.5 + use_cls: true + use_det: true + use_rec: true + use_space_char: true + width_height_ratio: 40 +Rec: + model_path: C:\Users\arkoh\projects\memos\memos\plugins\ocr\models\ch_PP-OCRv4_rec_infer.onnx + rec_batch_num: 6 + rec_img_shape: + - 3 + - 48 + - 320 + use_cuda: false