diff --git a/app.py b/app.py index ab0a6ac..04d3512 100644 --- a/app.py +++ b/app.py @@ -1,6 +1,7 @@ import logging import os import random +import re from typing import Union import openai @@ -30,7 +31,6 @@ def format_message(message: str) -> list: Raises: None """ - return [ { "role": "system", @@ -55,7 +55,6 @@ def extract_html_from_chatgpt_response(returned_string: str) -> Union[str, None] Raises: None """ - validator = HTMLParser(strict=True) try: validator.parse(returned_string) @@ -68,7 +67,7 @@ def extract_html_from_chatgpt_response(returned_string: str) -> Union[str, None] html = "" + match[1] html_match = html.split("") possible_html = html_match[0] + "" - if "(topic)" not in possible_html: + if "(topic)" not in possible_html and "(slugified_related_topic_name)" not in possible_html: return possible_html @@ -85,7 +84,6 @@ def get_random_topic() -> str: Raises: None """ - try: completion = openai.ChatCompletion.create( model="gpt-3.5-turbo", @@ -111,12 +109,41 @@ def get_random_topic() -> str: return get_random_topic() -def get_chatgpt_html_response(message: str) -> str: +def generate_topic_image(topic: str) -> str: + """ + Generates an image for the topic + + Args: + topic: The topic to generate an image for + + Returns: + The URL of the image + + Raises: + None + """ + try: + completion = openai.ChatCompletion.create( + model="gpt-3.5-turbo", + messages=format_message(f"Generate a prompt for dall-e about {topic}"), + ) + dalle_prompt = completion.choices[0].message.content + + logging.info(f"The Dall-E prompt is: {dalle_prompt}") + + image = openai.Image.create(prompt=dalle_prompt, n=1, size="256x256") + return image["data"][0]["url"] + except Exception: + generate_topic_image(topic) + + +def get_chatgpt_html_response(message: str, count: int = 0) -> str: """ Gets the response from chatgpt Args: message: The message to send to chatgpt + count: The number of times this function has been called Returns: The HTML response from chatgpt @@ -124,6 +151,10 @@ def get_chatgpt_html_response(message: str) -> str: Raises: None """ + count += 1 + + if count >= 5: + return f"Error! ChatGPT is having problems with this topic: {message}. Try a new one." logging.info(f"The topic to generate is {message}") @@ -138,21 +169,27 @@ def get_chatgpt_html_response(message: str) -> str: "should be '/infinite?topic=(slugified_related_topic_name)' and the text should be the name of the " "related topic. The name of these topics should not be more than 5 words. Finally, replace " "(new_topics) with a 5 links with completely new and random topics to talk about. These links " - "should follow the same rules as before. Respond only the code" + "should follow the same rules as before. Do not replace (image_source). Respond only the code" ) ), ) except openai.error.RateLimitError: - return get_chatgpt_html_response(message) + return get_chatgpt_html_response(message, count) response = completion.choices[0].message.content html = 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(message) + return get_chatgpt_html_response(message, count) - return html + pattern = r'src="([^"]*)"' + image = generate_topic_image(message) + + logging.info(f"The image is {image}") + + html_with_image = re.sub(pattern, f'src="{image}"', html) + return html_with_image @app.route("/") diff --git a/htmls.py b/htmls.py index 1b81dd0..fd53f25 100644 --- a/htmls.py +++ b/htmls.py @@ -24,6 +24,8 @@ prompt_html_template = """

(topic)

+ +
(topic_info)