Location>code7788 >text

Gradio and Streamlit Installation and Usage Tutorials

Popularity:237 ℃/2024-11-18 17:13:57

nearest (of locations)Sealos Devbox There is a little fire 🔥, more and more guys are starting to use Sealos Devbox, one guy wrote an article read close to 20,000 people!

A small friend asked me if I could come up with an article on deploying Gradio and Streamlit, and instantly I got energized and hurriedly searched for these two things, and you don't have to tell me, it's really quite interesting. I have to write a tutorial!

What are Gradio and Streamlit?

Gradio cap (a poem)Streamlit are two open source Python libraries for quickly building machine learning (ML) applications and interactive web applications, especially for projects that need to quickly build demos and prototypes.

Gradio

Gradio provides a clean way to build web applications that allow users to interact with machine learning models. With Gradio, developers can quickly deploy models as web interfaces and users can use them without programming experience.The well-known AI painting application Stable Diffusion WebUI was built using Gradio

Applicable Scenarios:

  • Build interaction interfaces for ML models, test and tune models.
  • Rapidly build proof of concepts (PoCs) or prototypes for product demonstrations.
  • For teaching, sharing and demonstrating ML models.

Streamlit

Streamlit is another framework widely used for building data visualizations, dashboards, and ML applications, and is designed to be simple and help developers quickly move from data scripts to interactive web applications.Streamlit is unique in that it has a near-code-native syntax, which makes it easy for Python developers to describe the layout of their applications directly in code.

Applicable Scenarios:

  • Build data analysis and visualization tools for exploring data.
  • Build dashboards for ML applications to monitor and adjust model parameters in real time.
  • Rapidly build demoable machine learning applications or product prototypes.

Why Devbox

While Gradio and Streamlit are very powerful, deploying them online is not an easy task. You need to set up a Python virtual environment locally, and then go through a series of steps such as packaging the image, automatically assigning a domain name, HTTPS certificates, and so on before you can go live.Such a complicated process directly dissuades most white people

And Devbox can make these tedious processes so simple and straightforward that it's a no-brainer.

Practical Deployment Guide

1. Create a Devbox project

First open your browser and go toSealos DesktopIf you want to create a new project, find and click "Devbox", then click "New Project" button, fill in the project name, select Python as the runtime environment, select the appropriate Python version, and fill in the basic information of the project:

  • Project name
  • Choose Python as the runtime environment
  • Choosing the Right Version of Python
  • Configuration resource specifications

Since the Gradio project defaults to port 7860, we need to change the port to 7860.

Once you have completed all your selections, click "Create" in the upper right corner.

Now that you have a Debox project, clicking on "Cursor" in the image below will open the connection to the Devbox development environment using Cursor.

When you open it for the first time, you will be prompted to install the Devbox plugin, which will automatically connect you to the development environment.

Now you can see the project information created by Devbox by default on the left side of the Cursor. Isn't it very simple?Directly omit the configuration of domain name resolution, apply for SSL certificates, configure the gateway and other non-development-related tedious operation, cool!

Click on Terminal above and select "New Terminal".

Enter the following command:

# Open the python virtual environment
source . /bin/activate
# Install gradio
pip install --upgrade gradio

The execution process is shown below:

2. Preparation of sample code

After Gradio is installed, copy the demo code of Gradio into the following code:

import gradio as gr

def greet(name, intensity):
    return "Hello, " + name + "!" * int(intensity)

demo = (
    fn=greet,
    inputs=["text", "slider"],
    outputs=["text"],
)

(server_name="0.0.0.0")

Then modify the startup script to add the virtual environment activation command:

# Open the python virtual environment
source . /bin/activate

3. Direct initiation

Now we're going to go directly through the script to start Gradio:

Now go back to the Devbox interface and go to the Gradio development environment details page:

Clicking on the extranet address opens the Gradio web interface.

It's a public domain, and it's automatically configured with an HTTPS certificate.Your colleagues, friends, and leaders can preview your latest developments in real time from anywhere!It's so cool!

You've just changed the code inside Cursor and your leader can see the results right away... just say it's fast! 😁

4. Deployment to production environment

As wonderful as the above startup method is, theTypically used for development demos only

For production environments, it's better to deploy after release to automatically enjoy the advanced features of the Sealos Cloud Platform, such as self-healing, elastic scaling, and so on.

Publishing a version is easy, click "Publish Version" directly from the project details page:

Enter the version configuration information and select "Send Version".

Wait for about 10s and click "Go Live" when you see the "Success" status. Victory is ahead!

Enter the "Go Live" interface, change the port in "Network Configuration" to 7860, and click "Deploy Application" in the upper right corner:

After the deployment is complete, wait for the application status to change to running, and then click on the public address to open the demo interface of the Gradio project.

Now, we are done! It's so silky smooth.

At this point, the deployment of the Gradio project is complete.

This deployment method has the following advantages over the previous method of launching directly in the development environment:

  1. You can tailor it to your businessexpand or reduce capacity at willIt is not a problem to scale up to 10 instances. Scaling up to 10 instances is not much of a problem.

  2. Self-healing feature. Even if the service process meets some unexpected conditions and exits, it will automatically start a new instance.

  3. Resource allocation can be adjusted at will, and it is no problem to pull up to 8C16G.

As you can see, production environments are better suited for deployment in Application Management after release.

The deployment of Streamlit is exactly the same as the deployment of Gradio, replace the above installation command and demo code with the followingStreamlit The will do.

concluding remarks

Devbox streamlines the complex development and deployment process through a minimalist visual interface, enabling developers to build and release projects more intuitively and efficiently. It lowers the bar by modularizing and automating the tedious details of deployment.

Through a unified interface, Devbox provides features such as configuration management, code deployment, version control, and resource monitoring - features that allow us to focus on application development rather than getting bogged down in tedious deployment details.

It's the age of AI, and getting online quickly is the most important thing.Those tedious deployment and configuration details old me don't want to care!