随着chat-gpt等机器人对话框架的流行,让一个名为gradio的框架也火热起来,这个框架可以开启一个http服务,并且带输入输出界面,可以让对话类的人工智能项目快速运行。
gradio号称可以快速部署ai可视化项目。
下面通过两个示例来感受一下,首先我们需要安装gradio库。
pip install gradio
接着编写如下的代码,用户输入一个字符串xxx,提交之后,输出一个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()
运行这段代码,可以开启7860端口监听http服务, 浏览器访问http://localhost:7860,可以打开如下界面:
再编写一个示例,是关于图像识别的,代码如下:
import torch
from PIL import Image
from torchvision import transforms
import gradio as gr
import jso