File size: 3,524 Bytes
7c8312b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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 67 68 69 70 71 72 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 121 |
from fastapi import FastAPI, Query
from fastapi.middleware.cors import CORSMiddleware
import pandas as pd
from filtered_search_engine import SmartRecommender
from reranker import Reranker
from intent_classifier import IntentClassifier
from keyword_boosting_layer import apply_keyword_boost
# ------------------------------
# Initialize App
# ------------------------------
app = FastAPI(
title="Salahkar AI Recommender",
description="Smart cultural, heritage & food recommendation engine for BharatVerse",
version="1.0.0"
)
from fastapi.staticfiles import StaticFiles
# CORS Support (allows frontend browser access)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Mount images folder to serve static files
app.mount("/images", StaticFiles(directory="images"), name="images")
# ------------------------------
# Load Core Components Once
# ------------------------------
print("๐ Loading dataset...")
df = pd.read_csv("salahkar_enhanced.csv")
print("๐ Loading smart recommendation engine...")
engine = SmartRecommender()
print("๐ Loading reranker model...")
reranker = Reranker()
print("๐ Loading intent recognizer...")
intent_detector = IntentClassifier()
print("๐ Salahkar AI Ready!")
# ------------------------------
# Routes
# ------------------------------
@app.get("/")
def root():
return {
"message": "๐ฎ๐ณ Welcome to Salahkar AI โ BharatVerse Intelligent Recommendation System",
"usage": "/recommend?query=your text"
}
@app.get("/recommend")
def get_recommendation(query: str = Query(..., description="User's search text"), k: int = 7):
print(f"\n๐ User Query: {query}")
# 1๏ธโฃ Detect intent
detected_intent = intent_detector.predict_intent(query)
print(f"๐ง Intent Detected: {detected_intent}")
# 2๏ธโฃ FAISS + Filter Search
results = engine.recommend(query, k=k)
# 3๏ธโฃ Prepare results for reranker
prepared = []
for name, domain, category, region, score in results:
row = df[df["name"] == name].iloc[0]
prepared.append({
"name": name,
"domain": domain,
"category": category,
"region": region,
"embedding_score": float(score),
"text": row["search_embedding_text"],
"image": row["image_file"]
})
# 4๏ธโฃ Re-rank using cross encoder
reranked_results = reranker.rerank(query, prepared)
# 5๏ธโฃ Apply keyword boosting
final_results = apply_keyword_boost(query, reranked_results)
# 6๏ธโฃ Format response for frontend
response = [
{
"name": item["name"],
"category": item["category"],
"domain": item["domain"],
"region": item["region"],
"score": float(item["final_score"]),
"image": f"/images/{item['image']}" if item.get("image") else None
}
for item in final_results[:k]
]
return {
"query": query,
"intent": detected_intent,
"results": response
}
# -------------------------------------------
# Run (Ignored by HuggingFace โ needed only for local testing)
# -------------------------------------------
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=7860)
|