| from fastapi import UploadFile |
| from functools import partial |
| from hashlib import sha256 |
| from uuid import UUID |
| import aiofiles |
| import json |
| import re |
| from config import ( |
| logger |
| ) |
|
|
| _snake_1 = partial(re.compile(r'(.)((?<![^A-Za-z])[A-Z][a-z]+)').sub, r'\1_\2') |
| _snake_2 = partial(re.compile(r'([a-z0-9])([A-Z])').sub, r'\1_\2') |
|
|
|
|
| |
| |
| |
| def snake_case(string: str) -> str: |
| return _snake_2(_snake_1(string)).casefold() |
|
|
|
|
| |
| |
| |
| def is_uuid(uuid: str) -> bool: |
| uuid = str(uuid) if isinstance(uuid, UUID) else uuid |
| return re.match(r"^[0-9a-f]{8}-?[0-9a-f]{4}-?4[0-9a-f]{3}-?[89ab][0-9a-f]{3}-?[0-9a-f]{12}$", uuid) |
|
|
|
|
| |
| |
| |
| async def save_file(file: UploadFile, file_path: str): |
| async with aiofiles.open(file_path, 'wb') as f: |
| await f.write(await file.read()) |
|
|
|
|
| |
| |
| |
| def get_sha256(contents: bytes): |
| return sha256(contents).hexdigest() |
|
|
|
|
| |
| |
| |
| def get_file_hash( |
| file_path: str, |
| ): |
| with open(file_path, 'rb') as f: |
| file_hash = sha256(f.read()).hexdigest() |
|
|
| return file_hash |
|
|
|
|
| |
| |
| |
| def sanitize_output( |
| str_output: str |
| ): |
| |
| res = str_output.replace("\n", '') |
|
|
| |
| if res[0] == '?': |
| res = res[1:] |
|
|
| |
| try: |
| json.loads(res) |
| except json.JSONDecodeError: |
| raise ValueError(f'LLM response is not valid JSON: {res}') |
|
|
| if 'message' not in res or 'tags' not in res or 'is_escalate' not in res: |
| raise ValueError(f'LLM response is missing required fields: {res}') |
|
|
| logger.debug(f'Output: {res}') |
| return res |
|
|
|
|
| |
| |
| |
| def sanitize_input( |
| str_input: str |
| ): |
| |
| str_input = str_input.replace("'", "") |
|
|
| logger.debug(f'Input: {str_input}') |
| return str_input |
|
|
|
|