-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
356 lines (300 loc) · 13.8 KB
/
app.py
File metadata and controls
356 lines (300 loc) · 13.8 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
from flask import Flask, jsonify, request, send_file, render_template, g, redirect, render_template, url_for, make_response
from flask_cors import CORS
import openai
import ssl
import logging
import string
import traceback
import random
import sqlite3
from functools import wraps
import hashlib
app = Flask(__name__)
CORS(app) # 启用 CORS 支持
API_key = ""
gpt_client = openai.OpenAI(api_key= API_key,)
def get_db():
db = getattr(g, '_database', None)
if db is None:
db = g._database = sqlite3.connect('db/mai.sqlite3')
db.row_factory = sqlite3.Row
setattr(g, '_database', db)
return db
@app.teardown_appcontext
def close_connection(exception):
db = getattr(g, '_database', None)
if db is not None:
db.close()
# def query_db(query, args=(), one=False, commit=False):
# db = get_db()
# cursor = db.execute(query, args)
# if commit:
# db.commit()
# last_id = cursor.lastrowid
# cursor.close()
# return last_id
# rows = cursor.fetchall()
# cursor.close()
# return rows[0] if rows and one else rows if rows else None
def query_db(query, args=(), one=False, commit=False):
db = get_db()
db.row_factory = sqlite3.Row # 设置row_factory
cursor = db.execute(query, args)
if commit:
db.commit()
last_id = cursor.lastrowid
cursor.close()
return last_id
rows = cursor.fetchall()
cursor.close()
if one:
return dict(rows[0]) if rows else None
else:
return [dict(row) for row in rows] if rows else None
def interact_with_gpt(msgs):
response = gpt_client.chat.completions.create(
model="gpt-4o",
messages=msgs
)
return response.choices[0].message.content
def get_user_from_cookie(request):
user_id = request.cookies.get('user_id')
password = request.cookies.get('user_password')
if user_id and password:
return query_db('select * from users where id = ? and password = ?', [user_id, password], one=True)
return None
@app.route('/get-creations', methods=['GET'])
def get_creations():
# 这里是模拟的数据获取,实际应该是数据库查询
user_id = request.cookies.get('user_id')
print(user_id)
db_data = query_db('SELECT * FROM creations WHERE user_id = ?', [user_id])
try:
creations = [{'type':i['type'],'content':i['content']} for i in db_data]
except Exception as e:
# 处理数据库查询中的其他可能错误
print(f"An error occurred: {e}")
creations = []
return jsonify(creations)
# @app.route('/save-data', methods=['POST'])
# def save_data():
# data = request.get_json()
# content = data.get('content')
# type = data.get('type')
# user_id = request.cookies.get('user_id')
# print(user_id)
# query_db('INSERT INTO creations (user_id, type, content) VALUES (?, ?, ?)', (user_id, type, content))
# data=query_db('SELECT * FROM creations')
# print(data)
# return jsonify({'success': True}) # 可以返回成功的状态
@app.route('/save-data', methods=['POST'])
def save_data():
data = request.get_json()
content = data.get('content')
type = data.get('type')
user_id = request.cookies.get('user_id')
# 插入数据并提交事务
query_db('INSERT INTO creations (user_id, type, content) VALUES (?, ?, ?)',
(user_id, type, content), commit=True)
# 检查插入后的数据
data = query_db('SELECT * FROM creations')
return jsonify({'success': True})
# ------------------------------ NORMAL PAGE ROUTES ----------------------------------
@app.route('/')
def index():
return send_file('templates/index.html')
@app.route('/register', methods=['GET', 'POST'])
def register():
return send_file('templates/register.html')
@app.route('/textstudio')
def textstudio():
# 假设你已经有 textstudio.html 页面准备好显示
return render_template('textstudio.html')
@app.route('/signup', methods=['POST'])
def signup():
data = request.get_json()
username = data['username']
email = data['email']
password = data['password']
# 检查用户名是否已存在
if query_db('SELECT id FROM users WHERE username = ?', [username], one=True):
return jsonify({'success': False, 'message': 'Username already exists'})
# 插入新用户并获取 ID
user_id = query_db('INSERT INTO users (username, email, password) VALUES (?, ?, ?)',
[username, email, password], commit=True)
if user_id:
resp = make_response(redirect(url_for('home')))
resp.set_cookie('user_id', str(user_id), httponly=True) # 更安全的 cookie 设置
resp.set_cookie('username', str(username), httponly=True)
return resp
else:
return jsonify({'success': False, 'message': 'Registration failed'})
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
data = request.get_json()
username = data.get('username')
password = data.get('password')
user = query_db('SELECT * FROM users WHERE username = ? AND password = ?', [username, password], one=True)
if user:
resp = make_response(jsonify({'success': True}))
resp.set_cookie('user_id', str(user['id']), httponly=True)
return resp
else:
return jsonify({'success': False, 'message': 'Invalid username or password'})
return send_file('templates/login.html')
@app.route('/home')
def home():
user_id = request.cookies.get('user_id')
# username = request.cookies.get('username')
# # 打印到控制台以进行调试
print(f"user id: {user_id}")
# print(f": {username}")
return send_file('templates/home.html')
@app.route('/form')
def form():
option = request.args.get('option', 'No option selected')
return render_template('form.html', option=option)
@app.route('/profile')
def profile():
user_id = request.cookies.get('user_id')
data = query_db('select * from users where id = ?', [user_id], one=True)
print(data)
return render_template('profile.html', username=data['username'],email=data['email'])
def get_user_from_cookie(request):
username = request.cookies.get('username')
password = request.cookies.get('user_password')
if user_id and password:
return query_db('select * from users where id = ? and password = ?', [username, password], one=True)
return None
def get_api_key(userid):
api_key = query_db(f"SELECT api_key FROM users WHERE id = {userid}")
return api_key[0][0]
def require_api_key(f):
@wraps(f)
def decorated_function(*args, **kwargs):
api_key = request.headers.get('Api-Key')
print(get_api_key(request.cookies.get('user_id')))
if not api_key or api_key != get_api_key(request.cookies.get('user_id')): # 确保这里是实际的 API 密钥
return jsonify({"error": "Unauthorized"}), 401
return f(*args, **kwargs)
return decorated_function
# @app.route('/generate', methods=['POST'])
# def generate_caption():
# print(request.form)
# platform_name = "Twitter"
# post_about = request.form.get('postAbout')
# target_audience = request.form.get('target_audience')
# key_talking_points = "biodegradable packaging, cruelty-free testing, all-natural ingredients"
# tone_of_voice = request.form.get('toneOfVoice')
# language = "English"
# include_hashtag = "Yes"
# list_of_hashtags = "#EcoFriendly #GreenBeauty"
# include_emoji = "Yes"
# # list_of_emojis = "🌿🐰"
# # - Include Hashtag: {include_hashtag} (If yes, specify: {list_of_hashtags})
# # - Include Emoji: {include_emoji} (If yes, specify: {list_of_emojis})
# # - Key Talking Points: {key_talking_points}
# advertisement_prompt = f"""Create an advertisement using the following details:
# - Platform where the ad will be posted: {platform_name}
# - Post about: {post_about}
# - Target Audience: {target_audience}
# - Tone of Voice: {tone_of_voice}
# - Language: {language}
# - number of Words: 100
# Ensure that the advertisement is engaging, concise, and adheres to the platform's guidelines.
# """
# input = [{"role": "user", "content": "For all the questions that following, give me two answers split by string: |||, in format: text1 ||| text2 "},{"role": "assistant", "content" : "ok"},
# {"role": "user", "content": advertisement_prompt}]
# #generated_data = interact_with_gpt(input)
# generated_data = "Introducing our latest line of eco-friendly cosmetics! 🌿🐰 Discover biodegradable packaging, cruelty-free testing, and all-natural ingredients. Join young adults passionate about sustainable living today! #EcoFriendly #GreenBeauty ||| Elevate your beauty routine with eco-conscious choices! 🌿🐰 Embrace biodegradable packaging, cruelty-free testing, and natural ingredients. Connect with like-minded individuals and make a positive impact! #EcoFriendly #GreenBeauty"
# leftbox = generated_data.split('|||')[0]
# rightbox = generated_data.split('|||')[1]
# return jsonify({'leftbox': leftbox, 'rightbox': rightbox})
@app.route('/generate', methods=['POST'])
def generate_caption():
# 获取表单数据
data = request.get_json()
print(data)
# 获取表单数据
platform_name = data.get('platformName', 'Twitter')
post_about = data.get('postAbout')
target_audience = data.get('targetAudience')
key_talking_points = data.get('keyTalkingPoints', 'biodegradable packaging, cruelty-free testing, all-natural ingredients')
tone_of_voice = data.get('toneOfVoice')
language = data.get('language', 'English')
include_hashtag = 'include Hashtag' if data.get('addHashtag') else 'do not add Hashtag'
# list_of_hashtags = data.get('listOfHashtags', '#EcoFriendly #GreenBeauty')
include_emoji = 'include Emoji' if data.get('addEmoji') else 'do not add Emoji'
# list_of_emojis = data.get('listOfEmojis', '🌿🐰')
# 构建广告提示语
print(include_emoji)
# - Include Hashtag: {include_hashtag} (If yes, add some hashtag, if No, do not include any hashtag)
# - Include Emoji: {include_emoji} (If yes, add some emoji, if No, do not include any emoji)
advertisement_prompt = f"""
Create two engaging and concise advertisements using the following details (provide two distinct texts separated by "|||"):
- this is a {platform_name}
- Post about: {post_about}
- Target Audience: {target_audience}
- Tone of Voice: {tone_of_voice}
- Language: {language}
- Key Talking Points: {key_talking_points}
- {include_hashtag}
- {include_emoji}
- Number of Words: 150
Ensure that the text is engaging, concise, and adheres to the platform's guidelines.
"""
# {"role": "user", "content": "For all the questions that follow, give me two answers split by string: |||, in format: text1 ||| text2"},
# {"role": "assistant", "content": "ok"},
input = [
{"role": "user", "content": advertisement_prompt}
]
# 调用 interact_with_gpt 函数生成数据
# generated_data = interact_with_gpt(input)
generated_data = interact_with_gpt(input) # 你可以替换为实际调用 GPT 的代码
print(generated_data)
# 分割生成的数据
leftbox = generated_data.split('|||')[0].strip()
rightbox = generated_data.split('|||')[1].strip()
return jsonify({'leftbox': leftbox, 'rightbox': rightbox})
@app.route('/api/data', methods=['POST'])
def get_data():
data = request.get_json() # 获取 JSON 数据
productName = data.get('product', 'erro') # 安全访问字典
productIntro = data.get('productIntro', 'erro')
userProfile = data.get('userProfile', 'erro')
question = f"""the name of the brand is {productName}, the product is {productIntro},
our target customers are {userProfile} based on these information
you need to create an ad copy"""
input = [{"role": "user", "content": question}]
gpt_resp = interact_with_gpt(input)
# print(gpt_resp)
#gpt_resp = "Discover PearPhone, the ultimate choice for the young and dynamic! With its ultra-slim design and practical features, PearPhone fits seamlessly into your lifestyle. Stay stylish and efficient, whether at work or play. Upgrade now to experience the perfect blend of lightness and utility with PearPhone."
return jsonify({'message': str(gpt_resp)})
@app.route('/api/renew', methods=['POST'])
def renew_content():
data = request.get_json() # 获取 JSON 数据
print(data)
originalContent = data["orginalContent"]
tone = data["tone"]
keyword = data["keyword"]
other = data["other"]
prompt = f"""
Revise the following content to match the specified requirements:
Original Content: {originalContent}
if the text include emoji, the new content also add emoji else do not add emoji
if the text include hashtag, the new content also add hashtag else do not add hashtag
Desired Tone: {'Leisure' if tone == 1 else 'Casual' if tone == 2 else 'Semi-professional' if tone == 3 else 'Professional'}
Keyword to Highlight: {keyword}
Additional Instructions: {other}
Please rewrite the content ensuring it is engaging, includes the specified keyword prominently, and adheres to the desired tone.
"""
input = [{"role": "user", "content": prompt}]
#new_content = "renew Introducing our latest line of eco-friendly cosmetics! Discover biodegradable packaging, cruelty-free testing, and all-natural ingredients. Join young adults passionate about sustainable living today! #EcoFriendly #GreenBeauty "
new_content = gpt_resp = interact_with_gpt(input)
#print(new_content)
return jsonify({'message': new_content})
if __name__ == '__main__':
# context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
# context.load_cert_chain('cert.pem', 'key.pem')
app.run(host='0.0.0.0', port=5000, debug=True)