Buckets:
| <meta charset="utf-8" /><meta name="hf:doc:metadata" content="{"title":"Webhook guide: Setup an automatic system to re-train a model when a dataset changes","local":"webhook-guide-setup-an-automatic-system-to-re-train-a-model-when-a-dataset-changes","sections":[{"title":"Prerequisite: Upload your dataset to the Hub","local":"prerequisite-upload-your-dataset-to-the-hub","sections":[],"depth":2},{"title":"Create a Webhook to react to the dataset’s changes","local":"create-a-webhook-to-react-to-the-datasets-changes","sections":[],"depth":2},{"title":"Create a Space to react to your Webhook","local":"create-a-space-to-react-to-your-webhook","sections":[],"depth":2},{"title":"Configure your Webhook to send events to your Space","local":"configure-your-webhook-to-send-events-to-your-space","sections":[],"depth":2}],"depth":1}"> | |
| <link href="/docs/hub/pr_2521/en/_app/immutable/assets/0.e3b0c442.css" rel="modulepreload"> | |
| <link rel="modulepreload" href="/docs/hub/pr_2521/en/_app/immutable/entry/start.d19e5ca7.js"> | |
| <link rel="modulepreload" href="/docs/hub/pr_2521/en/_app/immutable/chunks/scheduler.409792a1.js"> | |
| <link rel="modulepreload" href="/docs/hub/pr_2521/en/_app/immutable/chunks/singletons.1ece723a.js"> | |
| <link rel="modulepreload" href="/docs/hub/pr_2521/en/_app/immutable/chunks/index.0f0d9f26.js"> | |
| <link rel="modulepreload" href="/docs/hub/pr_2521/en/_app/immutable/chunks/paths.a67d9216.js"> | |
| <link rel="modulepreload" href="/docs/hub/pr_2521/en/_app/immutable/entry/app.98ab229a.js"> | |
| <link rel="modulepreload" href="/docs/hub/pr_2521/en/_app/immutable/chunks/preload-helper.78e52d9f.js"> | |
| <link rel="modulepreload" href="/docs/hub/pr_2521/en/_app/immutable/chunks/index.92d389ff.js"> | |
| <link rel="modulepreload" href="/docs/hub/pr_2521/en/_app/immutable/nodes/0.7799902b.js"> | |
| <link rel="modulepreload" href="/docs/hub/pr_2521/en/_app/immutable/chunks/each.e59479a4.js"> | |
| <link rel="modulepreload" href="/docs/hub/pr_2521/en/_app/immutable/nodes/255.e84cd0b8.js"> | |
| <link rel="modulepreload" href="/docs/hub/pr_2521/en/_app/immutable/chunks/CopyLLMTxtMenu.dcf7fb47.js"> | |
| <link rel="modulepreload" href="/docs/hub/pr_2521/en/_app/immutable/chunks/MermaidChart.svelte_svelte_type_style_lang.0c189c7e.js"> | |
| <link rel="modulepreload" href="/docs/hub/pr_2521/en/_app/immutable/chunks/CodeBlock.5ed269c1.js"><!-- HEAD_svelte-u9bgzb_START --><meta name="hf:doc:metadata" content="{"title":"Webhook guide: Setup an automatic system to re-train a model when a dataset changes","local":"webhook-guide-setup-an-automatic-system-to-re-train-a-model-when-a-dataset-changes","sections":[{"title":"Prerequisite: Upload your dataset to the Hub","local":"prerequisite-upload-your-dataset-to-the-hub","sections":[],"depth":2},{"title":"Create a Webhook to react to the dataset’s changes","local":"create-a-webhook-to-react-to-the-datasets-changes","sections":[],"depth":2},{"title":"Create a Space to react to your Webhook","local":"create-a-space-to-react-to-your-webhook","sections":[],"depth":2},{"title":"Configure your Webhook to send events to your Space","local":"configure-your-webhook-to-send-events-to-your-space","sections":[],"depth":2}],"depth":1}"><!-- HEAD_svelte-u9bgzb_END --> <p></p> <div class="items-center shrink-0 min-w-[100px] max-sm:min-w-[50px] justify-end ml-auto flex" style="float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"><div class="inline-flex rounded-md max-sm:rounded-sm"><button class="inline-flex items-center gap-1 h-7 max-sm:h-7 px-2 max-sm:px-1.5 text-sm font-medium text-gray-800 border border-r-0 rounded-l-md max-sm:rounded-l-sm border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-live="polite"><span class="inline-flex items-center justify-center rounded-md p-0.5 max-sm:p-0 hover:text-gray-800 dark:hover:text-gray-200"><svg class="sm:size-3.5 size-3" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg></span> <span>Copy page</span></button> <button class="inline-flex items-center justify-center w-6 max-sm:w-5 h-7 max-sm:h-7 disabled:pointer-events-none text-sm text-gray-500 hover:text-gray-700 dark:hover:text-white rounded-r-md max-sm:rounded-r-sm border border-l transition border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-haspopup="menu" aria-expanded="false" aria-label="Open copy menu"><svg class="transition-transform text-gray-400 overflow-visible sm:size-3.5 size-3 rotate-0" width="1em" height="1em" viewBox="0 0 12 7" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M1 1L6 6L11 1" stroke="currentColor"></path></svg></button></div> </div> <h1 class="relative group"><a id="webhook-guide-setup-an-automatic-system-to-re-train-a-model-when-a-dataset-changes" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#webhook-guide-setup-an-automatic-system-to-re-train-a-model-when-a-dataset-changes"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Webhook guide: Setup an automatic system to re-train a model when a dataset changes</span></h1> <p data-svelte-h="svelte-ze8gh0">This guide will help walk you through the setup of an automatic training pipeline on the Hugging Face platform | |
| using HF Datasets, Webhooks, Spaces, and AutoTrain.</p> <p data-svelte-h="svelte-8my4lw">We will build a Webhook that listens to changes on an image classification dataset and triggers a fine-tuning | |
| of <a href="https://huggingface.co/microsoft/resnet-50" rel="nofollow">microsoft/resnet-50</a> using <a href="https://huggingface.co/autotrain" rel="nofollow">AutoTrain</a>.</p> <h2 class="relative group"><a id="prerequisite-upload-your-dataset-to-the-hub" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#prerequisite-upload-your-dataset-to-the-hub"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Prerequisite: Upload your dataset to the Hub</span></h2> <p data-svelte-h="svelte-1bbg0un">We will use a <a href="https://huggingface.co/datasets/huggingface-projects/auto-retrain-input-dataset" rel="nofollow">simple image classification dataset</a> for the sake | |
| of the example. Learn more about uploading your data to the Hub <a href="https://huggingface.co/docs/datasets/upload_dataset" rel="nofollow">here</a>.</p> <p data-svelte-h="svelte-l6l0rz"><img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/hub/webhooks-guides/002-auto-retrain/dataset.png" alt="dataset"></p> <h2 class="relative group"><a id="create-a-webhook-to-react-to-the-datasets-changes" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#create-a-webhook-to-react-to-the-datasets-changes"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Create a Webhook to react to the dataset’s changes</span></h2> <p data-svelte-h="svelte-15jch62">First, let’s create a Webhook from your <a href="https://huggingface.co/settings/webhooks" rel="nofollow">settings</a>.</p> <ul data-svelte-h="svelte-vx6oz2"><li>Select your dataset as the target repository. We will target <a href="https://huggingface.co/datasets/huggingface-projects/input-dataset" rel="nofollow">huggingface-projects/input-dataset</a> in this example.</li> <li>You can put a dummy Webhook URL for now. Defining your Webhook will let you look at the events that will be sent to it. You can also replay them, which will be useful for debugging!</li> <li>Input a secret to make it more secure.</li> <li>Subscribe to “Repo update” events as we want to react to data changes</li></ul> <p data-svelte-h="svelte-1kjjv0e">Your Webhook will look like this:</p> <p data-svelte-h="svelte-1lz55rf"><img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/hub/webhooks-guides/002-auto-retrain/webhook-creation.png" alt="webhook-creation"></p> <h2 class="relative group"><a id="create-a-space-to-react-to-your-webhook" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#create-a-space-to-react-to-your-webhook"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Create a Space to react to your Webhook</span></h2> <p data-svelte-h="svelte-fqh90s">We now need a way to react to your Webhook events. An easy way to do this is to use a <a href="https://huggingface.co/docs/hub/spaces-overview" rel="nofollow">Space</a>!</p> <p data-svelte-h="svelte-sual4m">You can find an example Space <a href="https://huggingface.co/spaces/huggingface-projects/auto-retrain/tree/main" rel="nofollow">here</a>.</p> <p data-svelte-h="svelte-j52eq3">This Space uses Docker, Python, <a href="https://fastapi.tiangolo.com/" rel="nofollow">FastAPI</a>, and <a href="https://www.uvicorn.org" rel="nofollow">uvicorn</a> to run a simple HTTP server. Read more about Docker Spaces <a href="https://huggingface.co/docs/hub/spaces-sdks-docker" rel="nofollow">here</a>.</p> <p data-svelte-h="svelte-1euo8xv">The entry point is <a href="https://huggingface.co/spaces/huggingface-projects/auto-retrain/blob/main/src/main.py" rel="nofollow">src/main.py</a>. Let’s walk through this file and detail what it does:</p> <ol data-svelte-h="svelte-1xhscr8"><li>It spawns a FastAPI app that will listen to HTTP <code>POST</code> requests on <code>/webhook</code>:</li></ol> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-python "><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> fastapi <span class="hljs-keyword">import</span> FastAPI | |
| <span class="hljs-comment"># [...]</span> | |
| <span class="hljs-meta">@app.post(<span class="hljs-params"><span class="hljs-string">"/webhook"</span></span>)</span> | |
| <span class="hljs-keyword">async</span> <span class="hljs-keyword">def</span> <span class="hljs-title function_">post_webhook</span>(<span class="hljs-params"> | |
| <span class="hljs-comment"># ...</span> | |
| </span>): | |
| <span class="hljs-comment"># ...</span><!-- HTML_TAG_END --></pre></div> <ol start="2" data-svelte-h="svelte-199ucq9"><li><ol start="2"><li>This route checks that the <code>X-Webhook-Secret</code> header is present and that its value is the same as the one you set in your Webhook’s settings. The <code>WEBHOOK_SECRET</code> secret must be set in the Space’s settings and be the same as the secret set in your Webhook.</li></ol></li></ol> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-python "><!-- HTML_TAG_START --><span class="hljs-comment"># [...]</span> | |
| WEBHOOK_SECRET = os.getenv(<span class="hljs-string">"WEBHOOK_SECRET"</span>) | |
| <span class="hljs-comment"># [...]</span> | |
| <span class="hljs-meta">@app.post(<span class="hljs-params"><span class="hljs-string">"/webhook"</span></span>)</span> | |
| <span class="hljs-keyword">async</span> <span class="hljs-keyword">def</span> <span class="hljs-title function_">post_webhook</span>(<span class="hljs-params"> | |
| <span class="hljs-comment"># [...]</span> | |
| x_webhook_secret: <span class="hljs-type">Optional</span>[<span class="hljs-built_in">str</span>] = Header(<span class="hljs-params">default=<span class="hljs-literal">None</span></span>), | |
| <span class="hljs-comment"># ^ checks for the X-Webhook-Secret HTTP header</span> | |
| </span>): | |
| <span class="hljs-keyword">if</span> x_webhook_secret <span class="hljs-keyword">is</span> <span class="hljs-literal">None</span>: | |
| <span class="hljs-keyword">raise</span> HTTPException(<span class="hljs-number">401</span>) | |
| <span class="hljs-keyword">if</span> x_webhook_secret != WEBHOOK_SECRET: | |
| <span class="hljs-keyword">raise</span> HTTPException(<span class="hljs-number">403</span>) | |
| <span class="hljs-comment"># [...]</span><!-- HTML_TAG_END --></pre></div> <ol start="3" data-svelte-h="svelte-18lnge2"><li>The event’s payload is encoded as JSON. Here, we’ll be using pydantic models to parse the event payload. We also specify that we will run our Webhook only when:</li></ol> <ul data-svelte-h="svelte-1gbdb1z"><li>the event concerns the input dataset</li> <li>the event is an update on the repo’s content, i.e., there has been a new commit</li></ul> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-python "><!-- HTML_TAG_START --><span class="hljs-comment"># defined in src/models.py</span> | |
| <span class="hljs-keyword">class</span> <span class="hljs-title class_">WebhookPayloadEvent</span>(<span class="hljs-title class_ inherited__">BaseModel</span>): | |
| action: <span class="hljs-type">Literal</span>[<span class="hljs-string">"create"</span>, <span class="hljs-string">"update"</span>, <span class="hljs-string">"delete"</span>, <span class="hljs-string">"move"</span>] | |
| scope: <span class="hljs-built_in">str</span> | |
| <span class="hljs-keyword">class</span> <span class="hljs-title class_">WebhookPayloadRepo</span>(<span class="hljs-title class_ inherited__">BaseModel</span>): | |
| <span class="hljs-built_in">type</span>: <span class="hljs-type">Literal</span>[<span class="hljs-string">"dataset"</span>, <span class="hljs-string">"model"</span>, <span class="hljs-string">"space"</span>] | |
| name: <span class="hljs-built_in">str</span> | |
| <span class="hljs-built_in">id</span>: <span class="hljs-built_in">str</span> | |
| private: <span class="hljs-built_in">bool</span> | |
| headSha: <span class="hljs-built_in">str</span> | |
| <span class="hljs-keyword">class</span> <span class="hljs-title class_">WebhookPayload</span>(<span class="hljs-title class_ inherited__">BaseModel</span>): | |
| event: WebhookPayloadEvent | |
| repo: WebhookPayloadRepo | |
| <span class="hljs-comment"># [...]</span> | |
| <span class="hljs-meta">@app.post(<span class="hljs-params"><span class="hljs-string">"/webhook"</span></span>)</span> | |
| <span class="hljs-keyword">async</span> <span class="hljs-keyword">def</span> <span class="hljs-title function_">post_webhook</span>(<span class="hljs-params"> | |
| <span class="hljs-comment"># [...]</span> | |
| payload: WebhookPayload, | |
| <span class="hljs-comment"># ^ Pydantic model defining the payload format</span> | |
| </span>): | |
| <span class="hljs-comment"># [...]</span> | |
| <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> ( | |
| payload.event.action == <span class="hljs-string">"update"</span> | |
| <span class="hljs-keyword">and</span> payload.event.scope.startswith(<span class="hljs-string">"repo.content"</span>) | |
| <span class="hljs-keyword">and</span> payload.repo.name == config.input_dataset | |
| <span class="hljs-keyword">and</span> payload.repo.<span class="hljs-built_in">type</span> == <span class="hljs-string">"dataset"</span> | |
| ): | |
| <span class="hljs-comment"># no-op if the payload does not match our expectations</span> | |
| <span class="hljs-keyword">return</span> {<span class="hljs-string">"processed"</span>: <span class="hljs-literal">False</span>} | |
| <span class="hljs-comment">#[...]</span><!-- HTML_TAG_END --></pre></div> <ol start="4" data-svelte-h="svelte-1jgt1rb"><li>If the payload is valid, the next step is to create a project on AutoTrain, schedule a fine-tuning of the input model (<code>microsoft/resnet-50</code> in our example) on the input dataset, and create a discussion on the dataset when it’s done!</li></ol> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-python "><!-- HTML_TAG_START --><span class="hljs-keyword">def</span> <span class="hljs-title function_">schedule_retrain</span>(<span class="hljs-params">payload: WebhookPayload</span>): | |
| <span class="hljs-comment"># Create the autotrain project</span> | |
| <span class="hljs-keyword">try</span>: | |
| project = AutoTrain.create_project(payload) | |
| AutoTrain.add_data(project_id=project[<span class="hljs-string">"id"</span>]) | |
| AutoTrain.start_processing(project_id=project[<span class="hljs-string">"id"</span>]) | |
| <span class="hljs-keyword">except</span> requests.HTTPError <span class="hljs-keyword">as</span> err: | |
| <span class="hljs-built_in">print</span>(<span class="hljs-string">"ERROR while requesting AutoTrain API:"</span>) | |
| <span class="hljs-built_in">print</span>(<span class="hljs-string">f" code: <span class="hljs-subst">{err.response.status_code}</span>"</span>) | |
| <span class="hljs-built_in">print</span>(<span class="hljs-string">f" <span class="hljs-subst">{err.response.json()}</span>"</span>) | |
| <span class="hljs-keyword">raise</span> | |
| <span class="hljs-comment"># Notify in the community tab</span> | |
| notify_success(project[<span class="hljs-string">"id"</span>])<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-2wkt37">Visit the link inside the comment to review the training cost estimate, and start fine-tuning the model!</p> <p data-svelte-h="svelte-17amjt1"><img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/hub/webhooks-guides/002-auto-retrain/notification.png" alt="community tab notification"></p> <p data-svelte-h="svelte-1upb8bg">In this example, we used Hugging Face AutoTrain to fine-tune our model quickly, but you can of course plug in your training infrastructure!</p> <p data-svelte-h="svelte-1ieosw8">Feel free to duplicate the Space to your personal namespace and play with it. You will need to provide two secrets:</p> <ul data-svelte-h="svelte-1mva7ee"><li><code>WEBHOOK_SECRET</code> : the secret from your Webhook.</li> <li><code>HF_ACCESS_TOKEN</code> : a User Access Token with <code>write</code> rights. You can create one <a href="https://huggingface.co/settings/tokens" rel="nofollow">from your settings</a>.</li></ul> <p data-svelte-h="svelte-ch6cio">You will also need to tweak the <a href="https://huggingface.co/spaces/huggingface-projects/auto-retrain/blob/main/config.json" rel="nofollow"><code>config.json</code> file</a> to use the dataset and model of you choice:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-json "><!-- HTML_TAG_START --><span class="hljs-punctuation">{</span> | |
| <span class="hljs-attr">"target_namespace"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"the namespace where the trained model should end up"</span><span class="hljs-punctuation">,</span> | |
| <span class="hljs-attr">"input_dataset"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"the dataset on which the model will be trained"</span><span class="hljs-punctuation">,</span> | |
| <span class="hljs-attr">"input_model"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"the base model to re-train"</span><span class="hljs-punctuation">,</span> | |
| <span class="hljs-attr">"autotrain_project_prefix"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"A prefix for the AutoTrain project"</span> | |
| <span class="hljs-punctuation">}</span><!-- HTML_TAG_END --></pre></div> <h2 class="relative group"><a id="configure-your-webhook-to-send-events-to-your-space" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#configure-your-webhook-to-send-events-to-your-space"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Configure your Webhook to send events to your Space</span></h2> <p data-svelte-h="svelte-14g7hz7">Last but not least, you’ll need to configure your webhook to send POST requests to your Space.</p> <p data-svelte-h="svelte-51uzae">Let’s first grab our Space’s “direct URL” from the contextual menu. Click on “Embed this Space” and copy the “Direct URL”.</p> <p data-svelte-h="svelte-1gxz68g"><img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/hub/webhooks-guides/002-auto-retrain/duplicate-space.png" alt="embed this Space"></p> <p data-svelte-h="svelte-h01i34"><img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/hub/webhooks-guides/002-auto-retrain/direct-url.png" alt="direct URL"></p> <p data-svelte-h="svelte-1w4uowt">Update your Webhook to send requests to that URL:</p> <p data-svelte-h="svelte-lpgybe"><img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/hub/webhooks-guides/002-auto-retrain/update-webhook.png" alt="webhook settings"></p> <p data-svelte-h="svelte-9f0m9n">And that’s it! Now every commit to the input dataset will trigger a fine-tuning of ResNet-50 with AutoTrain 🎉</p> <a class="!text-gray-400 !no-underline text-sm flex items-center not-prose mt-4" href="https://github.com/huggingface/hub-docs/blob/main/docs/hub/webhooks-guide-auto-retrain.md" target="_blank"><svg class="mr-1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M31,16l-7,7l-1.41-1.41L28.17,16l-5.58-5.59L24,9l7,7z"></path><path d="M1,16l7-7l1.41,1.41L3.83,16l5.58,5.59L8,23l-7-7z"></path><path d="M12.419,25.484L17.639,6.552l1.932,0.518L14.351,26.002z"></path></svg> <span data-svelte-h="svelte-zjs2n5"><span class="underline">Update</span> on GitHub</span></a> <p></p> | |
| <script> | |
| { | |
| __sveltekit_1bollga = { | |
| assets: "/docs/hub/pr_2521/en", | |
| base: "/docs/hub/pr_2521/en", | |
| env: {} | |
| }; | |
| const element = document.currentScript.parentElement; | |
| const data = [null,null]; | |
| Promise.all([ | |
| import("/docs/hub/pr_2521/en/_app/immutable/entry/start.d19e5ca7.js"), | |
| import("/docs/hub/pr_2521/en/_app/immutable/entry/app.98ab229a.js") | |
| ]).then(([kit, app]) => { | |
| kit.start(app, element, { | |
| node_ids: [0, 255], | |
| data, | |
| form: null, | |
| error: null | |
| }); | |
| }); | |
| } | |
| </script> | |
Xet Storage Details
- Size:
- 32.4 kB
- Xet hash:
- 6e67e6341a58a24ad9ace024ff905984bef26be3dba8bc5bb16df4ebc132ac74
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.