I am making a project with a Python Flask backend. Before starting up the Flask server, I can load documents into a vectorstore with no issues.
I am trying to send a file from my React frontend to an API endpoint for it to be added into the vector database.
This is the API endpoint:
documents=[]
for key in request.files:
file = request.files[key]
filename = secure_filename(file.filename)
@app.route('/uploadFiles/', methods=['POST'])
@cross_origin()
def upload_file():
# Create a temporary file
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
file.save(temp_file.name)
temp_file_path = temp_file.name
print(temp_file_path)
if filename.endswith('.pdf'):
loader = PyPDFLoader(temp_file_path)
documents.extend(loader.load())
print(temp_file_path, len(documents))
elif filename.endswith('.docx') or filename.endswith('.doc'):
loader = Docx2txtLoader(temp_file_path)
documents.extend(loader.load())
print(temp_file_path, len(documents))
elif filename.endswith('.txt'):
loader = TextLoader(temp_file_path)
documents.extend(loader.load())
print(temp_file_path, len(documents))
elif filename.endswith('.csv'):
loader = CSVLoader(temp_file_path)
documents.extend(loader.load())
print(temp_file_path, len(documents))
elif filename.endswith('.pptx') or filename.endswith('.ppt'):
loader = UnstructuredPowerPointLoader(temp_file_path,mode='elements')
documents.extend(loader.load())
print(temp_file_path, len(documents))
upload_files(documents, vectordb)
# Delete the temporary file
os.remove(temp_file.name)
return jsonify({
'response': 'success'
})`
def upload_files(documents, chroma_db):
print("Begin Loading content from uploaded files...")
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
texts = text_splitter.split_documents(filter_complex_metadata(documents))
chroma_db = Chroma.from_documents(texts,
embedding=embedding,
persist_directory=persist_directory)
chroma_db.persist()
I am running into the following errors:
INFO:werkzeug:127.0.0.1 – – [17/Jun/2024 11:33:21] “GET /uploadFiles/ HTTP/1.1” 200 –
INFO:backoff:Backing off send_request(…) for 0.2s (requests.exceptions.ConnectionError: (‘Connection aborted.’, ConnectionResetError(54, ‘Connection reset by peer’)))
INFO:backoff:Backing off send_request(…) for 1.4s (requests.exceptions.ConnectionError: (‘Connection aborted.’, ConnectionResetError(54, ‘Connection reset by peer’)))
INFO:backoff:Backing off send_request(…) for 0.5s (requests.exceptions.ConnectionError: (‘Connection aborted.’, ConnectionResetError(54, ‘Connection reset by peer’)))
ERROR:backoff:Giving up send_request(…) after 4 tries (requests.exceptions.ConnectionError: (‘Connection aborted.’, ConnectionResetError(54, ‘Connection reset by peer’)))
user25607366 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.