Spaces:
Running
Running
Delete app.py
#2
by Kzzr0x - opened
app.py
DELETED
|
@@ -1,55 +0,0 @@
|
|
| 1 |
-
from fastapi import FastAPI, HTTPException, Query
|
| 2 |
-
import duckdb
|
| 3 |
-
from contextlib import asynccontextmanager
|
| 4 |
-
|
| 5 |
-
app = FastAPI()
|
| 6 |
-
|
| 7 |
-
@asynccontextmanager
|
| 8 |
-
async def lifespan(app: FastAPI):
|
| 9 |
-
print("🟢 Space ready.")
|
| 10 |
-
yield
|
| 11 |
-
|
| 12 |
-
@app.get("/")
|
| 13 |
-
def home():
|
| 14 |
-
return {"message": "API Live! Use /search?phone=9860319038 OR /search?aadhar=912712797710 OR /search?q=9860319038"}
|
| 15 |
-
|
| 16 |
-
@app.get("/search")
|
| 17 |
-
async def search_data(
|
| 18 |
-
phone: str = Query(None, description="Search by Phone Number"),
|
| 19 |
-
aadhar: str = Query(None, description="Search by Aadhar Number"),
|
| 20 |
-
q: str = Query(None, description="Search by either Phone or Aadhar (generic)")
|
| 21 |
-
):
|
| 22 |
-
# Agar koi bhi parameter nahi diya toh error
|
| 23 |
-
if not phone and not aadhar and not q:
|
| 24 |
-
raise HTTPException(status_code=400, detail="Provide phone, aadhar, or q parameter")
|
| 25 |
-
|
| 26 |
-
# Priority: q > phone > aadhar
|
| 27 |
-
search_term = q or phone or aadhar
|
| 28 |
-
|
| 29 |
-
con = duckdb.connect()
|
| 30 |
-
try:
|
| 31 |
-
con.execute("PRAGMA memory_limit='800MB'")
|
| 32 |
-
con.execute("PRAGMA threads=2")
|
| 33 |
-
con.execute("INSTALL httpfs; LOAD httpfs;")
|
| 34 |
-
|
| 35 |
-
PARQUET_URL = "https://huggingface.co/datasets/kzropx/icmr-parquet/resolve/main/icmr.parquet"
|
| 36 |
-
|
| 37 |
-
# Dono columns (Phone aur Aadhar) mein search karo
|
| 38 |
-
query = f"""
|
| 39 |
-
SELECT name, fathersName, phoneNumber, aadharNumber, address, district, pincode, state, town
|
| 40 |
-
FROM read_parquet('{PARQUET_URL}')
|
| 41 |
-
WHERE phoneNumber = ? OR aadharNumber = ?
|
| 42 |
-
LIMIT 1
|
| 43 |
-
"""
|
| 44 |
-
result = con.execute(query, [search_term, search_term]).fetchall()
|
| 45 |
-
|
| 46 |
-
if not result:
|
| 47 |
-
return {"status": "error", "message": f"'{search_term}' se koi data nahi mila."}
|
| 48 |
-
|
| 49 |
-
columns = [desc[0] for desc in con.description]
|
| 50 |
-
return {"status": "success", "data": dict(zip(columns, result[0]))}
|
| 51 |
-
|
| 52 |
-
except Exception as e:
|
| 53 |
-
raise HTTPException(status_code=500, detail=f"Query error: {str(e)}")
|
| 54 |
-
finally:
|
| 55 |
-
con.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|