Spaces:
Sleeping
Sleeping
File size: 11,423 Bytes
47e4aa2 |
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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
import logging
import gradio as gr # Aggiunto import mancante
from langchain_community.vectorstores import FAISS
from langchain_huggingface import HuggingFaceEmbeddings
import os
import shutil
import PyPDF2
from docx import Document
from langchain.text_splitter import RecursiveCharacterTextSplitter
from dataclasses import dataclass
import json
from datetime import datetime
# Initialize the text splitter
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=100)
# -------------- UTILITY FUNCTIONS --------------
@dataclass
class DocumentMetadata:
filename: str
title: str
author: str
upload_date: str
chunks: int
def to_dict(self):
return {
"filename": self.filename,
"title": self.title,
"author": self.author,
"upload_date": self.upload_date,
"chunks": self.chunks
}
def save_metadata(metadata_list, db_name):
db_path = f"faiss_index_{db_name}"
metadata_file = os.path.join(db_path, "metadata.json")
existing_metadata = []
if os.path.exists(metadata_file):
with open(metadata_file, 'r') as f:
existing_metadata = json.load(f)
existing_metadata.extend([m.to_dict() for m in metadata_list])
with open(metadata_file, 'w') as f:
json.dump(existing_metadata, f, indent=2)
def extract_text_from_pdf(file_path):
with open(file_path, 'rb') as f:
reader = PyPDF2.PdfReader(f)
text = ""
for page in reader.pages:
text += page.extract_text()
return text
def extract_text_from_docx(file_path):
doc = Document(file_path)
text = ""
for para in doc.paragraphs:
text += para.text + "\n"
return text
# -------------- CHATBOT TAB FUNCTIONS --------------
def answer_question(question, db_name="default_db"):
db_path = f"faiss_index_{db_name}"
if not os.path.exists(db_path):
logging.warning(f"L'indice FAISS per il database {db_name} non esiste.")
return "Database non trovato."
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
vectorstore = FAISS.load_local(db_path, embeddings, allow_dangerous_deserialization=True)
# Perform a similarity search
docs = vectorstore.similarity_search(question)
if not docs:
return "Nessun documento corrispondente alla query."
# Collect the document contents
results = [doc.page_content for doc in docs]
return "\n\n".join(results)
# -------------- 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.")
databases = list_databases()
return (f"Database {db_name} creato con successo.", databases)
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 []
# -------------- DOCUMENT MANAGEMENT TAB FUNCTIONS --------------
def upload_and_index(files, title, author, db_name="default_db"):
if not files:
return "Nessun file caricato."
documents = []
doc_metadata = []
for file in files:
try:
if file.name.endswith('.pdf'):
text = extract_text_from_pdf(file.name)
elif file.name.endswith('.docx'):
text = extract_text_from_docx(file.name)
else:
with open(file.name, 'r', encoding='utf-8') as f:
text = f.read()
chunks = text_splitter.split_text(text)
# Metadata per il documento
doc_meta = DocumentMetadata(
filename=os.path.basename(file.name),
title=title,
author=author,
upload_date=datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
chunks=len(chunks)
)
# Metadata per ogni chunk
for i, chunk in enumerate(chunks):
chunk_metadata = {
"content": chunk,
"source": os.path.basename(file.name),
"title": title,
"author": author,
"chunk_index": i,
"total_chunks": len(chunks),
"upload_date": doc_meta.upload_date
}
documents.append(chunk_metadata)
doc_metadata.append(doc_meta)
except Exception as e:
logging.error(f"Errore durante la lettura del file {file.name}: {e}")
continue
if documents:
try:
db_path = f"faiss_index_{db_name}"
os.makedirs(db_path, exist_ok=True)
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
texts = [doc["content"] for doc in documents]
metadatas = [{k: v for k, v in doc.items() if k != "content"} for doc in documents]
vectorstore = FAISS.from_texts(texts, embeddings, metadatas=metadatas)
vectorstore.save_local(db_path)
# Salva i metadati del documento
save_metadata(doc_metadata, db_name)
return f"Documenti indicizzati con successo nel database {db_name}!"
except Exception as e:
logging.error(f"Errore durante l'indicizzazione: {e}")
return f"Errore durante l'indicizzazione: {e}"
return "Nessun documento processato."
def list_indexed_files(db_name="default_db"):
db_path = f"faiss_index_{db_name}"
metadata_file = os.path.join(db_path, "metadata.json")
if not os.path.exists(metadata_file):
return "Nessun file nel database."
try:
with open(metadata_file, 'r') as f:
metadata = json.load(f)
output = []
for doc in metadata:
output.append(
f"π {doc['title']}\n"
f" Autore: {doc['author']}\n"
f" File: {doc['filename']}\n"
f" Chunks: {doc['chunks']}\n"
f" Caricato il: {doc['upload_date']}\n"
)
return "\n".join(output) if output else "Nessun documento nel database."
except Exception as e:
logging.error(f"Errore nella lettura dei metadati: {e}")
return f"Errore nella lettura dei metadati: {e}"
def delete_file_from_database(file_name, db_name="default_db"):
db_path = f"faiss_index_{db_name}"
file_list_path = os.path.join(db_path, "file_list.txt")
if not os.path.exists(file_list_path):
return "Database non trovato."
try:
# Leggi la lista dei file
with open(file_list_path, "r") as f:
files = f.readlines()
# Rimuovi il file dalla lista
files = [f.strip() for f in files if f.strip() != file_name]
# Riscrivi la lista aggiornata
with open(file_list_path, "w") as f:
for file in files:
f.write(f"{file}\n")
return f"File {file_name} rimosso dal database {db_name}."
except Exception as e:
return f"Errore durante la rimozione del file: {e}"
# -------------- DOCUMENT VISUALIZATION TAB FUNCTIONS --------------
def list_indexed_documents(db_name="default_db"):
db_path = f"faiss_index_{db_name}"
metadata_file = os.path.join(db_path, "metadata.json")
if not os.path.exists(db_path):
return f"Il database {db_name} non esiste."
if not os.path.exists(metadata_file):
return f"Nessun documento nel database {db_name}."
try:
with open(metadata_file, 'r') as f:
metadata = json.load(f)
if not metadata:
return "Nessun documento trovato nel database."
output_lines = ["π Documenti nel database:"]
for doc in metadata:
output_lines.extend([
f"\nπ Documento: {doc['title']}",
f" π Autore: {doc['author']}",
f" π File: {doc['filename']}",
f" π Caricato il: {doc['upload_date']}",
f" π Chunks: {doc['chunks']}"
])
result = "\n".join(output_lines)
logging.info(f"Documenti trovati nel database {db_name}: {result}")
return result
except Exception as e:
error_msg = f"Errore nella lettura dei metadati: {e}"
logging.error(error_msg)
return error_msg
# -------------- NEW FEATURES TAB FUNCTIONS --------------
def search_documents(query, db_name="default_db"):
db_path = f"faiss_index_{db_name}"
if not os.path.exists(db_path):
logging.warning(f"L'indice FAISS per il database {db_name} non esiste.")
return "Database non trovato."
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
vectorstore = FAISS.load_local(db_path, embeddings, allow_dangerous_deserialization=True)
# Perform a similarity search
docs = vectorstore.similarity_search(query)
if not docs:
return "Nessun documento corrispondente alla query."
# Collect the document contents
results = [doc.page_content for doc in docs]
return "\n\n".join(results)
def generate_summary(db_name="default_db"):
# Placeholder for summarization logic
return "This is a summary of the documents in the database." |