with HuggingFace and Gradio
Developers can easily present their work to a wide audience
Increase reproducibility of machine learning research
Easily identify and debug failure points of models
Learn how to:
gradio libraryAs a quick example of what we would like to build, check out the Keras Org on Hugging Face
Open any Space in your browser to use the model immediately
gradio is a Python library that lets you build web demos simply by specifying the list of input and output components expected by your machine learning model.
Gradio comes with a bunch of predefined components for different kinds of machine learning models.
For an image classifier, the expected input type is an Image and the output type is a Label.
For a speech recognition model, the expected input component is an Microphone (which lets users record from the browser) or Audio (which lets users drag-and-drop audio files), while the output type is Text.
For a question answering model, we expect 2 inputs: [Text, Text], one textbox for the paragraph and one for the question, and the output type is a single Text corresponding to the answer.
For all of the supported components, see the docs
In addition to the input and output types, Gradio expects a third parameter, which is the prediction function itself.
This parameter can be any regular Python function that takes in parameter(s) corresponding to the input component(s) and returns value(s) corresponding to the output component(s)
Create an app that generates a musical tone when provided a few different parameters.
def generate_tone(note, octave, duration):
sampling_rate = 48000
a4_freq, tones_from_a4 = 440, 12 * (octave - 4) + (note - 9)
frequency = a4_freq * 2 ** (tones_from_a4 / 12)
audio = np.linspace(0, int(duration), int(duration) * sampling_rate)
audio = (20000 * np.sin(audio * (2 * np.pi * frequency))).astype(np.int16)
return sampling_rate, audio
gr.Interface(
generate_tone, # function
[
gr.Dropdown(["C", "C#", "D", "D#", "E", "F", "F#",
"G", "G#", "A", "A#", "B"], type="index"),
gr.Slider(4, 6, step=1),
gr.Number(value=1, label="Duration in seconds"),
],
"audio",
title="Generate a Musical Tone!"
).launch()It is especially easy to demo a transformers model from Hugging Face’s Model Hub, using the special gr.Interface.load method.
Text-to-speech model built by Facebook:
You can go to the Hugging Face Model Hub and pick any model that performs one of the tasks supported in the transformers library
Create a Gradio demo for that model using gr.Interface.load.
Host the Demo (for free) on Hugging Face Spaces
First, create a Hugging Face account if you do not already have one, by visiting https://huggingface.co/ and clicking “Sign Up”
Once you are logged in, click on your profile picture and then click on “New Space” underneath it to get to this page: https://huggingface.co/new-space
Give your Space a name and a license. Select “Gradio” as the Space SDK, and then choose “Public” if you are fine with everyone accessing your Space and the underlying code
Then you will find a page that provides you instructions on how to upload your files into the Git repository for that Space. You may also need to add a requirements.txt file to specify any Python package dependencies.
Once you have pushed your files, that’s it! Spaces will automatically build your Gradio demo allowing you to share it with anyone, anywhere!
You can embed your Gradio demo on any website – in a blog, a portfolio page, or even in a colab notebook,
Example: Pictionary sketch recognition model below:
You find the link to our organization (HdM-…) in Moodle
Visit the organization page and click “Request to join this org” button, if you are not yet part of the org.
Then, once you have been approved to join the organization (and built your Gradio Demo and uploaded it to Spaces – see Sections above):
Settings > Rename or transfer this spaceNew owner.Congratulations! You have completed this tutorial 👍
Next, you may want to go back to the lab’s website
Acknowledgments: The slides are mainly based on a toolkit provided by Abubakar Abid from HuggingFace
Jan Kirenz