-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathterminal_interface.py
130 lines (100 loc) · 4.96 KB
/
terminal_interface.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# Copyright (C) 2023 AI&RD Research Group, Department of Information Engineering, University of Pisa
# SPDX-License-Identifier: Apache-2.0
import os
from pathlib import Path
import json
def retrieve_logs(container_name):
log_file = container_name + ".txt"
print(log_file)
Path("./logs").mkdir(parents=True, exist_ok=True)
os.system(f"docker logs {container_name} > ./logs/{log_file}")
os.system(f"docker logs {container_name}")
def retrieve_global_model(path, model_name):
"""
pass folder path to store the global model files
Format requested:
target_folder
folder_parent/target_folder
...
Starting folder: global_models
It is not necessary to have the folder structure prepared in advance,
the script will create what is missing in the file system
Three files will be saved in .npy format inside target_folder:
rules -- TSK_global_rules.npy
weights -- TSK_global_weights.npy
"""
path = "./global_models/"+path
Path(path).mkdir(parents=True, exist_ok=True)
os.system("docker cp aggregator_xai:/current_workspace/"+model_name+"_global_model_rules_antec.npy "+path+"/"+model_name+"_global_model_rules_antec.npy")
os.system("docker cp aggregator_xai:/current_workspace/"+model_name+"_global_model_rules_conseq.npy "+path+"/"+model_name+"_global_model_rules_conseq.npy")
os.system("docker cp aggregator_xai:/current_workspace/"+model_name+"_global_model_weights.npy "+path+"/"+model_name+"_global_model_weights.npy")
def rebuild_images():
if os.system("docker image inspect openfl_xai > /dev/null 2>&1") == 256:
os.system("docker build -f Dockerfile.openfl_xai -t openfl_xai .")
# remove all containers
os.system("docker rm -f $(docker ps -aq)")
# remove images
os.system("docker rmi openfl_xai/aggregator openfl_xai/collaborator")
# build collaborators images
os.system("docker build -f Dockerfile.xai_collaborator -t openfl_xai/collaborator .")
# build aggregator image
os.system("docker build -f Dockerfile.xai_aggregator -t openfl_xai/aggregator .")
return
def start_new_federation():
os.system("docker rm -f $(docker ps -aq)")
os.system("docker compose up -d")
def print_help():
# make a txt file with instructions and read - print it
os.system("clear")
print("---------------------------")
print("build: execute the docker build command to generate Docker images for AggregatorXAI and CollaboratorXAI components")
print("start: instantiate the containers as specified in docker-compose.yml. Upon creation, either containers will start AggregatorXAI or CollaboratorXAI instance")
print("status: execute command docker ps -aq to show all existing containers and their status onto the system")
print("logs: asks in input a container name, then print the output of the specified container on screen and into .txt file under /logs directory ")
print("save: intended use only after all contaienrs have terminated their execution without errors. \
Ask for folder name, then download under ./global_models/folder the aggregated model")
print("cls: clear screen output")
print("quit: terminate interface execution")
print("further information on the usage of the example can be found at: https://github.com/MattiaDaole/OpenFL-XAI#illustrative-example")
input("press enter to exit")
if __name__ == "__main__":
os.system("clear")
with open("./configuration.json", "r") as f:
config = json.load(f)
print("Configuration Imported:")
print(config)
while True:
print("---------------------------")
print("Command Aliases")
print("build - Build Images")
print("start - Start new Federation")
print("status - Check Containers status")
print("logs - Retrieve Container Logs")
print("save - Retrieve Global Model")
print("cls - Clear Interface Output")
print("help - Additional information about interface's commands")
print("quit - Quit")
print("---------------------------")
choice = input("Command: ")
if choice == "build":
rebuild_images()
elif choice == "start":
start_new_federation()
elif choice == "status":
os.system("docker ps -a")
elif choice == "logs":
container_name = input("Specify container name (examples: aggregator-xai, col0 ): ")
retrieve_logs(container_name)
elif choice == "save":
path = input("Specify Path to store model (no white spaces allowed): ")
retrieve_global_model(path, config["model_name"])
elif choice == "cls":
os.system("clear")
elif choice == "help":
print_help()
elif choice == "quit":
os.system("clear")
break
else:
print("Wrong Input")
continue