import{s as Ue,n as _e,o as Le}from"../chunks/scheduler.9cda857a.js";import{S as xe,i as Ze,e as i,s,c as o,h as ve,a as p,d as l,b as a,f as Ce,g as m,j as r,k as ue,l as ke,m as n,n as c,t as u,o as f,p as g}from"../chunks/index.003612b0.js";import{C as He}from"../chunks/CopyLLMTxtMenu.44d08a39.js";import{C as fe}from"../chunks/CodeBlock.a90632f1.js";import{H as X,E as We}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.e5cfda8c.js";function Be(ge){let h,R,P,z,b,F,T,G,M,he='<a href="https://github.com/mlfoundations/open_clip" rel="nofollow">OpenCLIP</a> is an open-source implementation of OpenAI’s CLIP.',Y,J,N,y,de='You can find OpenCLIP models by filtering at the left of the <a href="https://huggingface.co/models?library=open_clip&amp;sort=trending" rel="nofollow">models page</a>.',A,$,we='OpenCLIP models hosted on the Hub have a model card with useful information about the models. Thanks to OpenCLIP Hugging Face Hub integration, you can load OpenCLIP models with a few lines of code. You can also deploy these models using <a href="https://huggingface.co/inference-endpoints" rel="nofollow">Inference Endpoints</a>.',S,I,E,j,be=`To get started, you can follow the <a href="https://github.com/mlfoundations/open_clip#usage" rel="nofollow">OpenCLIP installation guide</a>.
You can also use the following one-line install through pip:`,O,C,q,U,D,_,Te="All OpenCLIP models can easily be loaded from the Hub:",K,L,ee,x,Me='Once loaded, you can encode the image and text to do <a href="https://huggingface.co/tasks/zero-shot-image-classification" rel="nofollow">zero-shot image classification</a>:',te,Z,le,v,Je="It outputs the probability of each possible class:",ne,k,se,H,ye="If you want to load a specific OpenCLIP model, you can click <code>Use in OpenCLIP</code> in the model card and you will be given a working snippet!",ae,d,$e='<img class="block dark:hidden" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/hub/openclip_repo_light.png"/> <img class="hidden dark:block" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/hub/openclip_repo.png"/>',ie,w,Ie='<img class="block dark:hidden" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/hub/openclip_snippet_light.png"/> <img class="hidden dark:block" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/hub/openclip_snippet.png"/>',pe,W,oe,B,je='<li>OpenCLIP <a href="https://github.com/mlfoundations/open_clip" rel="nofollow">repository</a></li> <li>OpenCLIP <a href="https://github.com/mlfoundations/open_clip/tree/main/docs" rel="nofollow">docs</a></li> <li>OpenCLIP <a href="https://huggingface.co/models?library=open_clip&amp;sort=trending" rel="nofollow">models in the Hub</a></li>',me,V,re,Q,ce;return b=new He({props:{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"}}),T=new X({props:{title:"Using OpenCLIP at Hugging Face",local:"using-openclip-at-hugging-face",headingTag:"h1"}}),J=new X({props:{title:"Exploring OpenCLIP on the Hub",local:"exploring-openclip-on-the-hub",headingTag:"h2"}}),I=new X({props:{title:"Installation",local:"installation",headingTag:"h2"}}),C=new fe({props:{code:"JTI0JTIwcGlwJTIwaW5zdGFsbCUyMG9wZW5fY2xpcF90b3JjaA==",highlighted:'$ pip <span class="hljs-keyword">install</span> open_clip_torch',wrap:!1}}),U=new X({props:{title:"Using existing models",local:"using-existing-models",headingTag:"h2"}}),L=new fe({props:{code:"aW1wb3J0JTIwb3Blbl9jbGlwJTBBJTBBbW9kZWwlMkMlMjBwcmVwcm9jZXNzJTIwJTNEJTIwb3Blbl9jbGlwLmNyZWF0ZV9tb2RlbF9mcm9tX3ByZXRyYWluZWQoJ2hmLWh1YiUzQWxhaW9uJTJGQ0xJUC1WaVQtZy0xNC1sYWlvbjJCLXMxMkItYjQySycpJTBBdG9rZW5pemVyJTIwJTNEJTIwb3Blbl9jbGlwLmdldF90b2tlbml6ZXIoJ2hmLWh1YiUzQWxhaW9uJTJGQ0xJUC1WaVQtZy0xNC1sYWlvbjJCLXMxMkItYjQySycp",highlighted:`<span class="hljs-keyword">import</span> open_clip

