网站首页 > 知识剖析 正文
由于最近在测试各种开源模型性能,在终端测试下载下来的ollama 模型交互较为不方便,搬运工基于Gradio 手撸一个可以启动各种大模型的聊天界面;聊天交互界实现效果如下:
现将实战代码分享如下:
# -*- coding: utf-8 -*-
import gradio as gr
import ollama
from typing import List, Tuple
model_name_dict = {
"qwen2.5:32b": "qwen2.5:32b",
"qwen2.5:72b": "qwen2.5:72b",
"qwen2.5-coder": "qwen2.5-coder:32b",
"qwq-32b": "qwq:32b-preview-q8_0",
}
# LLMAsServer 模型即服务, 启动聊天界面
class LLMAsServer:
def __init__(self, model_name: str = model_name_dict["qwen2.5:32b"], is_stream=False):
self.model_name = model_name
self.is_stream = is_stream
def generate(self, text: str):
stream = ollama.generate(
stream=self.is_stream,
model=self.model_name,
prompt=text,
)
response = ""
if self.is_stream:
for chunk in stream:
response += chunk["response"]
else:
response = stream["response"]
return response
# api_generate 流式输出
def _api_generate(self, chat_history: List):
text = chat_history[-1]["content"]
response = self.generate(text)
yield chat_history + [{"role": "assistant", "content": response}]
@staticmethod
def _handle_user_message(user_message: str, history: list):
"""Handle the user submitted message. Clear message box and append to the history."""
new_history = history + [{"role": "user", "content": user_message}]
return '', new_history
@staticmethod
def _reset_chat() -> tuple:
"""Reset the agent's chat history. And clear all dialogue boxes."""
return "", []
def run(self):
custom_css = """
#box {
height: 500px;
overflow-y: scroll !important;
}
#clear-button {
background-color: blue !important;
color: white !important; /* Optional: Set text color to white for better contrast */
border: none !important; /* Optional: Remove default border */
border-radius: 2px !important; /* Optional: Add rounded corners */
height: 150px !important; /* Set the height of the Textbox */
}
#message-input {
width: 100% !important; /* Make the Textbox take up the full available width */
max-width: calc(100% - 50px) !important; /* Adjust for the ClearButton's width */
height: 150px !important; /* Increase the height to 50px */
}
#submit-button {
background-color: green !important;
}
textarea{
height: 100px !important; /* Ensure the inner container takes up the full height */
border-radius: 5px !important; /* Optional: Add rounded corners */
border-color: red !important;
border-style: solid !important;
}
"""
demo = gr.Blocks(
theme=gr.themes.Default(
primary_hue="red",
secondary_hue="pink"),
# css="#box { height: 420px; overflow-y: scroll !important}",
css=custom_css,
)
with demo:
gr.Markdown(
"# AI Chat \n"
"### This Application is Powered by The LLM Model {} \n".format(self.model_name.upper()),
)
chat_window = gr.Chatbot(
label="Message History",
scale=5,
type='messages',
elem_id="chatbot-container"
)
with gr.Row():
message_input = gr.Textbox(label="Input", placeholder="Type your message here...", scale=5,
elem_id="message-input")
clear = gr.ClearButton(elem_id="clear-button")
submit_button = gr.Button("Submit", elem_id="submit-button")
submit_button.click(
fn=self._handle_user_message,
inputs=[message_input, chat_window],
outputs=[message_input, chat_window]
).then(
fn=self._api_generate,
inputs=chat_window,
outputs=chat_window
)
message_input.submit(
self._handle_user_message,
inputs=[message_input, chat_window],
outputs=[message_input, chat_window],
).then(
self._api_generate,
chat_window,
chat_window,
)
clear.click(self._reset_chat, None, [message_input, chat_window])
demo.launch(show_error=True, share=False, server_port=7861)
if __name__ == "__main__":
server = LLMAsServer(model_name=model_name_dict["qwen2.5-coder"])
server.run()
Gradio 构建聊天界面功能强大,能支持CSS 设置,还能编写js 代码插入其中;给构建你的机器学习应用与分享带来了方便。
关注机器学习搬运工,不定期分享最前沿的AI 算法。谢谢大家!
猜你喜欢
- 2024-12-26 css布局方案汇总(28个实例图文并茂)
- 2024-12-26 StackOverflow 2022 年度调查报告
- 2024-12-26 滑动页面时的控件设计规范——吸底&锚点
- 2024-12-26 css精髓:这些布局你都学废了吗? css布局是什么意思
- 2024-12-26 产品必会的30个Axure使用技巧 axure基础知识
- 2024-12-26 微信小程序开发极简入门(四):文件导入
- 2024-12-26 CSS新特性contain,控制页面的重绘与重排
- 2024-12-26 两句css代码实现全屏滚动效果-demo案例
- 2024-12-26 CSS实现溢出显示省略号 html溢出省略号
- 2024-12-26 详解css清除浮动方法 详解css清除浮动方法是什么
- 最近发表
-
- jQuery EasyUI使用教程:创建展开行详细编辑表单的CRUD应用
- CSDN免登陆复制代码的几种方法(csdn扫码登录怎么实现的)
- LayUi提高-Select控件使用(layui form select)
- 用 Playwright MCP 让 AI 改它自己写的屎山代码
- multiple-select.js中手动设置全选和取消全选
- 前端实现右键自定义菜单(html 自定义右键菜单)
- JavaScript脚本如何断言select下拉框的元素内容?
- 广州蓝景分享—实用的CSS技巧,助你成为更好的前端开发者
- MyBatis-Plus码之重器 lambda 表达式使用指南,开发效率瞬间提升80%
- Go语言之select的使用和实现原理(go select case)
- 标签列表
-
- xml (46)
- css animation (57)
- array_slice (60)
- htmlspecialchars (54)
- position: absolute (54)
- datediff函数 (47)
- array_pop (49)
- jsmap (52)
- toggleclass (43)
- console.time (63)
- .sql (41)
- ahref (40)
- js json.parse (59)
- html复选框 (60)
- css 透明 (44)
- css 颜色 (47)
- php replace (41)
- css nth-child (48)
- min-height (40)
- xml schema (44)
- css 最后一个元素 (46)
- location.origin (44)
- table border (49)
- html tr (40)
- video controls (49)