Spaces:
Runtime error
Runtime error
Update backend.py
Browse files- backend.py +17 -4
backend.py
CHANGED
|
@@ -1,18 +1,30 @@
|
|
|
|
|
| 1 |
# backend.py
|
|
|
|
|
|
|
| 2 |
from sentence_transformers import SentenceTransformer
|
| 3 |
import chromadb
|
| 4 |
from chromadb.config import Settings
|
| 5 |
from huggingface_hub import InferenceClient
|
| 6 |
|
| 7 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
embedding_model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 9 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 10 |
|
| 11 |
-
# Cargar índice persistente
|
| 12 |
-
chroma_client = chromadb.Client(Settings(
|
|
|
|
|
|
|
|
|
|
| 13 |
collection = chroma_client.get_collection("batalla_ayacucho_rag")
|
| 14 |
|
| 15 |
-
# Función principal
|
| 16 |
def responder_pregunta(pregunta: str, k=3, max_chars=1500) -> str:
|
| 17 |
query_emb = embedding_model.encode(pregunta).tolist()
|
| 18 |
resultados = collection.query(query_embeddings=[query_emb], n_results=k)
|
|
@@ -34,3 +46,4 @@ Respuesta:"""
|
|
| 34 |
)
|
| 35 |
|
| 36 |
return respuesta.strip()
|
|
|
|
|
|
| 1 |
+
|
| 2 |
# backend.py
|
| 3 |
+
import os
|
| 4 |
+
import zipfile
|
| 5 |
from sentence_transformers import SentenceTransformer
|
| 6 |
import chromadb
|
| 7 |
from chromadb.config import Settings
|
| 8 |
from huggingface_hub import InferenceClient
|
| 9 |
|
| 10 |
+
# 🗂️ Descomprimir base de datos vectorial si no existe
|
| 11 |
+
if not os.path.exists("chroma_db"):
|
| 12 |
+
with zipfile.ZipFile("chroma_db.zip", 'r') as zip_ref:
|
| 13 |
+
zip_ref.extractall("chroma_db")
|
| 14 |
+
print("✅ Base de datos Chroma descomprimida.")
|
| 15 |
+
|
| 16 |
+
# 🔍 Modelos
|
| 17 |
embedding_model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 18 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 19 |
|
| 20 |
+
# 📦 Cargar índice persistente
|
| 21 |
+
chroma_client = chromadb.Client(Settings(
|
| 22 |
+
anonymized_telemetry=False,
|
| 23 |
+
persist_directory="chroma_db"
|
| 24 |
+
))
|
| 25 |
collection = chroma_client.get_collection("batalla_ayacucho_rag")
|
| 26 |
|
| 27 |
+
# 🧠 Función principal
|
| 28 |
def responder_pregunta(pregunta: str, k=3, max_chars=1500) -> str:
|
| 29 |
query_emb = embedding_model.encode(pregunta).tolist()
|
| 30 |
resultados = collection.query(query_embeddings=[query_emb], n_results=k)
|
|
|
|
| 46 |
)
|
| 47 |
|
| 48 |
return respuesta.strip()
|
| 49 |
+
|