Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/SK-1287 | Add set-active to model resource #829

Merged
merged 1 commit into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions fedn/cli/client_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ def client_cmd(ctx):
@client_cmd.command("get-config")
@click.pass_context
def create_client(ctx, path: str, protocol: str, host: str, token: str = None, name: str = None, group: int = None):
r"""Return: \n
------ \n
client config file(s) with following content: \n
client_id: uuid \n
discover_host: controller, get from context file \n
name: client name (set prefix with options) \n
refresh_token: unique refresh token for client \n
token: unique access token for client \n
"""Generate client config file(s).
------
client config file(s) with following content:
client_id: uuid
discover_host: controller, get from context file
name: client name (set prefix with options)
refresh_token: unique refresh token for client
token: unique access token for client

"""
context_path = os.path.join(home_dir, ".fedn")
Expand Down Expand Up @@ -118,10 +118,10 @@ def create_client(ctx, path: str, protocol: str, host: str, token: str = None, n
@client_cmd.command("list")
@click.pass_context
def list_clients(ctx, protocol: str, host: str, port: str, token: str = None, n_max: int = None):
r"""Return: \n
------ \n
- count: number of clients \n
- result: list of clients \n
"""List clients.
------
- count: number of clients
- result: list of clients
"""
headers = {}

Expand All @@ -140,9 +140,9 @@ def list_clients(ctx, protocol: str, host: str, port: str, token: str = None, n_
@client_cmd.command("get")
@click.pass_context
def get_client(ctx, protocol: str, host: str, port: str, token: str = None, id: str = None):
r"""Return: \n
------ \n
- result: client with given id \n
"""Get client.
------
- result: client with given id
"""
response = get_response(protocol=protocol, host=host, port=port, endpoint=f"clients/{id}", token=token, headers={}, usr_api=False, usr_token=False)
print_response(response, "client", id)
Expand Down Expand Up @@ -209,7 +209,7 @@ def client_start_v2_cmd(
helper_type: str,
init: str,
):
"""Start a client."""
"""Start client."""
package = "local" if local_package else "remote"

config = {
Expand All @@ -232,7 +232,7 @@ def client_start_v2_cmd(

if init:
apply_config(init, config)
click.echo(f"\nClient configuration loaded from file: {init}")
click.echo(f"Client configuration loaded from file: {init}")

# to cater for old inputs
if config["discover_host"] is not None:
Expand Down
55 changes: 43 additions & 12 deletions fedn/cli/model_cmd.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import click
import requests

from .main import main
from .shared import CONTROLLER_DEFAULTS, get_response, print_response
from .shared import CONTROLLER_DEFAULTS, get_api_url, get_response, get_token, print_response


@main.group("model")
Expand All @@ -20,12 +21,7 @@ def model_cmd(ctx):
@model_cmd.command("list")
@click.pass_context
def list_models(ctx, protocol: str, host: str, port: str, token: str = None, session_id: str = None, n_max: int = None):
"""Return:
------
- count: number of models
- result: list of models

"""
"""List models."""
headers = {}

if n_max:
Expand All @@ -48,10 +44,45 @@ def list_models(ctx, protocol: str, host: str, port: str, token: str = None, ses
@model_cmd.command("get")
@click.pass_context
def get_model(ctx, protocol: str, host: str, port: str, token: str = None, id: str = None):
"""Return:
------
- result: model with given id

"""
"""Get model by id."""
response = get_response(protocol=protocol, host=host, port=port, endpoint=f"models/{id}", token=token, headers={}, usr_api=False, usr_token=False)
print_response(response, "model", id)


@click.option("-p", "--protocol", required=False, default=CONTROLLER_DEFAULTS["protocol"], help="Communication protocol of controller (api)")
@click.option("-H", "--host", required=False, default=CONTROLLER_DEFAULTS["host"], help="Hostname of controller (api)")
@click.option("-P", "--port", required=False, default=CONTROLLER_DEFAULTS["port"], help="Port of controller (api)")
@click.option("-t", "--token", required=False, help="Authentication token")
@click.option("-f", "--file", required=True, help="Path to the model file")
@model_cmd.command("set-active")
@click.pass_context
def set_active_model(ctx, protocol: str, host: str, port: str, token: str, file: str):
"""Set the initial model and upload to model repository."""
headers = {}
_token = get_token(token=token, usr_token=False)
if _token:
headers = {"Authorization": _token}

if file.endswith(".npz"):
helper = "numpyhelper"
elif file.endswith(".bin"):
helper = "binaryhelper"
else:
click.secho("Unsupported file type. Only .npz and .bin files are supported.", fg="red")
return

try:
# Set the active helper
url = get_api_url(protocol, host, port, "helpers/active", usr_api=False)
response = requests.put(url, json={"helper": helper}, headers=headers, verify=False)
response.raise_for_status()

# Upload the model file
url = get_api_url(protocol, host, port, "models/", usr_api=False)
with open(file, "rb") as model_file:
response = requests.post(url, files={"file": model_file}, data={"helper": helper}, headers=headers, verify=False)
response.raise_for_status()

click.secho("Model set as active and uploaded successfully.", fg="green")
except requests.exceptions.RequestException as e:
click.secho(f"Failed to set active model: {e}", fg="red")
2 changes: 1 addition & 1 deletion fedn/cli/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def get_context(context_path):
return context_data


def get_response(protocol: str, host: str, port: str, endpoint: str, token: str, headers: dict, usr_api: bool, usr_token: str):
def get_response(protocol: str, host: str, port: str, endpoint: str, token: str, headers: dict, usr_api: bool, usr_token: bool):
"""Utility function to retrieve response from get request based on provided information."""
url = get_api_url(protocol=protocol, host=host, port=port, endpoint=endpoint, usr_api=usr_api)

Expand Down
Loading