Spaces:
Running
Running
| from fastapi import FastAPI, HTTPException, Query | |
| import duckdb | |
| from contextlib import asynccontextmanager | |
| # Global connection object | |
| con = duckdb.connect() | |
| # === CREDITS === | |
| CREDITS = { | |
| "developer": "@kzr0x", | |
| "team": "TEAM DARK KNIGHT APIs", | |
| "channel": "@API_WALLAH" | |
| } | |
| async def lifespan(app: FastAPI): | |
| print("π’ DuckDB extensions load ho rahi hain...") | |
| con.execute("INSTALL httpfs; LOAD httpfs;") | |
| con.execute("PRAGMA memory_limit='12GB'") | |
| con.execute("PRAGMA threads=4") | |
| con.execute("SET enable_object_cache=true") | |
| PARQUET_URL = "https://huggingface.co/datasets/kzropx/icmr-parquet/resolve/main/icmr.parquet" | |
| # π₯ VIEW use karo (startup fast) | |
| con.execute(f"CREATE OR REPLACE VIEW users_view AS SELECT * FROM read_parquet('{PARQUET_URL}')") | |
| # Warm-up: sirf metadata load | |
| con.execute("SELECT 1 FROM users_view LIMIT 1") | |
| print("π’ VIEW ready! API fast hai.") | |
| yield | |
| con.close() | |
| app = FastAPI(title="Super Fast Search API", lifespan=lifespan) | |
| def home(): | |
| return {"status": "alive", "endpoints": {"search": "/search?mobile=NUMBER"}, "credits": CREDITS} | |
| async def search_mobile(mobile: str = Query(..., description="Enter mobile number to search")): | |
| try: | |
| # π₯ STEP 1: EXISTS check (LIMIT 1) β Missing number ke liye fast | |
| exists_query = "SELECT 1 FROM users_view WHERE phoneNumber = ? LIMIT 1" | |
| exists = con.execute(exists_query, [str(mobile)]).fetchone() | |
| if not exists: | |
| return { | |
| "success": False, | |
| "number": mobile, | |
| "message": "No data found for this number.", | |
| "total": 0, | |
| "results": [], | |
| "credits": CREDITS | |
| } | |
| # π₯ STEP 2: Full data fetch (ab local metadata cache se fast) | |
| query = """ | |
| SELECT name, fathersName, phoneNumber, aadharNumber, address, district, pincode, state, town | |
| FROM users_view | |
| WHERE phoneNumber = ? | |
| LIMIT 10 | |
| """ | |
| result = con.execute(query, [str(mobile)]).fetchall() | |
| results = [] | |
| for row in result: | |
| results.append({ | |
| "name": row[0], | |
| "father_name": row[1], | |
| "mobile": row[2], | |
| "aadhar": row[3], | |
| "address": row[4], | |
| "district": row[5], | |
| "pincode": row[6], | |
| "state": row[7], | |
| "town": row[8], | |
| "alternate": None, | |
| "circle": None, | |
| "email": None, | |
| "truecaller_name": None, | |
| "source": "icmr" | |
| }) | |
| return { | |
| "success": True, | |
| "number": mobile, | |
| "total": len(results), | |
| "results": results, | |
| "credits": CREDITS | |
| } | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=f"Query error: {str(e)}") |