-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
368 lines (285 loc) · 11.8 KB
/
server.py
File metadata and controls
368 lines (285 loc) · 11.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
357
358
359
360
361
362
363
364
365
366
367
from main import app, db
from main.models import User, BankLoan, UserLoan, Subscription, Transaction, Contact
from functools import wraps
from flask import jsonify
import datetime
import os, sys, json, random, base64, time
from flask import Flask, send_file, request
from flask_cors import CORS
import pyttsx3
import whisper
from transformers import pipeline
from s_graph import Graph
# classifier = pipeline("zero-shot-classification",
# model="valhalla/distilbart-mnli-12-1")
MODEL_URL = "C:\\Users\\Admin\\.cache\\huggingface\\hub\\models--valhalla--distilbart-mnli-12-1\\snapshots\\506336d4214470e3b3b36021358daae28e25ceac"
classifier = pipeline("zero-shot-classification", model=MODEL_URL)
whisper_model = whisper.load_model("base")
#voice engine setup
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('rate',180)
engine.setProperty('voice',voices[1].id)
sgraph = Graph(classifier)
def process_whisper(filename):
# load audio and pad/trim it to fit 30 seconds
audio = whisper.load_audio(filename)
audio = whisper.pad_or_trim(audio)
# make log-Mel spectrogram and move to the same device as the model
mel = whisper.log_mel_spectrogram(audio).to(whisper_model.device)
# detect the spoken language
# _, probs = whisper_model.detect_language(mel)
# print(f"Detected language: {max(probs, key=probs.get)}")
# decode the audio
options = whisper.DecodingOptions()
result = whisper.decode(whisper_model, mel, options)
return result.text
def process_zero_shot(text, labels):
out = classifier(text, labels)
print(out)
return out["labels"][0], out
def tts(text):
fname = f"{random.randrange(0,10000)}_out.mp3"
# say method on the engine that passing input text to be spoken
engine.save_to_file(text, fname)
engine.runAndWait()
binary_audio = None
with open(fname, "rb") as f:
binary_audio = f.read()
os.remove(fname)
return base64.b64encode(binary_audio).decode("utf-8")
def extract_json(func):
@wraps(func)
def wrapper(*args, **kwargs):
# probs = classifier("sdfsdfsdf", ["SDFsdfsdf", "sfsdfsd"])
# nstate = probs["labels"][0]
# return jsonify({"message": nstate})
content_type = request.headers.get("Content-Type")
if (content_type == 'application/json'):
data = request.get_json()
return func(data, *args, **kwargs)
else:
return jsonify({'error': 'Content-Type not supported!' })
return wrapper
def compute_max_loan(user):
#otigers shit here
return user.balance ** 2 / 2
@app.route('/users', methods=['POST'])
@extract_json
def create_user(data):
print(data)
user = User(username=data['username'], email=data['email'], password=data['password'],
phone_number= data["phone_number"],
firstname= data["firstname"], surname= data["surname"])
db.session.add(user)
db.session.commit()
return jsonify({'message': 'User created successfully'})
@app.route('/users/get_balance', methods=['GET'])
@extract_json
def get_user_balance(data):
user = User.query.filter_by(id=data["user_id"]).first()
return jsonify({'balance': user.balance})
@app.route('/users', methods=['GET'])
@extract_json
def get_user(data):
user = User.query.get(id=data["user_id"])
return jsonify(user)
@app.route('/users/', methods=['PUT'])
@extract_json
def update_user(data):
user = User.query.filter_by(data["user_id"]).first()
user.username = data['username']
user.email = data['email']
user.phone_number = data['phone_number']
user.firstname = data['firstname']
user.surname = data['surname']
db.session.commit()
return jsonify({'message': 'User updated successfully'})
@app.route('/users/', methods=['DELETE'])
@extract_json
def delete_user(data):
user = User.query.filter_by(data["user_id"]).first()
if user:
db.session.delete(user)
db.session.commit()
return jsonify({'message': 'User deleted successfully'})
else:
return jsonify({'error': 'User could not be found'})
@app.route('/users/<int:user_id>/get_bank_loans', methods=['GET'])
def get_user_loans(user_id):
loans = BankLoan.query.all(user_id=user_id)
print(loans)
return jsonify(loans) #not sure about this
@app.route('/bank_loans', methods=['POST'])
@extract_json
def create_bank_loan(data):
user = User.query.filter_by(data['user_id']).first()
if user:
if data['amount'] <= compute_max_loan(user):
loan = BankLoan(user=user, amount=data['amount'], interest_rate=data['interest_rate'])
user.balance += loan.amount #there obv needs to be other mechanisms tô track the loan repayment
db.session.add(loan)
db.session.commit()
return jsonify({'message': 'Loan created successfully'})
else:
return jsonify({'error': 'You do not qualify for this loan amount.'})
@app.route('/bank_loans/repay/', methods=['POST'])
@extract_json
def repay_bank_loan(data):
interest_rate = 0.2
loan = BankLoan.query.filter_by(data["loan_id"]).first()
user = User.query.filter_by(id=loan.user_id).first()
if loan and user:
amount_owed = loan.amount * interest_rate * ((datetime.datetime.now() - loan.start_date).days / 365)
if user.balance >= amount_owed:
user.balance -= loan.amount #very rudimentary
loan.active = False
loan.end_date = datetime.datetime.now()
db.session.commit()
return jsonify({'message': 'Loan repaid successfully'})
else:
return jsonify({'message': 'Loan could not be repaid. Insufficient funds!'})
else:
return jsonify({'error': 'Loan or User could not be found.'})
@app.route('/bank_loans', methods=['DELETE'])
@extract_json
def revert_bank_loan(data):
loan = BankLoan.query.filter_by(id=data["loan_id"]).first()
user = User.query.filter_by(id=loan.user_id).first()
if loan and user:
user.balance -= loan.amount #very rudimentary
db.session.delete(loan)
db.session.commit()
return jsonify({'message': 'Loan deleted successfully'})
else:
return jsonify({'error': 'Loan or User could not be found.'})
@app.route('/subscriptions/create', methods=['POST'])
@extract_json
def create_subscription(data):
subscription = Subscription(creator=data["user_id"], name=data['name'],
amount=data['amount'], interval=data['interval'],
action=data['action'])
db.session.add(subscription)
db.session.commit()
return jsonify({'message': 'Subscription created successfully'})
@app.route('/subscriptions/subscribe/', methods=['POST'])
@extract_json
def join_subscription(data, subscription_id):
user = User.query.get(data['user_id'])
subscription = Subscription.query.filter_by(id=data['subscription_id']).first()
#some many to many shit to happen here
db.session.commit()
return jsonify({'message': 'Subscription added successfully'})
@app.route('/subscriptions/', methods=['PUT'])
@extract_json
def update_subscription(data):
subscription = Subscription.query.filter_by(data['subscription_id']).first()
if data["owner_id"] == subscription.creator_id:
#maybe needs more nuance?
subscription.name = data['name']
subscription.amount = data['amount']
subscription.interval = data['interval']
subscription.end_date = data['end_date']
subscription.action = data['action']
db.session.commit()
return jsonify({'message': 'Subscription updated successfully'})
else:
return jsonify({'error': 'Only the owner can edit the subscription.'})
@app.route('/subscriptions/', methods=['DELETE'])
@extract_json
def delete_subscription(data):
subscription = Subscription.query.filter_by(id=data['subscription_id']).first()
db.session.delete(subscription)
db.session.commit()
return jsonify({'message': 'Subscription deleted successfully'})
@app.route('/contacts/upload', methods=['POST'])
@extract_json
def create_contact(data):
user = User.query.filter_by(id=data['user_id']).first()
phone_numbers = data['phone_numbers']
for number in phone_numbers:
corresponding_user = User.query.filter_by(phone_number = number).first()
if corresponding_user:
#create new contact
contact = Contact(luser_id=user.id, ruser_id= corresponding_user.id, phone_number=data['phone_number'] )
db.session.add(contact)
db.session.commit()
return jsonify({'message': 'Contacts created successfully'})
@app.route('/contacts', methods=['GET'])
@extract_json
def get_contacts(data):
contacts = Contact.query.all(luser_id= data["user_id"] )
return jsonify(contacts)
@app.route('/contacts', methods=['DELETE'])
@extract_json
def delete_contact(data):
contact = Contact.query.filter_by(id=data["contact_id"]).first()
if contact:
db.session.delete(contact)
db.session.commit()
return jsonify({'message': 'Contact deleted successfully'})
else:
return jsonify({'error': 'Contact could not be found'})
@app.route('/transactions', methods=['POST'])
@extract_json
def create_transaction(data):
sender = User.query.filter_by(data['user_id']).first()
receiver = User.query.filter_by(data['user_id']).first()
if sender and receiver and sender.balance >= data['amount']:
transaction = Transaction(sender_id = data['sender_id'], receiver_id= data['receiver_id'], amount=data['amount'], description=data['description'], category=data['category'])
sender.balance -= transaction.amount
receiver.balance += transaction.amount
db.session.add(transaction)
db.session.add(sender)
db.session.add(receiver)
db.session.commit()
else:
return jsonify({"error": "insufficient funds"})
return jsonify({'message': 'Transaction created successfully'})
@app.route('/transactions/all', methods=['GET'])
@extract_json
def get_transactions(data):
user = User.query.all(data['user_id'])
if user:
transactions = Transaction.query.all(sender_id = data["user_id"])
return jsonify(transactions)
return jsonify({'error': 'User could not be found.'})
@app.route('/transactions', methods=['DELETE'])
@extract_json
def revert_transaction(data):
transaction = Transaction.query.get(data['transaction_id'])
if transaction:
sender = User.query.filter_by(transaction.sender_id).first()
receiver = User.query.filter_by(transaction.receiver_id).first()
if sender and receiver:
sender.balance += transaction.amount
receiver.balance -= transaction.amount
db.session.delete(transaction)
db.session.add(sender)
db.session.add(receiver)
db.session.commit()
return jsonify({'message': 'Transaction deleted successfully'})
return jsonify({"error": "Sender or Receiver does not exist."})
else:
return jsonify({"error": "Transaction does not exist."})
@app.route('/ai_chat', methods=['POST'])
@extract_json
def ai_chat(data):
fname = f"{random.randrange(0,10000)}_in.ogg"
with open(fname, "wb") as f:
f.write(base64.b64decode(data["audio"]))
# meta should include user_id
text = process_whisper(fname)
# text = data["prompt"]
print(text)
os.remove(fname)
history = data["history"]
# state = data["state"]
meta = data["meta"]
history, next_state, text_output, meta = sgraph.exec_state(history, text, meta, classifier)
binary_audio = tts(text_output)
meta["state"] = next_state
return jsonify({"text" : text_output, "audio_raw" : binary_audio,
"history": history, "next_state": next_state, "meta": meta})
if __name__ == '__main__':
app.run(debug=True, port=5000)