Spaces:
Runtime error
Runtime error
Update backend.py
Browse files- backend.py +16 -18
backend.py
CHANGED
|
@@ -4,11 +4,9 @@ from bs4 import BeautifulSoup
|
|
| 4 |
from sentence_transformers import SentenceTransformer
|
| 5 |
import chromadb
|
| 6 |
from chromadb.config import Settings
|
| 7 |
-
from
|
| 8 |
-
import os
|
| 9 |
-
from huggingface_hub import InferenceClient
|
| 10 |
|
| 11 |
-
# 1. Funciones
|
| 12 |
def extraer_texto(url):
|
| 13 |
try:
|
| 14 |
resp = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'}, timeout=10)
|
|
@@ -52,7 +50,7 @@ embedding_model = SentenceTransformer("all-MiniLM-L6-v2")
|
|
| 52 |
chroma_client = chromadb.Client(Settings(anonymized_telemetry=False))
|
| 53 |
collection = chroma_client.get_or_create_collection("batalla_ayacucho_rag")
|
| 54 |
|
| 55 |
-
#
|
| 56 |
for url in urls:
|
| 57 |
texto = extraer_texto(url)
|
| 58 |
if texto:
|
|
@@ -60,14 +58,14 @@ for url in urls:
|
|
| 60 |
emb = embedding_model.encode(chunk).tolist()
|
| 61 |
collection.add(documents=[chunk], ids=[f"{url}_chunk_{i}"], embeddings=[emb])
|
| 62 |
|
| 63 |
-
#
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
)
|
| 69 |
|
| 70 |
-
#
|
| 71 |
def responder_pregunta(pregunta: str, k=3, max_chars=1500) -> str:
|
| 72 |
query_emb = embedding_model.encode(pregunta).tolist()
|
| 73 |
resultados = collection.query(query_embeddings=[query_emb], n_results=k)
|
|
@@ -80,12 +78,12 @@ Contexto hist贸rico:
|
|
| 80 |
Pregunta: {pregunta}
|
| 81 |
Respuesta:"""
|
| 82 |
|
| 83 |
-
|
| 84 |
-
prompt
|
| 85 |
-
max_new_tokens=
|
| 86 |
temperature=0.7,
|
| 87 |
-
top_p=0.
|
| 88 |
-
|
| 89 |
)
|
| 90 |
|
| 91 |
-
return
|
|
|
|
| 4 |
from sentence_transformers import SentenceTransformer
|
| 5 |
import chromadb
|
| 6 |
from chromadb.config import Settings
|
| 7 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# 1. Funciones de scraping y divisi贸n
|
| 10 |
def extraer_texto(url):
|
| 11 |
try:
|
| 12 |
resp = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'}, timeout=10)
|
|
|
|
| 50 |
chroma_client = chromadb.Client(Settings(anonymized_telemetry=False))
|
| 51 |
collection = chroma_client.get_or_create_collection("batalla_ayacucho_rag")
|
| 52 |
|
| 53 |
+
# Indexar los documentos
|
| 54 |
for url in urls:
|
| 55 |
texto = extraer_texto(url)
|
| 56 |
if texto:
|
|
|
|
| 58 |
emb = embedding_model.encode(chunk).tolist()
|
| 59 |
collection.add(documents=[chunk], ids=[f"{url}_chunk_{i}"], embeddings=[emb])
|
| 60 |
|
| 61 |
+
# 4. Cargar un modelo local (sin token)
|
| 62 |
+
# Puedes cambiar por otro modelo peque帽o si quieres
|
| 63 |
+
model_name = "google/gemma-2b-it" # es gratuito y funciona sin token
|
| 64 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 65 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto")
|
| 66 |
+
generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 67 |
|
| 68 |
+
# 5. Funci贸n para responder
|
| 69 |
def responder_pregunta(pregunta: str, k=3, max_chars=1500) -> str:
|
| 70 |
query_emb = embedding_model.encode(pregunta).tolist()
|
| 71 |
resultados = collection.query(query_embeddings=[query_emb], n_results=k)
|
|
|
|
| 78 |
Pregunta: {pregunta}
|
| 79 |
Respuesta:"""
|
| 80 |
|
| 81 |
+
output = generator(
|
| 82 |
+
prompt,
|
| 83 |
+
max_new_tokens=256,
|
| 84 |
temperature=0.7,
|
| 85 |
+
top_p=0.9,
|
| 86 |
+
do_sample=True
|
| 87 |
)
|
| 88 |
|
| 89 |
+
return output[0]["generated_text"].replace(prompt, "").strip()
|