使用gradio搭建服务(一)

文章展示了如何利用Gradio库创建交互式应用程序,包括处理文本、图像和复选框输入,以及展示文本和图像输出。示例中包含了图像翻转和数据框展示的功能,可用于简单的UI原型设计或模型演示。

使用参考gradio官方教程

  1. 三种基本输入:image,text,checkbox
  2. 多个输出,gr.outputs实现
import gradio as gr
import numpy as np
def predict(prompt,image,is_rain):
    print('prompt',type(prompt))
    print('image',type(image))
    print('is_rain',type(is_rain))

    image = np.fliplr(image)

    if prompt:
        output1 = prompt.upper()
        output2 = prompt.lower()
    else:
        output1=0
        output2 = 0
    return output1, output2, image

outputs = [
    gr.outputs.Textbox(label="Uppercase"),
    gr.outputs.Textbox(label="Lowercase"),
    gr.outputs.Image(type = 'numpy',label="Flipped Image")

]

demo = gr.Interface(
    fn=predict,
    inputs=[ "text",'image','checkbox'],
    outputs=outputs,
)
demo.launch()

输出

prompt <class 'str'>
image <class 'numpy.ndarray'>
is_rain <class 'bool'>

在这里插入图片描述
图片左边3个框是输入部分,显示的名字为传入参数的名字。右边的三个框是输出部分,名字是output中定义的label

  • 如果要同一局域网的其他设备访问服务,设置最后启动的launch(server_name="0.0.0.0"),访问的时候将127.0.0.1更换成运行设备的ip

显示表格

import gradio as gr
import numpy as np

import gradio as gr
import pandas as pd

def display_table(image):
    data = {'Name': ['Alice', 'Bob', 'Charlie'],
            'Age': [25, 30, 35],
            'City': ['New York', 'London', 'Paris']}
    df = pd.DataFrame(data)
    
    return df


# iface.launch()

if __name__ == "__main__":
    # demo.launch()
    demo = gr.Interface(fn=display_table,inputs=['image'], outputs="dataframe")
    demo.launch(server_name="0.0.0.0") ## ip地址+端口号

image
inputs=gr.Image(type="pil")

参考示例

  1. https://blog.youkuaiyun.com/sinat_26917383/article/details/133950480
  2. https://www.gradio.app/guides/image-classification-in-pytorch
  3. https://blog.youkuaiyun.com/2201_75761617/article/details/132385304
  4. 搭建多模态问答机器人https://github.com/airaria/Visual-Chinese-LLaMA-Alpaca/blob/main/scripts/inference/gradio_demo.py
使用Gradio搭建简单web页面可以按照以下步骤进行: ### 环境准备 需要确保已经安装了Python,以Python 3.10为例,还需要安装Gradio库。可以在Python安装目录下,在文件资源管理器的路径中输入`cmd`打开命令行,然后执行`pip install gradio`来安装Gradio库[^1]。 ### 创建Python文件 创建个名为`app.py`的Python文件,并将其Python解释器设置为之前安装好的Python 3.10 [^1]。 ### 编写示例代码 下面提供两种不同功能的示例代码: #### 简单问候应用 ```python import gradio as gr def greet(name): return f"Hello, {name}!" iface = gr.Interface(fn=greet, inputs="text", outputs="text", title="Greeting App") iface.launch() ``` 在上述代码中,定义了个`greet`函数,它接受个字符串参数`name`,并返回个包含问候语的字符串。然后使用`gr.Interface`创建了个接口对象`iface`,指定了函数`greet`、输入类型为文本、输出类型为文本以及页面标题。最后调用`iface.launch()`启动应用 [^1]。 #### NER实体识别应用 ```python import gradio as gr import os os.system('python -m spacy download en_core_web_sm') import spacy from spacy import displacy nlp = spacy.load("en_core_web_sm") def text_analysis(text): doc = nlp(text) html = displacy.render(doc, style="dep", page=True) return html iface = gr.Interface(fn=text_analysis, inputs="text", outputs="html", title="NER Entity Recognition") iface.launch() ``` 此代码首先下载并加载了`en_core_web_sm`的Spacy模型,定义了`text_analysis`函数用于对输入的文本进行分析,并使用`displacy`渲染依赖解析结果。接着创建了Gradio接口,指定输入为文本,输出为HTML,最后启动应用 [^2]。 ### 运行代码 在命令行中,进入`app.py`所在的目录,执行`python app.py`,Gradio会自动启动个本地服务器,并在浏览器中打开相应的web页面。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值