Spaces:
Running
Running
TahaFawzyElshrif commited on
Commit ·
3ce4cf9
1
Parent(s): 55d5564
working on queue, finalization
Browse files- Consumer.py +13 -17
- app.py +15 -63
- encryption_utils.py +45 -0
- utils.py +17 -0
Consumer.py
CHANGED
|
@@ -7,27 +7,23 @@ import json
|
|
| 7 |
from agent.agent_graph.StateTasks import ProblemState
|
| 8 |
import argparse
|
| 9 |
import redis
|
| 10 |
-
|
| 11 |
-
argparse_model = argparse.ArgumentParser()
|
| 12 |
-
argparse_model.add_argument("--id", type=int, default=0, help="Consumer ID")
|
| 13 |
-
|
| 14 |
-
"""Basic connection example.
|
| 15 |
-
"""
|
| 16 |
-
redis_host = os.environ["REDIS_HOST"]
|
| 17 |
-
redis_port = os.environ["REDIS_PORT"]
|
| 18 |
-
redis_password = os.environ["REDIS_PASSWORD"]
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
consumer_id = argparse_model.parse_args().id
|
| 27 |
|
| 28 |
|
| 29 |
RABBITMQ_URL = os.environ["RABBITMQ_URL"]
|
| 30 |
QUEUE_NAME = os.environ["QUEUE_NAME"]
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
|
| 33 |
|
|
@@ -58,12 +54,12 @@ def model_call(request):
|
|
| 58 |
password=redis_password,
|
| 59 |
)
|
| 60 |
|
| 61 |
-
user_id = "
|
| 62 |
-
msg_id = "
|
| 63 |
success = r.set(f'ANSWER_FOR_USER_ID{user_id}_OF_{msg_id}',json.dumps(answer))
|
| 64 |
|
| 65 |
|
| 66 |
-
return {"
|
| 67 |
|
| 68 |
def get_connection():
|
| 69 |
params = pika.URLParameters(RABBITMQ_URL)
|
|
|
|
| 7 |
from agent.agent_graph.StateTasks import ProblemState
|
| 8 |
import argparse
|
| 9 |
import redis
|
| 10 |
+
from encryption_utils import decrypt_token_from_json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
|
| 13 |
+
##################################################
|
| 14 |
+
# VARIABLES
|
| 15 |
+
##################################################
|
| 16 |
+
# args for this file
|
| 17 |
+
argparse_model = argparse.ArgumentParser()
|
| 18 |
+
argparse_model.add_argument("--id", type=int, default=0, help="Consumer ID")
|
| 19 |
consumer_id = argparse_model.parse_args().id
|
| 20 |
|
| 21 |
|
| 22 |
RABBITMQ_URL = os.environ["RABBITMQ_URL"]
|
| 23 |
QUEUE_NAME = os.environ["QUEUE_NAME"]
|
| 24 |
+
redis_host = os.environ["REDIS_HOST"]
|
| 25 |
+
redis_port = os.environ["REDIS_PORT"]
|
| 26 |
+
redis_password = os.environ["REDIS_PASSWORD"]
|
| 27 |
|
| 28 |
|
| 29 |
|
|
|
|
| 54 |
password=redis_password,
|
| 55 |
)
|
| 56 |
|
| 57 |
+
user_id = request["user_id"]
|
| 58 |
+
msg_id = request["msg_id"]
|
| 59 |
success = r.set(f'ANSWER_FOR_USER_ID{user_id}_OF_{msg_id}',json.dumps(answer))
|
| 60 |
|
| 61 |
|
| 62 |
+
return {"STATUS": success , "user_id": user_id, "msg_id": msg_id}
|
| 63 |
|
| 64 |
def get_connection():
|
| 65 |
params = pika.URLParameters(RABBITMQ_URL)
|
app.py
CHANGED
|
@@ -6,11 +6,21 @@ import sys
|
|
| 6 |
import os
|
| 7 |
import json
|
| 8 |
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')))
|
| 9 |
-
from pydantic import BaseModel
|
| 10 |
from agent.agent_graph.StateTasks import ProblemState
|
| 11 |
import subprocess
|
| 12 |
from Queue_Producer import send_message
|
| 13 |
import redis
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
##################################################
|
| 15 |
# START CONSUMERS in a separate process
|
| 16 |
##################################################
|
|
@@ -19,11 +29,8 @@ for i in range(1,4): # Start 3 consumers
|
|
| 19 |
|
| 20 |
|
| 21 |
##################################################
|
| 22 |
-
# START API
|
| 23 |
##################################################
|
| 24 |
-
redis_host = os.environ["REDIS_HOST"]
|
| 25 |
-
redis_port = os.environ["REDIS_PORT"]
|
| 26 |
-
redis_password = os.environ["REDIS_PASSWORD"]
|
| 27 |
|
| 28 |
# Create Redis connection (global to make the get very light)
|
| 29 |
redis_conn = redis.Redis(
|
|
@@ -33,68 +40,13 @@ redis_conn = redis.Redis(
|
|
| 33 |
username="default",
|
| 34 |
password=redis_password,
|
| 35 |
)
|
|
|
|
|
|
|
|
|
|
| 36 |
# Create app instance
|
| 37 |
app = FastAPI()
|
| 38 |
print("Starting API Server...")
|
| 39 |
|
| 40 |
-
class RequestModel(BaseModel):
|
| 41 |
-
prompt: str
|
| 42 |
-
ht_token_encrypted_dumped : str
|
| 43 |
-
user_email : str
|
| 44 |
-
user_name : str
|
| 45 |
-
memory: list[str]
|
| 46 |
-
last_state : str
|
| 47 |
-
|
| 48 |
-
class RequestAnswer(BaseModel):
|
| 49 |
-
user_id: str
|
| 50 |
-
msg_id:str
|
| 51 |
-
|
| 52 |
-
##################################################
|
| 53 |
-
import os
|
| 54 |
-
import hashlib
|
| 55 |
-
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
# ===== Key (same idea as Node) =====
|
| 59 |
-
def get_key():
|
| 60 |
-
raw_key = os.environ["TOKEN_KEY_ENCRYPTION"].encode()
|
| 61 |
-
return hashlib.sha256(raw_key).digest() # 32 bytes
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
# ===== Encrypt =====
|
| 65 |
-
def encrypt_token_to_json(token: str) -> dict:
|
| 66 |
-
key = get_key()
|
| 67 |
-
aesgcm = AESGCM(key)
|
| 68 |
-
|
| 69 |
-
iv = os.urandom(12) # same as crypto.randomBytes(12)
|
| 70 |
-
|
| 71 |
-
encrypted = aesgcm.encrypt(iv, token.encode(), None)
|
| 72 |
-
|
| 73 |
-
# في AESGCM في Python: التاج (tag) بيكون في آخر 16 بايت
|
| 74 |
-
ciphertext = encrypted[:-16]
|
| 75 |
-
tag = encrypted[-16:]
|
| 76 |
-
|
| 77 |
-
return {
|
| 78 |
-
"iv": iv.hex(),
|
| 79 |
-
"data": ciphertext.hex(),
|
| 80 |
-
"tag": tag.hex(),
|
| 81 |
-
}
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
# ===== Decrypt =====
|
| 85 |
-
def decrypt_token_from_json(enc: dict) -> str:
|
| 86 |
-
key = get_key()
|
| 87 |
-
aesgcm = AESGCM(key)
|
| 88 |
-
|
| 89 |
-
iv = bytes.fromhex(enc["iv"])
|
| 90 |
-
ciphertext = bytes.fromhex(enc["data"])
|
| 91 |
-
tag = bytes.fromhex(enc["tag"])
|
| 92 |
-
|
| 93 |
-
encrypted = ciphertext + tag
|
| 94 |
-
|
| 95 |
-
decrypted = aesgcm.decrypt(iv, encrypted, None)
|
| 96 |
-
|
| 97 |
-
return decrypted.decode()
|
| 98 |
|
| 99 |
##################################################
|
| 100 |
# ROUTES
|
|
|
|
| 6 |
import os
|
| 7 |
import json
|
| 8 |
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')))
|
|
|
|
| 9 |
from agent.agent_graph.StateTasks import ProblemState
|
| 10 |
import subprocess
|
| 11 |
from Queue_Producer import send_message
|
| 12 |
import redis
|
| 13 |
+
from utils import RequestModel, RequestAnswer
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
##################################################
|
| 17 |
+
# VARIABLES
|
| 18 |
+
##################################################
|
| 19 |
+
redis_host = os.environ["REDIS_HOST"]
|
| 20 |
+
redis_port = os.environ["REDIS_PORT"]
|
| 21 |
+
redis_password = os.environ["REDIS_PASSWORD"]
|
| 22 |
+
|
| 23 |
+
|
| 24 |
##################################################
|
| 25 |
# START CONSUMERS in a separate process
|
| 26 |
##################################################
|
|
|
|
| 29 |
|
| 30 |
|
| 31 |
##################################################
|
| 32 |
+
# START API and METHODS
|
| 33 |
##################################################
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
# Create Redis connection (global to make the get very light)
|
| 36 |
redis_conn = redis.Redis(
|
|
|
|
| 40 |
username="default",
|
| 41 |
password=redis_password,
|
| 42 |
)
|
| 43 |
+
|
| 44 |
+
# model and rag are not global for better security ,at least for this version
|
| 45 |
+
|
| 46 |
# Create app instance
|
| 47 |
app = FastAPI()
|
| 48 |
print("Starting API Server...")
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
##################################################
|
| 52 |
# ROUTES
|
encryption_utils.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import hashlib
|
| 3 |
+
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
# ===== Key (same idea as Node) =====
|
| 7 |
+
def get_key():
|
| 8 |
+
raw_key = os.environ["TOKEN_KEY_ENCRYPTION"].encode()
|
| 9 |
+
return hashlib.sha256(raw_key).digest() # 32 bytes
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
# ===== Encrypt =====
|
| 13 |
+
def encrypt_token_to_json(token: str) -> dict:
|
| 14 |
+
key = get_key()
|
| 15 |
+
aesgcm = AESGCM(key)
|
| 16 |
+
|
| 17 |
+
iv = os.urandom(12) # same as crypto.randomBytes(12)
|
| 18 |
+
|
| 19 |
+
encrypted = aesgcm.encrypt(iv, token.encode(), None)
|
| 20 |
+
|
| 21 |
+
# في AESGCM في Python: التاج (tag) بيكون في آخر 16 بايت
|
| 22 |
+
ciphertext = encrypted[:-16]
|
| 23 |
+
tag = encrypted[-16:]
|
| 24 |
+
|
| 25 |
+
return {
|
| 26 |
+
"iv": iv.hex(),
|
| 27 |
+
"data": ciphertext.hex(),
|
| 28 |
+
"tag": tag.hex(),
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
# ===== Decrypt =====
|
| 33 |
+
def decrypt_token_from_json(enc: dict) -> str:
|
| 34 |
+
key = get_key()
|
| 35 |
+
aesgcm = AESGCM(key)
|
| 36 |
+
|
| 37 |
+
iv = bytes.fromhex(enc["iv"])
|
| 38 |
+
ciphertext = bytes.fromhex(enc["data"])
|
| 39 |
+
tag = bytes.fromhex(enc["tag"])
|
| 40 |
+
|
| 41 |
+
encrypted = ciphertext + tag
|
| 42 |
+
|
| 43 |
+
decrypted = aesgcm.decrypt(iv, encrypted, None)
|
| 44 |
+
|
| 45 |
+
return decrypted.decode()
|
utils.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pydantic import BaseModel
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
class RequestModel(BaseModel):
|
| 5 |
+
prompt: str
|
| 6 |
+
ht_token_encrypted_dumped : str
|
| 7 |
+
user_email : str
|
| 8 |
+
user_name : str
|
| 9 |
+
user_id : str
|
| 10 |
+
msg_id : str
|
| 11 |
+
memory: list[str]
|
| 12 |
+
last_state : str
|
| 13 |
+
|
| 14 |
+
class RequestAnswer(BaseModel):
|
| 15 |
+
user_id: str
|
| 16 |
+
msg_id:str
|
| 17 |
+
|