Quick Guide: 10 minutes to deploy personalized ChatGPT website practice

In this guide, we will lead you to quickly deploy a ChatGPT website of your own. This website will allow users to interact with your AI model. We will use the Python language to write the back-end code, and use the Flask lightweight web framework to build the website. Now, let's get started!

Preparations
First, make sure that Python and the following dependent libraries are installed on your computer:
insert image description here

Flask: used to build the web framework
OpenAI: used to interact with the GPT model
can be installed by the following command:

pip install Flask openai

1. Create a Flask application

First, create a file called app.py and write the following code in it:

from flask import Flask, render_template, request, jsonify
import openai

app = Flask(__name__)

@app.route("/")
def home():
    return render_template("index.html")

@app.route("/api/chat", methods=["POST"])
def chat():
    message = request.json["message"]
    response = get_gpt_response(message)
    return jsonify({
    
    "response": response})

def get_gpt_response(message):
    # 在这里调用GPT模型,将消息传递给模型并获取回复
    # 示例代码:
    # response = openai.Completion.create(engine="text-davinci-002", prompt=message, max_tokens=150, n=1, stop=None, temperature=0.5)
    # return response.choices[0].text.strip()
    return "你好,这是一个示例回复。"

if __name__ == "__main__":
    app.run(debug=True)

2. Create a front-end template

Create a folder called templates in the project root directory and create a file called index.html inside it. Paste the following HTML code into this file:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>个性化ChatGPT网站实践</title>
</head>
<body>
    <h1>欢迎来到ChatGPT网站实践!</h1>
    <form id="chat-form">
        <label for="message">输入消息:</label>
        <input type="text" id="message" required>
        <button type="submit">发送</button>
    </form>
    <div id="chat-history"></div>

    <script>
        const chatForm = document.getElementById("chat-form");
        const chatHistory = document.getElementById("chat-history");

        chatForm.addEventListener("submit", async (event) => {
      
      
            event.preventDefault();

            const message = document.getElementById("message").value;
            chatHistory.innerHTML += `<p>你:${ 
        message}</p>`;

            const response = await fetch("/api/chat", {
      
      
                method: "POST",
                headers: {
      
      
                    "Content-Type": "application/json"
                },
                body: JSON.stringify({
      
       message })
            });

            const data = await response.json();
            chatHistory.innerHTML += `<p>AI:${ 
        data.response}</p>`;
        document.getElementById("message").value = "";
    });
</script>
</body>
</html>

3. Run the website

You can now start your Flask application by running:

python app.py

The application will run on http://127.0.0.1:5000/ . Visit this address with your browser, and you'll see a simple page with an input box and a send button. Users can type a message in the input box and press the send button to interact with the AI ​​model.

4. Customization and deployment

You can customize the front-end page according to your own needs. For example, you can add style sheets, change fonts and colors, etc. to make it more attractive. In addition, you can also consider deploying the application to cloud services, such as Heroku, AWS, etc., so that others can access your website.

Please note that in order for your ChatGPT website to interact with the actual AI model, you need to register an OpenAI API key and fill in the corresponding code in the get_gpt_response function. This guide only provides an example response, but when deploying, make sure to communicate with the model.

After completing the above steps, you have successfully created a ChatGPT website of your own. Now you can invite others to experience your website and show off your AI skills!

Guess you like

Origin blog.csdn.net/tuzajun/article/details/130376368