Gradio Crash Course - Fastest way to build & share Machine Learning apps

AssemblyAI
27 Oct 202213:54

Summary

TLDRIn this informative video, Patrick from Assembly AI introduces Gradio, a Python framework simplifying the creation of attractive machine learning apps with user-friendly web interfaces. He demonstrates building apps with various input types like text, images, and audio, and shows how to integrate them with machine learning models for displaying outputs elegantly. Patrick also covers deploying and sharing these apps, including using Hugging Face Spaces for free hosting, and touches on handling state in Gradio apps.

Takeaways

  • πŸ“ Gradio is a Python framework designed to simplify the creation of machine learning apps with user-friendly web interfaces.
  • πŸ› οΈ The framework allows for various input types such as text, images, and audio, which can be processed by machine learning models and displayed in an appealing manner.
  • πŸ”§ To get started with Gradio, one can follow the official quick start guide and install it using pip.
  • πŸ“‘ The script demonstrates creating a basic Gradio app with a Python file, where an interface is defined with input and output types and an execution function.
  • 🌐 Running the app starts a server, making it accessible through a local host, showcasing a text input field and a submit button for user interaction.
  • πŸ“ Gradio supports customization of input fields, including text boxes with multiline options and placeholders.
  • πŸ“ˆ The framework enables the use of multiple inputs and outputs, mixing different types of components like checkboxes, sliders, and text fields.
  • πŸ–ΌοΈ Gradio simplifies the integration of image and audio components, allowing users to upload and process media files with machine learning models.
  • 🧩 Gradio Blocks offer more flexibility for layout and data flow, allowing developers to define custom components and event handling.
  • πŸ”„ The script includes an example of integrating Gradio with Hugging Face Transformers for tasks like translating text from English to German.
  • πŸ”’ Gradio apps can manage state either through global variables shared across users or session-specific states that preserve user interactions.

Q & A

  • What is Gradio and its primary purpose?

    -Gradio is a Python framework designed to make it simple to build visually appealing machine learning apps with user-friendly web interfaces.

  • How do you install Gradio?

    -You can install Gradio using the command: `pip install gradio`.

  • What is the basic structure of a Gradio app?

    -A basic Gradio app involves importing Gradio, creating an interface with input and output types, defining a function to process the inputs and outputs, and launching the app with `demo.launch`.

  • What are some examples of input and output types that can be used in Gradio?

    -Examples include text, image, audio, checkbox, and slider for inputs, and text, number, and label for outputs.

  • How can you customize input fields in Gradio?

    -You can customize input fields by using Gradio components such as `gradio.Textbox`, specifying parameters like `lines` and `placeholder`.

  • How can multiple inputs and outputs be handled in Gradio?

    -Multiple inputs and outputs can be handled by using lists for the `inputs` and `outputs` parameters in the interface definition.

  • How does Gradio handle image and audio inputs?

    -Gradio provides specific components like `gradio.Image` for image inputs and `gradio.Audio` for audio inputs, which automatically handle file uploads and conversions to formats like numpy arrays.

  • What is the difference between Gradio Interface and Gradio Blocks?

    -Gradio Interface offers a high-level abstraction with default layout and components, while Gradio Blocks provides more flexibility for customizing layout and data flow.

  • How can state be managed in a Gradio app?

    -State can be managed using a global state variable, which is shared across all users, or using session state, which is unique to each user's session.

  • What are the two options for sharing and hosting a Gradio app?

    -You can share a Gradio app by setting `share=True` in the `demo.launch` method to get a public URL, or you can host it permanently for free on Hugging Face Spaces.

  • How can you deploy a Gradio app on Hugging Face Spaces?

    -To deploy on Hugging Face Spaces, create a new space, select the Gradio template, add your app code and requirements, and commit the changes. Your app will be hosted and available via a public URL.

  • What should you do if you have an existing GitHub repository and want to deploy your Gradio app to Hugging Face Spaces?

    -You can add the Hugging Face Spaces URL as a second remote to your GitHub repository and push the code using the provided commands to deploy the app.

Outlines

00:00

πŸ› οΈ Introduction to Gradio Framework

Patrick from Assembly AI introduces Gradio, a Python framework designed to simplify the creation of attractive machine learning applications. Gradio enables developers to construct web interfaces with various input types such as text, images, or audio, which can be processed by machine learning models and then displayed in an appealing manner. The video provides a step-by-step guide on getting started with Gradio, including installation via pip, creating a basic interface with input and output fields, and launching a demo application. It also touches on deploying and sharing these applications, showcasing the ease of building and customizing interfaces with Gradio.

05:02

