LLM--大模型快速展示(Gradio)

安装:pip install gradio

初始

  • 代码模版---> 创建页面模版
python 复制代码
import gradio as gr 

# 回调函数:具体处理函数
def greet(text):
    return "Hello World " + text + " !"

# 注意:inputs、outputs可以选择为text、image、音频
inface = gr.Interface(fn=greet, inputs="text", outputs="text")

# 创建
inface.launch()
  • 结果

创建输入与输出组件

了解即可,用到查

python 复制代码
# 定义输入组件列表 
input_list = [
    gr.Audio(sources=["microphone", "upload"], type="numpy", label="Audio File"),
    gr.Checkbox(label="Checkbox"),
    gr.ColorPicker(label="Color Picker"),
    gr.Dataframe(headers=["Col1", "Col2", "Col3"], label="Dataframe"), # 添加了列名以便显示
    gr.Dropdown(choices=["option 1", "option 2", "option 3"], label="Dropdown"),
    gr.File(label="File", type="binary"),
    gr.Image(sources=["webcam", "upload"], label="Image"),
    gr.Number(label="Number"),
    gr.Radio(choices=["option 1", "option 2", "option 3"], label="Radio"),
    gr.Slider(minimum=0, maximum=10, label="Slider", step=5),
    gr.Textbox(label="Textbox", lines=3, max_lines=7, placeholder="Placeholder"),
    gr.TextArea(label="Text Area", lines=3, max_lines=7, placeholder="Placeholder"),
    gr.Video(sources=["webcam", "upload"], label="Video"),
]

# 定义输出组件列表 
output_list = [
    gr.Textbox(label="Audio outputs", lines=7),
    gr.Textbox(label="Checkbox ouputs"),
    gr.Textbox(label="Color Picker ouputs"),
    gr.Textbox(label="Dataframe ouputs"),
    gr.Textbox(label="Dropdown ouputs"),
    gr.Textbox(label="File ouputs"),
    gr.Textbox(label="Image ouputs"),
    gr.Textbox(label="Number ouputs"),
    gr.Textbox(label="Radio ouputs"),
    gr.Textbox(label="Slider ouputs"),
    gr.Textbox(label="Textbox ouputs"),
    gr.Textbox(label="Text Area ouputs"),
    gr.Textbox(label="Video ouputs"),
]

# 创建页面
demo = gr.Interface(
    fn=process_all_inputs, 
    inputs=input_list, 
    outputs=output_list,
    title="Gradio 全组件演示",
    description="测试所有可用的输入和输出组件"
)

注意

  • 也可以不输入,写None即可
  • 不会问AI

布局容器组件

我学习感觉,适合个人搭建简单的页面,不适合复杂的,更不适合企业

  • 代码
python 复制代码
import gradio as gr 

gr.Blocks()
gr.Row()
gr.Column()
gr.Tab()
gr.Group()
gr.Accordion()
  • 讲解
组件 用途 说明
Blocks 根容器 所有自定义界面的基础,提供最大灵活性
Row 水平布局 内部组件横向排列
Column 垂直布局 内部组件纵向排列
Tab 标签页 创建可切换的多个页面
Group 分组 将组件视觉分组,无边框
Accordion 折叠面板 可展开/收起的分组容器
  • 一个简单的

代码

python 复制代码
import gradio as gr 

with gr.Blocks(title='演示') as demo: 
    with gr.Tab(label='txt2img'):
        with gr.Row():  # 水平布局
            with gr.Column(scale=15):   # 竖直布局
                txt1 = gr.Textbox(lines=2, label="")
                txt2 = gr.Textbox(lines=2, label="")
            with gr.Column(scale=1, min_width=1):
                button1 = gr.Button(value="1")
                button2 = gr.Button(value="2")
                button3 = gr.Button(value="3")
                button4 = gr.Button(value="4")
            with gr.Column():
                generate_button = gr.Button(value="Generate")
                with gr.Row():
                    dropdown1 = gr.Dropdown(["1", "2", "3"], label="Style1")
                    dropdown2 = gr.Dropdown(["1", "2", "3"], label="Style2")
                    
demo.launch()
  • 效果

对话

gr.ChatInterface:专为聊天机器人优化的快速开发工具

参数 类型 说明
fn 函数 必需,处理消息的函数,签名 (message, history) → str message:当前用户输入 history:历史输入
chatbot gr.Chatbot 自定义聊天窗口配置(高度、初始消息等)
textbox gr.Textbox 自定义输入框配置
title str 页面标题
theme str 主题样式("default" , "soft" , "monochrome" 等)
examples list 示例问题列表,用户可点击快速提问
retry_btn str/None 重试按钮文字,None 则隐藏
submit_btn str 提交按钮文字
undo_btn str/None 撤销最后一轮对话按钮
clear_btn str/None 清空全部对话按钮

案例

  • 代码
python 复制代码
import gradio as gr 

def Chatbot(message, history):
    
    response = "我是AI助手"
    
    return response

demo = gr.ChatInterface(
    fn=Chatbot,
    title="AI助手",
    examples=["你好", "介绍一下自己"],
    theme="soft"
)

demo.launch()
                    
  • 结果

参考资料

相关推荐
阿正呀2 分钟前
Redis怎样实现本地缓存的高效失效通知
jvm·数据库·python
昨夜见军贴06163 分钟前
IACheck与AI报告审核,开启供应商资质核验报告审核新篇章
人工智能
Flying pigs~~10 分钟前
Agent 完整面试指南:原理、框架、架构模式
大模型·prompt·agent·rag·agent架构·人工只能
m0_7263658317 分钟前
Ai漫剧系统 几分钟,让AI 把一篇小说变成了一部漫剧成片:从剧本到视频的全流程系统实现
人工智能·语言模型·ai作画·音视频
2501_9012005317 分钟前
mysql如何设置InnoDB引擎参数_优化innodb_buffer_pool
jvm·数据库·python
AIwenIPgeolocation29 分钟前
出海应用合规与风控平衡术:可信ID的全球安全实践
人工智能·安全
WordPress学习笔记30 分钟前
镌刻中式美学的高端WordPress主题
大数据·人工智能·wordpress
_.Switch31 分钟前
东方财富股票数据JS逆向:secids字段和AES加密实战
开发语言·前端·javascript·网络·爬虫·python·ecmascript
Mr_sst35 分钟前
Claude Code 部署与使用保姆级教程(2026 最新)
python·ai
直奔標竿38 分钟前
Java开发者AI转型第二十七课!Spring AI 个人知识库实战(六)——全栈闭环收官,解锁前端流式渲染终极技巧
java·开发语言·前端·人工智能·后端·spring