update.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. from logging import config
  4. import os
  5. import socket
  6. import json
  7. def update_model(client_socket, config):
  8. try:
  9. if os.path.exists("model") == False:
  10. os.mkdir("model")
  11. if os.path.exists("model/hotword") == False:
  12. os.mkdir("model/hotword")
  13. UpdateRequired = False
  14. client_socket.send("GET_MODEL_INFO".encode('utf-8'))
  15. readed_data = client_socket.recv(1024).decode('utf-8')
  16. current_info = json.loads(readed_data)
  17. if current_info["name"] != config["hotword"]:
  18. config["hotword"] = current_info["name"]
  19. UpdateRequired = True
  20. elif os.path.exists("model/hotword/"+config["hotword"]+".keras") == False:
  21. UpdateRequired = True
  22. elif os.path.getsize("model/hotword/"+config["hotword"]+".keras") != current_info["size"]:
  23. UpdateRequired = True
  24. if UpdateRequired == True and "hotword" in config and config["hotword"] is not None and config["hotword"] != "":
  25. client_socket.send("SEND_MODEL".encode('utf-8'))
  26. with open("model/hotword/"+config["hotword"]+".keras", 'wb') as model_file:
  27. while True:
  28. data = client_socket.recv(4096)
  29. if not data:
  30. break
  31. if data == b"____~EOF____~":
  32. break;
  33. else:
  34. model_file.write(data)
  35. print("Model updated successfully.")
  36. return True
  37. else:
  38. print("Model is already up to date.")
  39. return False
  40. except Exception as e:
  41. print(f"Error updating model: {e}")
  42. return False