import{s as je,n as Ce,o as Ue}from"../chunks/scheduler.d6170356.js";import{S as _e,i as Le,g as i,s,r as m,A as xe,h as p,f as l,c as a,j as Ie,u as r,x as o,k as re,y as Ze,a as n,v as c,d as u,t as h,w as g}from"../chunks/index.fcd4cc08.js";import{C as ce}from"../chunks/CodeBlock.db16bf50.js";import{H as Q,E as ve}from"../chunks/index.c1f68a2b.js";function ke(ue){let f,X,V,R,b,F,T,he='<a href="https://github.com/mlfoundations/open_clip" rel="nofollow">OpenCLIP</a> is an open-source implementation of OpenAI’s CLIP.',z,M,G,J,ge='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>.',Y,y,fe='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>.',N,$,A,I,de=`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:`,S,j,E,C,O,U,we="All OpenCLIP models can easily be loaded from the Hub:",q,_,D,L,be='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>:',K,x,ee,Z,Te="It outputs the probability of each possible class:",te,v,le,k,Me="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!",ne,d,Je='<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"/>',se,w,ye='<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"/>',ae,H,ie,W,$e='<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>',pe,B,oe,P,me;return b=new Q({props:{title:"Using OpenCLIP at Hugging Face",local:"using-openclip-at-hugging-face",headingTag:"h1"}}),M=new Q({props:{title:"Exploring OpenCLIP on the Hub",local:"exploring-openclip-on-the-hub",headingTag:"h2"}}),$=new Q({props:{title:"Installation",local:"installation",headingTag:"h2"}}),j=new ce({props:{code:"JTI0JTIwcGlwJTIwaW5zdGFsbCUyMG9wZW5fY2xpcF90b3JjaA==",highlighted:'$ pip <span class="hljs-keyword">install</span> open_clip_torch',wrap:!1}}),C=new Q({props:{title:"Using existing models",local:"using-existing-models",headingTag:"h2"}}),_=new ce({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-b42K&#x27;</span>)
tokenizer = open_clip.get_tokenizer(<span class="hljs-string">&#x27;hf-hub:laion/CLIP-ViT-g-14-laion2B-s12B-b42K&#x27;</span>)`,wrap:!1}}),x=new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HVyZXMlMjAlNDAlMjB0ZXh0X2ZlYXR1cmVzLlQpLnNvZnRtYXgoZGltJTNELTEpJTBBJTBBcHJpbnQoJTIyTGFiZWwlMjBwcm9icyUzQSUyMiUyQyUyMHRleHRfcHJvYnMpJTIw",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}}),v=new ce({props:{code:"TGFiZWwlMjBwcm9icyUzQSUyMHRlbnNvciglNUIlNUIwLjAwMjAlMkMlMjAwLjAwMzQlMkMlMjAwLjk5NDYlNUQlNUQp",highlighted:"Label probs: tensor([[0.0020, 0.0034, 0.9946]])",wrap:!1}}),H=new Q({props:{title:"Additional resources",local:"additional-resources",headingTag:"h2"}}),B=new ve({props:{source:"https://github.com/huggingface/hub-docs/blob/main/docs/hub/open_clip.md"}}),{c(){f=i("meta"),X=s(),V=i("p"),R=s(),m(b.$$.fragment),F=s(),T=i("p"),T.innerHTML=he,z=s(),m(M.$$.fragment),G=s(),J=i("p"),J.innerHTML=ge,Y=s(),y=i("p"),y.innerHTML=fe,N=s(),m($.$$.fragment),A=s(),I=i("p"),I.innerHTML=de,S=s(),m(j.$$.fragment),E=s(),m(C.$$.fragment),O=s(),U=i("p"),U.textContent=we,q=s(),m(_.$$.fragment),D=s(),L=i("p"),L.innerHTML=be,K=s(),m(x.$$.fragment),ee=s(),Z=i("p"),Z.textContent=Te,te=s(),m(v.$$.fragment),le=s(),k=i("p"),k.innerHTML=Me,ne=s(),d=i("div"),d.innerHTML=Je,se=s(),w=i("div"),w.innerHTML=ye,ae=s(),m(H.$$.fragment),ie=s(),W=i("ul"),W.innerHTML=$e,pe=s(),m(B.$$.fragment),oe=s(),P=i("p"),this.h()},l(e){const t=xe("svelte-u9bgzb",document.head);f=p(t,"META",{name:!0,content:!0}),t.forEach(l),X=a(e),V=p(e,"P",{}),Ie(V).forEach(l),R=a(e),r(b.$$.fragment,e),F=a(e),T=p(e,"P",{"data-svelte-h":!0}),o(T)!=="svelte-1gvreai"&&(T.innerHTML=he),z=a(e),r(M.$$.fragment,e),G=a(e),J=p(e,"P",{"data-svelte-h":!0}),o(J)!=="svelte-5du578"&&(J.innerHTML=ge),Y=a(e),y=p(e,"P",{"data-svelte-h":!0}),o(y)!=="svelte-1s055da"&&(y.innerHTML=fe),N=a(e),r($.$$.fragment,e),A=a(e),I=p(e,"P",{"data-svelte-h":!0}),o(I)!=="svelte-8hku74"&&(I.innerHTML=de),S=a(e),r(j.$$.fragment,e),E=a(e),r(C.$$.fragment,e),O=a(e),U=p(e,"P",{"data-svelte-h":!0}),o(U)!=="svelte-c4y4yi"&&(U.textContent=we),q=a(e),r(_.$$.fragment,e),D=a(e),L=p(e,"P",{"data-svelte-h":!0}),o(L)!=="svelte-bfy175"&&(L.innerHTML=be),K=a(e),r(x.$$.fragment,e),ee=a(e),Z=p(e,"P",{"data-svelte-h":!0}),o(Z)!=="svelte-162gbom"&&(Z.textContent=Te),te=a(e),r(v.$$.fragment,e),le=a(e),k=p(e,"P",{"data-svelte-h":!0}),o(k)!=="svelte-1vnus6m"&&(k.innerHTML=Me),ne=a(e),d=p(e,"DIV",{class:!0,"data-svelte-h":!0}),o(d)!=="svelte-qq7za1"&&(d.innerHTML=Je),se=a(e),w=p(e,"DIV",{class:!0,"data-svelte-h":!0}),o(w)!=="svelte-1btvat9"&&(w.innerHTML=ye),ae=a(e),r(H.$$.fragment,e),ie=a(e),W=p(e,"UL",{"data-svelte-h":!0}),o(W)!=="svelte-hsv1q9"&&(W.innerHTML=$e),pe=a(e),r(B.$$.fragment,e),oe=a(e),P=p(e,"P",{}),Ie(P).forEach(l),this.h()},h(){re(f,"name","hf:doc:metadata"),re(f,"content",He),re(d,"class","flex justify-center"),re(w,"class","flex justify-center")},m(e,t){Ze(document.head,f),n(e,X,t),n(e,V,t),n(e,R,t),c(b,e,t),n(e,F,t),n(e,T,t),n(e,z,t),c(M,e,t),n(e,G,t),n(e,J,t),n(e,Y,t),n(e,y,t),n(e,N,t),c($,e,t),n(e,A,t),n(e,I,t),n(e,S,t),c(j,e,t),n(e,E,t),c(C,e,t),n(e,O,t),n(e,U,t),n(e,q,t),c(_,e,t),n(e,D,t),n(e,L,t),n(e,K,t),c(x,e,t),n(e,ee,t),n(e,Z,t),n(e,te,t),c(v,e,t),n(e,le,t),n(e,k,t),n(e,ne,t),n(e,d,t),n(e,se,t),n(e,w,t),n(e,ae,t),c(H,e,t),n(e,ie,t),n(e,W,t),n(e,pe,t),c(B,e,t),n(e,oe,t),n(e,P,t),me=!0},p:Ce,i(e){me||(u(b.$$.fragment,e),u(M.$$.fragment,e),u($.$$.fragment,e),u(j.$$.fragment,e),u(C.$$.fragment,e),u(_.$$.fragment,e),u(x.$$.fragment,e),u(v.$$.fragment,e),u(H.$$.fragment,e),u(B.$$.fragment,e),me=!0)},o(e){h(b.$$.fragment,e),h(M.$$.fragment,e),h($.$$.fragment,e),h(j.$$.fragment,e),h(C.$$.fragment,e),h(_.$$.fragment,e),h(x.$$.fragment,e),h(v.$$.fragment,e),h(H.$$.fragment,e),h(B.$$.fragment,e),me=!1},d(e){e&&(l(X),l(V),l(R),l(F),l(T),l(z),l(G),l(J),l(Y),l(y),l(N),l(A),l(I),l(S),l(E),l(O),l(U),l(q),l(D),l(L),l(K),l(ee),l(Z),l(te),l(le),l(k),l(ne),l(d),l(se),l(w),l(ae),l(ie),l(W),l(pe),l(oe),l(P)),l(f),g(b,e),g(M,e),g($,e),g(j,e),g(C,e),g(_,e),g(x,e),g(v,e),g(H,e),g(B,e)}}}const He='{"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 We(ue){return Ue(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Xe extends _e{constructor(f){super(),Le(this,f,We,ke,je,{})}}export{Xe as component};
