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

参考资料

相关推荐
Li emily1 天前
解决了加密货币api多币种订阅时的数据乱序问题
人工智能·python·api·fastapi
山川绿水1 天前
bugku——PWN——overflow2
人工智能·web安全·网络安全
程序员cxuan1 天前
微信读书官方发了 skills,把我给秀麻了。
人工智能·后端·程序员
2301_781571421 天前
Golang格式化输出占位符都有什么_Golang fmt占位符教程【通俗】
jvm·数据库·python
fake_ss1981 天前
AI时代学习全栈项目开发的新范式
java·人工智能·学习·架构·个人开发·学习方法
asdzx671 天前
使用 Python 为 PDF 添加页码 (详细教程)
python·pdf·页码
nassi_1 天前
对AI工程问题的一些思考
大数据·人工智能·hadoop
AI技术控1 天前
《Transformers are Inherently Succinct》论文解读:从“能表达什么”到“多紧凑地表达”
人工智能·python·深度学习·机器学习·自然语言处理
蔡俊锋1 天前
AI记忆压缩术:从305GB到7.4GB的魔法
人工智能·ai·ai 记忆
Upsy-Daisy1 天前
AI Agent 项目学习笔记(二):Spring AI 与 ChatClient 主链路解析
人工智能·笔记·学习