2023-05-18 02:22:13 +03:00
|
|
|
# from stats import add_usage
|
|
|
|
import asyncio
|
|
|
|
import os
|
|
|
|
import tempfile
|
|
|
|
import time
|
2023-05-31 14:51:23 +03:00
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
from fastapi import UploadFile
|
|
|
|
from langchain.schema import Document
|
|
|
|
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
2023-06-17 00:36:53 +03:00
|
|
|
from utils.common import CommonsDep
|
2023-06-04 00:12:42 +03:00
|
|
|
from utils.file import compute_sha1_from_content, compute_sha1_from_file
|
2023-06-17 00:36:53 +03:00
|
|
|
from utils.vectors import create_summary, create_vector
|
2023-05-18 02:22:13 +03:00
|
|
|
|
2023-05-22 09:39:55 +03:00
|
|
|
|
2023-06-17 00:36:53 +03:00
|
|
|
async def process_file(commons: CommonsDep, file: UploadFile, loader_class, file_suffix, enable_summarization, user, user_openai_api_key):
|
2023-05-18 02:22:13 +03:00
|
|
|
documents = []
|
|
|
|
file_name = file.filename
|
|
|
|
file_size = file.file._file.tell() # Getting the size of the file
|
|
|
|
dateshort = time.strftime("%Y%m%d")
|
2023-05-22 09:39:55 +03:00
|
|
|
|
2023-05-18 02:22:13 +03:00
|
|
|
# Here, we're writing the uploaded file to a temporary file, so we can use it with your existing code.
|
|
|
|
with tempfile.NamedTemporaryFile(delete=False, suffix=file.filename) as tmp_file:
|
|
|
|
await file.seek(0)
|
|
|
|
content = await file.read()
|
|
|
|
tmp_file.write(content)
|
|
|
|
tmp_file.flush()
|
2023-05-22 09:39:55 +03:00
|
|
|
|
2023-05-18 02:22:13 +03:00
|
|
|
loader = loader_class(tmp_file.name)
|
|
|
|
documents = loader.load()
|
2023-05-22 09:39:55 +03:00
|
|
|
# Ensure this function works with FastAPI
|
|
|
|
file_sha1 = compute_sha1_from_file(tmp_file.name)
|
2023-05-18 02:22:13 +03:00
|
|
|
|
|
|
|
os.remove(tmp_file.name)
|
2023-06-17 02:32:03 +03:00
|
|
|
chunk_size = 1000
|
2023-05-18 02:22:13 +03:00
|
|
|
chunk_overlap = 0
|
|
|
|
|
2023-05-22 09:39:55 +03:00
|
|
|
text_splitter = RecursiveCharacterTextSplitter.from_tiktoken_encoder(
|
|
|
|
chunk_size=chunk_size, chunk_overlap=chunk_overlap)
|
2023-05-18 02:22:13 +03:00
|
|
|
|
2023-05-22 09:39:55 +03:00
|
|
|
documents = text_splitter.split_documents(documents)
|
|
|
|
|
|
|
|
for doc in documents:
|
|
|
|
metadata = {
|
|
|
|
"file_sha1": file_sha1,
|
|
|
|
"file_size": file_size,
|
|
|
|
"file_name": file_name,
|
|
|
|
"chunk_size": chunk_size,
|
|
|
|
"chunk_overlap": chunk_overlap,
|
|
|
|
"date": dateshort,
|
|
|
|
"summarization": "true" if enable_summarization else "false"
|
|
|
|
}
|
|
|
|
doc_with_metadata = Document(
|
|
|
|
page_content=doc.page_content, metadata=metadata)
|
2023-06-17 00:36:53 +03:00
|
|
|
create_vector(commons, user.email, doc_with_metadata, user_openai_api_key)
|
2023-06-04 00:12:42 +03:00
|
|
|
# add_usage(stats_db, "embedding", "audio", metadata={"file_name": file_meta_name,"file_type": ".txt", "chunk_size": chunk_size, "chunk_overlap": chunk_overlap})
|
|
|
|
|
2023-06-17 00:36:53 +03:00
|
|
|
# Remove the enable_summarization and ids
|
2023-05-22 09:39:55 +03:00
|
|
|
if enable_summarization and ids and len(ids) > 0:
|
2023-06-17 00:36:53 +03:00
|
|
|
create_summary(commons, document_id=ids[0], content = doc.page_content, metadata = metadata)
|
2023-05-18 02:22:13 +03:00
|
|
|
return
|
|
|
|
|
2023-05-22 09:39:55 +03:00
|
|
|
|
2023-05-31 14:51:23 +03:00
|
|
|
async def file_already_exists(supabase, file, user):
|
2023-06-17 00:36:53 +03:00
|
|
|
# TODO: user brain id instead of user
|
2023-05-18 02:22:13 +03:00
|
|
|
file_content = await file.read()
|
|
|
|
file_sha1 = compute_sha1_from_content(file_content)
|
2023-05-31 14:51:23 +03:00
|
|
|
response = supabase.table("vectors").select("id").filter("metadata->>file_sha1", "eq", file_sha1) \
|
|
|
|
.filter("user_id", "eq", user.email).execute()
|
2023-05-18 02:22:13 +03:00
|
|
|
return len(response.data) > 0
|
2023-06-06 01:38:15 +03:00
|
|
|
|
|
|
|
async def file_already_exists_from_content(supabase, file_content, user):
|
2023-06-17 00:36:53 +03:00
|
|
|
# TODO: user brain id instead of user
|
2023-06-06 01:38:15 +03:00
|
|
|
file_sha1 = compute_sha1_from_content(file_content)
|
|
|
|
response = supabase.table("vectors").select("id").filter("metadata->>file_sha1", "eq", file_sha1) \
|
|
|
|
.filter("user_id", "eq", user.email).execute()
|
|
|
|
return len(response.data) > 0
|