Spaces:
Sleeping
Sleeping
import logging | |
import os | |
import shutil | |
# -------------- DATABASE MANAGEMENT TAB FUNCTIONS -------------- | |
def create_database(db_name): | |
logging.info(f"Creating database: {db_name}") | |
db_path = f"faiss_index_{db_name}" | |
if os.path.exists(db_path): | |
return f"Il database '{db_name}' esiste già." | |
try: | |
os.makedirs(db_path) | |
logging.info(f"Database {db_name} created successfully.") | |
return f"Database '{db_name}' creato con successo." | |
except Exception as e: | |
logging.error(f"Errore nella creazione del database: {e}") | |
return f"Errore nella creazione del database: {e}" | |
def delete_database(db_name): | |
db_path = f"faiss_index_{db_name}" | |
if not os.path.exists(db_path): | |
return f"Il database '{db_name}' non esiste." | |
try: | |
shutil.rmtree(db_path) | |
logging.info(f"Database {db_name} eliminato con successo.") | |
return f"Database '{db_name}' eliminato con successo." | |
except OSError as e: | |
logging.error(f"Impossibile eliminare il database {db_name}: {e}") | |
return f"Impossibile eliminare il database '{db_name}': {e}" | |
def modify_database(old_db_name, new_db_name): | |
old_db_path = f"faiss_index_{old_db_name}" | |
new_db_path = f"faiss_index_{new_db_name}" | |
if not os.path.exists(old_db_path): | |
return f"Il database '{old_db_name}' non esiste." | |
if os.path.exists(new_db_path): | |
return f"Il database '{new_db_name}' esiste già." | |
try: | |
os.rename(old_db_path, new_db_path) | |
return f"Database '{old_db_name}' rinominato in '{new_db_name}' con successo." | |
except Exception as e: | |
return f"Errore durante la modifica del database: {e}" | |
def list_databases(): | |
try: | |
databases = [] | |
for item in os.listdir(): | |
if os.path.isdir(item) and item.startswith("faiss_index_"): | |
db_name = item.replace("faiss_index_", "") | |
databases.append(db_name) | |
# Ensure "default_db" is in the list | |
if "default_db" not in databases: | |
databases.append("default_db") | |
return databases | |
except Exception as e: | |
logging.error(f"Error listing databases: {e}") | |
return [] | |