πŸ“š Deep Dive into Gradio's Features and Components

This section delves deeper into Gradio's capabilities, exploring the use of different input and output widgets such as text boxes, images, audio files, and sliders. It explains how to customize these components and handle multiple inputs and outputs within an application. Patrick demonstrates how to apply image processing, like a sepia filter, and audio processing, suggesting the potential for speech-to-text functionality. The video also introduces Gradio Blocks for more layout flexibility and complex data flows, and it shows how to integrate Gradio with Hugging Face Transformers for tasks like translation, highlighting the framework's extensibility.

10:04

πŸ”— Managing State and Deployment of Gradio Apps

The final part of the script discusses state management in Gradio apps, illustrating two methods: global state, which is shared among all users, and session state, which is unique to each user session. A chatbot example is given to demonstrate session state usage. Additionally, the video outlines two methods for sharing and hosting Gradio apps: using the 'share=True' parameter in the demo launch for a public URL, and utilizing Hugging Face Spaces for free, permanent hosting. The process for setting up an app on Hugging Face Spaces is detailed, including creating a space, committing files, and pushing updates via Git. The video concludes with instructions for linking an existing GitHub repository to Hugging Face Spaces, offering a comprehensive guide to deploying Gradio applications.

Mindmap

Keywords

πŸ’‘Gradio

Gradio is a Python framework designed to simplify the process of building web interfaces for machine learning applications. It allows developers to create visually appealing and user-friendly apps that can handle various input types like text, images, or audio. In the video, Gradio is introduced as the main tool for creating machine learning apps, with the script demonstrating how to install it and use it to build an interface that interacts with a machine learning model.

πŸ’‘Machine Learning Model

A machine learning model is an algorithm that learns from data to make predictions or decisions. In the context of the video, the model is the backend component that processes inputs received from the Gradio interface and produces outputs. The script shows how to pass data to the model and display the model's results within the app.

πŸ’‘Web Interface

A web interface is the visual and interactive part of a website or web application that allows users to access features and services. In the video, the script focuses on building web interfaces using Gradio, which are used to input data into a machine learning model and display the model's outputs in a user-friendly manner.

πŸ’‘Input Types

Input types refer to the different kinds of data that a web interface can accept, such as text, images, or audio. The script discusses how Gradio enables the creation of interfaces with various input types, showcasing examples like text fields, image uploads, and audio file inputs.

πŸ’‘Output

In the context of the video, output refers to the result produced by the machine learning model after processing the input data. The script explains how to display these outputs in the Gradio interface, such as showing text or images that are the result of the model's processing.

πŸ’‘Interface Components

Interface components are the individual elements of a web interface, such as text boxes, sliders, or buttons. The video script describes how to use Gradio to create and customize these components, tailoring the interface to the needs of the machine learning application.

πŸ’‘Deploy

To deploy an application means to make it available to users by launching it on a server or hosting platform. The script explains how to deploy Gradio apps, providing options for sharing a local URL or using platforms like Hugging Face Spaces for hosting.

πŸ’‘Hugging Face Spaces

Hugging Face Spaces is a hosting platform for machine learning models and applications. The script mentions it as a service for deploying Gradio apps, allowing developers to share their applications publicly without managing their own servers.

πŸ’‘Global State

Global state refers to data that is accessible throughout an application, regardless of user sessions. The script discusses using global variables to maintain state in a Gradio app, but also notes the limitation that it shares this state among all users.

πŸ’‘Session State

Session state is data that is specific to an individual user's session within an application. The video script explains how to use session state in Gradio apps to maintain a chat history or other user-specific data that persists only for the duration of a single user's interaction with the app.

πŸ’‘Transformers

Transformers, in the context of this video, refer to a type of architecture for machine learning models, particularly those used in natural language processing tasks. The script includes an example of integrating Gradio with Hugging Face's Transformers library to create an app that translates English text to German.

Highlights

Introduction to Gradio, a Python framework for building machine learning apps with user-friendly web interfaces.

Gradio simplifies the process of creating web interfaces with various input types such as text, images, or audio for machine learning models.

Demonstration of deploying and sharing Gradio apps, showcasing the ease of use and accessibility.

Quick start guide for installing Gradio using pip and setting up a basic app with Python code.

Explanation of how to create a Gradio interface with input and output types and an executable function.

Live demonstration of a running Gradio app with a text input field and output display.

Deep dive into customizing input and output fields with Gradio's widgets for more control.

Example of using multiple inputs and outputs in a Gradio app, including text components, checkboxes, and sliders.

Showcasing image and audio components in Gradio for uploading and processing media files with machine learning models.

