国内部署 ChatGPT 网站教程
首先,我们需要准备以下工具和环境:
- Python3
- Pytorch
- Transformers
- Flask
- HTML、CSS、JavaScript
接下来,我们按照以下步骤进行部署:
- 下载 ChatGPT 代码
从 Github 上下载 ChatGPT 的代码,并解压到本地。
- 安装依赖库
在命令行中进入 ChatGPT 目录,输入以下命令安装需要的依赖库:
pip install -r requirements.txt
- 准备模型文件
在 ChatGPT 目录下新建一个文件夹,命名为'model',将训练好的模型文件放入其中。
- 编写 Flask app
在 ChatGPT 目录下新建一个名为'app.py' 的 Python 文件,编写 Flask app 的代码,如下所示:
from flask import Flask, request, jsonify, render_template
from transformers import GPT2LMHeadModel, GPT2Tokenizer
app = Flask(__name__)
tokenizer = GPT2Tokenizer.from_pretrained('model')
model = GPT2LMHeadModel.from_pretrained('model')
@app.route('/')
def home():
return render_template('index.html')
@app.route('/get-response', methods=['POST'])
def get_response():
data = request.get_json()
prompt = data['prompt']
input_ids = tokenizer.encode(prompt, return_tensors='pt')
output = model.generate(input_ids, max_length=1024, do_sample=True)
response = tokenizer.decode(output[0], skip_special_tokens=True)
return jsonify({'response': response})
其中,'/' 路由返回一个名为'index.html' 的 HTML 文件,该文件用于渲染 ChatGPT 的前端界面;'/get-response' 路由接受来自前端的 POST 请求,根据请求中的 prompt 参数生成回复,并将回复以 JSON 格式返回。
- 编写 HTML、CSS、JavaScript 文件
在 ChatGPT 目录下新建一个名为'static' 的文件夹,该文件夹用于存放静态资源文件,如 CSS、JavaScript 和图片等。在 static 目录下再新建一个名为'index.html' 的 HTML 文件,编写 ChatGPT 的前端界面代码,如下所示:
<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'>
<title>chatGPT</title>
<link rel='stylesheet' href='{{ url_for('static', filename='style.css') }}'>
</head>
<body>
<div class='container'>
<h1>chatGPT</h1>
<div class='chat-box'>
<div class='chat-log'>
</div>
<div class='chat-input'>
<input type='text' id='input-box' placeholder='Input your message...'>
<button id='send-btn' onclick='send()'>Send</button>
</div>
</div>
</div>
<script src='{{ url_for('static', filename='script.js') }}'></script>
</body>
</html>
其中,'{{ url_for('static', filename='style.css') }}' 用于引用位于 static 目录下的'style.css' 文件,'{{ url_for('static', filename='script.js') }}' 用于引用位于 static 目录下的'script.js' 文件。
在 static 目录下再新建一个名为'style.css' 的 CSS 文件,编写 ChatGPT 的样式代码,如下所示:
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: Arial, sans-serif;
}
.container {
max-width: 600px;
margin: 0 auto;
padding: 20px;
}
h1 {
text-align: center;
margin-bottom: 20px;
}
.chat-box {
border: 1px solid #ccc;
border-radius: 5px;
overflow: hidden;
}
.chat-log {
height: 400px;
padding: 10px;
overflow-y: scroll;
}
.chat-input {
padding: 10px;
display: flex;
}
.chat-input input[type='text'] {
flex: 1;
margin-right: 10px;
border-radius: 5px;
border: none;
padding: 10px;
}
.chat-input button {
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
padding: 10px;
cursor: pointer;
}
.chat-input button:hover {
background-color: #3e8e41;
}
在 static 目录下再新建一个名为'script.js' 的 JavaScript 文件,编写 ChatGPT 的脚本代码,如下所示:
const chatLog = document.querySelector('.chat-log');
const inputBox = document.querySelector('#input-box');
inputBox.addEventListener('keyup', function (evt) {
if (evt.keyCode === 13) {
evt.preventDefault();
document.querySelector('#send-btn').click();
}
});
function send() {
const message = inputBox.value.trim();
if (!message) {
return;
}
const xhr = new XMLHttpRequest();
xhr.open('POST', '/get-response');
xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
xhr.onload = function () {
const response = JSON.parse(xhr.responseText).response;
appendMessage('You', message);
appendMessage('chatGPT', response);
inputBox.value = '';
};
xhr.send(JSON.stringify({ prompt: message }));
}
function appendMessage(sender, message) {
const messageEl = document.createElement('div');
const senderEl = document.createElement('strong');
senderEl.textContent = sender + ': ';
messageEl.appendChild(senderEl);
messageEl.appendChild(document.createTextNode(message));
chatLog.appendChild(messageEl);
chatLog.scrollTop = chatLog.scrollHeight;
}
- 运行 Flask app
在命令行中进入 ChatGPT 目录,输入以下命令运行 Flask app:
python app.py
Flask app 运行后会监听本地的 5000 端口,我们在浏览器中访问 http://localhost:5000/ 即可进入 ChatGPT 的前端界面。
至此,我们已经成功部署了 ChatGPT 网站,可以和 ChatGPT 进行聊天了。
原文地址: https://www.cveoy.top/t/topic/m0i8 著作权归作者所有。请勿转载和采集!