Skip to content

Commit

Permalink
Read db from site
Browse files Browse the repository at this point in the history
On top of having a database to store our input/outputs in we now are able to access these results directly from our website
  • Loading branch information
mcnamee98 committed Aug 28, 2023
1 parent 36d79f6 commit 9bc2d42
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 39 deletions.
12 changes: 10 additions & 2 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 15 additions & 36 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import os

import openai
from flask import Flask, redirect, render_template, request, url_for
import psycopg2
import json
from flask import Flask, redirect, render_template, request, url_for

app = Flask(__name__)
openai.api_key = os.getenv("OPENAI_API_KEY")



# app = Flask(__name__)
# openai.api_key = os.getenv("OPENAI_API_KEY")

# Configure PostgreSQL connection
conn = psycopg2.connect(
host="localhost",
Expand Down Expand Up @@ -47,16 +41,11 @@ def index():

result = completion.choices[0].message.content

# Insert data into the database
insert_query = """
INSERT INTO api_calls (input_data, output_data, timestamp)
VALUES (%s, %s, now())
"""

insert_query = """
INSERT INTO api_calls (input_data, output_data, timestamp)
VALUES (%s, %s, now())
"""
input_data = {
"Keywords": keywords,
"Target Audience": audience,
Expand All @@ -65,34 +54,24 @@ def index():
cur.execute(insert_query, (json.dumps(input_data), json.dumps(result),))
conn.commit()

# input_data = f'{{"Keywords": "{keywords}", "Target Audience": "{audience}", "Article Length": "{length}"}}'
# result_db = result
# cur.execute(insert_query, (input_data, result_db))
# conn.commit()
# cur.execute(insert_query, (f"Keywords: {keywords}, Target Audience: {audience}, Article Length: {length}", result))
# conn.commit()

return render_template("index.html", result=result, keywords=keywords, audience=audience, length=length)

result = request.args.get("result")
return render_template("index.html", result=result)

@app.route("/previous-articles", methods=["GET"])
def previous_articles():
# Fetch the data from the database (assuming you have a SELECT query)
select_query = "SELECT input_data, timestamp FROM api_calls"
cur.execute(select_query)
previous_data = cur.fetchall()

return render_template("previous_articles.html", previous_data=previous_data)

@app.route("/article/<timestamp>", methods=["GET"])
def article_detail(timestamp):
select_query = "SELECT output_data FROM api_calls WHERE timestamp = %s"
cur.execute(select_query, (timestamp,))
article_content = cur.fetchone()

# def generate_prompt(animal):
# return """Suggest three names for an animal that is a superhero.
#
# Animal: Cat
# Names: Captain Sharpclaw, Agent Fluffball, The Incredible Feline
# Animal: Dog
# Names: Ruff the Protector, Wonder Canine, Sir Barks-a-Lot
# Animal: {}
# Names:""".format(
# animal.capitalize()
# )
#
# def gen_article(keywords, audience, length):
# return """"You are a helpful writing assistant. You will be given a handful of keywords,
# a target audience, and an article length. Please write the article to be Search Engine Optimized."
# """.format(
# audience.capitalize()
# )
return render_template("article_detail.html", article_content=article_content)
12 changes: 12 additions & 0 deletions templates/article_detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<title>Article Detail</title>
</head>
<body>
<h1>Article Detail</h1>
<pre>{{ article_content[0] }}</pre>
<a href="{{ url_for('index') }}" class="btn btn-primary">Back to Home</a>

</body>
</html>
6 changes: 5 additions & 1 deletion templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,15 @@ <h1>SEO Article Generator</h1>
<input type="text" name="length" placeholder="Enter an article length" required />

<input type="submit" value="Generate Article" />

<p> If you'd rather check out previously generated articles, click here! </p>
<a href="{{ url_for('previous_articles') }}" class="btn btn-primary">View Previous Articles</a>

</form>

{% if result %}
<div class="form-inputs">
<h2>Form Inputs:</h2>
<h2>Most Recent Inputs:</h2>
<p>Keywords: {{ keywords }}</p>
<p>Target Audience: {{ audience }}</p>
<p>Article Length: {{ length }}</p>
Expand Down
19 changes: 19 additions & 0 deletions templates/previous_articles.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<title>Previous Articles</title>
</head>
<body>
<h1>Previously Generated Articles</h1>
<ul>
{% for data in previous_data %}
<li>
<a href="{{ url_for('article_detail', timestamp=data[1]) }}">{{ data[0] }}</a>
</li>

{% endfor %}
<a href="{{ url_for('index') }}" class="btn btn-primary">Back to Home</a>

</ul>
</body>
</html>

0 comments on commit 9bc2d42

Please sign in to comment.