Update final_recommender.py
Browse files- final_recommender.py +77 -66
final_recommender.py
CHANGED
|
@@ -1,66 +1,77 @@
|
|
| 1 |
-
"""
|
| 2 |
-
FINAL SMART RECOMMENDER SYSTEM
|
| 3 |
-
Embeddings + Intent + Filtering + Reranking + Keyword Boosting
|
| 4 |
-
"""
|
| 5 |
-
|
| 6 |
-
from filtered_search_engine import SmartRecommender
|
| 7 |
-
from reranker import Reranker
|
| 8 |
-
from keyword_boosting_layer import apply_keyword_boost
|
| 9 |
-
import pandas as pd
|
| 10 |
-
|
| 11 |
-
class FinalSalahkar:
|
| 12 |
-
|
| 13 |
-
def __init__(self):
|
| 14 |
-
print("β¨ Initializing Final AI Recommender...")
|
| 15 |
-
self.engine = SmartRecommender()
|
| 16 |
-
self.reranker = Reranker()
|
| 17 |
-
self.df = pd.read_csv("salahkar_enhanced.csv")
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
print("
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
FINAL SMART RECOMMENDER SYSTEM
|
| 3 |
+
Embeddings + Intent + Filtering + Reranking + Keyword Boosting
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
from filtered_search_engine import SmartRecommender
|
| 7 |
+
from reranker import Reranker
|
| 8 |
+
from keyword_boosting_layer import apply_keyword_boost
|
| 9 |
+
import pandas as pd
|
| 10 |
+
|
| 11 |
+
class FinalSalahkar:
|
| 12 |
+
|
| 13 |
+
def __init__(self):
|
| 14 |
+
print("β¨ Initializing Final AI Recommender...")
|
| 15 |
+
self.engine = SmartRecommender()
|
| 16 |
+
self.reranker = Reranker()
|
| 17 |
+
self.df = pd.read_csv("salahkar_enhanced.csv")
|
| 18 |
+
|
| 19 |
+
# Optimization: Pre-compute name-to-row mapping for O(1) lookup
|
| 20 |
+
# This is much faster than running df[df["name"] == name] for every result
|
| 21 |
+
self.name_map = self.df.set_index("name").to_dict('index')
|
| 22 |
+
|
| 23 |
+
print("π System Ready!")
|
| 24 |
+
|
| 25 |
+
def ask(self, query, k=7):
|
| 26 |
+
print("\n==============================================")
|
| 27 |
+
print(f"π§ INPUT QUERY β {query}")
|
| 28 |
+
print("==============================================")
|
| 29 |
+
|
| 30 |
+
# STEP 1 β BASE SEARCH (filtered FAISS)
|
| 31 |
+
# Now returns (results_list, detected_intent)
|
| 32 |
+
results, intent = self.engine.recommend(query, k=k)
|
| 33 |
+
|
| 34 |
+
# STEP 2 β Prepare embedding text for reranking
|
| 35 |
+
prepared = []
|
| 36 |
+
for item in results:
|
| 37 |
+
name = item["name"]
|
| 38 |
+
|
| 39 |
+
# Optimization: FAST LOOKUP
|
| 40 |
+
row = self.name_map.get(name)
|
| 41 |
+
|
| 42 |
+
if row:
|
| 43 |
+
prepared.append({
|
| 44 |
+
"name": name,
|
| 45 |
+
"domain": item["domain"],
|
| 46 |
+
"category": item["category"],
|
| 47 |
+
"region": item["region"],
|
| 48 |
+
"embedding_score": item["score"],
|
| 49 |
+
"text": row["search_embedding_text"], # Use index text for reranking
|
| 50 |
+
"intent": intent
|
| 51 |
+
})
|
| 52 |
+
|
| 53 |
+
# STEP 3 β Cross-Encoder Reranking
|
| 54 |
+
ranked = self.reranker.rerank(query, prepared)
|
| 55 |
+
|
| 56 |
+
# STEP 4 β Keyword Boost (final scoring)
|
| 57 |
+
# This applies the location/food/heritage boosts we configured
|
| 58 |
+
boosted = apply_keyword_boost(query, ranked)
|
| 59 |
+
|
| 60 |
+
# STEP 5 β Display final sorted results
|
| 61 |
+
print("\nπ FINAL SMART RANKED RESULTS:")
|
| 62 |
+
for i, item in enumerate(boosted[:k]):
|
| 63 |
+
score = item.get('final_score', item.get('rerank_score', 0))
|
| 64 |
+
print(f"{i+1}. {item['name']} | Final Score: {round(score, 3)}")
|
| 65 |
+
|
| 66 |
+
return boosted
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
if __name__ == "__main__":
|
| 70 |
+
bot = FinalSalahkar()
|
| 71 |
+
|
| 72 |
+
# Test the complex new queries to verify accuracy
|
| 73 |
+
bot.ask("romantic historical place india")
|
| 74 |
+
bot.ask("spiritual peaceful temple")
|
| 75 |
+
bot.ask("best south indian spicy breakfast")
|
| 76 |
+
bot.ask("sweet festival food india")
|
| 77 |
+
bot.ask("famous beaches near ongole") # Test new data point
|