import{s as bl,f as fl,n as Il,o as gl}from"../chunks/scheduler.37c15a92.js";import{S as Cl,i as Gl,g as o,s as a,r as A,A as kl,h as i,f as t,c as n,j as pl,u as E,x as M,k as p,y as Bl,a as s,v as N,d as Q,t as Z,w as x}from"../chunks/index.2bf4358c.js";import{C as Ul}from"../chunks/CodeBlock.4e987730.js";import{D as Al}from"../chunks/DocNotebookDropdown.efc1fb7c.js";import{H as ol,E as El}from"../chunks/getInferenceSnippets.97e75ee2.js";function Nl(il){let m,v,W,R,u,S,y,_,j,rl="现在我们已经能够构建和共享基本界面了，让我们来探索一些 Interface 更高级的功能，比如会话状态和解释。",q,w,V,d,cl="Gradio 支持存储会话状态，以及保留多次提交的数据。会话状态对于构建聊天机器人等需要在用户与模型交互时保持数据的演示非常有用。请注意，会话状态不会在不同用户之间共享数据。",z,h,Ml="要将数据存储在会话状态中，你需要做三件事：",Y,J,ml="<li>向预测函数中传递一个额外的参数，该参数表示 Interface 此时的状态。</li> <li>在预测函数结束时，将状态的更新值添加到函数的返回值中。</li> <li>在创建 <code>Interface</code> 时添加 <code>state</code> 输入和 <code>state</code> 输出组件。</li>",X,T,ul="请参考下面的聊天机器人示例：",F,f,H,r,yl,L,U,jl=`请注意，输出组件的每次提交都被记录了下来。
注：我们还可以向状态参数传递一个默认值，该值会作为会话状态的初始值。`,K,b,P,I,wl="大多数机器学习模型都是黑盒子，函数的内部逻辑对使用的用户是隐藏的。为了提高透明度，我们设计了一个功能，使得为模型添加解释非常简单，只需将 Interface 类中的 interpretation 参数设置为 <code>default</code> 即可。这样，你的用户就可以理解输入的哪些部分对输出有影响。请看下面的简单界面示例，显示了一个带有解释功能的图像分类器：",D,g,O,C,dl=`提交一个输入，单击输出组件中的“Interpret”按钮，将显示图像中的哪一部分区域对
分类的结果产生了影响。`,ll,c,hl,el,G,Jl='除了 Gradio 提供的默认解释方法之外，你还可以在 <code>interpretation</code> 参数中指定 <code>shap</code> ，并设置 <code>num_shap</code> 参数。这将使用基于 Shapley 的解释方法，你可以在 <a href="https://christophm.github.io/interpretable-ml-book/shap.html" rel="nofollow">这里</a> 阅读关于 <code>shap</code> 更多的信息。最后，还可以将自己的解释函数传入 <code>interpretation</code> 参数。在 Gradio 的 <a href="https://gradio.app/getting_started/" rel="nofollow">开始页面</a> 中可以看到一个例子。',tl,k,Tl="这就是我们对 Gradio 的 <code>Interface</code> 类的深入研究。正如我们所看到的，这个类使得用几行 Python 代码创建机器学习演示变得简单。然而，有时你会想通过改变布局或将多个预测函数链接在一起来定制你的演示。如果我们能以某种方式将 <code>Interface</code> 拆分可定制的 “块”，那不是很好吗？幸运的是，我们可以做到！这是最后一节的主题。",sl,B,al,$,nl;return u=new ol({props:{title:"Interface 的高级功能",local:"Interface 的高级功能",headingTag:"h1"}}),y=new Al({props:{classNames:"absolute z-10 right-0 top-0",options:[{label:"Google Colab",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/chapter9/section6.ipynb"},{label:"Aws Studio",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/chapter9/section6.ipynb"}]}}),w=new ol({props:{title:"使用会话状态保存数据",local:"使用会话状态保存数据",headingTag:"h3"}}),f=new Ul({props:{code:"aW1wb3J0JTIwcmFuZG9tJTBBJTBBaW1wb3J0JTIwZ3JhZGlvJTIwYXMlMjBnciUwQSUwQSUwQWRlZiUyMGNoYXQobWVzc2FnZSUyQyUyMGhpc3RvcnkpJTNBJTBBJTIwJTIwJTIwJTIwaGlzdG9yeSUyMCUzRCUyMGhpc3RvcnklMjBvciUyMCU1QiU1RCUwQSUyMCUyMCUyMCUyMGlmJTIwbWVzc2FnZS5zdGFydHN3aXRoKCUyMkhvdyUyMG1hbnklMjIpJTNBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwcmVzcG9uc2UlMjAlM0QlMjByYW5kb20ucmFuZGludCgxJTJDJTIwMTApJTBBJTIwJTIwJTIwJTIwZWxpZiUyMG1lc3NhZ2Uuc3RhcnRzd2l0aCgl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word">import</span> gradio <span class="hljs-keyword">as</span> gr


<span class="hljs-keyword">def</span> <span class="hljs-title function_">chat</span>(<span class="hljs-params">message, history</span>):
    history = history <span class="hljs-keyword">or</span> []
    <span class="hljs-keyword">if</span> message.startswith(<span class="hljs-string">&quot;How many&quot;</span>):
        response = random.randint(<span class="hljs-number">1</span>, <span class="hljs-number">10</span>)
    <span class="hljs-keyword">elif</span> message.startswith(<span class="hljs-string">&quot;How&quot;</span>):
        response = random.choice([<span class="hljs-string">&quot;Great&quot;</span>, <span class="hljs-string">&quot;Good&quot;</span>, <span class="hljs-string">&quot;Okay&quot;</span>, <span class="hljs-string">&quot;Bad&quot;</span>])
    <span class="hljs-keyword">elif</span> message.startswith(<span class="hljs-string">&quot;Where&quot;</span>):
        response = random.choice([<span class="hljs-string">&quot;Here&quot;</span>, <span class="hljs-string">&quot;There&quot;</span>, <span class="hljs-string">&quot;Somewhere&quot;</span>])
    <span class="hljs-keyword">else</span>:
        response = <span class="hljs-string">&quot;I don&#x27;t know&quot;</span>
    history.append((message, response))
    <span class="hljs-keyword">return</span> history, history


iface = gr.Interface(
    chat,
    [<span class="hljs-string">&quot;text&quot;</span>, <span class="hljs-string">&quot;state&quot;</span>],
    [<span class="hljs-string">&quot;chatbot&quot;</span>, <span class="hljs-string">&quot;state&quot;</span>],
    allow_screenshot=<span class="hljs-literal">False</span>,
    allow_flagging=<span class="hljs-string">&quot;never&quot;</span>,
)
iface.launch()`,wrap:!1}}),b=new ol({props:{title:"通过解释来理解预测",local:"通过解释来理解预测",headingTag:"h3"}}),g=new Ul({props:{code:"aW1wb3J0JTIwcmVxdWVzdHMlMEFpbXBvcnQlMjB0ZW5zb3JmbG93JTIwYXMlMjB0ZiUwQSUwQWltcG9ydCUyMGdyYWRpbyUyMGFzJTIwZ3IlMEElMEFpbmNlcHRpb25fbmV0JTIwJTNEJTIwdGYua2VyYXMuYXBwbGljYXRpb25zLk1vYmlsZU5ldFYyKCklMjAlMjAlMjMlMjAlRTUlOEEl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IuSW50ZXJmYWNlKCUwQSUyMCUyMCUyMCUyMGZuJTNEY2xhc3NpZnlfaW1hZ2UlMkMlMjBpbnB1dHMlM0RpbWFnZSUyQyUyMG91dHB1dHMlM0RsYWJlbCUyQyUyMGludGVycHJldGF0aW9uJTNEJTIyZGVmYXVsdCUyMiUyQyUyMHRpdGxlJTNEdGl0bGUlMEEpLmxhdW5jaCgp",highlighted:`<span class="hljs-keyword">import</span> requests
<span class="hljs-keyword">import</span> tensorflow <span class="hljs-keyword">as</span> tf

<span class="hljs-keyword">import</span> gradio <span class="hljs-keyword">as</span> gr

inception_net = tf.keras.applications.MobileNetV2()  <span class="hljs-comment"># 加载模型</span>

<span class="hljs-comment"># 下载 ImageNet 的可读标签</span>
response = requests.get(<span class="hljs-string">&quot;https://git.io/JJkYN&quot;</span>)
labels = response.text.split(<span class="hljs-string">&quot;\\n&quot;</span>)


<span class="hljs-keyword">def</span> <span class="hljs-title function_">classify_image</span>(<span class="hljs-params">inp</span>):
    inp = inp.reshape((-<span class="hljs-number">1</span>, <span class="hljs-number">224</span>, <span class="hljs-number">224</span>, <span class="hljs-number">3</span>))
    inp = tf.keras.applications.mobilenet_v2.preprocess_input(inp)
    prediction = inception_net.predict(inp).flatten()
    <span class="hljs-keyword">return</span> {labels[i]: <span class="hljs-built_in">float</span>(prediction[i]) <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">1000</span>)}


image = gr.Image(shape=(<span class="hljs-number">224</span>, <span class="hljs-number">224</span>))
label = gr.Label(num_top_classes=<span class="hljs-number">3</span>)

title = <span class="hljs-string">&quot;Gradio Image Classifiction + Interpretation Example&quot;</span>
gr.Interface(
    fn=classify_image, inputs=image, outputs=label, interpretation=<span class="hljs-string">&quot;default&quot;</span>, title=title
).launch()`,wrap:!1}}),B=new El({props:{source:"https://github.com/huggingface/course/blob/main/chapters/zh-CN/chapter9/6.mdx"}}),{c(){m=o("meta"),v=a(),W=o("p"),R=a(),A(u.$$.fragment),S=a(),A(y.$$.fragment),_=a(),j=o("p"),j.textContent=rl,q=a(),A(w.$$.fragment),V=a(),d=o("p"),d.textContent=cl,z=a(),h=o("p"),h.textContent=Ml,Y=a(),J=o("ol"),J.innerHTML=ml,X=a(),T=o("p"),T.textContent=ul,F=a(),A(f.$$.fragment),H=a(),r=o("iframe"),L=a(),U=o("p"),U.textContent=jl,K=a(),A(b.$$.fragment),P=a(),I=o("p"),I.innerHTML=wl,D=a(),A(g.$$.fragment),O=a(),C=o("p"),C.textContent=dl,ll=a(),c=o("iframe"),el=a(),G=o("p"),G.innerHTML=Jl,tl=a(),k=o("p"),k.innerHTML=Tl,sl=a(),A(B.$$.fragment),al=a(),$=o("p"),this.h()},l(l){const e=kl("svelte-u9bgzb",document.head);m=i(e,"META",{name:!0,content:!0}),e.forEach(t),v=n(l),W=i(l,"P",{}),pl(W).forEach(t),R=n(l),E(u.$$.fragment,l),S=n(l),E(y.$$.fragment,l),_=n(l),j=i(l,"P",{"data-svelte-h":!0}),M(j)!=="svelte-djsadf"&&(j.textContent=rl),q=n(l),E(w.$$.fragment,l),V=n(l),d=i(l,"P",{"data-svelte-h":!0}),M(d)!=="svelte-8rb7o"&&(d.textContent=cl),z=n(l),h=i(l,"P",{"data-svelte-h":!0}),M(h)!=="svelte-1i7evrf"&&(h.textContent=Ml),Y=n(l),J=i(l,"OL",{"data-svelte-h":!0}),M(J)!=="svelte-1rk3mg8"&&(J.innerHTML=ml),X=n(l),T=i(l,"P",{"data-svelte-h":!0}),M(T)!=="svelte-13dngbh"&&(T.textContent=ul),F=n(l),E(f.$$.fragment,l),H=n(l),r=i(l,"IFRAME",{src:!0,frameborder:!0,height:!0,title:!0,class:!0,allow:!0,sandbox:!0}),pl(r).forEach(t),L=n(l),U=i(l,"P",{"data-svelte-h":!0}),M(U)!=="svelte-1071byc"&&(U.textContent=jl),K=n(l),E(b.$$.fragment,l),P=n(l),I=i(l,"P",{"data-svelte-h":!0}),M(I)!=="svelte-1qd70a6"&&(I.innerHTML=wl),D=n(l),E(g.$$.fragment,l),O=n(l),C=i(l,"P",{"data-svelte-h":!0}),M(C)!=="svelte-1pm8yr7"&&(C.textContent=dl),ll=n(l),c=i(l,"IFRAME",{src:!0,frameborder:!0,height:!0,title:!0,class:!0,allow:!0,sandbox:!0}),pl(c).forEach(t),el=n(l),G=i(l,"P",{"data-svelte-h":!0}),M(G)!=="svelte-167e4wc"&&(G.innerHTML=Jl),tl=n(l),k=i(l,"P",{"data-svelte-h":!0}),M(k)!=="svelte-1c2izjr"&&(k.innerHTML=Tl),sl=n(l),E(B.$$.fragment,l),al=n(l),$=i(l,"P",{}),pl($).forEach(t),this.h()},h(){p(m,"name","hf:doc:metadata"),p(m,"content",Ql),fl(r.src,yl="https://course-demos-Chatbot-Demo.hf.space")||p(r,"src",yl),p(r,"frameborder","0"),p(r,"height","350"),p(r,"title","Gradio app"),p(r,"class","container p-0 flex-grow space-iframe"),p(r,"allow","accelerometer; ambient-light-sensor; autoplay; battery; camera; document-domain; encrypted-media; fullscreen; geolocation; gyroscope; layout-animations; legacy-image-formats; magnetometer; microphone; midi; oversized-images; payment; picture-in-picture; publickey-credentials-get; sync-xhr; usb; vr ; wake-lock; xr-spatial-tracking"),p(r,"sandbox","allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"),fl(c.src,hl="https://course-demos-gradio-image-interpretation.hf.space")||p(c,"src",hl),p(c,"frameborder","0"),p(c,"height","570"),p(c,"title","Gradio app"),p(c,"class","container p-0 flex-grow space-iframe"),p(c,"allow","accelerometer; ambient-light-sensor; autoplay; battery; camera; document-domain; encrypted-media; fullscreen; geolocation; gyroscope; layout-animations; legacy-image-formats; magnetometer; microphone; midi; oversized-images; payment; picture-in-picture; publickey-credentials-get; sync-xhr; usb; vr ; wake-lock; xr-spatial-tracking"),p(c,"sandbox","allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads")},m(l,e){Bl(document.head,m),s(l,v,e),s(l,W,e),s(l,R,e),N(u,l,e),s(l,S,e),N(y,l,e),s(l,_,e),s(l,j,e),s(l,q,e),N(w,l,e),s(l,V,e),s(l,d,e),s(l,z,e),s(l,h,e),s(l,Y,e),s(l,J,e),s(l,X,e),s(l,T,e),s(l,F,e),N(f,l,e),s(l,H,e),s(l,r,e),s(l,L,e),s(l,U,e),s(l,K,e),N(b,l,e),s(l,P,e),s(l,I,e),s(l,D,e),N(g,l,e),s(l,O,e),s(l,C,e),s(l,ll,e),s(l,c,e),s(l,el,e),s(l,G,e),s(l,tl,e),s(l,k,e),s(l,sl,e),N(B,l,e),s(l,al,e),s(l,$,e),nl=!0},p:Il,i(l){nl||(Q(u.$$.fragment,l),Q(y.$$.fragment,l),Q(w.$$.fragment,l),Q(f.$$.fragment,l),Q(b.$$.fragment,l),Q(g.$$.fragment,l),Q(B.$$.fragment,l),nl=!0)},o(l){Z(u.$$.fragment,l),Z(y.$$.fragment,l),Z(w.$$.fragment,l),Z(f.$$.fragment,l),Z(b.$$.fragment,l),Z(g.$$.fragment,l),Z(B.$$.fragment,l),nl=!1},d(l){l&&(t(v),t(W),t(R),t(S),t(_),t(j),t(q),t(V),t(d),t(z),t(h),t(Y),t(J),t(X),t(T),t(F),t(H),t(r),t(L),t(U),t(K),t(P),t(I),t(D),t(O),t(C),t(ll),t(c),t(el),t(G),t(tl),t(k),t(sl),t(al),t($)),t(m),x(u,l),x(y,l),x(w,l),x(f,l),x(b,l),x(g,l),x(B,l)}}}const Ql='{"title":"Interface 的高级功能","local":"Interface 的高级功能","sections":[{"title":"使用会话状态保存数据","local":"使用会话状态保存数据","sections":[],"depth":3},{"title":"通过解释来理解预测","local":"通过解释来理解预测","sections":[],"depth":3}],"depth":1}';function Zl(il){return gl(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Sl extends Cl{constructor(m){super(),Gl(this,m,Zl,Nl,bl,{})}}export{Sl as component};
