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

张开发
2026/4/7 15:26:16 15 分钟阅读

分享文章

LLM--大模型快速展示(Gradio)
安装pip install gradio初始代码模版— 创建页面模版importgradioasgr# 回调函数具体处理函数defgreet(text):returnHello World text !# 注意inputs、outputs可以选择为text、image、音频infacegr.Interface(fngreet,inputstext,outputstext)# 创建inface.launch()结果创建输入与输出组件了解即可用到查# 定义输入组件列表input_list[gr.Audio(sources[microphone,upload],typenumpy,labelAudio File),gr.Checkbox(labelCheckbox),gr.ColorPicker(labelColor Picker),gr.Dataframe(headers[Col1,Col2,Col3],labelDataframe),# 添加了列名以便显示gr.Dropdown(choices[option 1,option 2,option 3],labelDropdown),gr.File(labelFile,typebinary),gr.Image(sources[webcam,upload],labelImage),gr.Number(labelNumber),gr.Radio(choices[option 1,option 2,option 3],labelRadio),gr.Slider(minimum0,maximum10,labelSlider,step5),gr.Textbox(labelTextbox,lines3,max_lines7,placeholderPlaceholder),gr.TextArea(labelText Area,lines3,max_lines7,placeholderPlaceholder),gr.Video(sources[webcam,upload],labelVideo),]# 定义输出组件列表output_list[gr.Textbox(labelAudio outputs,lines7),gr.Textbox(labelCheckbox ouputs),gr.Textbox(labelColor Picker ouputs),gr.Textbox(labelDataframe ouputs),gr.Textbox(labelDropdown ouputs),gr.Textbox(labelFile ouputs),gr.Textbox(labelImage ouputs),gr.Textbox(labelNumber ouputs),gr.Textbox(labelRadio ouputs),gr.Textbox(labelSlider ouputs),gr.Textbox(labelTextbox ouputs),gr.Textbox(labelText Area ouputs),gr.Textbox(labelVideo ouputs),]# 创建页面demogr.Interface(fnprocess_all_inputs,inputsinput_list,outputsoutput_list,titleGradio 全组件演示,description测试所有可用的输入和输出组件)注意也可以不输入写None即可不会问AI布局容器组件我学习感觉适合个人搭建简单的页面不适合复杂的更不适合企业代码importgradioasgr gr.Blocks()gr.Row()gr.Column()gr.Tab()gr.Group()gr.Accordion()讲解组件用途说明Blocks根容器所有自定义界面的基础提供最大灵活性Row水平布局内部组件横向排列Column垂直布局内部组件纵向排列Tab标签页创建可切换的多个页面Group分组将组件视觉分组无边框Accordion折叠面板可展开/收起的分组容器一个简单的代码importgradioasgrwithgr.Blocks(title演示)asdemo:withgr.Tab(labeltxt2img):withgr.Row():# 水平布局withgr.Column(scale15):# 竖直布局txt1gr.Textbox(lines2,label)txt2gr.Textbox(lines2,label)withgr.Column(scale1,min_width1):button1gr.Button(value1)button2gr.Button(value2)button3gr.Button(value3)button4gr.Button(value4)withgr.Column():generate_buttongr.Button(valueGenerate)withgr.Row():dropdown1gr.Dropdown([1,2,3],labelStyle1)dropdown2gr.Dropdown([1,2,3],labelStyle2)demo.launch()效果对话gr.ChatInterface专为聊天机器人优化的快速开发工具参数类型说明fn函数必需处理消息的函数签名(message, history) → strmessage当前用户输入history历史输入chatbotgr.Chatbot自定义聊天窗口配置高度、初始消息等textboxgr.Textbox自定义输入框配置titlestr页面标题themestr主题样式default,soft,monochrome等exampleslist示例问题列表用户可点击快速提问retry_btnstr/None重试按钮文字None则隐藏submit_btnstr提交按钮文字undo_btnstr/None撤销最后一轮对话按钮clear_btnstr/None清空全部对话按钮案例代码importgradioasgrdefChatbot(message,history):response我是AI助手returnresponse demogr.ChatInterface(fnChatbot,titleAI助手,examples[你好,介绍一下自己],themesoft)demo.launch()结果参考资料【Gradio系列教程】https://www.bilibili.com/video/BV19m411U7K8?vd_source1fd424333dd77a7d3e2e741f7d6fd4ee

更多文章