Download data/copilotj/plugin/__main__.py from introvoyz045/CopilotJ: direct link, hf CLI and curl.
- Browser
- Download file 5.22 kB
-
https://huggingface.co/datasets/introvoyz045/CopilotJ/resolve/main/data/copilotj/plugin/__main__.py
- Command line
-
hf download hf://datasets/introvoyz045/CopilotJ/data/copilotj/plugin/__main__.py
-
curl -L -o __main__.py https://huggingface.co/datasets/introvoyz045/CopilotJ/resolve/main/data/copilotj/plugin/__main__.py
5.22 kB
| # SPDX-FileCopyrightText: Copyright contributors to the CopilotJ project. | |
| # | |
| # SPDX-License-Identifier: Apache-2.0 | |
| import asyncio | |
| import base64 | |
| import json | |
| from collections.abc import Callable | |
| from io import BytesIO | |
| import click | |
| import rich | |
| import textual_image.renderable | |
| from PIL import Image as PILImage | |
| from copilotj.core import load_config | |
| from copilotj.plugin import HTTPPluginAPI, Verbosity | |
| from copilotj.plugin.api import ClientPluginAPI | |
| from copilotj.util import extract_base64_image | |
| def cli(ctx, server, verbosity): | |
| """CLI for interacting with the ImageJ plugin API.""" | |
| load_config() | |
| ctx.ensure_object(dict) | |
| ctx.obj["server"] = server | |
| match verbosity: | |
| case "low": | |
| ctx.obj["verbosity"] = Verbosity.NORMAL | |
| case "normal": | |
| ctx.obj["verbosity"] = Verbosity.NORMAL | |
| case "high": | |
| ctx.obj["verbosity"] = Verbosity.HIGH | |
| case _: | |
| ctx.obj["verbosity"] = Verbosity.NORMAL | |
| def run(get_api: Callable[[ClientPluginAPI], Callable], ctx, *args, **kwargs): | |
| async def runner(): | |
| apis = HTTPPluginAPI(ctx.obj["server"]) | |
| client_apis = apis.attach_single_client() | |
| api = get_api(client_apis) | |
| try: | |
| response = await api(*args, **kwargs) | |
| finally: | |
| await apis.close() | |
| return response | |
| loop = asyncio.new_event_loop() | |
| response = loop.run_until_complete(runner()) | |
| loop.close() | |
| if callable(getattr(response, "describe")): | |
| resp = response.describe(level=1, verbosity=ctx.obj["verbosity"]) | |
| else: | |
| resp = str(response) | |
| console = rich.console.Console() | |
| console.print(resp) | |
| def call_action(ctx, snapshot, action, parameters): | |
| """Capture the image from ImageJ.""" | |
| parsed = json.loads(parameters) if parameters else None | |
| run(lambda a: a.call_action, ctx, snapshot, action, parsed) | |
| def capture_image(ctx, title): | |
| """Capture the image from ImageJ.""" | |
| def capture_image(client: ClientPluginAPI): | |
| async def wrapper(title: str): | |
| result = await client.capture_image(title) | |
| b64 = extract_base64_image(result.image) | |
| image = PILImage.open(BytesIO(base64.b64decode(b64))) | |
| console = rich.console.Console() | |
| console.print(textual_image.renderable.Image(image)) | |
| return result | |
| return wrapper | |
| run(capture_image, ctx, title) | |
| def capture_screen(ctx): | |
| """Capture the current screen from ImageJ.""" | |
| def capture_screen(client: ClientPluginAPI): | |
| async def wrapper(): | |
| result = await client.capture_screen() | |
| console = rich.console.Console() | |
| for screen in result.screenshots: | |
| b64 = extract_base64_image(screen.image) | |
| image = PILImage.open(BytesIO(base64.b64decode(b64))) | |
| console.print(textual_image.renderable.Image(image)) | |
| return result | |
| return wrapper | |
| run(capture_screen, ctx) | |
| # ID of the early snapshot | |
| # ID of the later snapshot | |
| def compare_snapshots(ctx, early, later): | |
| """Compare two snapshots from ImageJ.""" | |
| run(lambda a: a.compare_snapshots, ctx, early, later) | |
| def get_operation_history(ctx, since): | |
| """Get the operation history from ImageJ.""" | |
| run(lambda a: a.get_operation_history, ctx, since) | |
| def take_snapshot(ctx): | |
| """Take a snapshot of the current ImageJ state.""" | |
| run(lambda a: a.take_snapshot, ctx) | |
| def run_script(ctx, language, script, script_file): | |
| """Run a script in ImageJ.""" | |
| # If a script file is provided, read its content | |
| if script_file: | |
| with open(script_file, "r") as f: | |
| script = f.read() | |
| assert script or script_file, "Either --script or --script-file must be provided" | |
| run(lambda a: a.run_script, ctx, language, script) | |
| def summarise_environment(ctx): | |
| """Get a summary of the current ImageJ environment.""" | |
| run(lambda a: a.summarise_environment, ctx) | |
| cli() | |