Created
June 21, 2023 00:53
-
-
Save TheLime1/77f6d58af28f0856e08350bcfffa4d74 to your computer and use it in GitHub Desktop.
Build an infinite website with Flask and OpenAI's GPT-3.5. This code combines Flask's routing and OpenAI's text generation to dynamically generate web pages based on URL paths and request data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, request | |
import os | |
import openai | |
import json | |
# flask --app main --debug run | |
openai.api_key = "YOUR_OPENAI_API_KEY" | |
app = Flask(__name__) | |
BASE_PROMPT = """Create a response document with content that matches the following URL path: | |
`{{URL_PATH}}` | |
The first line is the Content-Type of the response. | |
The following lines is the returned data. | |
In case of a html response, add relative href links with to related topics. | |
{{OPTIONAL_DATA}} | |
Content-Type: | |
""" | |
@app.route("/", methods = ['POST', 'GET']) | |
@app.route("/<path:path>", methods = ['POST', 'GET']) | |
def catch_all(path=""): | |
if request.form: | |
prompt = BASE_PROMPT.replace("{{OPTIONAL_DATA}}", f"form data: {json.dumps(request.form)}") | |
else: | |
prompt = BASE_PROMPT.replace("{{OPTIONAL_DATA}}", f"") | |
prompt = prompt.replace("{{URL_PATH}}", path) | |
response = openai.Completion.create( | |
model="text-davinci-003", | |
prompt=prompt, | |
temperature=0.7, | |
max_tokens=512, | |
top_p=1, | |
frequency_penalty=0, | |
presence_penalty=0 | |
) | |
ai_data = response.choices[0].text | |
print(ai_data) | |
content_type = ai_data.splitlines()[0] | |
response_data = "\n".join(ai_data.splitlines()[1:]) | |
return response_data, 200, {'Content-Type': content_type} | |
if __name__ == '__main__': | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment