mirror of
https://github.com/StanGirard/quivr.git
synced 2024-12-26 04:42:31 +03:00
33f49ee289
* feat: user can create api keys * fix: linting on build * Update backend/routes/api_key_routes.py * chore: rename and refactor AuthBearer * chore: add types
44 lines
2.3 KiB
Python
44 lines
2.3 KiB
Python
import os
|
|
|
|
from auth.auth_bearer import AuthBearer, get_current_user
|
|
from fastapi import APIRouter, Depends, Request, UploadFile
|
|
from models.users import User
|
|
from utils.file import convert_bytes, get_file_size
|
|
from utils.processors import filter_file
|
|
from utils.vectors import CommonsDep
|
|
|
|
upload_router = APIRouter()
|
|
|
|
def get_user_vectors(commons, user):
|
|
user_vectors_response = commons['supabase'].table("vectors").select(
|
|
"name:metadata->>file_name, size:metadata->>file_size", count="exact") \
|
|
.filter("user_id", "eq", user.email)\
|
|
.execute()
|
|
documents = user_vectors_response.data # Access the data from the response
|
|
# Convert each dictionary to a tuple of items, then to a set to remove duplicates, and then back to a dictionary
|
|
user_unique_vectors = [dict(t) for t in set(tuple(d.items()) for d in documents)]
|
|
return user_unique_vectors
|
|
|
|
def calculate_remaining_space(request, max_brain_size, max_brain_size_with_own_key, current_brain_size):
|
|
remaining_free_space = float(max_brain_size_with_own_key) - current_brain_size if request.headers.get('Openai-Api-Key') else float(max_brain_size) - current_brain_size
|
|
return remaining_free_space
|
|
|
|
@upload_router.post("/upload", dependencies=[Depends(AuthBearer())])
|
|
async def upload_file(request: Request, commons: CommonsDep, file: UploadFile, enable_summarization: bool = False, current_user: User = Depends(get_current_user)):
|
|
max_brain_size = os.getenv("MAX_BRAIN_SIZE")
|
|
max_brain_size_with_own_key = os.getenv("MAX_BRAIN_SIZE_WITH_KEY",209715200)
|
|
|
|
user_unique_vectors = get_user_vectors(commons, current_user)
|
|
current_brain_size = sum(float(doc['size']) for doc in user_unique_vectors)
|
|
|
|
remaining_free_space = calculate_remaining_space(request, max_brain_size, max_brain_size_with_own_key, current_brain_size)
|
|
|
|
file_size = get_file_size(file)
|
|
|
|
if remaining_free_space - file_size < 0:
|
|
message = {"message": f"❌ User's brain will exceed maximum capacity with this upload. Maximum file allowed is : {convert_bytes(remaining_free_space)}", "type": "error"}
|
|
else:
|
|
message = await filter_file(file, enable_summarization, commons['supabase'], current_user, openai_api_key=request.headers.get('Openai-Api-Key', None))
|
|
|
|
return message
|