-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
49 lines (38 loc) · 1.41 KB
/
server.py
File metadata and controls
49 lines (38 loc) · 1.41 KB
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
import argparse
import logging
import flwr as fl
from prometheus_client import Gauge, start_http_server
from strategy.strategy import FedCustom
# Initialize Logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Define a gauge to track the global model accuracy
accuracy_gauge = Gauge("model_accuracy", "Current accuracy of the global model")
# Define a gauge to track the global model loss
loss_gauge = Gauge("model_loss", "Current loss of the global model")
# Parse command line arguments
parser = argparse.ArgumentParser(description="Flower Server")
parser.add_argument(
"--number_of_rounds",
type=int,
default=100,
help="Number of FL rounds (default: 100)",
)
args = parser.parse_args()
# Function to Start Federated Learning Server
def start_fl_server(strategy, rounds):
try:
fl.server.start_server(
server_address="0.0.0.0:8080",
config=fl.server.ServerConfig(num_rounds=rounds),
strategy=strategy,
)
except Exception as e:
logger.error(f"FL Server error: {e}", exc_info=True)
# Main Function
if __name__ == "__main__":
# Start Prometheus Metrics Server
start_http_server(8000)
# Initialize Strategy Instance and Start FL Server
strategy_instance = FedCustom(accuracy_gauge=accuracy_gauge, loss_gauge=loss_gauge)
start_fl_server(strategy=strategy_instance, rounds=args.number_of_rounds)