Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -45,12 +45,28 @@ def create_article_embeddings(articles, model_name="paraphrase-multilingual-mpne
|
|
45 |
|
46 |
# 4. Buscar el artículo relevante
|
47 |
def find_article(question, article_keys, article_embeddings, model, articles):
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
question_embedding = model.encode(question, convert_to_tensor=True)
|
50 |
-
scores = util.pytorch_cos_sim(question_embedding,
|
51 |
best_match_idx = scores.argmax()
|
52 |
-
best_article_key =
|
53 |
-
return f"{best_article_key}\n{
|
|
|
54 |
|
55 |
# Flujo principal
|
56 |
def main():
|
|
|
45 |
|
46 |
# 4. Buscar el artículo relevante
|
47 |
def find_article(question, article_keys, article_embeddings, model, articles):
|
48 |
+
# Filtrar artículos relevantes usando palabras clave
|
49 |
+
keywords = question.lower().split() # Dividir pregunta en palabras clave
|
50 |
+
filtered_articles = {
|
51 |
+
key: value for key, value in articles.items()
|
52 |
+
if any(keyword in value.lower() for keyword in keywords)
|
53 |
+
}
|
54 |
+
|
55 |
+
if not filtered_articles:
|
56 |
+
# Si no hay artículos relevantes basados en palabras clave, usar todos
|
57 |
+
filtered_articles = articles
|
58 |
+
|
59 |
+
# Crear nuevos embeddings para los artículos filtrados
|
60 |
+
filtered_keys = list(filtered_articles.keys())
|
61 |
+
filtered_embeddings = model.encode(list(filtered_articles.values()), convert_to_tensor=True)
|
62 |
+
|
63 |
+
# Calcular similitud con la pregunta
|
64 |
question_embedding = model.encode(question, convert_to_tensor=True)
|
65 |
+
scores = util.pytorch_cos_sim(question_embedding, filtered_embeddings)
|
66 |
best_match_idx = scores.argmax()
|
67 |
+
best_article_key = filtered_keys[best_match_idx]
|
68 |
+
return f"{best_article_key}\n{filtered_articles[best_article_key]}"
|
69 |
+
|
70 |
|
71 |
# Flujo principal
|
72 |
def main():
|