Build Machine Learning Applications Easily with Gradio in Python

NeuralNine · Intermediate ·📐 ML Fundamentals ·2y ago

Key Takeaways

The video demonstrates how to build machine learning applications with graphical user interfaces using Gradio in Python, training a handwritten digit recognition model using TensorFlow and loading the MNIST dataset from TensorFlow's datasets library. It utilizes tools such as Gradio, TensorFlow, and Keras to create a simple UI for the model.

Full Transcript

what's going on guys welcome back in this video today we're going to learn how to easily build a machine learning application with a graphical user interface using gradio and we're going to do that by using a handwritten digit recognition as an example here so let us get right into [Music] it all right so as a motivational preview up front this is what we're going to end up with we're going to have a canvas on the left side and we're going to have the model predictions the model output on the right side and we can use this canvas to now draw something for example the digit three and then we're going to get some output here the prediction here at the top and then the likelihood of all the other digits in this case it's obviously a three if I do something like this it will still say it's a three but it's not as certain anymore if I just draw some some nonsense like this you can see it says it's a seven but maybe a two um I can try to draw a five again and you can see it recognized this is a five maybe I can go with an eight there you go so you can see this is what we're going to end up with and the main focus is on gradio building that user interface so this is going to be happening in two parts here we're going to train a model uh to do the handwritten digit recognition and then we're going to build a gradio application very easily that allows us to do this to use this in the browser all right so we're going to start by installing two packages that we're going to need for this video today for that we're going to open up the command line the terminal and we're going to type pip or pip 3 install and then we're going to need gradio which is what we're going to use for the actual application for the user interface so pip install gradio and we're going to need some machine learning framework you can go with tensorflow pytorch psychic learn if you want to do traditional machine learning it doesn't really matter because how you get to the predictions is Irrelevant for the gradio application the gradio application is going to give you an image and with that image you want to do some prediction and you want to return a result so you want to return uh um 40 individual classes How likely it is that they are the correct class so we're going to use in this video today tensor flow but you can go also as I said with psychic learn if you want to do some uh support Vector machine or some random Forest classifier whatever you want to go with but we're going to go with tensorflow here so we're going to install gradio and tensorflow and once these two packages are installed we're going to start by creating a file called training. py as I already mentioned we're going to split this into two parts here we're going to train the model first that is going to do the handwritten digit recognition and in the second part we're going to build the actual application around that model so we're going to use the uh model training code only once and then we're going to use the application which is going to load the model uh from a file that's the basic idea so what we're going to do here is we're going to say import tensorflow SF and then from tensorflow doas we're going to import layers and uh mod mods so in my case here py charm doesn't recognize the path for some reason but it still works you can check if it works on your side by just running and if you don't get a an error message this is just a warning if you don't get an error message it works so what we're going to do now is we're going to load the amness data set so the digits data set the handwritten digits uh data set from uh Kos itself so what we're going to do here is we're going to say train images and train labels then we're going to have test images test labels and this is going to be loaded from tensorflow docas DOD sets and then mist and then loore data so this is just going to load the data set automatically from Kos we don't need to import some uh CSV file we can just use Kos data sets directly here and we're going to have already a trained test split so we're going to have I think 60,000 images here in the training set and I think 10,000 images uh in the testing set and we can now just basically go ahead and um normalize the images so we're going to divide by 255 because we have the RGB values we divide by 255 to get values between 0 and 1 instead of between 0 and 255 um and how we do that is we say train images is equal to train images we're going to reshape here uh 60,000 instances 28 pixels 28 pixels one channel um and we want to have all of this as type float so that we can actually do the uh float 32bit we want to do um a division and because of that we want to have a float so divide it by 255 and then we can do do the same thing for the test images here test images and then what we can do here is we can take the labels and we can turn them into categorical um into categorical variables so instead of having just a label instead of just having 1 2 3 4 we're going to have for the individual labels uh a categorical representation so I can print it here so you can see what the difference is train labels and then we're going to say train labels is going to be come on train labels is going to be equal to tensorflow caras utils 2core categorical train labels and then you're going to see if I print it again it's going to look different uh what's the problem here cannot reshape ah because here we only have 10,000 uh but yeah here you can see what we have we have just um for every instance for every image we have a digit 5 Z 4 whatever and now we have here a one hot encoded representation so basically it's a zero for all the uh digits that are not the correct digit and then it's a one for the digit that's the correct digit so we just have a different representation which is more useful uh for the model that we want to build here so we can take this and we can copy it we can change this to test labels is equal to two categorical test labels um and then basically what we need to do is we just need to build a neural network architecture that is going to be trained on this data to make the prediction so we're going to say model is equal to models. sequential and then we're going to just um Define a simple architecture of uh starting with a convolutional layer so we're going to say layers.com on 2D we're going to have 32 here kernel size of 3x3 and then we're going to have this an activation function here R rectified linear unit the input shape is of course going to be equal to 28 281 um and then we're going to add some Max pooling layers here so we're going to have convolutional layer Max pooling convolutional Max pooling convolutional then we're going to flatten it then we're going to have some dense layers uh you can also change this if you want to you can keep it simpler if you wanted to train faster in my case I'm just going to go now with a Max pooling to D layer um and then we're going to basically uh we can copy this we can paste this we can change this here to 64 and of course we don't want to have the input shape anymore uh we're going to keep that layer here and what we're going to do then is we're going to have one more of these and then afterwards we're going to have um a flatten layer so layers. flatten and then we're going to just add for the complexity a dense layer with 64 neurons activation is going to be relu and then finally we're going to have our output layers so we're going to say model add layers dense with 10 output neurons because we have 10 digits and we want to have the probability for each individual uh digit all adding up to one and because of that the activation function is obviously going to be softmax because softmax does exactly that it takes the output and uh makes sure that it is um one when you add up all the neurons uh which basically gives us the probabilities uh and now all we need to do is we need to say model compile we're going to use as an Optimizer here the atom Optimizer as a loss function we're going to use the categorical cross entropy and the metric that we want to keep track of while training is going to be the accuracy metric uh and then we're going to say model fit train images train labels we're going to do this for five Epoch we're going to do this with a batch size of 64 and then we're going to have validation split off 0.1 and of course what you could do now here as well is you could evaluate the model with a test set we're not going to do this we're just going to keep it uh simple now because the focus is on the application but what we want to do in the end is we want to save the model so we want to save it as model. H5 for example and then we can just run this and wait for it to finish training it will take some time uh but first of all we have a problem metric is plural metrics now it should work there you go it's training we can skip this part and jump uh directly into the application okay so the training is done it tells tells me here that the format H5 is considered Legacy so probably you should go with caras instead uh I'm going to keep it like this now doesn't really matter for this uh particular video here now we have the model we have it as a file and all we need to do is we need to build an application around it because this model now is capable of recognizing handwritten digits so we can take an image we can pre-process it we can feed it into the model and we can get a most likely correct output the problem is that you cannot ship this to a user you can not give them the application or you cannot give them an application because you just have the model and the model doesn't do much you need something that uses the model and this is where gradio comes into play because now what we're going to do is we're going to say uh let's say app.py here or main py whatever you want to call it here now we're going to import gradio SG and we're going to import tensor flow ASF the first thing we want to do is want to load the model so the model is going to be loaded by TF Kos models load model model. H5 and now what we're going to do is we're going to say um a function here is going to be called recognize digit and the input is going to be an image and what we want to do in this function is we want to say okay if the image that we get here is not none um if that is the case so if the image is not none what we're going to do is we're going to say the image is going to be reshaped in the same way that our training data was reshaped so we're going to say here uh one image 28 pixels * 28 pixels and one channel as type float 32 divided by 255 this is exactly what we did when training the model which is why we do it here when applying the model as well and then what we do is we say prediction is whatever the model predicts this image to be so model predict image like this uh and this prediction now we can take it and we can transform it into a dictionary of probability so we can return as a result of this function here uh string of I which is uh the key and the value is going to be float of prediction zero I for I in range and then 10 because we have 10 possible digits otherwise we're just going to return an empty string so this now here is our function again if you are using pytorch if you're using psychic learn it doesn't matter you can just change this function the important thing is the function needs to take an image as an input and it needs to return some string because the string is going to be then displayed uh in a label and with this simple function now all we have to do is we have to define a very very simple gradio user interface to use this in an actual application so we're going to say here iFace or interface is going to be a gr interface and this interface will have the function being equal to recognized digit the inputs are going to be a gr image the shape of this image those those are all parameters from gradio so they are predefined the shape of the image is 28 * 28 the image mode is L and then we're going to say invert colors equals true and the important thing now here is the source and I think my camera is blocking this now uh the source has to be canvas because when you say Source equals canvas it basically provides you with a canvas that you can draw on which is exactly what we want to do otherwise you would have to upload images um all right and then what we need is we need an output label so gr label and we're going to say here number top classes equals and we can decide here now how many classes to display only the top three guesses all the guesses so if I say three for example we're going to only see the best three predictions for the given drawing and of course we need to also set your life equals true um and and to run this we can say ifas launch and if I didn't make any mistakes this should Now launch and be hosted in Local Host here and if I run this now you can see that this is what we end up with I can draw on this canvas and I can see here the results of the predictions I can draw some nonsense then it's less clear um yeah I can do it like this maybe I can draw a small Point down here you can see I get the top three guesses now what I can also do is I can go ahead and say give me all 10 then I would get more information but I would also get a more verbose output uh which is the same thing but you get more information but it's ALS it's also maybe too much information so you can see it's very confused if I just add a pixel down here doesn't really know what to think about this uh if I draw something like this it will tell me it's a two if I draw something like this five but if I change it maybe like like this oh it's still a five um yeah this is a six and we can also play around and see okay at what point does a three for example uh become a an eight you know maybe if I do this now it's an eight so at what point does this happen you can play around with this a little bit uh okay now this is very fast but if I add just a little bit here you can see how the probabilities change immediately so just very minor changes here here uh change a lot already uh yeah and you can do that with different components with different UI elements this is a simple example of just having a model that recognizes handwritten digits and immediately turning it into an application that someone can use so that's it for today's video I hope you enjoyed it and hope you learned something if so let me know by hitting a like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this Channel and hit the notification Bell to not miss a single future video for free other than that thank you much for watching see you on the next video and bye

