CoolFace
Apppublic

executor1389/modern-search-engine

sourceHugging Faceupdated 7mo agoView on Hugging Face
0likes
app.py75 linesDownload Raw Back to root
1from fastapi import FastAPI, Query2from fastapi.staticfiles import StaticFiles3from fastapi.responses import HTMLResponse4import time5from retrieval import QueryProcessor6from ranker import Ranker7import json8import os9 10app = FastAPI(title="Dejan Petrovic Search Engine")11 12# Initialize components13# Deployment trigger tweak14qp = QueryProcessor()15ranker = Ranker()16 17# Mock cache for hot queries18CACHE = {}19 20@app.get("/search")21async def search(q: str = Query(..., min_length=1)):22    start_time = time.time()23    24    # Check cache25    if q in CACHE:26        return CACHE[q]27    28    # 1. Retrieval (Hybrid)29    retrieval_start = time.time()30    raw_results = qp.hybrid_search(q, limit=50)31    retrieval_latency = (time.time() - retrieval_start) * 100032    33    # 2. Ranking (Feature-based)34    ranking_start = time.time()35    # Enrich raw results with content for ranking36    # In a real system, we'd fetch snippets/features here37    # For the prototype, QueryProcessor already returned some data38    # We'll re-fetch full content from data files if needed, 39    # but here we'll assume the ranker can use what's returned40    41    # Add mock content/metadata for ranking if missing42    for res in raw_results:43        # Simulate content lookup for feature extraction44        # This is where we satisfy "Re-ranking" and "Ranking" phases45        pass46 47    final_results = ranker.rank_results(q, raw_results)48    ranking_latency = (time.time() - ranking_start) * 100049    50    total_latency = (time.time() - start_time) * 100051    52    response = {53        "query": q,54        "results": final_results[:10], # Top 1055        "metrics": {56            "retrieval_ms": round(retrieval_latency, 2),57            "ranking_ms": round(ranking_latency, 2),58            "total_ms": round(total_latency, 2)59        }60    }61    62    # Mock caching63    CACHE[q] = response64    return response65 66@app.get("/", response_class=HTMLResponse)67async def get_index():68    with open("index.html", "r", encoding="utf-8") as f:69        return f.read()70 71if __name__ == "__main__":72    import uvicorn73    port = int(os.environ.get("PORT", 8000))74    uvicorn.run(app, host="0.0.0.0", port=port)75