Using async flask
This commit is contained in:
parent
dc8774d793
commit
6ed10e3590
8
app.py
8
app.py
@ -21,12 +21,12 @@ def index():
|
|||||||
|
|
||||||
|
|
||||||
@app.route("/start")
|
@app.route("/start")
|
||||||
def start():
|
async def start():
|
||||||
return get_chatgpt_html_response()
|
return await get_chatgpt_html_response()
|
||||||
|
|
||||||
|
|
||||||
@app.route("/infinite")
|
@app.route("/infinite")
|
||||||
def topic():
|
async def topic():
|
||||||
topic = request.args.get("topic", None) or request.args.get("topics", None)
|
topic = request.args.get("topic", None) or request.args.get("topics", None)
|
||||||
if not topic:
|
if not topic:
|
||||||
return redirect("/start")
|
return redirect("/start")
|
||||||
@ -37,4 +37,4 @@ def topic():
|
|||||||
logging.info(f"The topic is too long: {unslugified_topic}")
|
logging.info(f"The topic is too long: {unslugified_topic}")
|
||||||
return "Error! Your topic should be < 5 words"
|
return "Error! Your topic should be < 5 words"
|
||||||
|
|
||||||
return get_chatgpt_html_response(unslugified_topic)
|
return await get_chatgpt_html_response(unslugified_topic)
|
||||||
|
@ -30,7 +30,7 @@ def format_message(message: str, role: str) -> list:
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def extract_html_from_chatgpt_response(returned_string: str) -> Union[str, None]:
|
async def extract_html_from_chatgpt_response(returned_string: str) -> Union[str, None]:
|
||||||
"""
|
"""
|
||||||
Extracts the HTML from the chatgpt response
|
Extracts the HTML from the chatgpt response
|
||||||
|
|
||||||
@ -59,7 +59,7 @@ def extract_html_from_chatgpt_response(returned_string: str) -> Union[str, None]
|
|||||||
return possible_html
|
return possible_html
|
||||||
|
|
||||||
|
|
||||||
def get_random_topic() -> str:
|
async def get_random_topic() -> str:
|
||||||
"""
|
"""
|
||||||
Gets a random topic from chatgpt
|
Gets a random topic from chatgpt
|
||||||
|
|
||||||
@ -87,7 +87,7 @@ def get_random_topic() -> str:
|
|||||||
|
|
||||||
if len(topics) < 5:
|
if len(topics) < 5:
|
||||||
logging.info(f"The topics is too short: {topics}. Retrying...")
|
logging.info(f"The topics is too short: {topics}. Retrying...")
|
||||||
return get_random_topic()
|
return await get_random_topic()
|
||||||
|
|
||||||
logging.info(f"The topics list is: {topics}")
|
logging.info(f"The topics list is: {topics}")
|
||||||
topic = random.choice(topics)
|
topic = random.choice(topics)
|
||||||
@ -95,10 +95,10 @@ def get_random_topic() -> str:
|
|||||||
return topic.strip().title()
|
return topic.strip().title()
|
||||||
|
|
||||||
except openai.error.RateLimitError:
|
except openai.error.RateLimitError:
|
||||||
return get_random_topic()
|
return await get_random_topic()
|
||||||
|
|
||||||
|
|
||||||
def generate_topic_image(topic: str) -> str:
|
async def generate_topic_image(topic: str) -> str:
|
||||||
"""
|
"""
|
||||||
Generates an image for the topic
|
Generates an image for the topic
|
||||||
|
|
||||||
@ -126,10 +126,10 @@ def generate_topic_image(topic: str) -> str:
|
|||||||
image = openai.Image.create(prompt=dalle_prompt, n=1, size="256x256")
|
image = openai.Image.create(prompt=dalle_prompt, n=1, size="256x256")
|
||||||
return image["data"][0]["url"]
|
return image["data"][0]["url"]
|
||||||
except Exception:
|
except Exception:
|
||||||
generate_topic_image(topic)
|
return generate_topic_image(topic)
|
||||||
|
|
||||||
|
|
||||||
def get_chatgpt_html_response(topic: str = None, count: int = 0) -> str:
|
async def get_chatgpt_html_response(topic: str = None, count: int = 0) -> str:
|
||||||
"""
|
"""
|
||||||
Gets the response from chatgpt
|
Gets the response from chatgpt
|
||||||
|
|
||||||
@ -146,7 +146,7 @@ def get_chatgpt_html_response(topic: str = None, count: int = 0) -> str:
|
|||||||
count += 1
|
count += 1
|
||||||
|
|
||||||
if topic is None:
|
if topic is None:
|
||||||
topic = get_random_topic()
|
topic = await get_random_topic()
|
||||||
|
|
||||||
html = prompt_html_template.replace("(topic)", topic)
|
html = prompt_html_template.replace("(topic)", topic)
|
||||||
|
|
||||||
@ -172,17 +172,20 @@ def get_chatgpt_html_response(topic: str = None, count: int = 0) -> str:
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
except openai.error.RateLimitError:
|
except openai.error.RateLimitError:
|
||||||
return get_chatgpt_html_response(topic, count)
|
return await get_chatgpt_html_response(topic, count)
|
||||||
|
|
||||||
response = completion.choices[0].message.content
|
response = completion.choices[0].message.content
|
||||||
html = extract_html_from_chatgpt_response(response)
|
|
||||||
|
logging.info(f"The response is {response}")
|
||||||
|
|
||||||
|
html = await extract_html_from_chatgpt_response(response)
|
||||||
|
|
||||||
if not html:
|
if not html:
|
||||||
logging.info(f"No HTML was found. The response was {response}. Retrying...")
|
logging.info(f"No HTML was found. The response was {response}. Retrying...")
|
||||||
return get_chatgpt_html_response(topic, count)
|
return await get_chatgpt_html_response(topic, count)
|
||||||
|
|
||||||
pattern = r'src="([^"]*)"'
|
pattern = r'src="([^"]*)"'
|
||||||
image = generate_topic_image(topic)
|
image = await generate_topic_image(topic)
|
||||||
|
|
||||||
logging.info(f"The image is {image}")
|
logging.info(f"The image is {image}")
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
aiohttp==3.8.4
|
aiohttp==3.8.4
|
||||||
aiosignal==1.3.1
|
aiosignal==1.3.1
|
||||||
|
asgiref==3.6.0
|
||||||
async-timeout==4.0.2
|
async-timeout==4.0.2
|
||||||
attrs==23.1.0
|
attrs==23.1.0
|
||||||
blinker==1.6.2
|
blinker==1.6.2
|
||||||
@ -12,6 +13,7 @@ filelock==3.12.0
|
|||||||
Flask==2.3.2
|
Flask==2.3.2
|
||||||
frozenlist==1.3.3
|
frozenlist==1.3.3
|
||||||
greenlet==2.0.2
|
greenlet==2.0.2
|
||||||
|
gunicorn==20.1.0
|
||||||
html5lib==1.1
|
html5lib==1.1
|
||||||
identify==2.5.24
|
identify==2.5.24
|
||||||
idna==3.4
|
idna==3.4
|
||||||
|
Loading…
x
Reference in New Issue
Block a user