Skip to content

Commit

Permalink
added chatgpt prompter
Browse files Browse the repository at this point in the history
  • Loading branch information
bdelucia committed Oct 8, 2024
1 parent 5def359 commit a808109
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 15 deletions.
Binary file modified __pycache__/views.cpython-312.pyc
Binary file not shown.
4 changes: 2 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

main.register_blueprint(views, url_prefix="/")

##if __name__ == '__main__':
##main.run(debug=True, port=8000)
#if __name__ == '__main__':
#main.run(debug=True, port=8000)


6 changes: 1 addition & 5 deletions static/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ body {
max-width: 600px;
text-align: center;
animation: rgbShadow 8s infinite;
}

.container#news {
margin: 30px;
margin: 15px;
}

li.news-article {
Expand All @@ -31,7 +28,6 @@ li.news-article {
border-radius: 5px;
box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.2);
color: #f1f1f1;
box-shadow: 0 4px 12px #03dac6;
}
a.news-article {
font-size: 1.2em;
Expand Down
11 changes: 11 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@
<link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
</head>
<body style="background-image: url('static/background.jpg'); background-size: cover; background-position: center; background-repeat: no-repeat;">
<div class="container" id="chatgpt">
<h2>Ask ChatGPT something:</h2>
<form action="{{ url_for('views.process_prompt') }}" method="POST">
<label for="prompt">Enter your prompt:</label>
<input type="text" id="prompt" name="prompt" required>
<button type="submit">Submit</button>
</form>
<p>{{ response if response else '' }}</p>
</div>

<div class="container">
<h1>Hello Bobby!</h1>
<h2>Your fun fact of the day:</h2>
Expand All @@ -33,6 +43,7 @@ <h2>Current Weather in Peoria, AZ:</h2>
<img src="{{ url_for('static', filename='reddit.png') }}">
</a>
</div>

<div class="container" id="news">
<h2>Today's Top Science Articles:</h2>
<ul>
Expand Down
35 changes: 27 additions & 8 deletions views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
import math
import random
from dotenv import load_dotenv
from flask import Blueprint, render_template
from flask import Blueprint, render_template, request, redirect, url_for
import requests

# Flask blueprint loading
views = Blueprint(__name__, "views")

# Getting keys from secret python env file
load_dotenv()
#openai_api_key = os.getenv("OPENAI_API_KEY")
#openai.api_key = openai_api_key
openai_api_key = os.getenv("OPENAI_API_KEY")
openai.api_key = openai_api_key
openweather_api_key = os.getenv("OPENWEATHER_API_KEY")
news_api_key = os.getenv("NEWS_API_KEY")
fact_api_key = os.getenv("API_NINJAS_KEY")
Expand Down Expand Up @@ -40,13 +40,32 @@ def get_fun_fact():
url = 'https://api.api-ninjas.com/v1/facts?'
response = requests.get(url, headers={'X-Api-Key': fact_api_key})
fact = response.json()
return fact[0]['fact'] + "!"
return fact[0]['fact']

# Renders index.html with passed arguments
@views.route("/")
def home():
def get_api_responses():
# API responses
weather_data = get_weather_data("85308")
rounded_temp = math.ceil(weather_data["main"]["temp"]) # Rounds temperature up to whole number for better presentation
funFact = get_fun_fact()
news_articles = get_news()
return render_template("index.html", funFact = funFact, weather_data=weather_data, rounded_temp=rounded_temp, news_articles=news_articles)
return weather_data, rounded_temp, funFact, news_articles

@views.route('/process_prompt', methods=['POST'])
def process_prompt():
prompt = request.form.get('prompt')

response = openai.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}],
max_tokens = 100
)

weather_data, rounded_temp, funFact, news_articles = get_api_responses()
response = response.choices[0].message.content.strip()
return render_template('index.html', funFact = funFact, weather_data=weather_data, rounded_temp=rounded_temp, news_articles=news_articles, response=response)

# Renders index.html with passed arguments
@views.route("/")
def home():
weather_data, rounded_temp, funFact, news_articles = get_api_responses()
return render_template("index.html", funFact = funFact, weather_data=weather_data, rounded_temp=rounded_temp, news_articles=news_articles, response=None)

0 comments on commit a808109

Please sign in to comment.