Build a ChatGPT Website: Simple HTML, Python, and OpenAI Integration
Setting up a ChatGPT Website:
-
Create a new folder on your computer and name it 'ChatGPT'.
-
Inside the 'ChatGPT' folder, create two new files: 'index.html' and 'app.py'.
-
Open 'index.html' in a text editor and add the following code:
<!DOCTYPE html>
<html>
<head>
<title>ChatGPT</title>
</head>
<body>
<h1>ChatGPT</h1>
<form method='post' action='/ask'>
<label for='prompt'>Prompt:</label>
<input type='text' name='prompt' id='prompt'>
<br><br>
<label for='answer'>Answer:</label>
<input type='text' name='answer' id='answer' readonly>
<br><br>
<button type='submit'>Ask GPT-3</button>
</form>
</body>
</html>
-
Save and close 'index.html'.
-
Open 'app.py' in a text editor and add the following code:
import openai
from flask import Flask, request, jsonify
app = Flask(__name__)
openai.api_key = '<your_openai_api_key>'
@app.route('/')
def index():
return app.send_static_file('index.html')
@app.route('/ask', methods=['POST'])
def ask():
prompt = request.form['prompt']
response = openai.Completion.create(
engine='davinci',
prompt=prompt,
max_tokens=1024,
n=1,
stop=None,
temperature=0.5,
)
answer = response.choices[0].text.strip()
return jsonify({'answer': answer})
if __name__ == '__main__':
app.run(debug=True)
-
Replace '<your_openai_api_key>' with your actual OpenAI API key.
-
Save and close 'app.py'.
-
Open a terminal or command prompt and navigate to the 'ChatGPT' folder.
-
Install the required Python packages by running the following command:
pip install flask openai
- Once the packages are installed, start the Flask development server by running the following command:
python app.py
-
Open a web browser and navigate to
http://localhost:5000. -
You should see the ChatGPT website. Enter a prompt in the text box and click 'Ask GPT-3'. The answer will appear in the second text box.
Web Server Options:
Aside from Apache, there are several other web server options that can run Python and HTML:
-
Flask: Flask is a lightweight web framework for Python that makes it easy to create web applications. It's what we used in the ChatGPT example above.
-
Django: Django is a more robust web framework for Python that includes a wide range of built-in features and tools.
-
CherryPy: CherryPy is a minimalist web framework for Python that focuses on simplicity and ease of use.
-
Tornado: Tornado is a scalable, non-blocking web server and web application framework for Python.
To install any of these web servers, simply use pip to install the package:
pip install flask django cherrypy tornado
Then, follow the documentation for each framework to create your web application.
原文地址: https://www.cveoy.top/t/topic/lBis 著作权归作者所有。请勿转载和采集!