vcasas commited on
Commit
b46dd63
·
verified ·
1 Parent(s): 88177fc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -4
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
- """Busca el artículo más relevante para la pregunta utilizando embeddings."""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  question_embedding = model.encode(question, convert_to_tensor=True)
50
- scores = util.pytorch_cos_sim(question_embedding, article_embeddings)
51
  best_match_idx = scores.argmax()
52
- best_article_key = article_keys[best_match_idx]
53
- return f"{best_article_key}\n{articles[best_article_key]}"
 
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():