Update app.py
Browse files
app.py
CHANGED
|
@@ -2,31 +2,28 @@ from fastapi import FastAPI, HTTPException, Query
|
|
| 2 |
import duckdb
|
| 3 |
from contextlib import asynccontextmanager
|
| 4 |
|
| 5 |
-
# Global Connection
|
| 6 |
con = None
|
| 7 |
|
| 8 |
@asynccontextmanager
|
| 9 |
async def lifespan(app: FastAPI):
|
| 10 |
global con
|
| 11 |
-
print("🟢 DuckDB initialize ho raha hai
|
| 12 |
|
| 13 |
con = duckdb.connect()
|
| 14 |
con.execute("PRAGMA memory_limit='800MB'")
|
| 15 |
con.execute("PRAGMA threads=2")
|
| 16 |
-
|
| 17 |
-
# === SUPER FAST HACKS ===
|
| 18 |
-
# 1. Object cache ON - metadata memory mein cache rahega
|
| 19 |
con.execute("SET enable_object_cache=true")
|
| 20 |
-
# 2. Insertion order preserve mat karo - speed boost
|
| 21 |
con.execute("SET preserve_insertion_order=false")
|
| 22 |
-
# 3. Extension sirf ek baar load karo
|
| 23 |
con.execute("INSTALL httpfs; LOAD httpfs;")
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
|
| 28 |
|
| 29 |
-
|
|
|
|
|
|
|
| 30 |
yield
|
| 31 |
con.close()
|
| 32 |
|
|
@@ -34,34 +31,68 @@ app = FastAPI(lifespan=lifespan)
|
|
| 34 |
|
| 35 |
@app.get("/")
|
| 36 |
def home():
|
| 37 |
-
return {"message": "
|
| 38 |
|
| 39 |
@app.get("/search")
|
| 40 |
async def search_data(
|
| 41 |
-
|
| 42 |
aadhar: str = Query(None, description="Search by Aadhar Number"),
|
| 43 |
-
q: str = Query(None, description="Generic search
|
| 44 |
):
|
| 45 |
-
if not
|
| 46 |
-
raise HTTPException(status_code=400, detail="Provide
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
|
| 51 |
try:
|
|
|
|
| 52 |
query = f"""
|
| 53 |
-
SELECT
|
| 54 |
-
|
|
|
|
|
|
|
| 55 |
WHERE phoneNumber = ? OR aadharNumber = ?
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
"""
|
| 58 |
-
|
|
|
|
| 59 |
|
| 60 |
if not result:
|
| 61 |
-
return {"status": "error", "message": f"'{search_term}'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
except Exception as e:
|
| 67 |
raise HTTPException(status_code=500, detail=f"Query error: {str(e)}")
|
|
|
|
| 2 |
import duckdb
|
| 3 |
from contextlib import asynccontextmanager
|
| 4 |
|
| 5 |
+
# Global Connection
|
| 6 |
con = None
|
| 7 |
|
| 8 |
@asynccontextmanager
|
| 9 |
async def lifespan(app: FastAPI):
|
| 10 |
global con
|
| 11 |
+
print("🟢 DuckDB initialize ho raha hai...")
|
| 12 |
|
| 13 |
con = duckdb.connect()
|
| 14 |
con.execute("PRAGMA memory_limit='800MB'")
|
| 15 |
con.execute("PRAGMA threads=2")
|
|
|
|
|
|
|
|
|
|
| 16 |
con.execute("SET enable_object_cache=true")
|
|
|
|
| 17 |
con.execute("SET preserve_insertion_order=false")
|
|
|
|
| 18 |
con.execute("INSTALL httpfs; LOAD httpfs;")
|
| 19 |
|
| 20 |
+
# === DONO DATASETS KE URL ===
|
| 21 |
+
URL_ICMR = "https://huggingface.co/datasets/kzropx/icmr-parquet/resolve/main/icmr.parquet"
|
| 22 |
+
URL_INDDATA = "https://huggingface.co/datasets/tfqdeadlo/Inddatainonefile/resolve/main/train.parquet"
|
| 23 |
|
| 24 |
+
# Warm-up cache (sirf ICMR se, fast rahega)
|
| 25 |
+
con.execute(f"SELECT 1 FROM read_parquet('{URL_ICMR}') LIMIT 1")
|
| 26 |
+
print("🟢 DuckDB ready! Dono datasets search honge.")
|
| 27 |
yield
|
| 28 |
con.close()
|
| 29 |
|
|
|
|
| 31 |
|
| 32 |
@app.get("/")
|
| 33 |
def home():
|
| 34 |
+
return {"message": "Search via /search?number=xxx OR /search?aadhar=xxx"}
|
| 35 |
|
| 36 |
@app.get("/search")
|
| 37 |
async def search_data(
|
| 38 |
+
number: str = Query(None, description="Search by Phone Number"),
|
| 39 |
aadhar: str = Query(None, description="Search by Aadhar Number"),
|
| 40 |
+
q: str = Query(None, description="Generic search")
|
| 41 |
):
|
| 42 |
+
if not number and not aadhar and not q:
|
| 43 |
+
raise HTTPException(status_code=400, detail="Provide number, aadhar, or q")
|
| 44 |
+
|
| 45 |
+
search_term = q or number or aadhar
|
| 46 |
|
| 47 |
+
URL_ICMR = "https://huggingface.co/datasets/kzropx/icmr-parquet/resolve/main/icmr.parquet"
|
| 48 |
+
URL_INDDATA = "https://huggingface.co/datasets/tfqdeadlo/Inddatainonefile/resolve/main/train.parquet"
|
| 49 |
|
| 50 |
try:
|
| 51 |
+
# UNION ALL se dono datasets ek saath query honge
|
| 52 |
query = f"""
|
| 53 |
+
SELECT
|
| 54 |
+
'icmr' as source,
|
| 55 |
+
name, fathersName, phoneNumber, aadharNumber, address, district, pincode, state, town
|
| 56 |
+
FROM read_parquet('{URL_ICMR}')
|
| 57 |
WHERE phoneNumber = ? OR aadharNumber = ?
|
| 58 |
+
|
| 59 |
+
UNION ALL
|
| 60 |
+
|
| 61 |
+
SELECT
|
| 62 |
+
'inddata' as source,
|
| 63 |
+
name, fname, mobile, id, address, null, null, null, null
|
| 64 |
+
FROM read_parquet('{URL_INDDATA}')
|
| 65 |
+
WHERE mobile = ? OR id = ?
|
| 66 |
+
|
| 67 |
+
LIMIT 50
|
| 68 |
"""
|
| 69 |
+
# 4 parameters: ICMR ke liye 2, INDDATA ke liye 2
|
| 70 |
+
result = con.execute(query, [search_term, search_term, search_term, search_term]).fetchall()
|
| 71 |
|
| 72 |
if not result:
|
| 73 |
+
return {"status": "error", "message": f"'{search_term}' kisi bhi dataset mein nahi mila."}
|
| 74 |
+
|
| 75 |
+
# Format result
|
| 76 |
+
result_list = []
|
| 77 |
+
for row in result:
|
| 78 |
+
result_list.append({
|
| 79 |
+
"source": row[0],
|
| 80 |
+
"name": row[1],
|
| 81 |
+
"fathersName": row[2],
|
| 82 |
+
"phoneNumber": row[3],
|
| 83 |
+
"aadharNumber": row[4],
|
| 84 |
+
"address": row[5],
|
| 85 |
+
"district": row[6],
|
| 86 |
+
"pincode": row[7],
|
| 87 |
+
"state": row[8],
|
| 88 |
+
"town": row[9]
|
| 89 |
+
})
|
| 90 |
|
| 91 |
+
return {
|
| 92 |
+
"status": "success",
|
| 93 |
+
"total_results": len(result_list),
|
| 94 |
+
"data": result_list
|
| 95 |
+
}
|
| 96 |
|
| 97 |
except Exception as e:
|
| 98 |
raise HTTPException(status_code=500, detail=f"Query error: {str(e)}")
|