-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew_app.py
More file actions
96 lines (69 loc) · 3.39 KB
/
new_app.py
File metadata and controls
96 lines (69 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#final_code_post and get api combined
import os
from dotenv import load_dotenv
from openai import OpenAI
from flask import Flask, request, jsonify
app = Flask(__name__)
quotes_storage = []
class QuoteGenerator:
def __init__(self):
load_dotenv()
api_key = os.getenv("OPENAI_API_KEY")
self.client = OpenAI(api_key=api_key)
def generate_quote(self, event_name: str, event_description: str, category: str, number: int):
"""Generate multiple inspirational quotes for a given category (using yield)."""
for _ in range(number):
prompt = f"""
The name of the event is {event_name}, and the event description is: {event_description}.
Please provide a motivational quote to encourage, motivate the user and that must be highly relevant to the {category} category.
Strict guidelines to follow:
The quote must be high quality and precise.
The quote must include the author's name. If the quote is AI-generated, then do not show that it's AI-generated or unknown.
The quote must be inspirational, and relevant to {event_name}, {event_description}, and the {category} category.
Please ensure the output follows this exact JSON format:
{{
"event_name": "{event_name}",
"quote": "quote"
}}
Now, provide one valid, high-quality, and inspirational quote related to the {event_description} and the {category} category.
"""
try:
response = self.client.chat.completions.create(
model="gpt-4-turbo", # gpt-4o gpt-4-turbo-2024-04-09
messages=[
{"role": "system", "content": "You are a helpful assistant that generates inspirational, motivational quotes."},
{"role": "user", "content": prompt}
],
max_tokens=50,
temperature=1.5
)
yield response.choices[0].message.content.strip()
except Exception as e:
yield {"error": str(e)}
quote_generator = QuoteGenerator()
# for Post method
@app.route('/generate-quote', methods=['POST'])
def generate_quote():
"""API endpoint to generate motivational quotes and store them"""
data = request.get_json()
event_name = data.get("event_name")
event_description = data.get("event_description")
category = data.get("category")
number = data.get("number", 1)
if not event_name or not event_description or not category:
return jsonify({"error": "Missing required parameters: event_name, event_description, or category"}), 400
quotes = []
for quote in quote_generator.generate_quote(event_name, event_description, category, number):
quotes.append(quote)
quotes_storage.extend(quotes)
return jsonify({"quotes": quotes})
# for Get method
@app.route('/get-quotes', methods=['GET'])
def get_quotes():
"""API endpoint to fetch the generated quotes"""
if quotes_storage:
return jsonify({"quotes": quotes_storage})
else:
return jsonify({"message": "No quotes available yet. Please generate quotes via POST."})
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=5000)