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()
                    
  • 结果

参考资料

相关推荐
小雨下雨的雨1 天前
井字棋AI机器人实现详解 - Minimax算法实战-鸿蒙PC Electron框架完成
前端·人工智能·算法·华为·electron·鸿蒙
我没胡说八道1 天前
高校论文AI检测优化工具对比研究与实测分析(2026)
人工智能·深度学习·机器学习·计算机视觉·aigc·论文
秦亚伟1 天前
AI浪潮重塑融资租赁行业新格局
人工智能
love530love1 天前
LiveTalking 数字人项目 Windows 部署完全指南(EPGF 架构)
人工智能·windows·python·架构·livetalking·epgf
元启数宇1 天前
喷淋AI布点实战:8小时人工布点→20分钟自动出图
人工智能
哈哈,柳暗花明1 天前
人工智能专业术语详解(H)
人工智能·专业术语
圣殿骑士-Khtangc1 天前
AI 编程工具 2026 实战横评:Cursor 3 vs Claude Code vs Copilot,开发者选型完全指南
人工智能·copilot
云器科技1 天前
云器Lakehouse 2026年5月版本发布:拥抱 AI Agent,重塑数据智能开发新范式
人工智能
小鹰-上海鹰谷-电子实验记录本1 天前
第六届党建引领科创生态座谈会 | 邓光辉博士出席分享AI赋能创新药科研新范式
人工智能·ai·电子实验记录本·药企合规
遇事不決洛必達1 天前
【Python基础】GIL 锁是什么及其对爬虫的影响
爬虫·python·线程·进程·gil锁