Gradio Getting Started Example

    With the popularity of robot dialogue frameworks such as chat-gpt, a framework called gradio has also become popular. This framework can open an http service and has an input and output interface, allowing conversational artificial intelligence projects to run quickly.

    Gradio claims to be able to quickly deploy AI visualization projects.

    Let's take a look at two examples. First, we need to install the gradio library.

pip install gradio

    Then write the following code, the user inputs a string xxx, after submitting, output a hello, xxx.

import gradio as gr


def hello(name):
    return "hello," + name + "!"


def launch():
    demo = gr.Interface(fn=hello, inputs='text', outputs='text')
    demo.launch()


if __name__ == '__main__':
    launch()

    Run this code, you can open port 7860 to listen to http service, and visit http://localhost:7860 with your browser, you can open the following interface:

     Write another example about image recognition, the code is as follows:

import torch
from PIL import Image
from torchvision import transforms
import gradio as gr
import json

with open('imagenet-simple-labels.json', 'r') as load_f:
    labels = json.load(load_f)
model = torch.hub.load("pytorch/vision:v0.6.0", "resnet18", pretrained=True).eval()


def predict(inp):
    inp = Image.fromarray(inp.astype("uint8"), "RGB")
    inp = transforms.ToTensor()(inp).unsqueeze(0)
    with torch.no_grad():
        prediction = torch.nn.functional.softmax(model(inp)[0], dim=0)
    return {labels[i]: float(prediction[i]) for i in range(1000)}


inputs = gr.Image()
outputs = gr.Label(num_top_classes=3)
demo = gr.Interface(fn=predict, inputs=inputs, outputs=outputs)

if __name__ == '__main__':
    demo.launch()


    Running the code will download the pytorch/vision:v0.6.0 version and download a resnet18 model file: resnet18-f37072fd.pth to the .cache\torch\hub\checkpoints\ directory in the user directory.

    Run the print information as follows:

    We open the browser http://localhost:7860 and select the leopard and dog pictures we prepared in advance on the interface:

    Leopards are identified here, showing cheetah.

    Try it again with a different dog:

    The identification result is a Labrador.

    The three most likely results are set in the code, outputs = gr.Label( num_top_classes=3 ), so the three most likely situations are listed here.

     When the above code is run, a warning is reported:

D:\Program Files\Python\Python310\lib\site-packages\torchvision\models\_utils.py:208: UserWarning: The parameter 'pretrained' is deprecated since 0.13 and may be removed in the future, please use 'weights' instead.
  warnings.warn(
D:\Program Files\Python\Python310\lib\site-packages\torchvision\models\_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing `weights=ResNet18_Weights.IMAGENET1K_V1`. You can also use `weights=ResNet18_Weights.DEFAULT` to get the most up-to-date weights.
  warnings.warn(msg)

    It means that when torch.hub.load loads the model, the pretrained parameter is out of date, you can use weights=ResNet18_Weights.DEFAULT instead.

    After modifying the code, the warning will no longer be reported. As follows:

    As an example on the official website, there is a file in the article from https://git.io/JJkYN . It is no longer available for download, but it can be found directly on github: https://raw.githubusercontent.com/anishathalye/imagenet-simple-labels /master/imagenet-simple-labels.json

    Here it is downloaded in advance and then read through json. The content is 1000 target tags. 

Guess you like

Origin blog.csdn.net/feinifi/article/details/130816960