Buckets:
| # Advanced Interface features[[advanced-interface-features]] | |
| <CourseFloatingBanner chapter={9} | |
| classNames="absolute z-10 right-0 top-0" | |
| notebooks={[ | |
| {label: "Google Colab", value: "https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter9/section6.ipynb"}, | |
| {label: "Aws Studio", value: "https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter9/section6.ipynb"}, | |
| ]} /> | |
| Now that we can build and share a basic interface, let's explore some more advanced features such as state, and interpretation. | |
| ### Using state to persist data[[using-state-to-persist-data]] | |
| Gradio supports *session state*, where data persists across multiple submits within a | |
| page load. Session state is useful for building demos of, for example, chatbots where you want to | |
| persist data as the user interacts with the model. Note that session state does not share data between different users of your model. | |
| To store data in a session state, you need to do three things: | |
| 1. Pass in an *extra parameter* into your function, which represents the state of the interface. | |
| 1. At the end of the function, return the updated value of the state as an *extra return value*. | |
| 1. Add the 'state' input and 'state' output components when creating your `Interface`. | |
| See the chatbot example below: | |
| ```py | |
| import random | |
| import gradio as gr | |
| def chat(message, history): | |
| history = history or [] | |
| if message.startswith("How many"): | |
| response = random.randint(1, 10) | |
| elif message.startswith("How"): | |
| response = random.choice(["Great", "Good", "Okay", "Bad"]) | |
| elif message.startswith("Where"): | |
| response = random.choice(["Here", "There", "Somewhere"]) | |
| else: | |
| response = "I don't know" | |
| history.append((message, response)) | |
| return history, history | |
| iface = gr.Interface( | |
| chat, | |
| ["text", "state"], | |
| ["chatbot", "state"], | |
| allow_screenshot=False, | |
| allow_flagging="never", | |
| ) | |
| iface.launch() | |
| ``` | |
| <iframe src="https://course-demos-Chatbot-Demo.hf.space" frameBorder="0" height="350" title="Gradio app" class="container p-0 flex-grow space-iframe" 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" sandbox="allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"></iframe> | |
| Notice how the state of the output component persists across submits. | |
| Note: you can pass in a default value to the state parameter, | |
| which is used as the initial value of the state. | |
| ### Using interpretation to understand predictions[[using-interpretation-to-understand-predictions]] | |
| Most machine learning models are black boxes and the internal logic of the function is hidden from the end user. To encourage transparency, we've made it very easy to add interpretation to your model by simply setting the interpretation keyword in the Interface class to default. This allows your users to understand what parts of the input are responsible for the output. Take a look at the simple interface below which shows an image classifier that also includes interpretation: | |
| ```py | |
| import requests | |
| import tensorflow as tf | |
| import gradio as gr | |
| inception_net = tf.keras.applications.MobileNetV2() # load the model | |
| # Download human-readable labels for ImageNet. | |
| response = requests.get("https://git.io/JJkYN") | |
| labels = response.text.split("\n") | |
| def classify_image(inp): | |
| inp = inp.reshape((-1, 224, 224, 3)) | |
| inp = tf.keras.applications.mobilenet_v2.preprocess_input(inp) | |
| prediction = inception_net.predict(inp).flatten() | |
| return {labels[i]: float(prediction[i]) for i in range(1000)} | |
| image = gr.Image(shape=(224, 224)) | |
| label = gr.Label(num_top_classes=3) | |
| title = "Gradio Image Classifiction + Interpretation Example" | |
| gr.Interface( | |
| fn=classify_image, inputs=image, outputs=label, interpretation="default", title=title | |
| ).launch() | |
| ``` | |
| Test the interpretation function by submitting an input then clicking Interpret under the output component. | |
| <iframe src="https://course-demos-gradio-image-interpretation.hf.space" frameBorder="0" height="570" title="Gradio app" class="container p-0 flex-grow space-iframe" 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" sandbox="allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"></iframe> | |
| Besides the default interpretation method Gradio provides, you can also specify `shap` for the `interpretation` parameter and set the `num_shap` parameter. This uses Shapley-based interpretation, which you can read more about [here](https://christophm.github.io/interpretable-ml-book/shap.html). | |
| Lastly, you can also pass in your own interpretation function into the `interpretation` parameter. See an example in Gradio's getting started page [here](https://gradio.app/getting_started/). | |
| This wraps up our deep dive into the `Interface` class of Gradio. As we've seen, this class makes it simple to create machine learning demos in a few lines of Python code. However, sometimes you'll want to customise your demo by changing the layout or chaining multiple prediction functions together. Wouldn't it be nice if we could somehow split the `Interface` into customizable "blocks"? Fortunately, there is! That's the topic of the final section. | |
| <EditOnGithub source="https://github.com/huggingface/course/blob/main/chapters/en/chapter9/6.mdx" /> |
Xet Storage Details
- Size:
- 5.98 kB
- Xet hash:
- 3e07fdaf5afe8ec75c04faa3084f0eaf0b198aecc0394e3ead62b52afbbeda74
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.