Buckets:
| <meta charset="utf-8" /><meta name="hf:doc:metadata" content="{"title":"Integrieren Sie jedes ML-Framework mit dem Hub","local":"integrieren-sie-jedes-ml-framework-mit-dem-hub","sections":[{"title":"Ein flexibler Ansatz: Helfer","local":"ein-flexibler-ansatz-helfer","sections":[{"title":"from_pretrained","local":"frompretrained","sections":[],"depth":3},{"title":"push_to_hub","local":"pushtohub","sections":[],"depth":3},{"title":"Einschränkungen","local":"einschränkungen","sections":[],"depth":3}],"depth":2},{"title":"Ein komplexerer Ansatz: Klassenvererbung","local":"ein-komplexerer-ansatz-klassenvererbung","sections":[{"title":"Ein konkretes Beispiel: PyTorch","local":"ein-konkretes-beispiel-pytorch","sections":[{"title":"Wie verwendet man es?","local":"wie-verwendet-man-es","sections":[],"depth":4},{"title":"Implementierung","local":"implementierung","sections":[],"depth":4}],"depth":3}],"depth":2},{"title":"Kurzer Vergleich","local":"kurzer-vergleich","sections":[],"depth":2}],"depth":1}"> | |
| <link href="/docs/huggingface_hub/pr_4113/de/_app/immutable/assets/0.e3b0c442.css" rel="modulepreload"> | |
| <link rel="modulepreload" href="/docs/huggingface_hub/pr_4113/de/_app/immutable/entry/start.78846203.js"> | |
| <link rel="modulepreload" href="/docs/huggingface_hub/pr_4113/de/_app/immutable/chunks/scheduler.de5597d1.js"> | |
| <link rel="modulepreload" href="/docs/huggingface_hub/pr_4113/de/_app/immutable/chunks/singletons.c332abcc.js"> | |
| <link rel="modulepreload" href="/docs/huggingface_hub/pr_4113/de/_app/immutable/chunks/paths.162adda8.js"> | |
| <link rel="modulepreload" href="/docs/huggingface_hub/pr_4113/de/_app/immutable/entry/app.f549f98a.js"> | |
| <link rel="modulepreload" href="/docs/huggingface_hub/pr_4113/de/_app/immutable/chunks/preload-helper.cc57ebc4.js"> | |
| <link rel="modulepreload" href="/docs/huggingface_hub/pr_4113/de/_app/immutable/chunks/index.bf1177c2.js"> | |
| <link rel="modulepreload" href="/docs/huggingface_hub/pr_4113/de/_app/immutable/nodes/0.9ce3ec2a.js"> | |
| <link rel="modulepreload" href="/docs/huggingface_hub/pr_4113/de/_app/immutable/chunks/each.e59479a4.js"> | |
| <link rel="modulepreload" href="/docs/huggingface_hub/pr_4113/de/_app/immutable/nodes/6.2df2356f.js"> | |
| <link rel="modulepreload" href="/docs/huggingface_hub/pr_4113/de/_app/immutable/chunks/MermaidChart.svelte_svelte_type_style_lang.c5d5679c.js"> | |
| <link rel="modulepreload" href="/docs/huggingface_hub/pr_4113/de/_app/immutable/chunks/CodeBlock.4f67b398.js"><!-- HEAD_svelte-u9bgzb_START --><meta name="hf:doc:metadata" content="{"title":"Integrieren Sie jedes ML-Framework mit dem Hub","local":"integrieren-sie-jedes-ml-framework-mit-dem-hub","sections":[{"title":"Ein flexibler Ansatz: Helfer","local":"ein-flexibler-ansatz-helfer","sections":[{"title":"from_pretrained","local":"frompretrained","sections":[],"depth":3},{"title":"push_to_hub","local":"pushtohub","sections":[],"depth":3},{"title":"Einschränkungen","local":"einschränkungen","sections":[],"depth":3}],"depth":2},{"title":"Ein komplexerer Ansatz: Klassenvererbung","local":"ein-komplexerer-ansatz-klassenvererbung","sections":[{"title":"Ein konkretes Beispiel: PyTorch","local":"ein-konkretes-beispiel-pytorch","sections":[{"title":"Wie verwendet man es?","local":"wie-verwendet-man-es","sections":[],"depth":4},{"title":"Implementierung","local":"implementierung","sections":[],"depth":4}],"depth":3}],"depth":2},{"title":"Kurzer Vergleich","local":"kurzer-vergleich","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="integrieren-sie-jedes-ml-framework-mit-dem-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="#integrieren-sie-jedes-ml-framework-mit-dem-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>Integrieren Sie jedes ML-Framework mit dem Hub</span></h1> <p data-svelte-h="svelte-1uv5xno">Der Hugging Face Hub erleichtert das Hosten und Teilen von Modellen mit der Community. Er unterstützt <a href="https://huggingface.co/docs/hub/models-libraries" rel="nofollow">Dutzende von Bibliotheken</a> im Open Source-Ökosystem. Wir arbeiten ständig daran, diese Unterstützung zu erweitern, um kollaboratives Machine Learning voranzutreiben. Die <code>huggingface_hub</code>-Bibliothek spielt eine Schlüsselrolle in diesem Prozess und ermöglicht es jedem Python-Skript, Dateien einfach hochzuladen und zu laden.</p> <p data-svelte-h="svelte-1wuvm32">Es gibt vier Hauptwege, eine Bibliothek mit dem Hub zu integrieren:</p> <ol data-svelte-h="svelte-18hrhsl"><li><strong>Push to Hub</strong>: Implementieren Sie eine Methode, um ein Modell auf den Hub hochzuladen. | |
| Dies beinhaltet das Modellgewicht sowie <a href="https://huggingface.co/docs/huggingface_hub/how-to-model-cards" rel="nofollow">die Modellkarte</a> und alle anderen relevanten Informationen oder Daten, die für den Betrieb des Modells erforderlich sind (zum Beispiel Trainingsprotokolle). Diese Methode wird oft <code>push_to_hub()</code> genannt.</li> <li><strong>Download from Hub</strong>: Implementieren Sie eine Methode, um ein Modell vom Hub zu laden. | |
| Die Methode sollte die Modellkonfiguration/-gewichte herunterladen und das Modell laden. Diese Methode wird oft <code>from_pretrained</code> oder <code>load_from_hub()</code> genannt.</li> <li><strong>Widgets</strong>: Zeigen Sie ein Widget auf der Landing Page Ihrer Modelle auf dem Hub an. | |
| Dies ermöglicht es Benutzern, ein Modell schnell aus dem Browser heraus auszuprobieren.</li></ol> <p data-svelte-h="svelte-18fkufj">In diesem Leitfaden konzentrieren wir uns auf die ersten beiden Themen. Wir werden die beiden Hauptansätze vorstellen, die Sie zur Integration einer Bibliothek verwenden können, mit ihren Vor- und Nachteilen. Am Ende des Leitfadens ist alles zusammengefasst, um Ihnen bei der Auswahl zwischen den beiden zu helfen. Bitte beachten Sie, dass dies nur Richtlinien sind, die Sie an Ihre Anforderungen anpassen können.</p> <p data-svelte-h="svelte-1iwhhmw">Wenn Sie sich für Inferenz und Widgets interessieren, können Sie <a href="https://huggingface.co/docs/hub/models-adding-libraries#set-up-the-inference-api" rel="nofollow">diesem Leitfaden</a> folgen. In beiden Fällen können Sie sich an uns wenden, wenn Sie eine Bibliothek mit dem Hub integrieren und <a href="https://huggingface.co/docs/hub/models-libraries" rel="nofollow">in unserer Dokumentation</a> aufgeführt haben möchten.</p> <h2 class="relative group"><a id="ein-flexibler-ansatz-helfer" 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="#ein-flexibler-ansatz-helfer"><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>Ein flexibler Ansatz: Helfer</span></h2> <p data-svelte-h="svelte-d1foe8">Der erste Ansatz zur Integration einer Bibliothek in den Hub besteht tatsächlich darin, die <code>push_to_hub</code> und <code>from_pretrained</code> Methoden selbst zu implementieren. Dies gibt Ihnen volle Flexibilität hinsichtlich der Dateien, die Sie hoch-/herunterladen möchten, und wie Sie Eingaben, die speziell für Ihr Framework sind, behandeln. Sie können sich die beiden Leitfäden <a href="./upload">Dateien hochladen</a> und <a href="./download">Dateien herunterladen</a> ansehen, um mehr darüber zu erfahren, wie dies funktioniert. Dies ist zum Beispiel die Art und Weise, wie die FastAI-Integration implementiert ist (siehe <code>push_to_hub_fastai()</code> | |
| und <code>from_pretrained_fastai()</code>).</p> <p data-svelte-h="svelte-1v7bx6l">Die Implementierung kann zwischen den Bibliotheken variieren, aber der Workflow ist oft ähnlich.</p> <h3 class="relative group"><a id="frompretrained" 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="#frompretrained"><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>from_pretrained</span></h3> <p data-svelte-h="svelte-1m7ctz2">So sieht eine <code>from_pretrained</code> Methode normalerweise aus:</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=""><!-- HTML_TAG_START --><span class="hljs-keyword">def</span> <span class="hljs-title function_">from_pretrained</span>(<span class="hljs-params">model_id: <span class="hljs-built_in">str</span></span>) -> MyModelClass: | |
| <span class="hljs-comment"># Modell vom Hub herunterladen</span> | |
| cached_model = hf_hub_download( | |
| repo_id=repo_id, | |
| filename=<span class="hljs-string">"model.pkl"</span>, | |
| library_name=<span class="hljs-string">"fastai"</span>, | |
| library_version=get_fastai_version(), | |
| ) | |
| <span class="hljs-comment"># Modell laden</span> | |
| <span class="hljs-keyword">return</span> load_model(cached_model)<!-- HTML_TAG_END --></pre></div> <h3 class="relative group"><a id="pushtohub" 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="#pushtohub"><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>push_to_hub</span></h3> <p data-svelte-h="svelte-12y5xmc">Die <code>push_to_hub</code> Methode erfordert oft etwas mehr Komplexität, um die Repo-Erstellung, die Generierung der Modellkarte und das Speichern von Gewichten zu behandeln. Ein üblicher Ansatz besteht darin, all diese Dateien in einem temporären Ordner zu speichern, ihn hochzuladen und dann zu löschen.</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=""><!-- HTML_TAG_START --><span class="hljs-keyword">def</span> <span class="hljs-title function_">push_to_hub</span>(<span class="hljs-params">model: MyModelClass, repo_name: <span class="hljs-built_in">str</span></span>) -> <span class="hljs-literal">None</span>: | |
| api = HfApi() | |
| <span class="hljs-comment"># Repo erstellen, wenn noch nicht vorhanden und die zugehörige repo_id erhalten</span> | |
| repo_id = api.create_repo(repo_name, exist_ok=<span class="hljs-literal">True</span>) | |
| <span class="hljs-comment"># Modell in temporärem Ordner speichern und in einem enzigen Commit pushen</span> | |
| <span class="hljs-keyword">with</span> TemporaryDirectory() <span class="hljs-keyword">as</span> tmpdir: | |
| tmpdir = Path(tmpdir) | |
| <span class="hljs-comment"># Gewichte speichern</span> | |
| save_model(model, tmpdir / <span class="hljs-string">"model.safetensors"</span>) | |
| <span class="hljs-comment"># Modellkarte generieren</span> | |
| card = generate_model_card(model) | |
| (tmpdir / <span class="hljs-string">"README.md"</span>).write_text(card) | |
| <span class="hljs-comment"># Logs speichern</span> | |
| <span class="hljs-comment"># Diagramme speichern</span> | |
| <span class="hljs-comment"># Evaluationsmetriken speichern</span> | |
| <span class="hljs-comment"># ...</span> | |
| <span class="hljs-comment"># Auf den Hub pushen</span> | |
| <span class="hljs-keyword">return</span> api.upload_folder(repo_id=repo_id, folder_path=tmpdir)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-calp58">Dies ist natürlich nur ein Beispiel. Wenn Sie an komplexeren Manipulationen interessiert sind (entfernen von entfernten Dateien, hochladen von Gewichten on-the-fly, lokales Speichern von Gewichten, usw.), beachten Sie bitte den <a href="./upload">Dateien hochladen</a> Leitfaden.</p> <h3 class="relative group"><a id="einschränkungen" 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="#einschränkungen"><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>Einschränkungen</span></h3> <p data-svelte-h="svelte-bl4t77">Obwohl dieser Ansatz flexibel ist, hat er einige Nachteile, insbesondere in Bezug auf die Wartung. Hugging Face-Benutzer sind oft an zusätzliche Funktionen gewöhnt, wenn sie mit <code>huggingface_hub</code> arbeiten. Zum Beispiel ist es beim Laden von Dateien aus dem Hub üblich, Parameter wie folgt anzubieten:</p> <ul data-svelte-h="svelte-16nbf7a"><li><code>token</code>: zum Herunterladen aus einem privaten Repository</li> <li><code>revision</code>: zum Herunterladen von einem spezifischen Branch</li> <li><code>cache_dir</code>: um Dateien in einem spezifischen Verzeichnis zu cachen</li> <li><code>force_download</code>/<code>local_files_only</code>: um den Cache wieder zu verwenden oder nicht</li> <li><code>api_endpoint</code>/<code>proxies</code>: HTTP-Session konfigurieren</li></ul> <p data-svelte-h="svelte-1lnppi9">Beim Pushen von Modellen werden ähnliche Parameter unterstützt:</p> <ul data-svelte-h="svelte-q0zixo"><li><code>commit_message</code>: benutzerdefinierte Commit-Nachricht</li> <li><code>private</code>: ein privates Repository erstellen, falls nicht vorhanden</li> <li><code>create_pr</code>: erstellen Sie einen PR anstatt auf <code>main</code> zu pushen</li> <li><code>branch</code>: auf einen Branch pushen anstatt auf den <code>main</code> Branch</li> <li><code>allow_patterns</code>/<code>ignore_patterns</code>: filtern, welche Dateien hochgeladen werden sollen</li> <li><code>token</code></li> <li><code>api_endpoint</code></li> <li>…</li></ul> <p data-svelte-h="svelte-w69vgv">Alle diese Parameter können den zuvor gesehenen Implementierungen hinzugefügt und an die <code>huggingface_hub</code>-Methoden übergeben werden. | |
| Wenn sich jedoch ein Parameter ändert oder eine neue Funktion hinzugefügt wird, müssen Sie Ihr Paket aktualisieren. | |
| Die Unterstützung dieser Parameter bedeutet auch mehr Dokumentation, die Sie auf Ihrer Seite pflegen müssen. | |
| Um zu sehen, wie man diese Einschränkungen mildert, springen wir zu unserem nächsten Abschnitt <strong>Klassenvererbung</strong>.</p> <h2 class="relative group"><a id="ein-komplexerer-ansatz-klassenvererbung" 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="#ein-komplexerer-ansatz-klassenvererbung"><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>Ein komplexerer Ansatz: Klassenvererbung</span></h2> <p data-svelte-h="svelte-1kz5ea2">Wie wir oben gesehen haben, gibt es zwei Hauptmethoden, um Ihre Bibliothek mit dem Hub zu integrieren: Dateien hochladen (<code>push_to_hub</code>) und Dateien herunterladen (<code>from_pretrained</code>). Sie können diese Methoden selbst implementieren, aber das hat seine Tücken. Um dies zu bewältigen, bietet <code>huggingface_hub</code> ein Werkzeug an, das Klassenvererbung verwendet. Schauen wir uns an, wie es funktioniert!</p> <p data-svelte-h="svelte-d3dywx">In vielen Fällen implementiert eine Bibliothek ihr Modell bereits mit einer Python-Klasse. Die Klasse enthält die Eigenschaften des Modells und Methoden zum Laden, Ausführen, Trainieren und Evaluieren. Unser Ansatz besteht darin, diese Klasse zu erweitern, um Upload- und Download-Funktionen mit Mixins hinzuzufügen. Ein <a href="https://stackoverflow.com/a/547714" rel="nofollow">Mixin</a> ist eine Klasse, die dazu bestimmt ist, eine vorhandene Klasse mit einem Satz spezifischer Funktionen durch Mehrfachvererbung zu erweitern. <code>huggingface_hub</code> bietet sein eigenes Mixin, das <code>ModelHubMixin</code>. Der Schlüssel hier ist zu verstehen, wie es funktioniert und wie man es anpassen kann.</p> <p data-svelte-h="svelte-18mc3qm">Die Klasse [ModelHubMixin] implementiert 3 <em>öffentliche</em> Methoden (<code>push_to_hub</code>, <code>save_pretrained</code> und <code>from_pretrained</code>). Dies sind die Methoden, die Ihre Benutzer aufrufen werden, um Modelle mit Ihrer Bibliothek zu laden/speichern. <code>ModelHubMixin</code> definiert auch 2 private Methoden (<code>_save_pretrained</code> und <code>_from_pretrained</code>). Diese müssen Sie implementieren. Um Ihre Bibliothek zu integrieren, sollten Sie:</p> <ol data-svelte-h="svelte-alyawy"><li>Lassen Sie Ihre Modell-Klasse von <code>ModelHubMixin</code> erben.</li> <li>Implementieren Sie die privaten Methoden: | |
| <ul><li><code>_save_pretrained()</code>: Methode, die als Eingabe einen Pfad zu einem Verzeichnis nimmt und das Modell dort speichert. Sie müssen die gesamte Logik zum Speichern Ihres Modells in dieser Methode schreiben: Modellkarte, Modellgewichte, Konfigurationsdateien, Trainingsprotokolle und Diagramme. Alle relevanten Informationen für dieses Modell müssen von dieser Methode behandelt werden. | |
| <a href="https://huggingface.co/docs/hub/model-cards" rel="nofollow">Model Cards</a> sind besonders wichtig, um Ihr Modell zu beschreiben. Weitere Details finden Sie in <a href="./model-cards">unserem Implementierungsleitfaden</a>.</li> <li>[~ModelHubMixin._from_pretrained]: <strong>Klassenmethode</strong>, die als Eingabe eine <code>model_id</code> nimmt und ein instanziiertes Modell zurückgibt. Die Methode muss die relevanten Dateien herunterladen und laden.</li></ul></li> <li>Sie sind fertig!</li></ol> <p data-svelte-h="svelte-116c8ma">Der Vorteil der Verwendung von <code>ModelHubMixin</code> besteht darin, dass Sie, sobald Sie sich um die Serialisierung/das Laden der Dateien gekümmert haben, bereit sind los zu legen. Sie müssen sich keine Gedanken über Dinge wie Repository-Erstellung, Commits, PRs oder Revisionen machen. All dies wird von dem Mixin gehandhabt und steht Ihren Benutzern zur Verfügung. Das Mixin stellt auch sicher, dass öffentliche Methoden gut dokumentiert und typisiert sind.</p> <h3 class="relative group"><a id="ein-konkretes-beispiel-pytorch" 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="#ein-konkretes-beispiel-pytorch"><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>Ein konkretes Beispiel: PyTorch</span></h3> <p data-svelte-h="svelte-1macysc">Ein gutes Beispiel für das, was wir oben gesehen haben, ist <code>PyTorchModelHubMixin</code>, unsere Integration für das PyTorch-Framework. Dies ist eine einsatzbereite Integration.</p> <h4 class="relative group"><a id="wie-verwendet-man-es" 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="#wie-verwendet-man-es"><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>Wie verwendet man es?</span></h4> <p data-svelte-h="svelte-1el4w9u">Hier ist, wie jeder Benutzer ein PyTorch-Modell vom/auf den Hub laden/speichern kann:</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=""><!-- HTML_TAG_START --><span class="hljs-meta">>>> </span><span class="hljs-keyword">import</span> torch | |
| <span class="hljs-meta">>>> </span><span class="hljs-keyword">import</span> torch.nn <span class="hljs-keyword">as</span> nn | |
| <span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> PyTorchModelHubMixin | |
| <span class="hljs-comment"># 1. Definieren Sie Ihr Pytorch-Modell genau so, wie Sie es gewohnt sind</span> | |
| <span class="hljs-meta">>>> </span><span class="hljs-keyword">class</span> <span class="hljs-title class_">MyModel</span>(nn.Module, PyTorchModelHubMixin): <span class="hljs-comment"># Mehrfachvererbung</span> | |
| <span class="hljs-meta">... </span> <span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self</span>): | |
| <span class="hljs-meta">... </span> <span class="hljs-built_in">super</span>().__init__() | |
| <span class="hljs-meta">... </span> self.param = nn.Parameter(torch.rand(<span class="hljs-number">3</span>, <span class="hljs-number">4</span>)) | |
| <span class="hljs-meta">... </span> self.linear = nn.Linear(<span class="hljs-number">4</span>, <span class="hljs-number">5</span>) | |
| <span class="hljs-meta">... </span> <span class="hljs-keyword">def</span> <span class="hljs-title function_">forward</span>(<span class="hljs-params">self, x</span>): | |
| <span class="hljs-meta">... </span> <span class="hljs-keyword">return</span> self.linear(x + self.param) | |
| <span class="hljs-meta">>>> </span>model = MyModel() | |
| <span class="hljs-comment"># 2. (optional) Modell in lokales Verzeichnis speichern</span> | |
| <span class="hljs-meta">>>> </span>model.save_pretrained(<span class="hljs-string">"path/to/my-awesome-model"</span>) | |
| <span class="hljs-comment"># 3. Modellgewichte an den Hub übertragen</span> | |
| <span class="hljs-meta">>>> </span>model.push_to_hub(<span class="hljs-string">"my-awesome-model"</span>) | |
| <span class="hljs-comment"># 4. Modell vom Hub initialisieren</span> | |
| <span class="hljs-meta">>>> </span>model = MyModel.from_pretrained(<span class="hljs-string">"username/my-awesome-model"</span>)<!-- HTML_TAG_END --></pre></div> <h4 class="relative group"><a id="implementierung" 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="#implementierung"><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>Implementierung</span></h4> <p data-svelte-h="svelte-oavmpb">Die Implementierung ist tatsächlich sehr einfach, und die vollständige Implementierung finden Sie <a href="https://github.com/huggingface/huggingface_hub/blob/main/src/huggingface_hub/hub_mixin.py" rel="nofollow">hier</a>.</p> <ol data-svelte-h="svelte-zz071o"><li>Zuerst, erben Ihrer Klasse von <code>ModelHubMixin</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=""><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> ModelHubMixin | |
| <span class="hljs-keyword">class</span> <span class="hljs-title class_">PyTorchModelHubMixin</span>(<span class="hljs-title class_ inherited__">ModelHubMixin</span>): | |
| (...)<!-- HTML_TAG_END --></pre></div> <ol start="2" data-svelte-h="svelte-8qqg6p"><li>Implementieren der <code>_save_pretrained</code> Methode:</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=""><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> ModelCard, ModelCardData | |
| <span class="hljs-keyword">class</span> <span class="hljs-title class_">PyTorchModelHubMixin</span>(<span class="hljs-title class_ inherited__">ModelHubMixin</span>): | |
| (...) | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">_save_pretrained</span>(<span class="hljs-params">self, save_directory: Path</span>): | |
| <span class="hljs-string">"""Generiere Modellkarte und speichere Gewichte von einem Pytorch-Modell in einem lokalen Verzeichnis."""</span> | |
| model_card = ModelCard.from_template( | |
| card_data=ModelCardData( | |
| license=<span class="hljs-string">'mit'</span>, | |
| library_name=<span class="hljs-string">"pytorch"</span>, | |
| ... | |
| ), | |
| model_summary=..., | |
| model_type=..., | |
| ... | |
| ) | |
| (save_directory / <span class="hljs-string">"README.md"</span>).write_text(<span class="hljs-built_in">str</span>(model)) | |
| torch.save(obj=self.module.state_dict(), f=save_directory / <span class="hljs-string">"pytorch_model.bin"</span>)<!-- HTML_TAG_END --></pre></div> <ol start="3" data-svelte-h="svelte-1tllved"><li>Implementieren der <code>_from_pretrained</code> Methode:</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=""><!-- HTML_TAG_START --><span class="hljs-keyword">class</span> <span class="hljs-title class_">PyTorchModelHubMixin</span>(<span class="hljs-title class_ inherited__">ModelHubMixin</span>): | |
| (...) | |
| <span class="hljs-meta"> @classmethod </span><span class="hljs-comment"># Muss eine Klassenmethode sein!</span> | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">_from_pretrained</span>(<span class="hljs-params"> | |
| cls, | |
| *, | |
| model_id: <span class="hljs-built_in">str</span>, | |
| revision: <span class="hljs-built_in">str</span>, | |
| cache_dir: <span class="hljs-built_in">str</span>, | |
| force_download: <span class="hljs-built_in">bool</span>, | |
| proxies: <span class="hljs-type">Optional</span>[<span class="hljs-built_in">dict</span>], | |
| local_files_only: <span class="hljs-built_in">bool</span>, | |
| token: <span class="hljs-type">Union</span>[<span class="hljs-built_in">str</span>, <span class="hljs-built_in">bool</span>, <span class="hljs-literal">None</span>], | |
| map_location: <span class="hljs-built_in">str</span> = <span class="hljs-string">"cpu"</span>, <span class="hljs-comment"># zusätzliches Argument</span> | |
| strict: <span class="hljs-built_in">bool</span> = <span class="hljs-literal">False</span>, <span class="hljs-comment"># zusätzliches Argument</span> | |
| **model_kwargs, | |
| </span>): | |
| <span class="hljs-string">"""Load Pytorch pretrained weights and return the loaded model."""</span> | |
| <span class="hljs-keyword">if</span> os.path.isdir(model_id): <span class="hljs-comment"># Kann entweder ein lokales Verzeichnis sein</span> | |
| <span class="hljs-built_in">print</span>(<span class="hljs-string">"Loading weights from local directory"</span>) | |
| model_file = os.path.join(model_id, <span class="hljs-string">"pytorch_model.bin"</span>) | |
| <span class="hljs-keyword">else</span>: <span class="hljs-comment"># Oder ein Modell am Hub</span> | |
| model_file = hf_hub_download( <span class="hljs-comment"># Herunterladen vom Hub, gleiche Eingabeargumente</span> | |
| repo_id=model_id, | |
| filename=<span class="hljs-string">"pytorch_model.bin"</span>, | |
| revision=revision, | |
| cache_dir=cache_dir, | |
| force_download=force_download, | |
| token=token, | |
| local_files_only=local_files_only, | |
| ) | |
| <span class="hljs-comment"># Modell laden und zurückgeben - benutzerdefinierte Logik je nach Ihrem Framework</span> | |
| model = cls(**model_kwargs) | |
| state_dict = torch.load(model_file, map_location=torch.device(map_location)) | |
| model.load_state_dict(state_dict, strict=strict) | |
| model.<span class="hljs-built_in">eval</span>() | |
| <span class="hljs-keyword">return</span> model<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-13k5c2u">Und das war’s! Ihre Bibliothek ermöglicht es Benutzern nun, Dateien vom und zum Hub hoch- und herunterzuladen.</p> <h2 class="relative group"><a id="kurzer-vergleich" 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="#kurzer-vergleich"><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>Kurzer Vergleich</span></h2> <p data-svelte-h="svelte-1mc7cfl">Lassen Sie uns die beiden Ansätze, die wir gesehen haben, schnell mit ihren Vor- und Nachteilen zusammenfassen. Die untenstehende Tabelle ist nur indikativ. Ihr Framework könnte einige Besonderheiten haben, die Sie berücksichtigen müssen. Dieser Leitfaden soll nur Richtlinien und Ideen geben, wie Sie die Integration handhaben können. Kontaktieren Sie uns in jedem Fall, wenn Sie Fragen haben!</p> <table data-svelte-h="svelte-1or8grg"><thead><tr><th align="center">Integration</th> <th align="center">Mit Helfern</th> <th align="center">Mit <code>ModelHubMixin</code></th></tr></thead> <tbody><tr><td align="center">Benutzererfahrung</td> <td align="center"><code>model = load_from_hub(...)</code><br><code>push_to_hub(model, ...)</code></td> <td align="center"><code>model = MyModel.from_pretrained(...)</code><br><code>model.push_to_hub(...)</code></td></tr> <tr><td align="center">Flexibilität</td> <td align="center">Sehr flexibel.<br>Sie haben die volle Kontrolle über die Implementierung.</td> <td align="center">Weniger flexibel.<br>Ihr Framework muss eine Modellklasse haben.</td></tr> <tr><td align="center">Wartung</td> <td align="center">Mehr Wartung, um Unterstützung für Konfiguration und neue Funktionen hinzuzufügen. Könnte auch das Beheben von Benutzerproblemen erfordern.</td> <td align="center">Weniger Wartung, da die meisten Interaktionen mit dem Hub in <code>huggingface_hub</code> implementiert sind.</td></tr> <tr><td align="center">Dokumentation/Typ-Annotation</td> <td align="center">Manuell zu schreiben.</td> <td align="center">Teilweise durch <code>huggingface_hub</code> behandelt.</td></tr></tbody></table> <a class="!text-gray-400 !no-underline text-sm flex items-center not-prose mt-4" href="https://github.com/huggingface/huggingface_hub/blob/main/docs/source/de/guides/integrations.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_1iqh95q = { | |
| assets: "/docs/huggingface_hub/pr_4113/de", | |
| base: "/docs/huggingface_hub/pr_4113/de", | |
| env: {} | |
| }; | |
| const element = document.currentScript.parentElement; | |
| const data = [null,null]; | |
| Promise.all([ | |
| import("/docs/huggingface_hub/pr_4113/de/_app/immutable/entry/start.78846203.js"), | |
| import("/docs/huggingface_hub/pr_4113/de/_app/immutable/entry/app.f549f98a.js") | |
| ]).then(([kit, app]) => { | |
| kit.start(app, element, { | |
| node_ids: [0, 6], | |
| data, | |
| form: null, | |
| error: null | |
| }); | |
| }); | |
| } | |
| </script> | |
Xet Storage Details
- Size:
- 47.4 kB
- Xet hash:
- a2242bb84dded680b107021b8c4a829753bbe9dba8f7a567c57dcc3d19f7a5ce
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.