【服务器摆设】云端摆设Gradio情感聊天bot应用(ubuntu服务器+Nginx反向署理 ...

打印 上一主题 下一主题

主题 1646|帖子 1646|积分 4938

【服务器摆设】从0-1举行摆设Gradio情感聊天AI bot云端应用(ubuntu服务器+Nginx反向署理+内网穿透)

Gradio是一个常用于创建呆板学习和深度学习的Web应用程序,它提供了一个用户友好的界面,允许开辟者举行快速构建和摆设并制作可视化界面,本文会先容怎样将Gradio应用摆设到云服务器上,并使用反向署理来实现外链访问
Step1:先预备一台能够运行代码情况的服务器

流程:服务器租用→ssh连接→情况搭建→查抄情况
服务器租借购买可以参考该教程阿里云服务器租用搭建教程,可以根据自身实际需要选择需要的服务器,选择服务器后使用Xshell长途连接云服务器设置情况(Xshell自行在网络中下载),打开Xshell输入服务器的公网ip

输入要登录的账户名(主机名)

输入在云服务器中设置的登录密码,密码精确即完成SSH连接

开始设置应用所需要的python情况,假如需要设置java或者c++情况也可以参考别的的服务器设置教程

安装python捏造情况所需要的依靠包
  1. sudo apt update
  2. sudo apt-get install python3-venv
复制代码
创建python捏造情况并激活
情况创建:python3 -m venv my-venv​ (情况名可自定)
激活情况:source work/bin/activate​(改为你的路径,linux默认~为起始目录)

安装应用所需要的依靠包,目前安装有两种策略,一种直接用pip install packge​举行安装或者将所需要包写入requirements.txt,或者使用以下语句举行安装​​
  1. nano requirements.txt
复制代码


这里所需要安装的包写进去


  1. pip install -r requirements.txt
复制代码


安装完后可以用pip list​检察所需要的包是否安装完成


Step 2:搭建一个Gradio应用,这里以文心一言的Enrie SDKt为例

流程:编写一个Gradio应用→上传应用文件→设置后台服务→测试Gradio应用
首选在服务器上创建一个用于测试应用的文件目录
  1. mkdir gradio_test/bot
复制代码
要编写一个Gradio Web应用,本文通过使用Enrie的sdk实现一个实现一个情感伙伴bot聊天,其代码如下举例先容:
  1. import gradio as gr
  2. import json
  3. import erniebot
  4. history = []
  5. # 导入角色设置
  6. with open('../config/enireRoleConfig.json', 'r', encoding='utf-8') as file:
  7.     role_config = json.load(file)
  8. # 角色和描述
  9. roles = role_config['roles']
  10. # css样式
  11. custom_css = """
  12. <style>
  13. @import url('https://fonts.googleapis.com/css2?family=Pacifico&family=Permanent+Marker&family=Roboto+Slab&family=Dancing+Script&display=swap');
  14. body, .gradio-container {
  15.     display: flex;
  16.     flex-direction: column;
  17.     min-height: 100vh;
  18.     margin: 0;
  19. }
  20. .main {
  21.     flex-grow: 1;
  22.     display: flex;
  23.     flex-direction: column;
  24. }
  25. .contain {
  26.     flex-grow: 1;
  27.     display: flex;
  28.     flex-direction: column;
  29. }
  30. #component-0 {
  31.     flex-grow: 1;
  32.     display: flex;
  33.     flex-direction: column;
  34. }
  35. #component-0 > .chat-interface {
  36.     flex-grow: 1;
  37.     display: flex;
  38.     flex-direction: column;
  39. }
  40. #component-0 > .chat-interface > .message-list {
  41.     flex-grow: 1;
  42.     overflow-y: auto;
  43. }
  44. </style>
  45. """
  46. def predict(message, history, option, api_key, role_selector):
  47.     model = "ernie-bot"
  48.     erniebot.api_type = 'aistudio'
  49.     erniebot.access_token = api_key
  50.     # 模型选择
  51.     if option == "ernie-bot":
  52.         model = 'ernie-bot'
  53.     elif option == "ernie-bot-turbo":
  54.         model = 'ernie-bot-turbo'
  55.     elif option == "ernie-bot-4":
  56.         model = 'ernie-bot-4'
  57.     # 角色选择
  58.     system_prompt = roles[role_selector]['prompt']
  59.     # 输入消息
  60.     json_str = {'role': 'user', 'content': message}
  61.     messages = ""
  62.     history = [{k: v for k, v in item.items() if k != 'metadata'} for item in history]
  63.     history.append(json_str)
  64.     # 调用API获取回复
  65.     response_stream = erniebot.ChatCompletion.create(
  66.         model=model,
  67.         messages=history,
  68.         stream=True,
  69.         system = system_prompt
  70.     )
  71.     # 流式输出回复
  72.     for response in response_stream:
  73.         chat = response.get_result()
  74.         messages = "".join([messages, chat])
  75.         print(chat, end='', flush=True)
  76.         yield messages
  77.     print("")
  78.     print(history)
  79. def update_description(role):
  80.     descriptions = {
  81.         "温柔女友": f"""<p class='role-description' style='
  82.                 color: #FF69B4;
  83.                 font-size: 24px;
  84.                 font-family: 'Pacifico', cursive;
  85.                 text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
  86.                 '>{roles[role]['description']}</p>""",
  87.         "可爱甜心": f"""<p class='role-description' style='
  88.                 color: #DDA0DD;
  89.                 font-size: 24px;
  90.                 font-family: 'Permanent Marker', cursive;
  91.                 text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
  92.                 '>{roles[role]['description']}</p>""",
  93.         "霸道总裁": f"""<p class='role-description' style='
  94.                 color: #4169E1;
  95.                 font-size: 24px;
  96.                 font-family: 'Roboto Slab', serif;
  97.                 text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
  98.                 '>{roles[role]['description']}</p>""",
  99.         "贴心闺蜜": f"""<p class='role-description' style='
  100.                 color: #32CD32;
  101.                 font-size: 24px;
  102.                 font-family: 'Dancing Script', cursive;
  103.                 text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
  104.                 '>{roles[role]['description']}</p>"""
  105.     }
  106.     return descriptions.get(role, "<p>请选择一个角色</p>")
  107. # 部署页面设计
  108. with gr.Blocks(css=custom_css) as demo:
  109.     html_title = gr.HTML("""
  110.     <h1 style='text-align:center; font-family: "Brush Script MT", cursive; font-size: 48px; color: #4B0082;'>Elegant AI Chat Assistant</h1>
  111.     <style>
  112.         @keyframes fadeIn {
  113.             from { opacity: 0; }
  114.             to { opacity: 1; }
  115.         }
  116.         #description {
  117.             animation: fadeIn 2s ease-in-out;
  118.         }
  119.     </style>
  120.     """)
  121.     html_component = gr.HTML(
  122.         update_description("温柔女友"),  # 默认显示第一个角色的描述
  123.         elem_id="role-description"
  124.     )
  125.     with gr.Row():
  126.         with gr.Column():
  127.             gr.HTML("<h2 style='text-align:center; font-family: 'Microsoft YaHei'; font-size: 36px; color: #4B0082;'>请输入你的API key</h2>")
  128.             api_key = gr.Textbox(
  129.                 placeholder="输入你的API Key",
  130.                 value="", # 输入你的apikey
  131.                 container=False,
  132.                 interactive=True,
  133.                 type="password",
  134.                 label="API Key"
  135.             )
  136.         option = gr.Radio(
  137.             choices=["ernie-bot", "ernie-bot-turbo", "ernie-4.0"],
  138.             value="ernie-bot-turbo",
  139.             label="模型选择"
  140.         )
  141.     role_selector = gr.Radio(
  142.         choices=["温柔女友", "可爱甜心", "霸道总裁", "贴心闺蜜"],
  143.         value="温柔女友",
  144.         label="角色选择",
  145.         interactive=True,
  146.         elem_id="role_selector"
  147.     )
  148.     role_selector.change(fn=update_description, inputs=role_selector, outputs=html_component)
  149.     with gr.Column(elem_id="chatbot-wrapper"):
  150.         gr.ChatInterface(
  151.             predict,
  152.             type="messages",
  153.             theme="ocean",
  154.             additional_inputs=[option, api_key, role_selector],
  155.             fill_height=True,
  156.         )
  157. # 本地127.0.0.1,服务端0.0.0.0
  158. demo.launch(server_name="0.0.0.0", server_port=7861,root_path="/bot")
复制代码
用JSON文件对脚色人设举行统一设置(注意,Enire的system prompt只能1024字符,在脚色设定上只管不要超字符),以下是我结合gpt生成几个人设设定
[code]{
    "roles": {
        "温柔女友": {
            "description": "玉莹是一位温柔可爱的女友,她时刻陪伴在男友身边,给予倾听、安慰和鼓励。她能用温暖的话语和浪漫的举动让男友感受到满满的爱意,为男友提供情绪价值,成为男友生活中的甜蜜依靠。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

慢吞云雾缓吐愁

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表