Using async flask

This commit is contained in:
Roger Gonzalez 2023-05-16 10:21:01 -03:00
parent dc8774d793
commit 6ed10e3590
Signed by: rogs
GPG Key ID: C7ECE9C6C36EC2E6
3 changed files with 21 additions and 16 deletions

8
app.py
View File

@ -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)

View File

@ -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}")

View File

@ -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