{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "id": "hlBugIxD1mM4" }, "outputs": [], "source": [ "!pip install torch safetensors huggingface_hub" ] }, { "cell_type": "code", "source": [ "import torch\n", "import torch.nn as nn\n", "\n", "from safetensors.torch import save_file" ], "metadata": { "id": "Va1tYSgo1wdP" }, "execution_count": 2, "outputs": [] }, { "cell_type": "code", "source": [ "class TinyModel(nn.Module):\n", " def __init__(self):\n", " super().__init__()\n", "\n", " self.layer1 = nn.Linear(4, 8)\n", " self.relu = nn.ReLU()\n", " self.layer2 = nn.Linear(8, 2)\n", "\n", " def forward(self, x):\n", " x = self.layer1(x)\n", " x = self.relu(x)\n", " x = self.layer2(x)\n", " return x" ], "metadata": { "id": "WIW3Ay8k1zb_" }, "execution_count": 3, "outputs": [] }, { "cell_type": "code", "source": [ "model = TinyModel()\n", "\n", "print(model)" ], "metadata": { "id": "9YeQA-G811UQ" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "save_file(model.state_dict(), \"tiny_empty_model.safetensors\")" ], "metadata": { "id": "n9BZ2UGW14Sb" }, "execution_count": 5, "outputs": [] }, { "cell_type": "code", "source": [ "import os\n", "\n", "print(os.listdir())" ], "metadata": { "id": "6fkroAm0164Z" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "from huggingface_hub import login\n", "\n", "login()" ], "metadata": { "id": "pjpGmemW2Tvb" }, "execution_count": 7, "outputs": [] }, { "cell_type": "code", "source": [ "from huggingface_hub import HfApi\n", "\n", "api = HfApi()\n", "\n", "api.upload_file(\n", " path_or_fileobj=\"tiny_empty_model.safetensors\",\n", " path_in_repo=\"tiny_empty_model.safetensors\",\n", " repo_id=\"harishforaiandml/tiny-empty-safetensor-model\",\n", " repo_type=\"model\",\n", ")" ], "metadata": { "id": "nrh5F23-2WGa" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "readme = \"\"\"\n", "# Tiny SafeTensor Model\n", "\n", "This is my first empty SafeTensor model uploaded from Google Colab.\n", "\n", "- Framework: PyTorch\n", "- Format: SafeTensor\n", "\"\"\"\n", "\n", "with open(\"README.md\", \"w\") as f:\n", " f.write(readme)" ], "metadata": { "id": "2q_KBzNx2hTY" }, "execution_count": 9, "outputs": [] }, { "cell_type": "code", "source": [ "api.upload_file(\n", " path_or_fileobj=\"README.md\",\n", " path_in_repo=\"README.md\",\n", " repo_id=\"harishforaiandml/tiny-empty-safetensor-model\",\n", " repo_type=\"model\",\n", ")" ], "metadata": { "id": "p-v0zbFP2m99" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "from safetensors.torch import load_file\n", "\n", "state_dict = load_file(\"tiny_empty_model.safetensors\")\n", "\n", "new_model = TinyModel()\n", "new_model.load_state_dict(state_dict)\n", "\n", "print(\"Model loaded successfully!\")" ], "metadata": { "id": "KqJRvQea2rNG" }, "execution_count": null, "outputs": [] } ] }