-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathflask-server.py
323 lines (269 loc) · 10.9 KB
/
flask-server.py
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
import os
import json
import threading
from flask import Flask, request, jsonify
import sqlalchemy
from google.cloud import pubsub_v1
from google.cloud.sql.connector import Connector
from dotenv import load_dotenv
from openai import OpenAI
from flask_cors import CORS
from queue import Queue
load_dotenv()
app = Flask(__name__)
CORS(app)
DB_CONNECTION_STRING = os.getenv('DB_CONNECTION_STRING')
DB_USER = os.getenv('DB_USER')
DB_PASSWORD = os.getenv('DB_PASSWORD')
DB_NAME = os.getenv('DB_NAME')
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
client = OpenAI(api_key=OPENAI_API_KEY)
PROJECT_ID = os.getenv('PROJECT_ID')
TOPIC_NAME = os.getenv('TOPIC_NAME')
SUBSCRIPTION_NAME = os.getenv('SUBSCRIPTION_NAME')
class PubSubManager:
def __init__(self, project_id, topic_name, subscription_name):
self.project_id = project_id
self.topic_name = topic_name
self.subscription_name = subscription_name
self.publisher = pubsub_v1.PublisherClient()
self.subscriber = pubsub_v1.SubscriberClient()
def publish(self, message):
topic_path = self.publisher.topic_path(self.project_id, self.topic_name)
message_json = json.dumps({'message': message})
message_bytes = message_json.encode('utf-8')
publish_future = self.publisher.publish(topic_path, data=message_bytes)
publish_future.result()
def subscribe(self, callback):
subscription_path = self.subscriber.subscription_path(self.project_id, self.subscription_name)
streaming_pull_future = self.subscriber.subscribe(subscription_path, callback=callback)
streaming_pull_future.result()
class DatabaseManager:
def __init__(self, connection_string, db_user, db_password, db_name):
self.connection_string = connection_string
self.db_user = db_user
self.db_password = db_password
self.db_name = db_name
self.connector = Connector()
def get_engine(self):
def getconn():
return self.connector.connect(
self.connection_string,
"pymysql",
user=self.db_user,
password=self.db_password,
db=self.db_name
)
return sqlalchemy.create_engine("mysql+pymysql://", creator=getconn)
messages = Queue()
def callback(message):
messages.put(message.data.decode('utf-8'))
message.ack()
pubsub_manager = PubSubManager(PROJECT_ID, TOPIC_NAME, SUBSCRIPTION_NAME)
db_manager = DatabaseManager(DB_CONNECTION_STRING, DB_USER, DB_PASSWORD, DB_NAME)
pool = db_manager.get_engine()
@app.route('/api/complete-chore', methods=['POST'])
def complete_chore():
data = request.json
chore_id = data.get('choreId')
if not chore_id:
return jsonify({'error': 'Missing choreId'}), 400
with pool.connect() as conn:
conn.execute(
sqlalchemy.text('UPDATE Chore SET status = TRUE WHERE id = :chore_id'),
{'chore_id': chore_id}
)
conn.commit()
chore_name = conn.execute(
sqlalchemy.text('SELECT name FROM Chore WHERE id = :chore_id'),
{'chore_id': chore_id}
).scalar()
pubsub_manager.publish(chore_name)
return jsonify({'message': 'Chore marked as completed'})
@app.route('/api/start-parent-session', methods=['GET'])
def start_parent_session():
def run_subscription():
pubsub_manager.subscribe(callback)
thread = threading.Thread(target=run_subscription)
thread.daemon = True
thread.start()
return jsonify({'message': 'Parent session started and listening for messages'})
@app.route('/api/get-messages', methods=['GET'])
def get_messages():
messages_list = []
while not messages.empty():
message = messages.get()
messages_list.append(message)
return jsonify(messages_list)
def chat_with_gpt(item, description=None, model="gpt-3.5-turbo"):
try:
messages = [
{"role": "system", "content": "You are a helpful assistant, knowledgeable about shopping, pricing, and providing specific recommendations."},
{"role": "user", "content": f"I am a child planning to buy a {item}."}
]
if description:
messages.append({"role": "user", "content": f"It should be like this: {description}"})
messages.append({"role": "system", "content": f"Based on the description, provide a specific price recommendation for the {item}."})
response = client.chat.completions.create(model=model, messages=messages)
if response.choices:
choice = response.choices[0]
if hasattr(choice, 'message') and hasattr(choice.message, 'content'):
return choice.message.content
else:
return "Sorry, I couldn't process that response."
else:
return "No response received from the API."
except Exception as e:
print(f"An error occurred: {e}")
return "Sorry, there was an error."
@app.route('/api/gpt-chat', methods=['POST'])
def gpt_chat():
data = request.json
item = data.get('item')
description = data.get('description', None)
response = chat_with_gpt(item, description)
return jsonify({'response': response})
@app.route('/api/insert-item', methods=['POST'])
def insert_item():
data = request.json
try:
item = data.get('item')
price = data.get('price')
if item is not None and price is not None:
with pool.connect() as conn:
conn.execute(
sqlalchemy.text('INSERT INTO SavingsGoal (item, price) VALUES (:item, :price)'),
{'item': item, 'price': price}
)
conn.commit()
return jsonify({'message': 'Item added successfully'})
else:
return jsonify({'error': 'Missing item or price data'}), 400
except Exception as e:
return jsonify({'error': 'An error occurred'}), 500
@app.route('/api/insert-chore', methods=['POST'])
def insert_chore():
data = request.json
try:
name = data.get('name')
compensation = data.get('compensation')
if name is not None and compensation is not None:
with pool.connect() as conn:
conn.execute(
sqlalchemy.text('INSERT INTO Chore (name, compensation, status) VALUES (:name, :compensation, 0)'),
{'name': name, 'compensation': compensation}
)
conn.commit()
return jsonify({'message': 'Chore added successfully'})
else:
return jsonify({'error': 'Missing name or compensation data'}), 400
except Exception as e:
return jsonify({'error': 'An error occurred'}), 500
@app.route('/api/insert-allowance', methods=['POST'])
def insert_allowance():
data = request.json
try:
amount = data.get('amount')
if amount is not None:
with pool.connect() as conn:
conn.execute(
sqlalchemy.text('INSERT INTO Allowance (amount) VALUES (:amount)'),
{'amount': amount}
)
conn.commit()
return jsonify({'message': 'Allowance added successfully'})
else:
return jsonify({'error': 'Missing amount data'}), 400
except Exception as e:
return jsonify({'error': 'An error occurred'}), 500
@app.route('/api/mark-chore-complete', methods=['POST'])
def mark_chore_complete():
data = request.json
chore_id = data.get('choreId')
try:
with pool.connect() as conn:
conn.execute(
sqlalchemy.text('UPDATE Chore SET status = TRUE WHERE id = :chore_id'),
{'chore_id': chore_id}
)
conn.commit()
chore_name = conn.execute(
sqlalchemy.text('SELECT name FROM Chore WHERE id = :chore_id'),
{'chore_id': chore_id}
).scalar()
pubsub_manager.publish(chore_name)
return jsonify({'message': 'Chore marked as completed'})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/get-item', methods=['GET'])
def get_item():
try:
with pool.connect() as conn:
item = conn.execute(
sqlalchemy.text('SELECT item FROM SavingsGoal')
).scalar()
if item:
return jsonify({'item': item})
else:
return jsonify({'item': None})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/get-price', methods=['GET'])
def get_price():
try:
with pool.connect() as conn:
price = conn.execute(
sqlalchemy.text('SELECT price FROM SavingsGoal')
).scalar()
if price:
return jsonify({'price': price})
else:
return jsonify({'price': None})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/get-allowance', methods=['GET'])
def get_allowance():
try:
with pool.connect() as conn:
allowance = conn.execute(
sqlalchemy.text('SELECT amount FROM Allowance ORDER BY id DESC LIMIT 1')
).scalar() or 0
return jsonify({'allowance': allowance})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/get-chores', methods=['GET'])
def get_chores():
try:
with pool.connect() as conn:
ids = conn.execute(sqlalchemy.text('SELECT id FROM Chore')).fetchall()
names = conn.execute(sqlalchemy.text('SELECT name FROM Chore')).fetchall()
compensations = conn.execute(sqlalchemy.text('SELECT compensation FROM Chore')).fetchall()
statuses = conn.execute(sqlalchemy.text('SELECT status FROM Chore')).fetchall()
chores = []
for i in range(len(ids)):
chore = {
'id': ids[i][0],
'name': names[i][0],
'compensation': float(compensations[i][0]),
'isComplete': bool(statuses[i][0])
}
chores.append(chore)
return jsonify(chores)
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/get-total-balance', methods=['GET'])
def get_total_balance():
try:
with pool.connect() as conn:
total_compensation = conn.execute(
sqlalchemy.text('SELECT SUM(compensation) FROM Chore WHERE status = TRUE')
).scalar() or 0
allowance = conn.execute(
sqlalchemy.text('SELECT amount FROM Allowance ORDER BY id DESC LIMIT 1')
).scalar() or 0
total_balance = total_compensation + allowance
return jsonify({'total_balance': total_balance})
except Exception as e:
return jsonify({'error': str(e)}), 500
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)