2023-06-28 20:39:27 +03:00
|
|
|
from uuid import UUID
|
|
|
|
|
2023-06-14 22:21:13 +03:00
|
|
|
from auth.auth_bearer import AuthBearer, get_current_user
|
2023-06-28 20:39:27 +03:00
|
|
|
from fastapi import APIRouter, Depends, Query
|
|
|
|
from models.brains import Brain
|
|
|
|
from models.settings import common_dependencies
|
2023-06-11 00:59:16 +03:00
|
|
|
from models.users import User
|
|
|
|
|
|
|
|
explore_router = APIRouter()
|
|
|
|
|
2023-06-22 18:50:06 +03:00
|
|
|
|
2023-06-29 19:26:03 +03:00
|
|
|
@explore_router.get("/explore/", dependencies=[Depends(AuthBearer())], tags=["Explore"])
|
|
|
|
async def explore_endpoint(brain_id: UUID = Query(..., description="The ID of the brain"), current_user: User = Depends(get_current_user)):
|
2023-06-15 15:43:40 +03:00
|
|
|
"""
|
|
|
|
Retrieve and explore unique user data vectors.
|
|
|
|
"""
|
2023-06-28 20:39:27 +03:00
|
|
|
brain = Brain(id=brain_id)
|
|
|
|
unique_data = brain.get_unique_brain_files()
|
|
|
|
|
2023-06-22 18:50:06 +03:00
|
|
|
unique_data.sort(key=lambda x: int(x["size"]), reverse=True)
|
2023-06-11 00:59:16 +03:00
|
|
|
return {"documents": unique_data}
|
|
|
|
|
|
|
|
|
2023-06-22 18:50:06 +03:00
|
|
|
@explore_router.delete(
|
2023-06-30 10:08:40 +03:00
|
|
|
"/explore/{file_name}/", dependencies=[Depends(AuthBearer())], tags=["Explore"]
|
2023-06-22 18:50:06 +03:00
|
|
|
)
|
2023-06-28 20:39:27 +03:00
|
|
|
async def delete_endpoint(file_name: str, current_user: User = Depends(get_current_user), brain_id: UUID = Query(..., description="The ID of the brain")):
|
2023-06-15 15:43:40 +03:00
|
|
|
"""
|
|
|
|
Delete a specific user file by file name.
|
|
|
|
"""
|
2023-06-28 20:39:27 +03:00
|
|
|
brain = Brain(id=brain_id)
|
|
|
|
brain.delete_file_from_brain(file_name)
|
|
|
|
|
|
|
|
return {"message": f"{file_name} of brain {brain_id} has been deleted by user {current_user.email}."}
|
2023-06-11 00:59:16 +03:00
|
|
|
|
2023-06-22 18:50:06 +03:00
|
|
|
|
|
|
|
@explore_router.get(
|
2023-06-30 10:08:40 +03:00
|
|
|
"/explore/{file_name}/", dependencies=[Depends(AuthBearer())], tags=["Explore"]
|
2023-06-22 18:50:06 +03:00
|
|
|
)
|
|
|
|
async def download_endpoint(
|
|
|
|
file_name: str, current_user: User = Depends(get_current_user)
|
|
|
|
):
|
2023-06-15 15:43:40 +03:00
|
|
|
"""
|
|
|
|
Download a specific user file by file name.
|
|
|
|
"""
|
2023-06-28 20:39:27 +03:00
|
|
|
# check if user has the right to get the file: add brain_id to the query
|
|
|
|
|
2023-06-19 23:54:01 +03:00
|
|
|
commons = common_dependencies()
|
2023-06-22 18:50:06 +03:00
|
|
|
response = (
|
|
|
|
commons["supabase"]
|
|
|
|
.table("vectors")
|
|
|
|
.select(
|
|
|
|
"metadata->>file_name, metadata->>file_size, metadata->>file_extension, metadata->>file_url",
|
|
|
|
"content",
|
|
|
|
)
|
2023-06-28 20:39:27 +03:00
|
|
|
.match({"metadata->>file_name": file_name})
|
2023-06-22 18:50:06 +03:00
|
|
|
.execute()
|
|
|
|
)
|
2023-06-11 00:59:16 +03:00
|
|
|
documents = response.data
|
|
|
|
return {"documents": documents}
|