From 6ed10e35903e6fb864507a9f1b88473649d13e22 Mon Sep 17 00:00:00 2001 From: Roger Gonzalez Date: Tue, 16 May 2023 10:21:01 -0300 Subject: [PATCH] Using async flask --- app.py | 8 ++++---- chatgpt_helper.py | 27 +++++++++++++++------------ requirements.txt | 2 ++ 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/app.py b/app.py index 6beb65c..bd8357b 100644 --- a/app.py +++ b/app.py @@ -21,12 +21,12 @@ def index(): @app.route("/start") -def start(): - return get_chatgpt_html_response() +async def start(): + return await get_chatgpt_html_response() @app.route("/infinite") -def topic(): +async def topic(): topic = request.args.get("topic", None) or request.args.get("topics", None) if not topic: return redirect("/start") @@ -37,4 +37,4 @@ def topic(): logging.info(f"The topic is too long: {unslugified_topic}") return "Error! Your topic should be < 5 words" - return get_chatgpt_html_response(unslugified_topic) + return await get_chatgpt_html_response(unslugified_topic) diff --git a/chatgpt_helper.py b/chatgpt_helper.py index f709f9c..4d723ab 100644 --- a/chatgpt_helper.py +++ b/chatgpt_helper.py @@ -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 @@ -59,7 +59,7 @@ def extract_html_from_chatgpt_response(returned_string: str) -> Union[str, None] return possible_html -def get_random_topic() -> str: +async def get_random_topic() -> str: """ Gets a random topic from chatgpt @@ -87,7 +87,7 @@ def get_random_topic() -> str: if len(topics) < 5: 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}") topic = random.choice(topics) @@ -95,10 +95,10 @@ def get_random_topic() -> str: return topic.strip().title() 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 @@ -126,10 +126,10 @@ def generate_topic_image(topic: str) -> str: image = openai.Image.create(prompt=dalle_prompt, n=1, size="256x256") return image["data"][0]["url"] 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 @@ -146,7 +146,7 @@ def get_chatgpt_html_response(topic: str = None, count: int = 0) -> str: count += 1 if topic is None: - topic = get_random_topic() + topic = await get_random_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: - return get_chatgpt_html_response(topic, count) + return await get_chatgpt_html_response(topic, count) 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: 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="([^"]*)"' - image = generate_topic_image(topic) + image = await generate_topic_image(topic) logging.info(f"The image is {image}") diff --git a/requirements.txt b/requirements.txt index 0194892..f448c16 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,6 @@ aiohttp==3.8.4 aiosignal==1.3.1 +asgiref==3.6.0 async-timeout==4.0.2 attrs==23.1.0 blinker==1.6.2 @@ -12,6 +13,7 @@ filelock==3.12.0 Flask==2.3.2 frozenlist==1.3.3 greenlet==2.0.2 +gunicorn==20.1.0 html5lib==1.1 identify==2.5.24 idna==3.4