qidao123.com技术社区-IT企服评测·应用市场
标题:
【服务器摆设】云端摆设Gradio情感聊天bot应用(ubuntu服务器+Nginx反向署理+内网穿透)
[打印本页]
作者:
慢吞云雾缓吐愁
时间:
2025-3-30 11:57
标题:
【服务器摆设】云端摆设Gradio情感聊天bot应用(ubuntu服务器+Nginx反向署理+内网穿透)
【服务器摆设】从0-1举行摆设Gradio情感聊天AI bot云端应用(ubuntu服务器+Nginx反向署理+内网穿透)
Gradio是一个常用于创建呆板学习和深度学习的Web应用程序,它提供了一个用户友好的界面,允许开辟者举行快速构建和摆设并制作可视化界面,本文会先容怎样将Gradio应用摆设到云服务器上,并使用反向署理来实现外链访问
Step1:先预备一台能够运行代码情况的服务器
流程:服务器租用→ssh连接→情况搭建→查抄情况
服务器租借购买可以参考该教程阿里云服务器租用搭建教程,可以根据自身实际需要选择需要的服务器,选择服务器后使用Xshell长途连接云服务器设置情况(Xshell自行在网络中下载),打开Xshell输入服务器的公网ip
输入要登录的账户名(主机名)
输入在云服务器中设置的登录密码,密码精确即完成
SSH连接
开始设置应用所需要的python情况,假如需要设置java或者c++情况也可以参考别的的服务器设置教程
安装python捏造情况所需要的依靠包
sudo apt update
sudo apt-get install python3-venv
复制代码
创建python捏造情况并激活
情况创建:python3 -m venv my-venv (情况名可自定)
激活情况:source work/bin/activate(改为你的路径,linux默认~为起始目录)
安装应用所需要的依靠包,目前安装有两种策略,一种直接用pip install packge举行安装或者将所需要包写入requirements.txt,或者使用以下语句举行安装
nano requirements.txt
复制代码
这里所需要安装的包写进去
pip install -r requirements.txt
复制代码
安装完后可以用pip list检察所需要的包是否安装完成
Step 2:搭建一个Gradio应用,这里以文心一言的Enrie SDKt为例
流程:编写一个Gradio应用→上传应用文件→设置后台服务→测试Gradio应用
首选在服务器上创建一个用于测试应用的文件目录
mkdir gradio_test/bot
复制代码
要编写一个Gradio Web应用,本文通过使用Enrie的sdk实现一个实现一个情感伙伴bot聊天,其代码如下举例先容:
import gradio as gr
import json
import erniebot
history = []
# 导入角色设置
with open('../config/enireRoleConfig.json', 'r', encoding='utf-8') as file:
role_config = json.load(file)
# 角色和描述
roles = role_config['roles']
# css样式
custom_css = """
<style>
@import url('https://fonts.googleapis.com/css2?family=Pacifico&family=Permanent+Marker&family=Roboto+Slab&family=Dancing+Script&display=swap');
body, .gradio-container {
display: flex;
flex-direction: column;
min-height: 100vh;
margin: 0;
}
.main {
flex-grow: 1;
display: flex;
flex-direction: column;
}
.contain {
flex-grow: 1;
display: flex;
flex-direction: column;
}
#component-0 {
flex-grow: 1;
display: flex;
flex-direction: column;
}
#component-0 > .chat-interface {
flex-grow: 1;
display: flex;
flex-direction: column;
}
#component-0 > .chat-interface > .message-list {
flex-grow: 1;
overflow-y: auto;
}
</style>
"""
def predict(message, history, option, api_key, role_selector):
model = "ernie-bot"
erniebot.api_type = 'aistudio'
erniebot.access_token = api_key
# 模型选择
if option == "ernie-bot":
model = 'ernie-bot'
elif option == "ernie-bot-turbo":
model = 'ernie-bot-turbo'
elif option == "ernie-bot-4":
model = 'ernie-bot-4'
# 角色选择
system_prompt = roles[role_selector]['prompt']
# 输入消息
json_str = {'role': 'user', 'content': message}
messages = ""
history = [{k: v for k, v in item.items() if k != 'metadata'} for item in history]
history.append(json_str)
# 调用API获取回复
response_stream = erniebot.ChatCompletion.create(
model=model,
messages=history,
stream=True,
system = system_prompt
)
# 流式输出回复
for response in response_stream:
chat = response.get_result()
messages = "".join([messages, chat])
print(chat, end='', flush=True)
yield messages
print("")
print(history)
def update_description(role):
descriptions = {
"温柔女友": f"""<p class='role-description' style='
color: #FF69B4;
font-size: 24px;
font-family: 'Pacifico', cursive;
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
'>{roles[role]['description']}</p>""",
"可爱甜心": f"""<p class='role-description' style='
color: #DDA0DD;
font-size: 24px;
font-family: 'Permanent Marker', cursive;
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
'>{roles[role]['description']}</p>""",
"霸道总裁": f"""<p class='role-description' style='
color: #4169E1;
font-size: 24px;
font-family: 'Roboto Slab', serif;
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
'>{roles[role]['description']}</p>""",
"贴心闺蜜": f"""<p class='role-description' style='
color: #32CD32;
font-size: 24px;
font-family: 'Dancing Script', cursive;
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
'>{roles[role]['description']}</p>"""
}
return descriptions.get(role, "<p>请选择一个角色</p>")
# 部署页面设计
with gr.Blocks(css=custom_css) as demo:
html_title = gr.HTML("""
<h1 style='text-align:center; font-family: "Brush Script MT", cursive; font-size: 48px; color: #4B0082;'>Elegant AI Chat Assistant</h1>
<style>
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
#description {
animation: fadeIn 2s ease-in-out;
}
</style>
""")
html_component = gr.HTML(
update_description("温柔女友"), # 默认显示第一个角色的描述
elem_id="role-description"
)
with gr.Row():
with gr.Column():
gr.HTML("<h2 style='text-align:center; font-family: 'Microsoft YaHei'; font-size: 36px; color: #4B0082;'>请输入你的API key</h2>")
api_key = gr.Textbox(
placeholder="输入你的API Key",
value="", # 输入你的apikey
container=False,
interactive=True,
type="password",
label="API Key"
)
option = gr.Radio(
choices=["ernie-bot", "ernie-bot-turbo", "ernie-4.0"],
value="ernie-bot-turbo",
label="模型选择"
)
role_selector = gr.Radio(
choices=["温柔女友", "可爱甜心", "霸道总裁", "贴心闺蜜"],
value="温柔女友",
label="角色选择",
interactive=True,
elem_id="role_selector"
)
role_selector.change(fn=update_description, inputs=role_selector, outputs=html_component)
with gr.Column(elem_id="chatbot-wrapper"):
gr.ChatInterface(
predict,
type="messages",
theme="ocean",
additional_inputs=[option, api_key, role_selector],
fill_height=True,
)
# 本地127.0.0.1,服务端0.0.0.0
demo.launch(server_name="0.0.0.0", server_port=7861,root_path="/bot")
复制代码
用JSON文件对脚色人设举行统一设置(注意,Enire的system prompt只能1024字符,在脚色设定上只管不要超字符),以下是我结合gpt生成几个人设设定
[code]{
"roles": {
"温柔女友": {
"description": "玉莹是一位温柔可爱的女友,她时刻陪伴在男友身边,给予倾听、安慰和鼓励。她能用温暖的话语和浪漫的举动让男友感受到满满的爱意,为男友提供情绪价值,成为男友生活中的甜蜜依靠。
欢迎光临 qidao123.com技术社区-IT企服评测·应用市场 (https://dis.qidao123.com/)
Powered by Discuz! X3.4