Added logging and improved on topic list retrieval
This commit is contained in:
parent
69ea04ea44
commit
239c2f1f45
72
app.py
72
app.py
@ -1,9 +1,11 @@
|
||||
import logging
|
||||
import os
|
||||
import random
|
||||
from typing import Union
|
||||
|
||||
import openai
|
||||
from dotenv import load_dotenv
|
||||
from flask import Flask, request
|
||||
from flask import Flask, redirect, request
|
||||
from html5lib import HTMLParser
|
||||
|
||||
from htmls import index_html, prompt_html_template
|
||||
@ -12,6 +14,7 @@ load_dotenv()
|
||||
|
||||
app = Flask(__name__)
|
||||
openai.api_key = os.getenv("OPENAI_KEY")
|
||||
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
||||
|
||||
|
||||
def format_message(message: str) -> list:
|
||||
@ -56,22 +59,52 @@ def extract_html_from_chatgpt_response(returned_string: str) -> Union[str, None]
|
||||
validator = HTMLParser(strict=True)
|
||||
try:
|
||||
validator.parse(returned_string)
|
||||
logging.info("Valid HTML Found")
|
||||
return returned_string
|
||||
except Exception:
|
||||
match = returned_string.split("<html>")
|
||||
if len(match) > 1:
|
||||
logging.info("No valid HTML found, trying to parse...")
|
||||
html = "<html>" + match[1]
|
||||
html_match = html.split("</html>")
|
||||
if len(html_match) > 1:
|
||||
html = html_match[0] + "</html>"
|
||||
possible_html = html_match[0] + "</html>"
|
||||
if "(topic)" not in possible_html:
|
||||
return possible_html
|
||||
|
||||
|
||||
def get_random_topic() -> str:
|
||||
"""
|
||||
Gets a random topic from chatgpt
|
||||
|
||||
Args:
|
||||
None
|
||||
|
||||
Returns:
|
||||
A random topic
|
||||
|
||||
Raises:
|
||||
None
|
||||
"""
|
||||
|
||||
try:
|
||||
validator.parse(html)
|
||||
return html
|
||||
except Exception:
|
||||
return None
|
||||
completion = openai.ChatCompletion.create(
|
||||
model="gpt-3.5-turbo",
|
||||
messages=format_message(
|
||||
"Give me a random list of topics to talk about in a comma separated list. Each "
|
||||
"topic should be less than 5 words long and should not contain commas"
|
||||
),
|
||||
)
|
||||
|
||||
topics = completion.choices[0].message.content.split(",")
|
||||
topic = random.choice(topics)
|
||||
|
||||
return topic.strip()
|
||||
|
||||
except openai.error.RateLimitError:
|
||||
return get_random_topic()
|
||||
|
||||
|
||||
def get_chatgpt_response(message: str) -> str:
|
||||
def get_chatgpt_html_response(message: str) -> str:
|
||||
"""
|
||||
Gets the response from chatgpt
|
||||
|
||||
@ -94,17 +127,19 @@ def get_chatgpt_response(message: str) -> str:
|
||||
"(topic) with the name of the selected topic and (topic_info) with 5 parragraphs talking about the "
|
||||
"topic. Replace (related_links) with 10 links related to the topic. For these links, the href "
|
||||
"should be '/infinite?topic=(slugified_related_topic_name)' and the text should be the name of the "
|
||||
"related topic. Respond only the converted code"
|
||||
"related topic. The name of these topics should not be more than 5 words. Respond only the code"
|
||||
)
|
||||
),
|
||||
)
|
||||
except openai.error.RateLimitError:
|
||||
return get_chatgpt_response(message)
|
||||
return get_chatgpt_html_response(message)
|
||||
|
||||
html = extract_html_from_chatgpt_response(completion.choices[0].message.content)
|
||||
response = completion.choices[0].message.content
|
||||
html = extract_html_from_chatgpt_response(response)
|
||||
|
||||
if not html:
|
||||
return get_chatgpt_response(message)
|
||||
logging.info(f"No HTML was found. The response was {response}. Retrying...")
|
||||
return get_chatgpt_html_response(message)
|
||||
|
||||
return html
|
||||
|
||||
@ -116,10 +151,17 @@ def index():
|
||||
|
||||
@app.route("/start")
|
||||
def start():
|
||||
return get_chatgpt_response("a random topic")
|
||||
random_topic = get_random_topic()
|
||||
logging.info(f"The random topic is {random_topic}")
|
||||
return get_chatgpt_html_response(random_topic)
|
||||
|
||||
|
||||
@app.route("/infinite")
|
||||
def topic():
|
||||
topic = request.args.get("topic")
|
||||
return get_chatgpt_response(topic)
|
||||
topic = request.args.get("topic", None)
|
||||
if not topic:
|
||||
return redirect("/start")
|
||||
if len(topic.split("_")) > 5:
|
||||
return "Error! Your topic should be < 5 words"
|
||||
|
||||
return get_chatgpt_html_response(topic)
|
||||
|
Loading…
x
Reference in New Issue
Block a user