model, preprocess = open_clip.create_model_from_pretrained(<span class="hljs-string">&#x27;hf-hub:laion/CLIP-ViT-g-14-laion2B-s12B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yQyUyMGtlZXBkaW0lM0RUcnVlKSUwQSUwQSUyMCUyMCUyMCUyMHRleHRfcHJvYnMlMjAlM0QlMjAoMTAwLjAlMjAqJTIwaW1hZ2VfZmVhdHVyZXMlMjAlNDAlMjB0ZXh0X2ZlYXR1cmVzLlQpLnNvZnRtYXgoZGltJTNELTEpJTBBJTBBcHJpbnQoJTIyTGFiZWwlMjBwcm9icyUzQSUyMiUyQyUyMHRleHRfcHJvYnMpJTIw",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> PIL <span class="hljs-keyword">import</span> Image
<span class="hljs-keyword">import</span> requests

url = <span class="hljs-string">&#x27;http://images.cocodataset.org/val2017/000000039769.jpg&#x27;</span>
image = Image.<span class="hljs-built_in">open</span>(requests.get(url, stream=<span class="hljs-literal">True</span>).raw)
image = preprocess(image).unsqueeze(<span class="hljs-number">0</span>)
text = tokenizer([<span class="hljs-string">&quot;a diagram&quot;</span>, <span class="hljs-string">&quot;a dog&quot;</span>, <span class="hljs-string">&quot;a cat&quot;</span>])

<span class="hljs-keyword">with</span> torch.no_grad(), torch.cuda.amp.autocast():
    image_features = model.encode_image(image)
    text_features = model.encode_text(text)
    image_features /= image_features.norm(dim=-<span class="hljs-number">1</span>, keepdim=<span class="hljs-literal">True</span>)
    text_features /= text_features.norm(dim=-<span class="hljs-number">1</span>, keepdim=<span class="hljs-literal">True</span>)

    text_probs = (<span class="hljs-number">100.0</span> * image_features @ text_features.T).softmax(dim=-<span class="hljs-number">1</span>)

<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Label probs:&quot;</span>, text_probs) `,wrap:!1}}),k=new fe({props:{code:"TGFiZWwlMjBwcm9icyUzQSUyMHRlbnNvciglNUIlNUIwLjAwMjAlMkMlMjAwLjAwMzQlMkMlMjAwLjk5NDYlNUQlNUQp",highlighted:"Label probs: tensor([[0.0020, 0.0034, 0.9946]])",wrap:!1}}),W=new X({props:{title:"Additional resources",local:"additional-resources",headingTag:"h2"}}),V=new We({props:{source:"https://github.com/huggingface/hub-docs/blob/main/docs/hub/open_clip.md"}}),{c(){h=i("meta"),R=s(),P=i("p"),z=s(),o(b.$$.fragment),F=s(),o(T.$$.fragment),G=s(),M=i("p"),M.innerHTML=he,Y=s(),o(J.$$.fragment),N=s(),y=i("p"),y.innerHTML=de,A=s(),$=i("p"),$.innerHTML=we,S=s(),o(I.$$.fragment),E=s(),j=i("p"),j.innerHTML=be,O=s(),o(C.$$.fragment),q=s(),o(U.$$.fragment),D=s(),_=i("p"),_.textContent=Te,K=s(),o(L.$$.fragment),ee=s(),x=i("p"),x.innerHTML=Me,te=s(),o(Z.$$.fragment),le=s(),v=i("p"),v.textContent=Je,ne=s(),o(k.$$.fragment),se=s(),H=i("p"),H.innerHTML=ye,ae=s(),d=i("div"),d.innerHTML=$e,ie=s(),w=i("div"),w.innerHTML=Ie,pe=s(),o(W.$$.fragment),oe=s(),B=i("ul"),B.innerHTML=je,me=s(),o(V.$$.fragment),re=s(),Q=i("p"),this.h()},l(e){const t=ve("svelte-u9bgzb",document.head);h=p(t,"META",{name:!0,content:!0}),t.forEach(l),R=a(e),P=p(e,"P",{}),Ce(P).forEach(l),z=a(e),m(b.$$.fragment,e),F=a(e),m(T.$$.fragment,e),G=a(e),M=p(e,"P",{"data-svelte-h":!0}),r(M)!=="svelte-1gvreai"&&(M.innerHTML=he),Y=a(e),m(J.$$.fragment,e),N=a(e),y=p(e,"P",{"data-svelte-h":!0}),r(y)!=="svelte-5du578"&&(y.innerHTML=de),A=a(e),$=p(e,"P",{"data-svelte-h":!0}),r($)!=="svelte-1s055da"&&($.innerHTML=we),S=a(e),m(I.$$.fragment,e),E=a(e),j=p(e,"P",{"data-svelte-h":!0}),r(j)!=="svelte-8hku74"&&(j.innerHTML=be),O=a(e),m(C.$$.fragment,e),q=a(e),m(U.$$.fragment,e),D=a(e),_=p(e,"P",{"data-svelte-h":!0}),r(_)!=="svelte-c4y4yi"&&(_.textContent=Te),K=a(e),m(L.$$.fragment,e),ee=a(e),x=p(e,"P",{"data-svelte-h":!0}),r(x)!=="svelte-bfy175"&&(x.innerHTML=Me),te=a(e),m(Z.$$.fragment,e),le=a(e),v=p(e,"P",{"data-svelte-h":!0}),r(v)!=="svelte-162gbom"&&(v.textContent=Je),ne=a(e),m(k.$$.fragment,e),se=a(e),H=p(e,"P",{"data-svelte-h":!0}),r(H)!=="svelte-1vnus6m"&&(H.innerHTML=ye),ae=a(e),d=p(e,"DIV",{class:!0,"data-svelte-h":!0}),r(d)!=="svelte-qq7za1"&&(d.innerHTML=$e),ie=a(e),w=p(e,"DIV",{class:!0,"data-svelte-h":!0}),r(w)!=="svelte-1btvat9"&&(w.innerHTML=Ie),pe=a(e),m(W.$$.fragment,e),oe=a(e),B=p(e,"UL",{"data-svelte-h":!0}),r(B)!=="svelte-hsv1q9"&&(B.innerHTML=je),me=a(e),m(V.$$.fragment,e),re=a(e),Q=p(e,"P",{}),Ce(Q).forEach(l),this.h()},h(){ue(h,"name","hf:doc:metadata"),ue(h,"content",Ve),ue(d,"class","flex justify-center"),ue(w,"class","flex justify-center")},m(e,t){ke(document.head,h),n(e,R,t),n(e,P,t),n(e,z,t),c(b,e,t),n(e,F,t),c(T,e,t),n(e,G,t),n(e,M,t),n(e,Y,t),c(J,e,t),n(e,N,t),n(e,y,t),n(e,A,t),n(e,$,t),n(e,S,t),c(I,e,t),n(e,E,t),n(e,j,t),n(e,O,t),c(C,e,t),n(e,q,t),c(U,e,t),n(e,D,t),n(e,_,t),n(e,K,t),c(L,e,t),n(e,ee,t),n(e,x,t),n(e,te,t),c(Z,e,t),n(e,le,t),n(e,v,t),n(e,ne,t),c(k,e,t),n(e,se,t),n(e,H,t),n(e,ae,t),n(e,d,t),n(e,ie,t),n(e,w,t),n(e,pe,t),c(W,e,t),n(e,oe,t),n(e,B,t),n(e,me,t),c(V,e,t),n(e,re,t),n(e,Q,t),ce=!0},p:_e,i(e){ce||(u(b.$$.fragment,e),u(T.$$.fragment,e),u(J.$$.fragment,e),u(I.$$.fragment,e),u(C.$$.fragment,e),u(U.$$.fragment,e),u(L.$$.fragment,e),u(Z.$$.fragment,e),u(k.$$.fragment,e),u(W.$$.fragment,e),u(V.$$.fragment,e),ce=!0)},o(e){f(b.$$.fragment,e),f(T.$$.fragment,e),f(J.$$.fragment,e),f(I.$$.fragment,e),f(C.$$.fragment,e),f(U.$$.fragment,e),f(L.$$.fragment,e),f(Z.$$.fragment,e),f(k.$$.fragment,e),f(W.$$.fragment,e),f(V.$$.fragment,e),ce=!1},d(e){e&&(l(R),l(P),l(z),l(F),l(G),l(M),l(Y),l(N),l(y),l(A),l($),l(S),l(E),l(j),l(O),l(q),l(D),l(_),l(K),l(ee),l(x),l(te),l(le),l(v),l(ne),l(se),l(H),l(ae),l(d),l(ie),l(w),l(pe),l(oe),l(B),l(me),l(re),l(Q)),l(h),g(b,e),g(T,e),g(J,e),g(I,e),g(C,e),g(U,e),g(L,e),g(Z,e),g(k,e),g(W,e),g(V,e)}}}const Ve='{"title":"Using OpenCLIP at Hugging Face","local":"using-openclip-at-hugging-face","sections":[{"title":"Exploring OpenCLIP on the Hub","local":"exploring-openclip-on-the-hub","sections":[],"depth":2},{"title":"Installation","local":"installation","sections":[],"depth":2},{"title":"Using existing models","local":"using-existing-models","sections":[],"depth":2},{"title":"Additional resources","local":"additional-resources","sections":[],"depth":2}],"depth":1}';function Pe(ge){return Le(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Ge extends xe{constructor(h){super(),Ze(this,h,Pe,Be,Ue,{})}}export{Ge as component};
