Spaces:
Runtime error
Runtime error
File size: 6,342 Bytes
abb6f94 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
from fastapi import FastAPI, APIRouter, UploadFile, HTTPException
from fastapi import File, UploadFile
from fastapi.staticfiles import StaticFiles
from fastapi.responses import JSONResponse
from pathlib import Path
from typing import List
import os
import shutil
import logging
from app.data_pipeline.data_loader import DocumentLoader
from app.data_pipeline.embedding_manager import split_text,initialize_embedding_model,create_and_store_embeddings
import warnings
warnings.filterwarnings("ignore")
logger = logging.getLogger(__name__)
from app.settings import Config
conf = Config()
upload_router = APIRouter()
UPLOAD_DIR = conf.UPLOAD_DIR
COLLECTION_NAME = conf.COLLECTION_NAME
PERSIST_DIRECTORY = conf.PERSIST_DIRECTORY
# Type of files allowed to be uploaded
def is_allowed_file(filename):
allowed_extensions = {"pdf", "csv", "doc", "docx", "txt", "xlsx", "xls"}
return "." in filename and filename.rsplit(".", 1)[1].lower() in allowed_extensions
def empty_folder(folder_path):
# Check if the folder exists
if os.path.exists(folder_path):
# Iterate through all items in the folder
for item in os.listdir(folder_path):
item_path = os.path.join(folder_path, item)
# Remove files and folders
if os.path.isfile(item_path) or os.path.islink(item_path):
os.remove(item_path)
elif os.path.isdir(item_path):
shutil.rmtree(item_path)
print(f"The folder '{folder_path}' has been emptied.")
else:
print(f"The folder '{folder_path}' does not exist.")
@upload_router.post("/upload")
async def upload_files(files: List[UploadFile] = File(...)):
try:
# Empty the upload directory
empty_folder(UPLOAD_DIR)
logger.info(f"{UPLOAD_DIR} is now empty.")
# Check if UPLOAD_DIR exists
if not os.path.exists(UPLOAD_DIR):
logger.error(f"Upload directory '{UPLOAD_DIR}' does not exist.")
return JSONResponse(content={"error": f"Folder '{UPLOAD_DIR}' does not exist"}, status_code=404)
# Save uploaded files
for uploaded_file in files:
if not is_allowed_file(uploaded_file.filename):
logger.error(f"File type of '{uploaded_file.filename}' not allowed.")
return JSONResponse(content={"error": "File type not allowed"}, status_code=400)
file_path = os.path.join(UPLOAD_DIR, uploaded_file.filename)
with open(file_path, "wb") as buffer:
buffer.write(uploaded_file.file.read())
logger.info(f"File '{uploaded_file.filename}' uploaded successfully.")
# Load documents from the upload directory
try:
document_loader = DocumentLoader(UPLOAD_DIR)
documents = document_loader.load_all_documents()
logger.info(f"Loaded {len(documents)} documents.")
except Exception as e:
logger.error(f"Error loading documents: {e}")
return JSONResponse(content={"error": "Failed to load documents"}, status_code=500)
# Process documents into chunks for embedding
try:
chunks = split_text(documents)
logger.info(f"Processed {len(chunks)} chunks for embedding.")
except Exception as e:
logger.error(f"Error processing documents: {e}")
return JSONResponse(content={"error": "Failed to process documents"}, status_code=500)
# Initialize the embedding model
try:
embedding_function = initialize_embedding_model()
except Exception as e:
logger.error(f"Error initializing embedding model: {e}")
return JSONResponse(content={"error": "Failed to initialize embedding model"}, status_code=500)
# Create and store embeddings
try:
create_and_store_embeddings(chunks, COLLECTION_NAME, embedding_function, PERSIST_DIRECTORY)
logger.info("Embeddings created and stored successfully.")
except Exception as e:
logger.error(f"Error creating or storing embeddings: {e}")
return JSONResponse(content={"error": "Failed to create and store embeddings"}, status_code=500)
# Return success message if everything is successful
return JSONResponse(content={"message": "Documents successfully loaded and processed."})
except Exception as e:
logger.error(f"Unexpected error in upload_files endpoint: {e}")
raise HTTPException(status_code=500, detail="Internal server error.")
# @upload_router.post("/upload")
# async def upload_files(files: List[UploadFile] = File(...)):
# empty_folder(UPLOAD_DIR)
# logger.info(f" {UPLOAD_DIR} is empty Now")
# if not os.path.exists(UPLOAD_DIR):
# logger.error(f"{UPLOAD_DIR}' does not exist")
# return JSONResponse(content={"error": f"Folder '{UPLOAD_DIR}' does not exist"}, status_code=404)
# for uploaded_file in files:
# if not is_allowed_file(uploaded_file.filename):
# logger.error(f"File type not allowed")
# return JSONResponse(content={"error": "File type not allowed"}, status_code=400)
# file_path = os.path.join(UPLOAD_DIR, uploaded_file.filename)
# with open(file_path, "wb") as buffer:
# buffer.write(uploaded_file.file.read())
# logger.info(f"Files uploaded successfully")
# try:
# document_loader = DocumentLoader(UPLOAD_DIR)
# documents = document_loader.load_all_documents()
# logger.info(f"Loaded {len(documents)} documents.")
# except Exception as e:
# logger.error(f"Error loading documents: {e}")
# return
# try:
# chunks = split_text(documents)
# logger.info(f"Processed {len(chunks)} chunks for embedding.", )
# except Exception as e:
# logger.error(f"Error processing documents: {e}")
# return
# try:
# embedding_function = initialize_embedding_model()
# except Exception:
# return # Stop execution if embedding model fails
# create_and_store_embeddings(chunks, COLLECTION_NAME, embedding_function, PERSIST_DIRECTORY)
# logger.info(f'Documents Successfully loades')
# return JSONResponse(content={"message": "Documents Successfully loades"})
|