Spaces:
Sleeping
Sleeping
Shageenderan Sapai commited on
Commit ·
15aadee
1
Parent(s): e951db9
Added proper HTTP error codes
Browse files- .vscode/settings.json +7 -0
- app/__pycache__/assistants.cpython-312.pyc +0 -0
- app/__pycache__/main.cpython-312.pyc +0 -0
- app/__pycache__/user.cpython-312.pyc +0 -0
- app/__pycache__/utils.cpython-312.pyc +0 -0
- app/main.py +79 -21
- app/utils.py +9 -7
.vscode/settings.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"python.testing.pytestArgs": [
|
| 3 |
+
"."
|
| 4 |
+
],
|
| 5 |
+
"python.testing.unittestEnabled": false,
|
| 6 |
+
"python.testing.pytestEnabled": true
|
| 7 |
+
}
|
app/__pycache__/assistants.cpython-312.pyc
CHANGED
|
Binary files a/app/__pycache__/assistants.cpython-312.pyc and b/app/__pycache__/assistants.cpython-312.pyc differ
|
|
|
app/__pycache__/main.cpython-312.pyc
CHANGED
|
Binary files a/app/__pycache__/main.cpython-312.pyc and b/app/__pycache__/main.cpython-312.pyc differ
|
|
|
app/__pycache__/user.cpython-312.pyc
CHANGED
|
Binary files a/app/__pycache__/user.cpython-312.pyc and b/app/__pycache__/user.cpython-312.pyc differ
|
|
|
app/__pycache__/utils.cpython-312.pyc
CHANGED
|
Binary files a/app/__pycache__/utils.cpython-312.pyc and b/app/__pycache__/utils.cpython-312.pyc differ
|
|
|
app/main.py
CHANGED
|
@@ -47,6 +47,12 @@ class ChangeDateItem(BaseModel):
|
|
| 47 |
user_id: str
|
| 48 |
date: str
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
@app.get("/ok")
|
| 51 |
def ok_endpoint():
|
| 52 |
return {"message": "ok"}
|
|
@@ -56,8 +62,16 @@ def get_user_by_id(user_id: str, api_key: str = Security(get_api_key)):
|
|
| 56 |
try:
|
| 57 |
user = get_user(user_id)
|
| 58 |
return {"user_info": user.user_info, "user_messages": user.get_messages()}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
except Exception as e:
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
@app.post("/create_user")
|
| 63 |
def create_user(request: CreateUserItem, api_key: str = Security(get_api_key)):
|
|
@@ -67,22 +81,34 @@ def create_user(request: CreateUserItem, api_key: str = Security(get_api_key)):
|
|
| 67 |
# check if user exists by looking for pickle file in users/data
|
| 68 |
if os.path.exists(f'users/data/{request.user_id}.pkl'):
|
| 69 |
return {"message": f"[OK] User already exists: {request.user_id}"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
else:
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
# upload user pickle file to s3 bucket
|
| 77 |
-
filename = f'{user.user_id}.pkl'
|
| 78 |
-
response = upload_file_to_s3(filename)
|
| 79 |
-
|
| 80 |
-
# move user pickle from to_upload to data
|
| 81 |
-
if response:
|
| 82 |
-
return {"message": f"[OK] User created: {user.user_id}"}
|
| 83 |
|
| 84 |
except Exception as e:
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
@app.post("/chat")
|
| 88 |
def chat(request: ChatItem, api_key: str = Security(get_api_key)):
|
|
@@ -91,8 +117,16 @@ def chat(request: ChatItem, api_key: str = Security(get_api_key)):
|
|
| 91 |
user = get_user(request.user_id)
|
| 92 |
response = user.send_message(request.message)
|
| 93 |
return {"response": response}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
except Exception as e:
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
@app.post("/change_date")
|
| 98 |
# request = {"date": "2022-01-01 Wed 10:00:00"} i.e. %Y-%m-%d %a %H:%M:%S
|
|
@@ -115,13 +149,29 @@ def change_date(request: ChangeDateItem, api_key: str = Security(get_api_key)):
|
|
| 115 |
|
| 116 |
# Update user
|
| 117 |
update = update_user(user)
|
| 118 |
-
if
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
| 122 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
except Exception as e:
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
@app.post("/reset_user")
|
| 127 |
def reset_user_messages(request: CreateUserItem, api_key: str = Security(get_api_key)):
|
|
@@ -131,5 +181,13 @@ def reset_user_messages(request: CreateUserItem, api_key: str = Security(get_api
|
|
| 131 |
user.reset()
|
| 132 |
update_user(user)
|
| 133 |
return {"response": "ok"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
except Exception as e:
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
user_id: str
|
| 48 |
date: str
|
| 49 |
|
| 50 |
+
class ErrorResponse(BaseModel):
|
| 51 |
+
status: str = "error"
|
| 52 |
+
code: int
|
| 53 |
+
message: str
|
| 54 |
+
timestamp: datetime = datetime.now()
|
| 55 |
+
|
| 56 |
@app.get("/ok")
|
| 57 |
def ok_endpoint():
|
| 58 |
return {"message": "ok"}
|
|
|
|
| 62 |
try:
|
| 63 |
user = get_user(user_id)
|
| 64 |
return {"user_info": user.user_info, "user_messages": user.get_messages()}
|
| 65 |
+
except LookupError:
|
| 66 |
+
raise HTTPException(
|
| 67 |
+
status_code=status.HTTP_404_NOT_FOUND,
|
| 68 |
+
detail=f"User with ID {user_id} not found"
|
| 69 |
+
)
|
| 70 |
except Exception as e:
|
| 71 |
+
raise HTTPException(
|
| 72 |
+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 73 |
+
detail=str(e)
|
| 74 |
+
)
|
| 75 |
|
| 76 |
@app.post("/create_user")
|
| 77 |
def create_user(request: CreateUserItem, api_key: str = Security(get_api_key)):
|
|
|
|
| 81 |
# check if user exists by looking for pickle file in users/data
|
| 82 |
if os.path.exists(f'users/data/{request.user_id}.pkl'):
|
| 83 |
return {"message": f"[OK] User already exists: {request.user_id}"}
|
| 84 |
+
|
| 85 |
+
user_info, matters_most = get_user_info(request.user_id)
|
| 86 |
+
if not user_info:
|
| 87 |
+
raise HTTPException(
|
| 88 |
+
status_code=status.HTTP_400_BAD_REQUEST,
|
| 89 |
+
detail="Could not fetch user information from DB"
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
user = User(request.user_id, user_info, client, openai_assistant, matters_most)
|
| 93 |
+
user.save_user()
|
| 94 |
+
|
| 95 |
+
# upload user pickle file to s3 bucket
|
| 96 |
+
filename = f'{user.user_id}.pkl'
|
| 97 |
+
upload = upload_file_to_s3(filename)
|
| 98 |
+
|
| 99 |
+
if upload == True:
|
| 100 |
+
return {"message": f"[OK] User created: {user.user_id}"}
|
| 101 |
else:
|
| 102 |
+
raise HTTPException(
|
| 103 |
+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 104 |
+
detail="Failed to upload user pickle to S3"
|
| 105 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
except Exception as e:
|
| 108 |
+
raise HTTPException(
|
| 109 |
+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 110 |
+
detail=str(e)
|
| 111 |
+
)
|
| 112 |
|
| 113 |
@app.post("/chat")
|
| 114 |
def chat(request: ChatItem, api_key: str = Security(get_api_key)):
|
|
|
|
| 117 |
user = get_user(request.user_id)
|
| 118 |
response = user.send_message(request.message)
|
| 119 |
return {"response": response}
|
| 120 |
+
except LookupError:
|
| 121 |
+
raise HTTPException(
|
| 122 |
+
status_code=status.HTTP_404_NOT_FOUND,
|
| 123 |
+
detail=f"User with ID {request.user_id} not found"
|
| 124 |
+
)
|
| 125 |
except Exception as e:
|
| 126 |
+
raise HTTPException(
|
| 127 |
+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 128 |
+
detail=str(e)
|
| 129 |
+
)
|
| 130 |
|
| 131 |
@app.post("/change_date")
|
| 132 |
# request = {"date": "2022-01-01 Wed 10:00:00"} i.e. %Y-%m-%d %a %H:%M:%S
|
|
|
|
| 149 |
|
| 150 |
# Update user
|
| 151 |
update = update_user(user)
|
| 152 |
+
if not update:
|
| 153 |
+
raise HTTPException(
|
| 154 |
+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 155 |
+
detail=f"Failed to update user [{user.user_id}]"
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
return {"response": response}
|
| 159 |
|
| 160 |
+
except ValueError as e:
|
| 161 |
+
raise HTTPException(
|
| 162 |
+
status_code=status.HTTP_400_BAD_REQUEST,
|
| 163 |
+
detail=str(e)
|
| 164 |
+
)
|
| 165 |
+
except LookupError:
|
| 166 |
+
raise HTTPException(
|
| 167 |
+
status_code=status.HTTP_404_NOT_FOUND,
|
| 168 |
+
detail=f"User with ID {request.user_id} not found"
|
| 169 |
+
)
|
| 170 |
except Exception as e:
|
| 171 |
+
raise HTTPException(
|
| 172 |
+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 173 |
+
detail=str(e)
|
| 174 |
+
)
|
| 175 |
|
| 176 |
@app.post("/reset_user")
|
| 177 |
def reset_user_messages(request: CreateUserItem, api_key: str = Security(get_api_key)):
|
|
|
|
| 181 |
user.reset()
|
| 182 |
update_user(user)
|
| 183 |
return {"response": "ok"}
|
| 184 |
+
except LookupError:
|
| 185 |
+
raise HTTPException(
|
| 186 |
+
status_code=status.HTTP_404_NOT_FOUND,
|
| 187 |
+
detail=f"User with ID {request.user_id} not found"
|
| 188 |
+
)
|
| 189 |
except Exception as e:
|
| 190 |
+
raise HTTPException(
|
| 191 |
+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 192 |
+
detail=str(e)
|
| 193 |
+
)
|
app/utils.py
CHANGED
|
@@ -55,7 +55,7 @@ def get_user(user_id):
|
|
| 55 |
user = User.load_user(user_id, client)
|
| 56 |
return user
|
| 57 |
else:
|
| 58 |
-
raise
|
| 59 |
|
| 60 |
def get_api_key(api_key_header: str = Security(api_key_header)) -> str:
|
| 61 |
if api_key_header in api_keys:
|
|
@@ -140,10 +140,10 @@ def upload_file_to_s3(filename):
|
|
| 140 |
)
|
| 141 |
return True
|
| 142 |
except (FileNotFoundError, NoCredentialsError, PartialCredentialsError) as e:
|
| 143 |
-
|
| 144 |
-
return False
|
| 145 |
|
| 146 |
def download_file_from_s3(filename, bucket):
|
|
|
|
| 147 |
try:
|
| 148 |
if AWS_ACCESS_KEY and AWS_SECRET_KEY:
|
| 149 |
session = boto3.session.Session(
|
|
@@ -155,11 +155,13 @@ def download_file_from_s3(filename, bucket):
|
|
| 155 |
session = boto3.session.Session()
|
| 156 |
|
| 157 |
s3_client = session.client('s3')
|
| 158 |
-
with open(
|
| 159 |
s3_client.download_fileobj(bucket, f"staging/users/{filename}", f)
|
| 160 |
return True
|
| 161 |
-
except
|
| 162 |
print(f"Error downloading file from S3: {e}")
|
|
|
|
|
|
|
| 163 |
return False
|
| 164 |
|
| 165 |
def update_user(user):
|
|
@@ -167,9 +169,9 @@ def update_user(user):
|
|
| 167 |
filename = f'{user.user_id}.pkl'
|
| 168 |
if upload_file_to_s3(filename):
|
| 169 |
os.remove(os.path.join('users', 'data', filename))
|
| 170 |
-
return
|
| 171 |
else:
|
| 172 |
-
|
| 173 |
|
| 174 |
def upload_mementos_to_db(user_id):
|
| 175 |
# Database connection parameters
|
|
|
|
| 55 |
user = User.load_user(user_id, client)
|
| 56 |
return user
|
| 57 |
else:
|
| 58 |
+
raise LookupError(f"User [{user_id}] pickle does not exist in S3")
|
| 59 |
|
| 60 |
def get_api_key(api_key_header: str = Security(api_key_header)) -> str:
|
| 61 |
if api_key_header in api_keys:
|
|
|
|
| 140 |
)
|
| 141 |
return True
|
| 142 |
except (FileNotFoundError, NoCredentialsError, PartialCredentialsError) as e:
|
| 143 |
+
return False, e
|
|
|
|
| 144 |
|
| 145 |
def download_file_from_s3(filename, bucket):
|
| 146 |
+
file_path = os.path.join('users', 'data', filename)
|
| 147 |
try:
|
| 148 |
if AWS_ACCESS_KEY and AWS_SECRET_KEY:
|
| 149 |
session = boto3.session.Session(
|
|
|
|
| 155 |
session = boto3.session.Session()
|
| 156 |
|
| 157 |
s3_client = session.client('s3')
|
| 158 |
+
with open(file_path, 'wb') as f:
|
| 159 |
s3_client.download_fileobj(bucket, f"staging/users/{filename}", f)
|
| 160 |
return True
|
| 161 |
+
except Exception as e:
|
| 162 |
print(f"Error downloading file from S3: {e}")
|
| 163 |
+
if os.path.exists(file_path):
|
| 164 |
+
os.remove(file_path)
|
| 165 |
return False
|
| 166 |
|
| 167 |
def update_user(user):
|
|
|
|
| 169 |
filename = f'{user.user_id}.pkl'
|
| 170 |
if upload_file_to_s3(filename):
|
| 171 |
os.remove(os.path.join('users', 'data', filename))
|
| 172 |
+
return True
|
| 173 |
else:
|
| 174 |
+
return False
|
| 175 |
|
| 176 |
def upload_mementos_to_db(user_id):
|
| 177 |
# Database connection parameters
|