Introduction to Gradio Blocks for more flexible layouts and data flows in app development.

Integration of Gradio with Hugging Face Transformers for tasks like translating text from English to German.

Demonstration of using Gradio Blocks to create a more complex app layout with rows and columns.

Explanation of handling state in Gradio apps, with both global and session state options.

Live example of a chatbot using session state in Gradio to maintain conversation history.

Instructions on sharing and hosting Gradio apps, including creating a public URL for sharing.

Guide on hosting Gradio apps for free on Hugging Face Spaces, providing a platform for app deployment.

Additional note on using Git commands to push existing GitHub repositories to Hugging Face Spaces for hosting.

Closing remarks encouraging viewers to like, subscribe, and look forward to future videos.

Transcripts

play00:00

hi everyone I'm Patrick from assembly Ai

play00:02

and in this video we learn about radio

play00:04

gradio is a python framework that makes

play00:06

it super simple to build nice looking

play00:08

machine learning apps so with this you

play00:10

can build friendly web interfaces that

play00:13

have different types of inputs for

play00:14

example text images or audio and then

play00:16

you can pass this to your machine

play00:18

learning model and then you can show the

play00:19

output in a nice way so in this video I

play00:22

show you everything you need to get

play00:23

started and in the end I also show you

play00:25

how we can deploy and share these apps

play00:27

so let's get started so to get started

play00:29

let's follow the official quick start

play00:31

guide so we can install this with Pip

play00:33

install radio and now let's copy the

play00:36

first example code and switch to our

play00:39

editor so here I already have a file

play00:41

app.pi and I also already installed this

play00:44

so let's paste in the code so we import

play00:46

gradual sgr and then we create a gradual

play00:50

interface an interface provides a high

play00:53

level abstraction to create these demos

play00:55

so it gets input types and output types

play00:59

and then a function that is executed in

play01:01

between and then we say demo.launch and

play01:05

now if we run python app.pi then this

play01:08

will start a server so now our app is

play01:11

running and we can visit our localhost

play01:13

and here we see we have a text input

play01:16

field where we can put in a name and

play01:18

then click on submit and then the

play01:20

function is executed and then we see the

play01:23

output field which is also a text field

play01:25

so this is what we specified here with

play01:28

just a string the inputs is a text the

play01:31

outputs is a text and in between we

play01:34

execute this function so this function

play01:37

needs one parameter since we have one

play01:39

input parameter and then it also needs

play01:42

one output that we one return parameter

play01:46

that we return because we also have one

play01:48

output field so yeah this is the

play01:50

simplest way how to get started and how

play01:52

to build these interfaces and now let's

play01:54

dive into this a little bit deeper so

play01:57

first of all sa input or output field we

play01:59

can just use a string like here so we

play02:02

used a text we can also use an image or

play02:05

a audio field or for example a label for

play02:08

the output and then it will

play02:10

automatically create these switches for

play02:12

us or if we want a little bit more

play02:14

control we can create them ourselves so

play02:17

instead of just using the string we use

play02:20

gradio and then we can use the different

play02:22

widgets for example here we use a text

play02:25

box and then if we have a look at the

play02:28

parameters so for example we can say we

play02:30

want lines equals two and we want a

play02:33

placeholder so we say name here and now

play02:37

let's save this and then let's rerun our

play02:40

app and by the way to see all the

play02:43

available widgets we can have a look at

play02:46

the official documentation so here you

play02:49

see all the different components that

play02:51

you can use and we have a look at a few

play02:53

of them in a few moments but now let's

play02:55

actually reload our app and now here we

play02:58

see we have our text box now with two

play03:01

lines and everything else is still the

play03:03

same so this is one thing that we can do

play03:06

and now we can also use multiple inputs

play03:09

and outputs so let's see how to do this

play03:11

so to use multiple inputs and outputs

play03:13

instead of just using one component here

play03:16

we can use a list and then for example

play03:19

let's use a text component then we also

play03:22

want a check box and then we also want a

play03:26

radio dot slider from 0 to 100 so we can

play03:30

mix and match strings and the actual

play03:33

components and then for the outputs so

play03:36

let's reformat this a little bit for the

play03:38

outputs we also use a list where we want

play03:41

a text field and then let's also use a

play03:45

number field for example and then we

play03:48

also have to modify the functions so now

play03:51

let me copy and paste this here so since

play03:54

we have three inputs we also need three

play03:56

parameters here and then we need to

play03:59

return turn two outputs here since we

play04:02

have two outputs and now here for

play04:04

example we make the salutation dependent

play04:07

on the is morning Boolean so this is a

