Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,21 +18,45 @@ load_dotenv()
|
|
| 18 |
|
| 19 |
# --- Backend Functions ---
|
| 20 |
|
|
|
|
|
|
|
| 21 |
def get_stored_files():
|
| 22 |
-
"""Retrieve a list of files currently stored in the Pinecone index
|
| 23 |
try:
|
| 24 |
-
|
| 25 |
-
pc = Pinecone(api_key=PINECONE_API_KEY)
|
| 26 |
index_name = os.environ.get("PINECONE_INDEX_NAME")
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
return []
|
| 32 |
|
| 33 |
index = pc.Index(index_name)
|
| 34 |
-
|
| 35 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
results = index.query(vector=[0.0] * 3072, top_k=10000, include_metadata=True)
|
| 37 |
|
| 38 |
unique_files = set()
|
|
@@ -41,11 +65,16 @@ def get_stored_files():
|
|
| 41 |
if 'metadata' in match and 'source' in match.metadata:
|
| 42 |
unique_files.add(match.metadata['source'])
|
| 43 |
|
|
|
|
| 44 |
return sorted(list(unique_files))
|
|
|
|
| 45 |
except Exception as e:
|
| 46 |
-
print(f"
|
|
|
|
|
|
|
| 47 |
return []
|
| 48 |
|
|
|
|
| 49 |
def delete_file_from_vectorstore(filename):
|
| 50 |
"""Deletes all vectors associated with a specific filename from Pinecone using the pinecone library."""
|
| 51 |
if not filename:
|
|
@@ -203,4 +232,4 @@ with gr.Blocks(theme=gr.themes.Soft(), title="PDF Uploader") as demo:
|
|
| 203 |
)
|
| 204 |
|
| 205 |
if __name__ == "__main__":
|
| 206 |
-
demo.launch()
|
|
|
|
| 18 |
|
| 19 |
# --- Backend Functions ---
|
| 20 |
|
| 21 |
+
import traceback # Assicurati di aggiungere questo import all'inizio del file
|
| 22 |
+
|
| 23 |
def get_stored_files():
|
| 24 |
+
"""Retrieve a list of files currently stored in the Pinecone index with enhanced debugging."""
|
| 25 |
try:
|
| 26 |
+
api_key = os.environ.get("PINECONE_API_KEY")
|
|
|
|
| 27 |
index_name = os.environ.get("PINECONE_INDEX_NAME")
|
| 28 |
|
| 29 |
+
# --- LOG DI DEBUG ---
|
| 30 |
+
print("--- DEBUG: Esecuzione di get_stored_files ---")
|
| 31 |
+
if not api_key:
|
| 32 |
+
print("--- DEBUG ERROR: La variabile PINECONE_API_KEY non è impostata!")
|
| 33 |
+
return []
|
| 34 |
+
if not index_name:
|
| 35 |
+
print("--- DEBUG ERROR: La variabile PINECONE_INDEX_NAME non è impostata!")
|
| 36 |
+
return []
|
| 37 |
+
print(f"--- DEBUG: Tento la connessione all'indice Pinecone: '{index_name}' ---")
|
| 38 |
+
# --- FINE LOG DI DEBUG ---
|
| 39 |
+
|
| 40 |
+
pc = Pinecone(api_key=api_key)
|
| 41 |
+
|
| 42 |
+
# Controlla se l'indice esiste veramente
|
| 43 |
+
existing_indexes = [index_info["name"] for index_info in pc.list_indexes()]
|
| 44 |
+
print(f"--- DEBUG: Indici trovati nell'account: {existing_indexes} ---")
|
| 45 |
+
|
| 46 |
+
if index_name not in existing_indexes:
|
| 47 |
+
print(f"--- DEBUG WARNING: L'indice '{index_name}' non esiste. Restituisco una lista vuota. ---")
|
| 48 |
return []
|
| 49 |
|
| 50 |
index = pc.Index(index_name)
|
| 51 |
+
|
| 52 |
+
# Controlla le statistiche dell'indice per vedere se contiene vettori
|
| 53 |
+
stats = index.describe_index_stats()
|
| 54 |
+
print(f"--- DEBUG: Statistiche dell'indice '{index_name}': {stats} ---")
|
| 55 |
+
if stats.get('total_vector_count', 0) == 0:
|
| 56 |
+
print(f"--- DEBUG INFO: L'indice '{index_name}' è vuoto. Restituisco una lista vuota. ---")
|
| 57 |
+
return []
|
| 58 |
+
|
| 59 |
+
# Se l'indice non è vuoto, procedi con la query
|
| 60 |
results = index.query(vector=[0.0] * 3072, top_k=10000, include_metadata=True)
|
| 61 |
|
| 62 |
unique_files = set()
|
|
|
|
| 65 |
if 'metadata' in match and 'source' in match.metadata:
|
| 66 |
unique_files.add(match.metadata['source'])
|
| 67 |
|
| 68 |
+
print(f"--- DEBUG: File unici trovati: {list(unique_files)} ---")
|
| 69 |
return sorted(list(unique_files))
|
| 70 |
+
|
| 71 |
except Exception as e:
|
| 72 |
+
print(f"--- DEBUG EXCEPTION: Errore critico durante il recupero dei file! ---")
|
| 73 |
+
# Stampa l'errore completo per un'analisi dettagliata
|
| 74 |
+
traceback.print_exc()
|
| 75 |
return []
|
| 76 |
|
| 77 |
+
|
| 78 |
def delete_file_from_vectorstore(filename):
|
| 79 |
"""Deletes all vectors associated with a specific filename from Pinecone using the pinecone library."""
|
| 80 |
if not filename:
|
|
|
|
| 232 |
)
|
| 233 |
|
| 234 |
if __name__ == "__main__":
|
| 235 |
+
demo.launch(share=True)
|