Update advanced_training_notebook.ipynb

This commit is contained in:
MasterPhooey
2025-01-05 18:30:22 -06:00
committed by GitHub
parent f8cdb149d9
commit 03fb3fa0e2

View File

@@ -640,73 +640,49 @@
"id": "ex_UIWvwtjAN"
},
"outputs": [],
"source": [
"source": [
"import shutil\n",
"import json\n",
"from http.server import HTTPServer, SimpleHTTPRequestHandler\n",
"import threading\n",
"from pathlib import Path\n",
"from IPython.display import FileLink\n",
"\n",
"# Copy the TFLite model file to the working directory\n",
"# Define the source path and desired download location for the TFLite file\n",
"source_path = \"trained_models/wakeword/tflite_stream_state_internal_quant/stream_state_internal_quant.tflite\"\n",
"destination_path = \"./stream_state_internal_quant.tflite\"\n",
"\n",
"# Copy the TFLite file to the current working directory\n",
"shutil.copy(source_path, destination_path)\n",
"\n",
"# Define the JSON metadata for the model\n",
"# Define the JSON file content\n",
"json_data = {\n",
" \"type\": \"micro\",\n",
" \"wake_word\": \"hey_norman\", # Adjust based on your target wake word\n",
" \"wake_word\": \"khum_puter\", # Adjust this if the target_word changes dynamically\n",
" \"author\": \"master phooey\",\n",
" \"website\": \"https://github.com/MasterPhooey/MicroWakeWord-Trainer-Docker\",\n",
" \"model\": \"stream_state_internal_quant.tflite\",\n",
" \"trained_languages\": [\"en\"],\n",
" \"version\": 2,\n",
" \"micro\": {\n",
" \"probability_cutoff\": 0.97, # Threshold for wake word detection\n",
" \"sliding_window_size\": 5, # Frames averaged for predictions\n",
" \"probability_cutoff\": 0.97,\n",
" \"sliding_window_size\": 5,\n",
" \"feature_step_size\": 10,\n",
" \"tensor_arena_size\": 30000, # Memory allocation for TensorFlow Lite model\n",
" \"tensor_arena_size\": 30000,\n",
" \"minimum_esphome_version\": \"2024.7.0\"\n",
" }\n",
"}\n",
"\n",
"# Write the metadata to a JSON file\n",
"# Define the JSON file path\n",
"json_path = \"./stream_state_internal_quant.json\"\n",
"\n",
"# Write the JSON file\n",
"with open(json_path, \"w\") as json_file:\n",
" json.dump(json_data, json_file, indent=2)\n",
"\n",
"# Custom HTTPRequestHandler to set the Content-Disposition header\n",
"class CustomHTTPRequestHandler(SimpleHTTPRequestHandler):\n",
" def end_headers(self):\n",
" if self.path.endswith(\".json\"):\n",
" self.send_header(\"Content-Disposition\", \"attachment\")\n",
" super().end_headers()\n",
"\n",
"# Start an HTTP server in a separate thread\n",
"def start_server():\n",
" server = HTTPServer((\"0.0.0.0\", 8000), CustomHTTPRequestHandler)\n",
" print(\"Serving files on http://localhost:8000\")\n",
" server.serve_forever()\n",
"\n",
"thread = threading.Thread(target=start_server, daemon=True)\n",
"thread.start()\n",
"\n",
"# Generate download links with styled HTML\n",
"html_content = f\"\"\"\n",
"<div align=\\\"center\\\">,\n",
"<img src=\\\"https://raw.githubusercontent.com/MasterPhooey/MicroWakeWord-Trainer-Docker/refs/heads/main/mmw.png\\\" alt=\\\"MicroWakeWord Trainer Logo\\\" width=\\\"100\\\" />\\n\"\n",
"<h1>MicroWakeWord Trainer Docker</h1>\n",
"</div>\\n\n",
"<h3 style=\"color:orange;\">Your files are ready for download:</h3>\n",
"<ul>\n",
" <li><b><a href=\"http://localhost:8000/stream_state_internal_quant.tflite\" target=\"_blank\" style=\"color:orange;\">TFLite Model: stream_state_internal_quant.tflite</a></b></li>\n",
" <li><b><a href=\"http://localhost:8000/stream_state_internal_quant.json\" target=\"_blank\" style=\"color:orange;\">JSON Metadata: stream_state_internal_quant.json</a></b></li>\n",
"</ul>\n",
"<p style=\"font-size:12px; color:gray;\">Click the links to download the files. Ensure the files are moved to the correct directory for deployment.</p>\n",
"\"\"\"\n",
"\n",
"from IPython.display import HTML\n",
"display(HTML(html_content))"
"# Generate download links for both files\n",
"print(\"Download your files:\")\n",
"print(\"TFLite Model:\")\n",
"display(FileLink(destination_path))\n",
"print(\"\\nJSON Metadata:\")\n",
"display(FileLink(json_path))"
]
}
],