Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -217,8 +217,11 @@ def create_db():
|
|
| 217 |
files = request.files.getlist('folder') # Folder uploads (multiple files)
|
| 218 |
single_files = request.files.getlist('file') # Single file uploads
|
| 219 |
|
| 220 |
-
|
| 221 |
-
|
|
|
|
|
|
|
|
|
|
| 222 |
return "No files uploaded", 400
|
| 223 |
|
| 224 |
# Create upload directory
|
|
@@ -226,46 +229,36 @@ def create_db():
|
|
| 226 |
print(f"Base Upload Path: {upload_base_path}")
|
| 227 |
os.makedirs(upload_base_path, exist_ok=True)
|
| 228 |
|
| 229 |
-
# Process
|
| 230 |
-
if
|
| 231 |
-
for file in files:
|
| 232 |
-
file_name = secure_filename(file.filename) # Ensure the file name is safe
|
| 233 |
-
file_path = os.path.join(upload_base_path, file_name)
|
| 234 |
-
|
| 235 |
-
# Ensure the directory exists before saving the file
|
| 236 |
-
print(f"Saving to: {file_path}")
|
| 237 |
-
os.makedirs(os.path.dirname(file_path), exist_ok=True)
|
| 238 |
-
|
| 239 |
-
# Save the file
|
| 240 |
-
file.save(file_path)
|
| 241 |
-
|
| 242 |
-
# Process single files (if any)
|
| 243 |
-
if single_files:
|
| 244 |
for file in single_files:
|
| 245 |
-
if file.filename
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
os.makedirs(os.path.dirname(file_path), exist_ok=True)
|
| 256 |
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
|
| 262 |
-
# Generate datastore
|
| 263 |
asyncio.run(generate_data_store(upload_base_path, db_name))
|
| 264 |
-
|
| 265 |
return redirect(url_for('list_dbs'))
|
| 266 |
|
| 267 |
return render_template('create_db.html')
|
| 268 |
|
|
|
|
| 269 |
@app.route('/list-dbs', methods=['GET'])
|
| 270 |
def list_dbs():
|
| 271 |
vector_dbs = [name for name in os.listdir(VECTOR_DB_FOLDER) if os.path.isdir(os.path.join(VECTOR_DB_FOLDER, name))]
|
|
|
|
| 217 |
files = request.files.getlist('folder') # Folder uploads (multiple files)
|
| 218 |
single_files = request.files.getlist('file') # Single file uploads
|
| 219 |
|
| 220 |
+
print("==================folder==>", files)
|
| 221 |
+
print("==================single_files==>", single_files)
|
| 222 |
+
|
| 223 |
+
# Ensure at least one valid file is uploaded
|
| 224 |
+
if not any(file.filename.strip() for file in files) and not any(file.filename.strip() for file in single_files):
|
| 225 |
return "No files uploaded", 400
|
| 226 |
|
| 227 |
# Create upload directory
|
|
|
|
| 229 |
print(f"Base Upload Path: {upload_base_path}")
|
| 230 |
os.makedirs(upload_base_path, exist_ok=True)
|
| 231 |
|
| 232 |
+
# Process single file uploads first (if any exist)
|
| 233 |
+
if any(file.filename.strip() for file in single_files):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
for file in single_files:
|
| 235 |
+
if file.filename.strip(): # Ensure the file is valid
|
| 236 |
+
file_name = secure_filename(file.filename)
|
| 237 |
+
file_path = os.path.join(upload_base_path, file_name)
|
| 238 |
+
print(f"Saving single file to: {file_path}")
|
| 239 |
+
file.save(file_path)
|
| 240 |
+
|
| 241 |
+
# If single file is uploaded, skip folder processing
|
| 242 |
+
print("Single file uploaded, skipping folder processing.")
|
| 243 |
+
asyncio.run(generate_data_store(upload_base_path, db_name))
|
| 244 |
+
return redirect(url_for('list_dbs'))
|
|
|
|
| 245 |
|
| 246 |
+
# Process folder files only if valid files exist
|
| 247 |
+
if any(file.filename.strip() for file in files):
|
| 248 |
+
for file in files:
|
| 249 |
+
if file.filename.strip(): # Ensure it's a valid file
|
| 250 |
+
file_name = secure_filename(file.filename)
|
| 251 |
+
file_path = os.path.join(upload_base_path, file_name)
|
| 252 |
+
print(f"Saving folder file to: {file_path}")
|
| 253 |
+
file.save(file_path)
|
| 254 |
|
| 255 |
+
# Generate datastore
|
| 256 |
asyncio.run(generate_data_store(upload_base_path, db_name))
|
|
|
|
| 257 |
return redirect(url_for('list_dbs'))
|
| 258 |
|
| 259 |
return render_template('create_db.html')
|
| 260 |
|
| 261 |
+
|
| 262 |
@app.route('/list-dbs', methods=['GET'])
|
| 263 |
def list_dbs():
|
| 264 |
vector_dbs = [name for name in os.listdir(VECTOR_DB_FOLDER) if os.path.isdir(os.path.join(VECTOR_DB_FOLDER, name))]
|