From 054cbf6a5dbb7731711f51da6c0aa630716aedd0 Mon Sep 17 00:00:00 2001 From: Patrick_Pluto Date: Fri, 20 Sep 2024 14:40:16 +0200 Subject: [PATCH 1/6] PUSH THE BACKEND TO THE LIMIT --- py/api.py | 39 +++++++++++++++++++++++++++++++++++++-- py/install.sh | 1 - py/requirements.txt | 3 ++- 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/py/api.py b/py/api.py index 1e1f785..aa226eb 100644 --- a/py/api.py +++ b/py/api.py @@ -3,10 +3,12 @@ from flask_cors import CORS import secrets from ai import AI from db import DB +from OpenSSL import crypto class API: def __init__(self): + self.crypt_size = 4096 self.app = Flask(__name__) self.ai_response = {} self.ai = AI() @@ -16,7 +18,7 @@ class API: def run(self): @self.app.route('/interstellar/api/ai_create', methods=['GET']) def create_ai(): - access_token = secrets.token_urlsafe(4096) + access_token = secrets.token_urlsafe(self.crypt_size) self.ai_response[access_token] = "" return jsonify({'status': 200, 'access_token': access_token}) @@ -51,7 +53,40 @@ class API: if action == "check_credentials": self.db.check_credentials(data) - ssl_context = ('cert.pem', 'key.pem') + email_address = "emailAddress" + common_name = "commonName" + country_name = "NT" + locality_name = "localityName" + state_or_province_name = "stateOrProvinceName" + organization_name = "organizationName" + organization_unit_name = "organizationUnitName" + serial_number = 0 + validity_start_in_seconds = 0 + validity_end_in_seconds = 10 * 365 * 24 * 60 * 60 + k = crypto.PKey() + k.generate_key(crypto.TYPE_RSA, 4096) + cert = crypto.X509() + cert.get_subject().C = country_name + cert.get_subject().ST = state_or_province_name + cert.get_subject().L = locality_name + cert.get_subject().O = organization_name + cert.get_subject().OU = organization_unit_name + cert.get_subject().CN = common_name + cert.get_subject().emailAddress = email_address + cert.set_serial_number(serial_number) + cert.gmtime_adj_notBefore(validity_start_in_seconds) + cert.gmtime_adj_notAfter(validity_end_in_seconds) + cert.set_issuer(cert.get_subject()) + cert.set_pubkey(k) + cert.sign(k, 'sha512') + + with open("cert.pem", "wt") as f: + f.write(crypto.dump_certificate(crypto.FILETYPE_PEM, cert).decode("utf-8")) + + with open("key.pem", "wt") as f: + f.write(crypto.dump_privatekey(crypto.FILETYPE_PEM, k).decode("utf-8")) + + ssl_context = ("cert.pem", "key.pem") self.app.run(debug=True, host='0.0.0.0', port=5000, ssl_context=ssl_context) diff --git a/py/install.sh b/py/install.sh index 31bb21e..1fbdcba 100644 --- a/py/install.sh +++ b/py/install.sh @@ -1,4 +1,3 @@ -openssl req -x509 -newkey rsa:4096 -nodes -keyout key.pem -out cert.pem -days 365 python -m venv venv source venv/bin/activate pip install -r requirements.txt diff --git a/py/requirements.txt b/py/requirements.txt index b4f811a..144c571 100644 --- a/py/requirements.txt +++ b/py/requirements.txt @@ -1,4 +1,5 @@ flask flask-cors ollama -mistralai \ No newline at end of file +mistralai +pyOpenSSL \ No newline at end of file From b924d30616d78eaa1da5d5cf0940a3b8eeee667a Mon Sep 17 00:00:00 2001 From: Patrick_Pluto Date: Fri, 20 Sep 2024 15:26:18 +0200 Subject: [PATCH 2/6] More Database Functions --- py/api.py | 4 +++- py/db.py | 16 +++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/py/api.py b/py/api.py index aa226eb..7b09e1d 100644 --- a/py/api.py +++ b/py/api.py @@ -49,7 +49,9 @@ class API: if action == "change_password": self.db.update_password(data) if action == "get_data": - self.db.get_additional_info(data) + self.db.get_data(data) + if action == "change_data": + self.db.change_data(data) if action == "check_credentials": self.db.check_credentials(data) diff --git a/py/db.py b/py/db.py index 37c9ca0..ef82111 100644 --- a/py/db.py +++ b/py/db.py @@ -19,9 +19,17 @@ class DB: user_data = {"hashed_password": hashed_password} self.database[username] = user_data + def change_data(self, data): + username = data.get['username'] + data = data.get['data'] + if not self.check_credentials(data): + return False + + self.database[username]['data'] = data + return True + def update_password(self, data): username = data.get['username'] - old_password = data.get['old_password'] new_password = data.get['new_password'] if not self.check_credentials(data): return False @@ -40,12 +48,10 @@ class DB: entered_hashed_password = self.hash_password(password) return stored_hashed_password == entered_hashed_password - def get_additional_info(self, data): + def get_data(self, data): username = data.get['username'] - password = data.get['password'] if not self.check_credentials(data): return None - send_back = self.database[username] - del send_back['hashed_password'] + send_back = self.database[username].get['data'] return send_back From 7b6f91c1170a293fb9a8ff7a22a81fdb72ee8fb5 Mon Sep 17 00:00:00 2001 From: Patrick_Pluto Date: Fri, 20 Sep 2024 15:46:02 +0200 Subject: [PATCH 3/6] fixed stuff with the backend and the pesky cpython cache --- .gitignore | 1 + py/ai.py | 4 ---- py/api.py | 15 ++++++++++----- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index ea5d9c5..93a7e59 100644 --- a/.gitignore +++ b/.gitignore @@ -38,6 +38,7 @@ next-env.d.ts .idea/ venv/ +__pycache__/ key.pem cert.pem diff --git a/py/ai.py b/py/ai.py index c51f2d2..0a57b57 100644 --- a/py/ai.py +++ b/py/ai.py @@ -12,13 +12,9 @@ class AI: options={"temperature": 0.5}, ) - for i in messages: - print(i) - return_class.ai_response[access_token] = "" for chunk in stream: - print(chunk['message']['content']) return_class.ai_response[access_token] += chunk['message']['content'] @staticmethod diff --git a/py/api.py b/py/api.py index 7b09e1d..646af55 100644 --- a/py/api.py +++ b/py/api.py @@ -26,11 +26,16 @@ class API: def send_ai(): data = request.get_json() messages = data.get('messages') + model_type = data.get('model_type') ai_model = data.get('ai_model') access_token = data.get('access_token') if access_token not in self.ai_response: return jsonify({'status': 401, 'error': 'Invalid access token'}) - self.ai.process_local(ai_model, messages, self, access_token) + + if model_type == "local": + self.ai.process_local(ai_model, messages, self, access_token) + if model_type == "mistral": + self.ai.process_mistralai(ai_model, messages, self, access_token) return jsonify({'status': 200}) @self.app.route('/interstellar/api/ai_get', methods=['GET']) @@ -46,13 +51,13 @@ class API: data = request.args.get('data') if action == "create_account": self.db.add_user(data) - if action == "change_password": + elif action == "change_password": self.db.update_password(data) - if action == "get_data": + elif action == "get_data": self.db.get_data(data) - if action == "change_data": + elif action == "change_data": self.db.change_data(data) - if action == "check_credentials": + elif action == "check_credentials": self.db.check_credentials(data) email_address = "emailAddress" From 6ed98894f33ed364d13f646badd1cb990648629a Mon Sep 17 00:00:00 2001 From: Patrick_Pluto Date: Fri, 20 Sep 2024 15:48:25 +0200 Subject: [PATCH 4/6] Update .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index ea5d9c5..93a7e59 100644 --- a/.gitignore +++ b/.gitignore @@ -38,6 +38,7 @@ next-env.d.ts .idea/ venv/ +__pycache__/ key.pem cert.pem From 579274000ce6c5e447a8b7ac0d2d5828ede1f394 Mon Sep 17 00:00:00 2001 From: Patrick_Pluto Date: Fri, 20 Sep 2024 15:48:43 +0200 Subject: [PATCH 5/6] Delete py/__pycache__/ai.cpython-312.pyc --- py/__pycache__/ai.cpython-312.pyc | Bin 1927 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 py/__pycache__/ai.cpython-312.pyc diff --git a/py/__pycache__/ai.cpython-312.pyc b/py/__pycache__/ai.cpython-312.pyc deleted file mode 100644 index a1c10a8f80335103a3b77aa85e3690ddecdfa8d8..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1927 zcmZ`)O=ufO6rR~X$@a>QWXFzwLRHr`75-~YBPfIhr}=RxHMrzr(XuSN>uA?2tvb6V zmXT>GJ_zbSOb>2P)v2^NhxXJSOHPGevcw=UZ6T0S=%FB20fSF{qt(iP>67-&%zJO% zo1OW-*XZIr_qj9H(vxeUjl+eqiPkS^?T zL2!mIaw+j|Hb`L_dEazBqS}jE;0<&u6txl?D1&vB;S65KTWGHpnGs;jofwNnJ|$3Y z{*D$dj73J^;o)ig1!@<07@DS7VU^GTZ3&gIj&)AwP5emMYvtJ#O85r)D2C9c=!s!? zN!)AY!f3lkK)P7sEGhIJww$gW%I6Odn_azdY;QI>#bQr{-Ji7+_Bjz04uxT`BxUPE7aH#|bV5m77 zaSER4CQUv=35B?CE{n9VvlW454r#MD-2p@czP z&w$EAQO(&xaaJ{F3Pl<#5T}{Zb~M%AxG;Zb=9GmUgZmk%Ptl+8f!*Pq;aXy#cJXqp z7xZZ5-6wsozvvJXon@)fjS@rU`QPRE!&e_H)Oi@yB{cU|S*}Ktel%Gb@uM@(FAkS) zvXJpFJN(3GBObXC%bG)kflRK2-+qZ=?AaNXx5@lGD!y5gm}n-TyUwX=0@KM zX0)xB*!*um0Xt&xhtrkb{n+@o@ml}D?#j-}7untXPX1~CMEUk1kGdybNGLH_8Q71f z%F?r; Date: Fri, 20 Sep 2024 16:06:40 +0200 Subject: [PATCH 6/6] It saves data now yay. --- py/api.py | 1 + py/db.py | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/py/api.py b/py/api.py index 646af55..b52870f 100644 --- a/py/api.py +++ b/py/api.py @@ -13,6 +13,7 @@ class API: self.ai_response = {} self.ai = AI() self.db = DB() + self.db.load_database() CORS(self.app) def run(self): diff --git a/py/db.py b/py/db.py index ef82111..76470c9 100644 --- a/py/db.py +++ b/py/db.py @@ -1,5 +1,5 @@ -import json import hashlib +import json class DB: @@ -26,6 +26,7 @@ class DB: return False self.database[username]['data'] = data + self.save_database() return True def update_password(self, data): @@ -36,6 +37,7 @@ class DB: hashed_new_password = self.hash_password(new_password) self.database[username].update({"hashed_password": hashed_new_password}) + self.save_database() return True def check_credentials(self, data): @@ -55,3 +57,14 @@ class DB: send_back = self.database[username].get['data'] return send_back + + def save_database(self): + with open("database.json", 'w') as file: + json.dump(self.database, file) + + def load_database(self): + try: + with open("database.json", 'r') as file: + self.database = json.load(file) + except FileNotFoundError: + pass