play04:11

check box so then this is a Boolean

play04:12

parameter and this is a number already

play04:15

because we use a slider so this is

play04:18

automatically done for us and then we

play04:20

return these two outputs so let's again

play04:22

restart our app and then let's have a

play04:26

look at this so if we reload this so now

play04:29

we see we have three Fields I can put my

play04:31

name I can use the jackbox and I can

play04:35

have the slider here and then I submit

play04:37

this and we get two different outputs so

play04:41

yeah this is how we can use multiple

play04:43

inputs and outputs then let me quickly

play04:45

show you how this image and audio

play04:47

components look like because they also

play04:49

make it super simple to upload images or

play04:51

audio files and then pass them to our

play04:53

models so let me copy and paste the code

play04:56

here for you so here we use a gradient

play04:59

your image component as input and here

play05:02

we specify a shape and then for the

play05:04

output we just use a string and then for

play05:06

the function we get an input image as

play05:09

input parameter and this is already a

play05:11

numpy array so here we can for example

play05:13

apply a sepia filter by saying by using

play05:17

the numpy dot product and then we also

play05:19

return a image and now if we restart our

play05:23

app and wait a few moments and then if

play05:26

we go to our app and reloads the page

play05:29

then here we see we have an image

play05:31

component as the input so here we can

play05:34

drop an image or we can click to upload

play05:36

and then here we can select an image for

play05:38

example and then click on submit and now

play05:41

we see we have the Sapia filter applied

play05:43

so this is how easily we can work here

play05:46

with image components in gradual and the

play05:49

same with audio files so let me copy and

play05:51

paste this here as well so for the audio

play05:55

we simply use this radio dot audio

play05:58

component and now if if we again restart

play06:01

our app then let's have a look how this

play06:04

looks like so here I noticed now it's

play06:06

it's using a separate Port so here I

play06:09

have to open this as well and here we

play06:12

can again drop our file or click to

play06:14

upload and then for example select this

play06:16

and now here we can even listen to this

play06:19

so let's listen deep learning that

play06:21

builds

play06:22

puppy eyes to

play06:24

do and then we can submit this and here

play06:26

for example we could apply speech to

play06:28

text and then display this as a text

play06:31

field so yeah this is how images and

play06:33

audio components look like now let's

play06:35

learn about radio blocks so until now we

play06:38

always use the gradual interface which

play06:40

is a very high level abstraction so it

play06:42

does a lot of things for you for example

play06:44

it already adds these two buttons and it

play06:46

already defines a two column layout so

play06:49

if you want more flexibility about the

play06:51

layouts and data flows you can use a

play06:54

gradual block so for this let me copy

play06:57

and paste this here as well so here we

play06:59

say with radio blocks as demo so we

play07:02

always use this as a context manager and

play07:05

then inside we Define our components

play07:07

again and then for example a button and

play07:10

then here we add an event so we say

play07:12

button click and then what will happen

play07:14

so here we again execute the function

play07:17

and it gets these inputs and outputs and

play07:20

now let's restart our app so then if we

play07:23

have a look at this now we see we know

play07:24

longer have two columns by default so

play07:27

here we have our input and output box

play07:28

and then we have the grid function so

play07:31

this works as well and now let's have a

play07:33

look at a more complex example so as a

play07:37

second blocks example let me copy and

play07:40

paste this as well so here I also show

play07:43

you how we can integrate this with

play07:45

hugging face Transformers so for this of

play07:47

course we have to also install pip

play07:49

install Transformers then we download a

play07:52

model from the hugging face Hub so here

play07:54

this is a model that does translation

play07:57

from English to German so in this

play08:00

function we return we run our model and

play08:02

then return the German text then again

play08:05

we say with gradual blocks and then here

play08:08

we Define our layout so we say with

play08:10

gradual row and then we add two columns

play08:13

so all of these are used as context

play08:16

managers and then here we add our Fields

play08:18

then we have a button and again say

play08:21

button click where we add this function

play08:23

that should be X executed and then the

play08:26

fields and then we also add some

play08:28

examples in the bottom so let's again

play08:30

restart the app and have a look at how

play08:33

this looks like so here we get two

play08:35

columns again and here we have an

play08:37

English text and then here we also have

play08:39

examples so let's select one and then we

play08:41

can click on translate and now this is

play08:44

running the model and here we have the

play08:45

German text so this is actually correct

play08:47

so yeah this is how we can use the

play08:50

gradual blocks and in a moment I also

play08:52

show you how we can deploy this app so

play08:55

and then share this so but first I want

play08:58

to show you one more thing so I want to

play09:00

