Upload main.ipynb with huggingface_hub
Browse files1770 unmodified lines1771177217731774177517761770 unmodified lines" imgsz=416\n",")"]}],"metadata": {1770 unmodified lines17711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751770 unmodified lines" imgsz=416\n",")"]},{"cell_type": "code","execution_count": null,"id": "312e2447","metadata": {},"outputs": [],"source": ["import time\n","import os\n","import subprocess\n","from ultralytics.utils import callbacks\n","\n","README_FILE_PATH = \"README.md\"\n","REPO_ID = \"Qbsoon/image-corner-rotation\"\n","UPDATE_FREQUENCY = 'epoch'\n","BATCH_UPDATE_INTERVAL = 50\n","TIME_UPDATE_INTERVAL_SECONDS = 300\n","LAST_UPDATE_TIME_FOR_README = time.time()\n","\n","status_data = {\n"," \"attempt\": 1,\n"," \"modelName\": \"YourModelName\",\n"," \"startDate\": \"N/A\",\n"," \"lastUpdate\": \"N/A\",\n"," \"datasetSize\": \"251440 Images\",\n"," \"equipment\": \"1x RTX 4070Ti Super (16GB VRAM)\",\n"," \"estimatedTotalTime\": \"~N/A\",\n"," \"currentEpoch\": 0,\n"," \"totalEpochs\": 0,\n"," \"overallProgress\": 0.0,\n"," \"statusText\": \"Initializing...\",\n"," \"training_active\": False\n","}\n","\n","def generate_widget_html(current_status):\n"," spinner_animation_style = \"animation: spin 1s linear infinite;\" if current_status[\"training_active\"] else \"display: none;\"\n"," display_progress = min(current_status[\"overallProgress\"], 100.0)\n","\n"," start_date_str = current_status[\"startDate\"]\n"," last_update_str = current_status[\"lastUpdate\"]\n"," if isinstance(start_date_str, float): start_date_str = time.strftime(\"%b %d, %Y\", time.localtime(start_date_str))\n"," if isinstance(last_update_str, float): last_update_str = time.strftime(\"%b %d, %Y %H:%M:%S\", time.localtime(last_update_str))\n","\n","\n"," html = f\"\"\"\n","<div class=\"card\">\n"," <div class=\"header\">\n"," <div class=\"spinner\" style=\"{spinner_animation_style}\"></div>\n"," <h2>{current_status[\"modelName\"]} training</h2>\n"," </div>\n"," <div class=\"info-blocks\">\n"," <div class=\"info-block\"><strong>START DATE:</strong> {start_date_str}</div>\n"," <div class=\"info-block\"><strong>LAST UPDATE:</strong> {last_update_str}</div>\n"," <div class=\"info-block\"><strong>DATASET SIZE:</strong> {current_status[\"datasetSize\"]}</div>\n"," <div class=\"info-block\"><strong>EQUIPMENT:</strong> {current_status[\"equipment\"]}</div>\n"," <div class=\"info-block\"><strong>TRAINING TIME:</strong> {current_status[\"estimatedTotalTime\"]}</div>\n"," </div>\n"," <div class=\"progress-container\">\n"," <div class=\"progress-bar\" style=\"width: {display_progress}%;\"></div>\n"," </div>\n"," <div class=\"status-text\">{current_status[\"statusText\"]}</div>\n","</div>\n","\"\"\"\n"," return html\n","\n","def update_readme_and_upload_with_hf_cli(widget_html_content):\n"," global REPO_ID\n"," if REPO_ID == \"YOUR_USERNAME/YOUR_MODEL_NAME\":\n"," print(\"ERROR: Please update REPO_ID in the script with your Hugging Face repository ID.\")\n"," return\n","\n"," try:\n"," with open(README_FILE_PATH, \"r\", encoding=\"utf-8\") as f:\n"," readme_content = f.read()\n","\n"," start_placeholder = \"<!-- WIDGET_START -->\"\n"," end_placeholder = \"<!-- WIDGET_END -->\"\n"," \n"," start_index = readme_content.find(start_placeholder)\n"," end_index = readme_content.find(end_placeholder)\n","\n"," if start_index != -1 and end_index != -1:\n"," pre_widget = readme_content[:start_index + len(start_placeholder)]\n"," post_widget = readme_content[end_index:]\n"," new_readme_content = pre_widget + \"\\n\" + widget_html_content + \"\\n\" + post_widget\n"," else:\n"," print(f\"Warning: Widget placeholders not found in {README_FILE_PATH}. Creating new content with widget.\")\n"," # Updated CSS here\n"," html_template_start = \"\"\"\n","<html>\n","<head>\n"," <style>\n"," /* Paste your full CSS styles here */\n"," body {{ font-family: Arial, sans-serif; }} /* Note: Double curly braces for f-string like formatting if this were an f-string, but it's a direct string here so single is fine. Keeping double for safety if it's ever converted. */\n"," .card {{ background: #FF8C00; border-radius: 16px; padding: 24px; width: 100%; max-width: 700px; box-shadow: 0 6px 12px rgba(0,0,0,0.2); color: #fff; box-sizing: border-box; margin: 20px auto; }}\n"," .card .header {{ display: flex; align-items: center; margin-bottom: 16px; }}\n"," .card .spinner {{ width: 24px; height: 24px; margin-right: 12px; border: 4px solid rgba(255,255,255,0.5); border-top-color: #fff; border-radius: 50%; animation: spin 1s linear infinite; }}\n"," .card .header h2 {{ font-size: 1.2rem; margin: 0; }}\n"," .card .info-blocks {{ display: flex; flex-wrap: nowrap; border: 1px solid rgba(255,255,255,0.5); border-radius: 12px; overflow: hidden; margin-bottom: 16px; }}\n"," .card .info-block {{ flex-grow: 1; flex-shrink: 1; flex-basis: 0; min-width: 0; padding: 8px 12px; font-size: 0.85rem; text-align: center; box-sizing: border-box; }}\n"," .card .info-block + .info-block {{ border-left: 1px solid rgba(255,255,255,0.5); }}\n"," .card .info-block strong {{ display: block; margin-bottom: 4px; font-weight: 600; text-transform: uppercase; }}\n"," .card .progress-container {{ background: rgba(255,255,255,0.3); border-radius: 12px; overflow: hidden; height: 14px; margin-bottom: 8px; }}\n"," .card .progress-bar {{ width: 0%; height: 100%; background: #fff; transition: width 0.5s ease-out; }}\n"," .card .status-text {{ font-size: 0.9rem; text-align: center; }}\n"," @keyframes spin {{ to {{ transform: rotate(360deg); }} }}\n"," </style>\n","</head>\n","<body>\n","{widget_content}\n","</body>\n","</html>\n","\"\"\"\n"," new_readme_content = html_template_start.format(widget_content=start_placeholder + \"\\n\" + widget_html_content + \"\\n\" + end_placeholder)\n","\n","\n"," with open(README_FILE_PATH, \"w\", encoding=\"utf-8\") as f:\n"," f.write(new_readme_content)\n","\n"," commit_message = f\"Update training progress: {status_data.get('statusText', 'Progress update')}\"\n"," subprocess.run([\n"," \"huggingface-cli\", \"upload\",\n"," REPO_ID,\n"," README_FILE_PATH,\n"," \"README.md\",\n"," \"--commit-message\", commit_message\n"," ], check=True, capture_output=True, text=True)\n"," print(f\"README.md uploaded to {REPO_ID} at {time.strftime('%Y-%m-%d %H:%M:%S')}\")\n","\n"," except FileNotFoundError:\n"," print(f\"Error: {README_FILE_PATH} not found. Ensure it exists in the current directory: {os.getcwd()}\")\n"," except subprocess.CalledProcessError as e:\n"," print(f\"Error during huggingface-cli upload: {e}\")\n"," print(f\"stdout: {e.stdout}\")\n"," print(f\"stderr: {e.stderr}\")\n"," except Exception as e:\n"," print(f\"An unexpected error occurred: {e}\")\n","\n","def on_train_start_callback(trainer):\n"," global LAST_UPDATE_TIME_FOR_README, status_data\n"," status_data[\"training_active\"] = True\n"," status_data[\"totalEpochs\"] = trainer.epochs\n"," status_data[\"startDate\"] = time.time()\n"," status_data[\"lastUpdate\"] = time.time()\n"," status_data[\"statusText\"] = \"Training started...\"\n"," status_data[\"overallProgress\"] = 0.0\n"," status_data[\"currentEpoch\"] = 0\n"," widget_html = generate_widget_html(status_data)\n"," update_readme_and_upload_with_hf_cli(widget_html)\n"," LAST_UPDATE_TIME_FOR_README = time.time()\n","\n","def on_epoch_end_callback(trainer):\n"," global LAST_UPDATE_TIME_FOR_README, status_data\n"," status_data[\"currentEpoch\"] = trainer.epoch + 1\n"," current_epoch_progress = 1.0\n"," overall_progress_val = ((trainer.epoch + current_epoch_progress) / trainer.epochs) * 100\n"," status_data[\"overallProgress\"] = round(overall_progress_val, 2)\n"," status_data[\"statusText\"] = f\"Fine-Tuning... {status_data['overallProgress']}% (Epoch {status_data['currentEpoch']}/{status_data['totalEpochs']})\"\n"," status_data[\"lastUpdate\"] = time.time()\n"," \n"," if UPDATE_FREQUENCY == 'epoch':\n"," widget_html = generate_widget_html(status_data)\n"," update_readme_and_upload_with_hf_cli(widget_html)\n"," LAST_UPDATE_TIME_FOR_README = time.time()\n","\n","def on_batch_end_callback(trainer):\n"," global LAST_UPDATE_TIME_FOR_README, status_data\n","\n"," if UPDATE_FREQUENCY == 'batch' and (trainer.batch_idx + 1) % BATCH_UPDATE_INTERVAL == 0:\n"," current_epoch_progress = (trainer.batch_idx + 1) / len(trainer.train_loader)\n"," overall_progress_val = ((trainer.epoch + current_epoch_progress) / trainer.epochs) * 100\n"," status_data[\"overallProgress\"] = round(overall_progress_val, 2)\n"," status_data[\"statusText\"] = f\"Fine-Tuning... {status_data['overallProgress']}% (Epoch {trainer.epoch + 1}/{trainer.epochs})\"\n"," status_data[\"lastUpdate\"] = time.time()\n"," widget_html = generate_widget_html(status_data)\n"," update_readme_and_upload_with_hf_cli(widget_html)\n"," LAST_UPDATE_TIME_FOR_README = time.time()\n","\n","def on_train_end_callback(trainer):\n"," global status_data\n"," status_data[\"training_active\"] = False\n"," status_data[\"overallProgress\"] = 100.0\n"," status_data[\"statusText\"] = \"Training completed!\"\n"," status_data[\"lastUpdate\"] = time.time()\n"," widget_html = generate_widget_html(status_data)\n"," update_readme_and_upload_with_hf_cli(widget_html)\n"," print(\"Final README.md update uploaded.\")\n","\n","\n","if hasattr(model, 'callbacks'):\n"," model.callbacks = {}\n"," \n","model.add_callback(\"on_train_start\", on_train_start_callback)\n","model.add_callback(\"on_epoch_end\", on_epoch_end_callback)\n","if UPDATE_FREQUENCY == 'batch':\n"," model.add_callback(\"on_batch_end\", on_batch_end_callback)\n","model.add_callback(\"on_train_end\", on_train_end_callback)"]}],"metadata": {
main.ipynb+199