Original Description

Today we learn about Gradio, which allows us to easily build machine learning applications with graphical user interfaces. ◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾ 📚 Programming Books & Merch 📚 🐍 The Python Bible Book: https://www.neuralnine.com/books/ 💻 The Algorithm Bible Book: https://www.neuralnine.com/books/ 👕 Programming Merch: https://www.neuralnine.com/shop 💼 Services 💼 💻 Freelancing & Tutoring: https://www.neuralnine.com/services 🌐 Social Media & Contact 🌐 📱 Website: https://www.neuralnine.com/ 📷 Instagram: https://www.instagram.com/neuralnine 🐦 Twitter: https://twitter.com/neuralnine 🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/ 📁 GitHub: https://github.com/NeuralNine 🎙 Discord: https://discord.gg/JU4xr8U3dm
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from NeuralNine · NeuralNine · 0 of 60

← Previous Next →
1 Visualizing Stock Data With Candlestick Charts in Python
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
2 Python Beginner Tutorial #1 - Installation and First Program
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
3 Python Beginner Tutorial #2 - Variables and Data Types
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
4 Python Beginner Tutorial #3 - Operators and User Input
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
5 Python Beginner Tutorial #4 - If Statements and Conditions
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
6 Python Beginner Tutorial #5 - Loops
Python Beginner Tutorial #5 - Loops
NeuralNine
7 Python Beginner Tutorial #6 - Sequences and Collections
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
8 Python Beginner Tutorial #7 - Functions
Python Beginner Tutorial #7 - Functions
NeuralNine
9 Python Beginner Tutorial #8 - Exception Handling
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
10 Python Beginner Tutorial #9 - File Operations
Python Beginner Tutorial #9 - File Operations
NeuralNine
11 Python Beginner Tutorial #10 - String Functions
Python Beginner Tutorial #10 - String Functions
NeuralNine
12 Python Intermediate Tutorial #1 - Classes and Objects
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
13 Python Intermediate Tutorial #2 - Inheritance
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
14 Python Intermediate Tutorial #3 - Multithreading
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
15 Python Intermediate Tutorial #4 - Synchronizing Threads
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
16 Python Intermediate Tutorial #5 - Events and Daemon Threads
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
17 Python Intermediate Tutorial #6 - Queues
Python Intermediate Tutorial #6 - Queues
NeuralNine
18 Python Intermediate Tutorial #7 - Sockets and Network Programming
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
19 Python Intermediate Tutorial #8 - Database Programming
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
20 Python Intermediate Tutorial #9 - Recursion
Python Intermediate Tutorial #9 - Recursion
NeuralNine
21 Python Intermediate Tutorial #10 - XML Processing
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
22 Python Intermediate Tutorial #11 - Logging
Python Intermediate Tutorial #11 - Logging
NeuralNine
23 Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
24 Python Data Science Tutorial #2 - NumPy Arrays
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
25 Python Data Science Tutorial #3 - Numpy Functions
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
26 Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
27 Python Data Science Tutorial #5 - Subplots and Multiple Windows
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
28 Python Data Science Tutorial #6 - Matplotlib Styling
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
29 Python Data Science Tutorial #7 - Bar Charts with Matplotlib
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
30 Python Data Science Tutorial #8 - Pie Charts with Matplotlib
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
31 Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
32 Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
33 Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
34 Python Data Science Tutorial #12 - Pandas Series
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
35 Python Data Science Tutorial #13 - Pandas Data Frames
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
36 Python Data Science Tutorial #14 - Pandas Statistics
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
37 Python Data Science Tutorial #15 - Pandas Sorting and Functions
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
38 Python Data Science Tutorial #16 - Pandas Merging Data Frames
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
39 Python Data Science Tutorial #17 - Pandas Queries
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
40 Python Machine Learning Tutorial #1 - What is Machine Learning?
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
41 Python Machine Learning Tutorial #2 - Linear Regression
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
42 Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
43 Python Machine Learning #4 - Support Vector Machines
Python Machine Learning #4 - Support Vector Machines
NeuralNine
44 Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
45 Python Machine Learning Tutorial #6 - K-Means Clustering
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
46 Python Machine Learning Tutorial #7 - Neural Networks
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
47 Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
48 Generating Poetic Texts with Recurrent Neural Networks in Python
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
49 Stock Portfolio Visualization with Matplotlib in Python
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
50 Analyzing Coronavirus with Python (COVID-19)
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
51 Making Text Images Readable Again with Python and OpenCV
Making Text Images Readable Again with Python and OpenCV
NeuralNine
52 Neural Networks Simply Explained (Theory)
Neural Networks Simply Explained (Theory)
NeuralNine
53 Motion Filtering with OpenCV in Python
Motion Filtering with OpenCV in Python
NeuralNine
54 Top 5 Programming Languages To Learn in 2020
Top 5 Programming Languages To Learn in 2020
NeuralNine
55 Simple TCP Chat Room in Python
Simple TCP Chat Room in Python
NeuralNine
56 Image Classification with Neural Networks in Python
Image Classification with Neural Networks in Python
NeuralNine
57 Edge Detection with OpenCV in Python
Edge Detection with OpenCV in Python
NeuralNine
58 S&P 500 Web Scraping with Python
S&P 500 Web Scraping with Python
NeuralNine
59 Simple Sentiment Text Analysis in Python
Simple Sentiment Text Analysis in Python
NeuralNine
60 Introduction - Algorithms & Data Structures #1
Introduction - Algorithms & Data Structures #1
NeuralNine

