Project Demos with Gradio

Look at this tutorial if you are creating a demo to present your final project!

Why a Demo?

Before you start creating your demo, think about why you would like to do a demo. A demo is a nice way to present your work because it allows the audience to interact directly with your project. You should make sure that your demo is interactive. For instance, a demo where users click a “test” button and see the confusion matrix for your pre-defined test set isn’t a great way to present your work. It would be better to have a poster with the confusion matrix and other information about your project.

For instance, imagine you are creating a sentiment classification model.

  • A bad demo would have a “test” button, which would show the confusion matrix on your pre-defined test set. Why is this bad? The same information would be better presented on a poster, along with more details about other components of your project.
  • A good demo would allow users to enter their own sentence, and would show the probability predicted by both of the models that you have trained, a Naive Bayes model and a BERT model. Why is this good? The user gets to see for themselves how your model performs, which they wouldn’t be able to do from just looking at some metrics on paper. They can test the limits of your model outside of a constrained environment.

Saving your Models

If your model takes more than a second to train (and it probably does!), you shouldn’t train your model when people click “submit” in your demo. This means that you should have a version of your model saved to a file and load it when you initialize your demo. Here are some instructions on how to do that with a pytorch model, and Here are some instructions on how to do that with a sklearn model. If you are using Hugging Face AutoModel and Trainer code, you might want to look at these instructions. If you’ve written your own custom modeling code, you might need to use a library like pickle to save the state of your model to disk.

Hugging Face Spaces

Hugging Face spaces allow you to host web applications for free that show off your NLP projects! Your gradio demo should be deployed in a space within the midd457 organization. I have created spaces for students who have indicated that they would like to do demos. If you’ve changed your mind, I can make a space for you too!

You can edit files directly within your space. If you know how to use git, that is also an option if you prefer - the space is just a git repo.

Gradio

If you’d like a demo to work off of, check out the Demo of Demos space that I created in the midd457 Hugging Face organization.

If you’d like more guidance, the Hugging Face NLP Course has a nice tutorial on creating Gradio applications. Focus on the examples that use the types of inputs and outputs that you’d like to use in your demo.

Things to look out for

If you need libraries installed, they should be added to requirements.txt. You might be able to just put the library name, but if there are errors, make sure you are using the same version that is used in your development environment.