gradio常用组件

gradio常用组件

1.gradio程序启动

shell 复制代码
import gradio as gr
def tab():
    pass
with gr.Blocks() as ui:
    gr.Markdown("# <center>🕵️‍♂️gradio test 🕵️‍♂️</center>")
    tab()
ui.launch(server_name='127.0.0.1', server_port=8080,show_api=False, debug=True,share=True)

2.写入html相关代码

shell 复制代码
gr.Markdown("# <center>🕵️‍♂️ Chatglm robot  🕵️‍♂️</center>")

3.文本框

shell 复制代码
# placeholder:默认提示词
text=gr.Textbox( label='user',placeholder='input question')

4. 回车触发事件

shell 复制代码
msg = gr.Textbox( label='user',placeholder='input question')
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(bot, chatbot, chatbot)

5.选择按钮框

shell 复制代码
mode_name= gr.Radio(['小红', '小白', '小胖', '小黑'], label='name', value='小红')

6.下拉框

shell 复制代码
# value:默认值
# label:组件名称
mode_type= gr.Dropdown(['1','2','3','4'], label='type', value='1')

7.点击按钮

shell 复制代码
# inputs:输入组件(输入参数),outputs:输出组件(输出参数)
# fn:触发时调用的函数
button = gr.Button("点击")
button.click(fn=update_keys, inputs=[model_key], outputs=prompt_key)

8.清空按钮

shell 复制代码
text=gr.Textbox( label='user',placeholder='input question')
clear = gr.Button("清除历史记录")
clear.click(lambda: None, None, text, queue=False)

9.监听组件

  • 输入组件变化,输出组件也变化

    shell 复制代码
    import gradio as gr
    def tab():
        mode_input=gr.Textbox( label='user',placeholder='input question')
        mode_output =gr.Textbox( label='user',placeholder='input question')
        # mode_input值改变,mode_output值也会跟着改变
        # inputs:输入组件(输入参数),outputs:输出组件(输出参数)
        # fn:触发时调用的函数
        mode_input.change(fn=lambda x:x, inputs=mode_input, outputs=mode_output)
    with gr.Blocks() as ui:
        gr.Markdown("# <center>🕵️‍♂️gradio test 🕵️‍♂️</center>")
        tab()
    ui.launch(server_name='127.0.0.1', server_port=8080,show_api=False, debug=True,share=True)
  • 输入组件变化,对应的下拉框显示的值也变化

    shell 复制代码
    # data为pandas文件
    mode_type= gr.Dropdown(types, label='type', value=types[0])
    mode_title = gr.Dropdown(list(data[data['type']==types[0]]['title'].unique()), label='title')
    mode_type.change(fn=lambda x:gr.update(choices=list(data[data['type']==x]['title'].unique())), inputs=mode_type, outputs=mode_title)

10.输出流

shell 复制代码
import time
import gradio as gr

def user(user_message, history):
    return "", history + [[user_message, None]]

def bot(history):
    bot_message = history[-1][0]
    history[-1][1] = ""
    for character in bot_message:
        history[-1][1] += character
        time.sleep(0.05)
        yield history
def robot_tab():
    chatbot = gr.Chatbot(label='robot')
    msg = gr.Textbox( label='user',placeholder='input question')
	  button = gr.Button("generate answer")
   # then: 监听事件
    button.click(user, [msg, chatbot], [msg, chatbot], queue=False).then(bot, chatbot, chatbot)
    clear = gr.Button("清除历史记录")
    clear.click(lambda: None, None, chatbot, queue=False)

with gr.Blocks() as ui:
    gr.Markdown("# <center>🕵️‍♂️ stream  🕵️‍♂️</center>")
    robot_tab()
# 使用队列提交数据
ui.queue()
ui.launch(server_name='127.0.0.1', server_port=8080,show_api=False, debug=True,share=True)

11.template

  • 模板:下代码在这个模板的基础下修改即可

    shell 复制代码
    import cv2
    import gradio as gr
    
    
    def update(mode_name,mode_age,mode_gender,mode_height):
        # image = image.convert('RGB')
        text=f'我叫{mode_name},性别{mode_gender},身高{mode_height},今年{mode_age}岁。'
        return text
        # return image
    
    def prompt_tab():
        with gr.Column():
            with gr.Row():
                image = gr.Image(type='pil', label="Image")
                with gr.Column():
                    mode_name= gr.Radio(['小红', '小白', '小胖', '小黑'], label='name', value='小红')
                    mode_age = gr.Radio(['18', '30', '40', '50'], label='age', value='18')
                    mode_gender = gr.Radio(['女', '男'], label='gender', value='女')
                    mode_height = gr.Dropdown(['160','165','170','175','180'], value='160', label='height')
            prompt = gr.Textbox(label="Prompt")
        button = gr.Button("Personal Information")
        button.click(fn=update, inputs=[mode_name,mode_age,mode_gender,mode_height], outputs=prompt)
    
    
    def image_analysis(input_image):
        output_image =input_image
        return output_image
    
    def analyze_tab():
        with gr.Column():
            with gr.Row():
                # 创建输入组件
                input_image = gr.Image(type='numpy', label="Image")
                with gr.Column():
                    # 创建输出组件
                    # output_image = gr.outputs.Image(type='pil', label="Image")
                    output_image = gr.outputs.Image(type='numpy',label="Image")
        button = gr.Button("Analyze")
        button.click(image_analysis, inputs=[input_image], outputs=[output_image])
    
    with gr.Blocks() as ui:
        gr.Markdown("# <center>🕵️‍♂️ gradio project  🕵️‍♂️</center>")
        with gr.Tab("Prompt"):
            prompt_tab()
        with gr.Tab("Analyze"):
            analyze_tab()
    
    ui.launch(server_name='127.0.0.1', server_port=8080,show_api=False, debug=True,share=True)
相关推荐
结衣结衣.7 小时前
protobuf介绍与快速上手
java·服务器·html
源力祁老师7 小时前
Odoo日志系统核心组件_logger
网络·数据库·php
郝学胜-神的一滴7 小时前
深入理解Linux套接字(Socket)编程:从原理到实践
linux·服务器·开发语言·网络·c++·程序人生·算法
向前V7 小时前
Flutter for OpenHarmony轻量级开源记事本App实战:笔记编辑器
开发语言·笔记·python·flutter·游戏·开源·编辑器
时艰.7 小时前
JVM — Java 类加载机制
java·开发语言·jvm
小小码农Come on7 小时前
QT中窗口位置、相对位置、绝对位置
android·开发语言·qt
diediedei7 小时前
C++中的适配器模式变体
开发语言·c++·算法
郝学胜-神的一滴7 小时前
Python中的Mixin继承:灵活组合功能的强大模式
开发语言·python·程序人生
叫我:松哥7 小时前
基于python强化学习的自主迷宫求解,集成迷宫生成、智能体训练、模型评估等
开发语言·人工智能·python·机器学习·pygame
晚霞的不甘7 小时前
Flutter for OpenHarmony 创意实战:打造一款炫酷的“太空舱”倒计时应用
开发语言·前端·flutter·正则表达式·前端框架·postman