| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- from logging import config
- import os
- import socket
- import json
- def update_model(client_socket, config):
- try:
- if os.path.exists("model") == False:
- os.mkdir("model")
- if os.path.exists("model/hotword") == False:
- os.mkdir("model/hotword")
-
- UpdateRequired = False
- client_socket.send("GET_MODEL_INFO".encode('utf-8'))
- readed_data = client_socket.recv(1024).decode('utf-8')
- current_info = json.loads(readed_data)
- if current_info["name"] != config["hotword"]:
- config["hotword"] = current_info["name"]
- UpdateRequired = True
- elif os.path.exists("model/hotword/"+config["hotword"]+".keras") == False:
- UpdateRequired = True
- elif os.path.getsize("model/hotword/"+config["hotword"]+".keras") != current_info["size"]:
- UpdateRequired = True
-
- if UpdateRequired == True and "hotword" in config and config["hotword"] is not None and config["hotword"] != "":
- client_socket.send("SEND_MODEL".encode('utf-8'))
- with open("model/hotword/"+config["hotword"]+".keras", 'wb') as model_file:
- while True:
- data = client_socket.recv(4096)
- if not data:
- break
- if data == b"____~EOF____~":
- break;
- else:
- model_file.write(data)
- print("Model updated successfully.")
- return True
- else:
- print("Model is already up to date.")
- return False
- except Exception as e:
- print(f"Error updating model: {e}")
- return False
|