Update app.py
Browse files1 unmodified line2345678910111213141516171819202122232425262728293031321 unmodified line343536373839404142434445464748495051525354555657585960616263646566671 unmodified lineimport duckdbfrom contextlib import asynccontextmanager# Global Connection rakhocon = None@asynccontextmanagerasync def lifespan(app: FastAPI):global conprint("🟢 DuckDB initialize ho raha hai (Sirf ek baar)...")con = duckdb.connect()con.execute("PRAGMA memory_limit='800MB'")con.execute("PRAGMA threads=2")# === SUPER FAST HACKS ===# 1. Object cache ON - metadata memory mein cache rahegacon.execute("SET enable_object_cache=true")# 2. Insertion order preserve mat karo - speed boostcon.execute("SET preserve_insertion_order=false")# 3. Extension sirf ek baar load karocon.execute("INSTALL httpfs; LOAD httpfs;")# Pehli query hi metadata cache karne ke liye (warm-up)PARQUET_URL = "https://huggingface.co/datasets/kzropx/icmr-parquet/resolve/main/icmr.parquet"con.execute(f"SELECT 1 FROM read_parquet('{PARQUET_URL}') LIMIT 1")print("🟢 DuckDB ready! Metadata cached. API fast hogi.")yieldcon.close()1 unmodified line@app.get("/")def home():return {"message": "API Live! Use /search?phone=9860319038 OR /search?aadhar=912712797710"}@app.get("/search")async def search_data(phone: str = Query(None, description="Search by Phone Number"),aadhar: str = Query(None, description="Search by Aadhar Number"),q: str = Query(None, description="Generic search (phone or aadhar)")):if not phone and not aadhar and not q:raise HTTPException(status_code=400, detail="Provide phone, aadhar, or q")search_term = q or phone or aadharPARQUET_URL = "https://huggingface.co/datasets/kzropx/icmr-parquet/resolve/main/icmr.parquet"try:query = f"""SELECT name, fathersName, phoneNumber, aadharNumber, address, district, pincode, state, townFROM read_parquet('{PARQUET_URL}')WHERE phoneNumber = ? OR aadharNumber = ?LIMIT 1"""result = con.execute(query, [search_term, search_term]).fetchall()if not result:return {"status": "error", "message": f"'{search_term}' se koi data nahi mila."}columns = [desc[0] for desc in con.description]return {"status": "success", "data": dict(zip(columns, result[0]))}except Exception as e:raise HTTPException(status_code=500, detail=f"Query error: {str(e)}")1 unmodified line2345678910111213141516171819202122232425262728291 unmodified line31323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697981 unmodified lineimport duckdbfrom contextlib import asynccontextmanager# Global Connectioncon = None@asynccontextmanagerasync def lifespan(app: FastAPI):global conprint("🟢 DuckDB initialize ho raha hai...")con = duckdb.connect()con.execute("PRAGMA memory_limit='800MB'")con.execute("PRAGMA threads=2")con.execute("SET enable_object_cache=true")con.execute("SET preserve_insertion_order=false")con.execute("INSTALL httpfs; LOAD httpfs;")# === DONO DATASETS KE URL ===URL_ICMR = "https://huggingface.co/datasets/kzropx/icmr-parquet/resolve/main/icmr.parquet"URL_INDDATA = "https://huggingface.co/datasets/tfqdeadlo/Inddatainonefile/resolve/main/train.parquet"# Warm-up cache (sirf ICMR se, fast rahega)con.execute(f"SELECT 1 FROM read_parquet('{URL_ICMR}') LIMIT 1")print("🟢 DuckDB ready! Dono datasets search honge.")yieldcon.close()1 unmodified line@app.get("/")def home():return {"message": "Search via /search?number=xxx OR /search?aadhar=xxx"}@app.get("/search")async def search_data(number: str = Query(None, description="Search by Phone Number"),aadhar: str = Query(None, description="Search by Aadhar Number"),q: str = Query(None, description="Generic search")):if not number and not aadhar and not q:raise HTTPException(status_code=400, detail="Provide number, aadhar, or q")search_term = q or number or aadharURL_ICMR = "https://huggingface.co/datasets/kzropx/icmr-parquet/resolve/main/icmr.parquet"URL_INDDATA = "https://huggingface.co/datasets/tfqdeadlo/Inddatainonefile/resolve/main/train.parquet"try:# UNION ALL se dono datasets ek saath query hongequery = f"""SELECT'icmr' as source,name, fathersName, phoneNumber, aadharNumber, address, district, pincode, state, townFROM read_parquet('{URL_ICMR}')WHERE phoneNumber = ? OR aadharNumber = ?UNION ALLSELECT'inddata' as source,name, fname, mobile, id, address, null, null, null, nullFROM read_parquet('{URL_INDDATA}')WHERE mobile = ? OR id = ?LIMIT 50"""# 4 parameters: ICMR ke liye 2, INDDATA ke liye 2result = con.execute(query, [search_term, search_term, search_term, search_term]).fetchall()if not result:return {"status": "error", "message": f"'{search_term}' kisi bhi dataset mein nahi mila."}# Format resultresult_list = []for row in result:result_list.append({"source": row[0],"name": row[1],"fathersName": row[2],"phoneNumber": row[3],"aadharNumber": row[4],"address": row[5],"district": row[6],"pincode": row[7],"state": row[8],"town": row[9]})return {"status": "success","total_results": len(result_list),"data": result_list}except Exception as e:raise HTTPException(status_code=500, detail=f"Query error: {str(e)}")
app.py+56-25