当前位置: 首页 > news >正文

gradio常用组件

gradio常用组件

  • 1.gradio程序启动
  • 2.写入html相关代码
  • 3.文本框
  • 4. 回车触发事件
  • 5.选择按钮框
  • 6.下拉框
  • 7.点击按钮
  • 8.清空按钮
  • 9.监听组件
  • 10.输出流
  • 11.template

1.gradio程序启动

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相关代码

gr.Markdown("# <center>🕵️‍♂️ Chatglm robot  🕵️‍♂️</center>")

3.文本框

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

4. 回车触发事件

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

5.选择按钮框

mode_name= gr.Radio(['小红', '小白', '小胖', '小黑'], label='name', value='小红')

6.下拉框

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

7.点击按钮

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

8.清空按钮

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

9.监听组件

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

    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)
    
  • 输入组件变化,对应的下拉框显示的值也变化

    # 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.输出流

import time
import gradio as grdef 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] += charactertime.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

  • 模板:下代码在这个模板的基础下修改即可
    
    import cv2
    import gradio as grdef 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 imagedef 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_imagereturn output_imagedef 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)
http://www.lryc.cn/news/118505.html

相关文章:

  • vcode开发go
  • 聊城大学823软件工程考研
  • Spring Initailizr--快速入门--SpringBoot的选择
  • 大数据课程I1——Kafka的概述
  • 视图簇 se54 sm34 se54
  • 风丘科技将亮相 EVM ASIA 2023
  • 腾讯云服务器S6、SA3、S5、SA2等CVM实例介绍
  • 使用kickstart和anaconda自动化安装centos系统
  • Delphi7通过VB6之COM对象调用PowerBASIC写的DLL功能
  • 中电金信:ChatGPT一夜爆火,知识图谱何以应战?
  • 单细胞分类和预测任务
  • 那些年的Webview开发经验记录
  • ssh-keygen详解
  • 自动方向识别式 LSF型电平转换芯片
  • 轻量的工作流引擎:告别低效,创造新高!
  • Linux-mysql安装
  • 山东布谷科技直播平台搭建游戏开发技术分享:数据存储的重要意义
  • MyBatisPlus的介绍
  • 麻省理工学院利用水泥和炭黑制造出超级电容器
  • 【Spring Cloud Alibaba】Linux安装RocketMQ以及RocketMQ Dashboard可视化工具
  • 【杂谈】电脑城的消失:时代变迁中的一段峡谷
  • Webpack怎么使用?
  • 异步编排CompletableFuture
  • linux_常用命令
  • Mac OS键盘常用快捷键
  • 【腾讯云 Cloud Studio 实战训练营】通过云IDE构建Web3项目
  • 湖南麒麟系统非正常关机导致VNC启动失败原因分析
  • 机器学习---监督学习和非监督学习
  • 【OS】请问,一个需要运行内存10GB的游戏,可以运行在32位物理内存为64GB的电脑上吗?
  • C语言 冒泡排序