Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ from filtered_search_engine import SmartRecommender
|
|
| 6 |
from reranker import Reranker
|
| 7 |
from intent_classifier import IntentClassifier
|
| 8 |
from keyword_boosting_layer import apply_keyword_boost
|
|
|
|
| 9 |
|
| 10 |
# ------------------------------
|
| 11 |
# Initialize App
|
|
@@ -27,14 +28,22 @@ app.add_middleware(
|
|
| 27 |
allow_headers=["*"],
|
| 28 |
)
|
| 29 |
|
| 30 |
-
# Mount images folder to serve static files
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
# ------------------------------
|
| 34 |
# Load Core Components Once
|
| 35 |
# ------------------------------
|
| 36 |
print("π Loading dataset...")
|
| 37 |
df = pd.read_csv("salahkar_enhanced.csv")
|
|
|
|
|
|
|
| 38 |
|
| 39 |
print("π Loading smart recommendation engine...")
|
| 40 |
engine = SmartRecommender()
|
|
@@ -45,6 +54,9 @@ reranker = Reranker()
|
|
| 45 |
print("π Loading intent recognizer...")
|
| 46 |
intent_detector = IntentClassifier()
|
| 47 |
|
|
|
|
|
|
|
|
|
|
| 48 |
print("π Salahkar AI Ready!")
|
| 49 |
|
| 50 |
|
|
@@ -65,13 +77,19 @@ def get_recommendation(query: str = Query(..., description="User's search text")
|
|
| 65 |
|
| 66 |
print(f"\nπ User Query: {query}")
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
# 1οΈβ£ Detect intent
|
| 69 |
detected_intent = intent_detector.predict_intent(query)
|
| 70 |
print(f"π§ Intent Detected: {detected_intent}")
|
| 71 |
|
| 72 |
# 2οΈβ£ FAISS + Filter Search
|
| 73 |
# engine.recommend returns (results_list, intent)
|
| 74 |
-
|
|
|
|
| 75 |
|
| 76 |
# 3οΈβ£ Prepare results for reranker
|
| 77 |
prepared = []
|
|
@@ -81,7 +99,12 @@ def get_recommendation(query: str = Query(..., description="User's search text")
|
|
| 81 |
category = item["category"]
|
| 82 |
region = item["region"]
|
| 83 |
score = item["score"]
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
prepared.append({
|
| 86 |
"name": name,
|
| 87 |
"domain": domain,
|
|
@@ -111,12 +134,17 @@ def get_recommendation(query: str = Query(..., description="User's search text")
|
|
| 111 |
for item in final_results[:k]
|
| 112 |
]
|
| 113 |
|
| 114 |
-
|
| 115 |
"query": query,
|
| 116 |
"intent": detected_intent,
|
| 117 |
"results": response
|
| 118 |
}
|
| 119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
# -------------------------------------------
|
| 122 |
# Run (Ignored by HuggingFace β needed only for local testing)
|
|
|
|
| 6 |
from reranker import Reranker
|
| 7 |
from intent_classifier import IntentClassifier
|
| 8 |
from keyword_boosting_layer import apply_keyword_boost
|
| 9 |
+
from cache_manager import CacheManager
|
| 10 |
|
| 11 |
# ------------------------------
|
| 12 |
# Initialize App
|
|
|
|
| 28 |
allow_headers=["*"],
|
| 29 |
)
|
| 30 |
|
| 31 |
+
# Mount images folder to serve static files with Cache-Control
|
| 32 |
+
class CachedStaticFiles(StaticFiles):
|
| 33 |
+
def file_response(self, *args, **kwargs):
|
| 34 |
+
response = super().file_response(*args, **kwargs)
|
| 35 |
+
response.headers["Cache-Control"] = "public, max-age=31536000"
|
| 36 |
+
return response
|
| 37 |
+
|
| 38 |
+
app.mount("/images", CachedStaticFiles(directory="images"), name="images")
|
| 39 |
|
| 40 |
# ------------------------------
|
| 41 |
# Load Core Components Once
|
| 42 |
# ------------------------------
|
| 43 |
print("π Loading dataset...")
|
| 44 |
df = pd.read_csv("salahkar_enhanced.csv")
|
| 45 |
+
# Optimization: Create O(1) lookup map
|
| 46 |
+
name_to_row = df.set_index("name").to_dict('index')
|
| 47 |
|
| 48 |
print("π Loading smart recommendation engine...")
|
| 49 |
engine = SmartRecommender()
|
|
|
|
| 54 |
print("π Loading intent recognizer...")
|
| 55 |
intent_detector = IntentClassifier()
|
| 56 |
|
| 57 |
+
print("π Initializing Cache Manager...")
|
| 58 |
+
cache = CacheManager(capacity=200, ttl_seconds=3600)
|
| 59 |
+
|
| 60 |
print("π Salahkar AI Ready!")
|
| 61 |
|
| 62 |
|
|
|
|
| 77 |
|
| 78 |
print(f"\nπ User Query: {query}")
|
| 79 |
|
| 80 |
+
# 0οΈβ£ Check Cache
|
| 81 |
+
cached_response = cache.get(query)
|
| 82 |
+
if cached_response:
|
| 83 |
+
return cached_response
|
| 84 |
+
|
| 85 |
# 1οΈβ£ Detect intent
|
| 86 |
detected_intent = intent_detector.predict_intent(query)
|
| 87 |
print(f"π§ Intent Detected: {detected_intent}")
|
| 88 |
|
| 89 |
# 2οΈβ£ FAISS + Filter Search
|
| 90 |
# engine.recommend returns (results_list, intent)
|
| 91 |
+
# Optimization: Pass detected_intent to avoid re-running classification
|
| 92 |
+
rec_results, _ = engine.recommend(query, k=k, intent=detected_intent)
|
| 93 |
|
| 94 |
# 3οΈβ£ Prepare results for reranker
|
| 95 |
prepared = []
|
|
|
|
| 99 |
category = item["category"]
|
| 100 |
region = item["region"]
|
| 101 |
score = item["score"]
|
| 102 |
+
|
| 103 |
+
# Optimization: O(1) lookup instead of O(N) dataframe filter
|
| 104 |
+
row = name_to_row.get(name)
|
| 105 |
+
if not row:
|
| 106 |
+
continue
|
| 107 |
+
|
| 108 |
prepared.append({
|
| 109 |
"name": name,
|
| 110 |
"domain": domain,
|
|
|
|
| 134 |
for item in final_results[:k]
|
| 135 |
]
|
| 136 |
|
| 137 |
+
final_response = {
|
| 138 |
"query": query,
|
| 139 |
"intent": detected_intent,
|
| 140 |
"results": response
|
| 141 |
}
|
| 142 |
|
| 143 |
+
# 7οΈβ£ Save to Cache
|
| 144 |
+
cache.set(query, final_response)
|
| 145 |
+
|
| 146 |
+
return final_response
|
| 147 |
+
|
| 148 |
|
| 149 |
# -------------------------------------------
|
| 150 |
# Run (Ignored by HuggingFace β needed only for local testing)
|