show you how we can handle State and

play09:02

graduate apps and for this let me copy

play09:04

and paste the code in here and there are

play09:06

two ways how to do it first we can use a

play09:09

global State and for this we use a

play09:12

global variable for example data equals

play09:15

a list and now inside the function we

play09:18

can add items to this list the only

play09:20

problem with a global state is that this

play09:23

is shared between all use user so if

play09:25

multiple users use your app then they

play09:27

all add items to the same Global

play09:30

variable so there is a second way to do

play09:33

this and for this we can use a session

play09:35

state so here as the last parameter

play09:38

inside the function we add the history

play09:40

and now in this example we use a chat

play09:44

bot and then for the inputs and outputs

play09:46

as last input field we use the state for

play09:50

the inputs and the outputs and now

play09:52

inside the function we can now access

play09:54

the session State and then say this is a

play09:57

list and then we can add their responses

play10:00

to this list and then we also have to

play10:02

return this as last parameter here

play10:04

because we want to keep this as the

play10:06

output and now we have this as session

play10:08

state so let's run the app so now here

play10:11

we can chat with our bot so we can say

play10:13

hello and then here we see the output

play10:16

fields and now it's not very clever so

play10:18

it says I don't know and then we can ask

play10:20

for example how many and then we get a

play10:22

new answer so as you can see the old

play10:25

responses are still stored so this is

play10:28

how we can use session State and now let

play10:30

me show you how we can share and host

play10:31

the apps now let me show you how we can

play10:33

share and host a radio app so for this

play10:35

we have two options and the first one is

play10:38

when we say demo launch we can say share

play10:41

equals true and now when we run this

play10:44

then this will also create a public URL

play10:47

that we can use for sharing so let's

play10:49

wait a few moments and there we have it

play10:51

so we have a local URL but now we also

play10:53

get this public URL and if we visit this

play10:57

URL then here we have our app that is up

play11:00

and running so we have the example that

play11:02

we've seen before where we can put in a

play11:05

English text and then click on translate

play11:07

and now this will run the model and here

play11:09

we get the translation so this is the

play11:12

first option or as a second option so

play11:15

here we see for free permanent hosting

play11:18

we can check out hacking phase

play11:20

basis so if we go to this website then

play11:23

of course we need an account so we can

play11:25

create this for free then we can click

play11:27

on create a new space and now we can

play11:30

give this a name for example demo then

play11:33

we can also select the stars for

play11:35

streamlit or gradual or static for

play11:37

example a flask app in this case I want

play11:39

radio and now I say public and then

play11:42

create space and then there are

play11:44

different ways to get started for

play11:46

example we can say git clone and then

play11:49

add our file and then push this or we

play11:52

can add a this remote to an existing git

play11:55

repository that we already have or we

play11:58

can do this manually so in this case I

play12:01

say app file and then create new file

play12:03

and then we need a file that is called

play12:05

app.pi and now let me copy and paste the

play12:10

code from here and now here we can

play12:13

remove share equals true and then we can

play12:18

commit this and now in this example

play12:21

since we also use the Transformers we

play12:24

need to specify this as requirement so

play12:27

here we say create new file and now in

play12:30

this case we want a require

play12:34

requirements.txt and in this case I know

play12:38

that we need torch and then the

play12:40

Transformers then again we don't have to

play12:44

specify gradual here this is already

play12:46

included then we say commit new file and

play12:51

now we go back to this side and now here

play12:53

it says your app should be running on

play12:55

this page after a few moments so let's

play12:58

wait a little bit and now after

play13:00

refreshing we have our app hosted on

play13:02

hugging face spaces and now here again

play13:05

we can select an English text and click

play13:07

on translate and now this is running the

play13:09

model in the background and this is

play13:11

working as well so this is how we can

play13:13

host a grade your app for free on

play13:15

hugging face spaces and now one more

play13:18

note if we already have a existing

play13:20

GitHub git repository for example that

play13:23

is already on GitHub then we can use

play13:25

this command by saying git remote at

play13:29

space and then this address so if we go

play13:31

to the website and here this is the full

play13:34

URL and then we add this as a separate a

play13:38

second remote and then we say git push

play13:40

and this command and now we can also

play13:42

push this to to this space again via git

play13:45

so yeah this is all I wanted to show you

play13:47

I hope you enjoyed this video leave us a

play13:49

like and consider subscribing to our

play13:51

Channel and then I hope to see you next

play13:52

time bye

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
GradioMachine LearningWeb InterfacesPythonApp DevelopmentML ModelsUser InterfaceInput TypesDeploy AppsHugging FaceTransformers