Update app.py
Browse files
app.py
CHANGED
|
@@ -1,120 +1,126 @@
|
|
| 1 |
-
from fastapi import FastAPI, Query
|
| 2 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
-
import pandas as pd
|
| 4 |
-
|
| 5 |
-
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
|
| 12 |
-
# ------------------------------
|
| 13 |
-
app = FastAPI(
|
| 14 |
-
title="Salahkar AI Recommender",
|
| 15 |
-
description="Smart cultural, heritage & food recommendation engine for BharatVerse",
|
| 16 |
-
version="1.0.0"
|
| 17 |
-
)
|
| 18 |
-
|
| 19 |
-
from fastapi.staticfiles import StaticFiles
|
| 20 |
-
|
| 21 |
-
# CORS Support (allows frontend browser access)
|
| 22 |
-
app.add_middleware(
|
| 23 |
-
CORSMiddleware,
|
| 24 |
-
allow_origins=["*"],
|
| 25 |
-
allow_credentials=True,
|
| 26 |
-
allow_methods=["*"],
|
| 27 |
-
allow_headers=["*"],
|
| 28 |
-
)
|
| 29 |
-
|
| 30 |
-
# Mount images folder to serve static files
|
| 31 |
-
app.mount("/images", StaticFiles(directory="images"), name="images")
|
| 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()
|
| 41 |
-
|
| 42 |
-
print("๐ Loading reranker model...")
|
| 43 |
-
reranker = Reranker()
|
| 44 |
-
|
| 45 |
-
print("๐ Loading intent recognizer...")
|
| 46 |
-
intent_detector = IntentClassifier()
|
| 47 |
-
|
| 48 |
-
print("๐ Salahkar AI Ready!")
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
# ------------------------------
|
| 52 |
-
# Routes
|
| 53 |
-
# ------------------------------
|
| 54 |
-
|
| 55 |
-
@app.get("/")
|
| 56 |
-
def root():
|
| 57 |
-
return {
|
| 58 |
-
"message": "๐ฎ๐ณ Welcome to Salahkar AI โ BharatVerse Intelligent Recommendation System",
|
| 59 |
-
"usage": "/recommend?query=your text"
|
| 60 |
-
}
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
@app.get("/recommend")
|
| 64 |
-
def get_recommendation(query: str = Query(..., description="User's search text"), k: int = 7):
|
| 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 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
"
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
#
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Query
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
import pandas as pd
|
| 4 |
+
|
| 5 |
+
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
|
| 12 |
+
# ------------------------------
|
| 13 |
+
app = FastAPI(
|
| 14 |
+
title="Salahkar AI Recommender",
|
| 15 |
+
description="Smart cultural, heritage & food recommendation engine for BharatVerse",
|
| 16 |
+
version="1.0.0"
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
from fastapi.staticfiles import StaticFiles
|
| 20 |
+
|
| 21 |
+
# CORS Support (allows frontend browser access)
|
| 22 |
+
app.add_middleware(
|
| 23 |
+
CORSMiddleware,
|
| 24 |
+
allow_origins=["*"],
|
| 25 |
+
allow_credentials=True,
|
| 26 |
+
allow_methods=["*"],
|
| 27 |
+
allow_headers=["*"],
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
# Mount images folder to serve static files
|
| 31 |
+
app.mount("/images", StaticFiles(directory="images"), name="images")
|
| 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()
|
| 41 |
+
|
| 42 |
+
print("๐ Loading reranker model...")
|
| 43 |
+
reranker = Reranker()
|
| 44 |
+
|
| 45 |
+
print("๐ Loading intent recognizer...")
|
| 46 |
+
intent_detector = IntentClassifier()
|
| 47 |
+
|
| 48 |
+
print("๐ Salahkar AI Ready!")
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
# ------------------------------
|
| 52 |
+
# Routes
|
| 53 |
+
# ------------------------------
|
| 54 |
+
|
| 55 |
+
@app.get("/")
|
| 56 |
+
def root():
|
| 57 |
+
return {
|
| 58 |
+
"message": "๐ฎ๐ณ Welcome to Salahkar AI โ BharatVerse Intelligent Recommendation System",
|
| 59 |
+
"usage": "/recommend?query=your text"
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
@app.get("/recommend")
|
| 64 |
+
def get_recommendation(query: str = Query(..., description="User's search text"), k: int = 7):
|
| 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 |
+
rec_results, _ = engine.recommend(query, k=k)
|
| 75 |
+
|
| 76 |
+
# 3๏ธโฃ Prepare results for reranker
|
| 77 |
+
prepared = []
|
| 78 |
+
for item in rec_results:
|
| 79 |
+
name = item["name"]
|
| 80 |
+
domain = item["domain"]
|
| 81 |
+
category = item["category"]
|
| 82 |
+
region = item["region"]
|
| 83 |
+
score = item["score"]
|
| 84 |
+
row = df[df["name"] == name].iloc[0]
|
| 85 |
+
prepared.append({
|
| 86 |
+
"name": name,
|
| 87 |
+
"domain": domain,
|
| 88 |
+
"category": category,
|
| 89 |
+
"region": region,
|
| 90 |
+
"embedding_score": float(score),
|
| 91 |
+
"text": row["search_embedding_text"],
|
| 92 |
+
"image": row["image_file"]
|
| 93 |
+
})
|
| 94 |
+
|
| 95 |
+
# 4๏ธโฃ Re-rank using cross encoder
|
| 96 |
+
reranked_results = reranker.rerank(query, prepared)
|
| 97 |
+
|
| 98 |
+
# 5๏ธโฃ Apply keyword boosting
|
| 99 |
+
final_results = apply_keyword_boost(query, reranked_results)
|
| 100 |
+
|
| 101 |
+
# 6๏ธโฃ Format response for frontend
|
| 102 |
+
response = [
|
| 103 |
+
{
|
| 104 |
+
"name": item["name"],
|
| 105 |
+
"category": item["category"],
|
| 106 |
+
"domain": item["domain"],
|
| 107 |
+
"region": item["region"],
|
| 108 |
+
"score": float(item["final_score"]),
|
| 109 |
+
"image": f"/images/{item['image']}" if item.get("image") else None
|
| 110 |
+
}
|
| 111 |
+
for item in final_results[:k]
|
| 112 |
+
]
|
| 113 |
+
|
| 114 |
+
return {
|
| 115 |
+
"query": query,
|
| 116 |
+
"intent": detected_intent,
|
| 117 |
+
"results": response
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
# -------------------------------------------
|
| 122 |
+
# Run (Ignored by HuggingFace โ needed only for local testing)
|
| 123 |
+
# -------------------------------------------
|
| 124 |
+
if __name__ == "__main__":
|
| 125 |
+
import uvicorn
|
| 126 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|