This video teaches how to build machine learning applications with graphical user interfaces using Gradio in Python. It covers training a handwritten digit recognition model using TensorFlow and loading the MNIST dataset from TensorFlow's datasets library. The video demonstrates how to create a simple UI for the model using Gradio and customize it to display the input image and predictions.

Key Takeaways
  1. Install Gradio and TensorFlow using pip
  2. Create a file called training.py
  3. Import necessary libraries from TensorFlow
  4. Load the MNIST dataset from TensorFlow's datasets library
  5. Train images and test images
  6. Normalize images by dividing by 255
  7. Reshape images to 28x28x1
  8. Convert labels to categorical variables
  9. Build a neural network architecture with convolutional and dense layers
  10. Train model with 5 epochs, batch size 64, and 10% validation split
💡 Gradio provides an easy-to-use interface for building machine learning applications with graphical user interfaces, allowing for rapid prototyping and deployment of models.

Related Reads

📰
AI Training Data Services: The Complete Enterprise Guide to Building Accurate AI Models (2026)
Learn how to build accurate AI models with the right training data services for your enterprise, and why it matters for AI success
Medium · Machine Learning
📰
Machine Learning Concepts Explained #4: Features and Labels
Learn the difference between features and labels in machine learning and their role in supervised models
Medium · AI
📰
Machine Learning Concepts Explained #4: Features and Labels
Learn the difference between features and labels in machine learning and their role in supervised learning
Medium · Machine Learning
📰
Machine Learning Concepts Explained #4: Features and Labels
Learn the difference between features and labels in machine learning and their role in training supervised models
Medium · Data Science
Up next
Dropout in Deep Learning
AnuTech-CH
Watch →