-
Notifications
You must be signed in to change notification settings - Fork 36
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-1102 | Simplify ClientAPI and add support for inference #726
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
797d9a7
Fixed spelling error
niklastheman 685236f
Added readme to display how to use client api
niklastheman b1dcdbe
Update README.rst
niklastheman f3ec362
Refactor/SK-1144 | Simplify on_train and on_validate (#731)
benjaminastrand 10ba44f
Merge branch 'master' into feature/SK-1102
benjaminastrand 257d27a
Add send_model_prediction to GrpcHandler
benjaminastrand 8c3de60
Add predict to ClientAPI + Ruff
benjaminastrand d2d8cc7
Bug fix - send ModelUpdate in status
benjaminastrand 40cb3d5
Create messages for update, validate and predict in GrpcHandler methods
benjaminastrand d3b6998
Update README - remove printouts and round_id
benjaminastrand 9b0e82a
Changed name ClientAPI -> FednClient
benjaminastrand f9cbe6b
Merge branch 'master' into feature/SK-1102
benjaminastrand 1d3cb0a
Fix - replace client_name with client_id in get_model_from_combiner
benjaminastrand c25bb22
change start-v2 -> start
Wrede 7e11b81
fix print_logs
Wrede 9bdb95b
fix
Wrede fd2b3df
fix
Wrede 082bc8c
remove echo
Wrede 21f9d05
fix
Wrede 38969e8
fix endpoint in wait_for
Wrede File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
Creating Your Own Client | ||
======================== | ||
|
||
This guide will help you create your own client for the FEDn network. | ||
|
||
Step-by-Step Instructions | ||
------------------------- | ||
|
||
1. **Create a virutal environment**: Start by creating a virtual environment and activating it. | ||
|
||
.. code-block:: bash | ||
|
||
python3 -m venv fedn-env | ||
source fedn-env/bin/activate | ||
|
||
|
||
2. **Install FEDn**: Install the FEDn package by running the following command: | ||
|
||
.. code-block:: bash | ||
|
||
pip install fedn | ||
|
||
3. **Create your client**: Copy and paste the code Below into a new file called `client_example.py`. | ||
|
||
.. code-block:: python | ||
|
||
import argparse | ||
|
||
from fedn.network.clients.fedn_client import FednClient, ConnectToApiResult | ||
|
||
|
||
def get_api_url(api_url: str, api_port: int): | ||
url = f"{api_url}:{api_port}" if api_port else api_url | ||
if not url.endswith("/"): | ||
url += "/" | ||
return url | ||
|
||
def on_train(in_model): | ||
training_metadata = { | ||
"num_examples": 1, | ||
"batch_size": 1, | ||
"epochs": 1, | ||
"lr": 1, | ||
} | ||
|
||
metadata = {"training_metadata": training_metadata} | ||
|
||
# Do your training here, out_model is your result... | ||
out_model = in_model | ||
|
||
return out_model, metadata | ||
|
||
def on_validate(in_model): | ||
# Calculate metrics here... | ||
metrics = { | ||
"test_accuracy": 0.9, | ||
"test_loss": 0.1, | ||
"train_accuracy": 0.8, | ||
"train_loss": 0.2, | ||
} | ||
return metrics | ||
|
||
def on_predict(in_model): | ||
# Do your prediction here... | ||
prediction = { | ||
"prediction": 1, | ||
"confidence": 0.9, | ||
} | ||
return prediction | ||
|
||
|
||
def main(api_url: str, api_port: int, token: str = None): | ||
fedn_client = FednClient(train_callback=on_train, validate_callback=on_validate, predict_callback=on_predict) | ||
|
||
url = get_api_url(api_url, api_port) | ||
|
||
name = input("Enter Client Name: ") | ||
fedn_client.set_name(name) | ||
|
||
client_id = str(uuid.uuid4()) | ||
fedn_client.set_client_id(client_id) | ||
|
||
controller_config = { | ||
"name": name, | ||
"client_id": client_id, | ||
"package": "local", | ||
"preferred_combiner": "", | ||
} | ||
|
||
result, combiner_config = fedn_client.connect_to_api(url, token, controller_config) | ||
|
||
if result != ConnectToApiResult.Assigned: | ||
print("Failed to connect to API, exiting.") | ||
return | ||
|
||
result: bool = fedn_client.init_grpchandler(config=combiner_config, client_name=client_id, token=token) | ||
|
||
if not result: | ||
return | ||
|
||
fedn_client.run() | ||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser(description="Client Example") | ||
parser.add_argument("--api-url", type=str, required=True, help="The API URL") | ||
parser.add_argument("--api-port", type=int, required=False, help="The API Port") | ||
parser.add_argument("--token", type=str, required=False, help="The API Token") | ||
|
||
args = parser.parse_args() | ||
main(args.api_url, args.api_port, args.token) | ||
|
||
|
||
4. **Run the client**: Run the client by executing the following command: | ||
|
||
.. code-block:: bash | ||
|
||
python client_example.py --api-url <full-api-url> --token <api-token> | ||
|
||
Replace `<api-url>` and `<api-token>` with the URL and token of the FEDn API. *Example when running a local FEDn instance: python client_example.py --api-url http://localhost:8092* | ||
|
||
5. **Start training**: Create a session and start training by using either the FEDn CLI or the FEDn UI. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is better to give a minimal, but working example. This could be confusing.