Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,27 +12,22 @@ CREDITS = {
|
|
| 12 |
"channel": "@API_WALLAH"
|
| 13 |
}
|
| 14 |
|
| 15 |
-
# Lifespan use karke app start hote hi heavy initialization ek baar karenge
|
| 16 |
@asynccontextmanager
|
| 17 |
async def lifespan(app: FastAPI):
|
| 18 |
-
print("DuckDB extensions load ho rahi hain...")
|
| 19 |
con.execute("INSTALL httpfs; LOAD httpfs;")
|
| 20 |
-
|
| 21 |
-
# 🔥 OPTIMIZATION: Memory limit + threads set karo
|
| 22 |
con.execute("PRAGMA memory_limit='12GB'")
|
| 23 |
con.execute("PRAGMA threads=4")
|
| 24 |
con.execute("SET enable_object_cache=true")
|
| 25 |
|
| 26 |
-
# Parquet file ka correct URL
|
| 27 |
PARQUET_URL = "https://huggingface.co/datasets/kzropx/icmr-parquet/resolve/main/icmr.parquet"
|
| 28 |
|
| 29 |
-
# 🔥
|
| 30 |
-
|
| 31 |
-
con.execute(f"CREATE OR REPLACE TABLE users_table AS SELECT * FROM read_parquet('{PARQUET_URL}')")
|
| 32 |
|
| 33 |
-
# Warm-up:
|
| 34 |
-
con.execute("SELECT 1 FROM
|
| 35 |
-
print("
|
| 36 |
yield
|
| 37 |
con.close()
|
| 38 |
|
|
@@ -40,28 +35,16 @@ app = FastAPI(title="Super Fast Search API", lifespan=lifespan)
|
|
| 40 |
|
| 41 |
@app.get("/")
|
| 42 |
def home():
|
| 43 |
-
return {
|
| 44 |
-
"status": "alive",
|
| 45 |
-
"endpoints": {
|
| 46 |
-
"search": "/search?mobile=NUMBER"
|
| 47 |
-
},
|
| 48 |
-
"credits": CREDITS
|
| 49 |
-
}
|
| 50 |
|
| 51 |
@app.get("/search")
|
| 52 |
async def search_mobile(mobile: str = Query(..., description="Enter mobile number to search")):
|
| 53 |
try:
|
| 54 |
-
# 🔥
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
FROM users_table
|
| 58 |
-
WHERE phoneNumber = ?
|
| 59 |
-
LIMIT 100
|
| 60 |
-
"""
|
| 61 |
-
|
| 62 |
-
result = con.execute(query, [str(mobile)]).fetchall()
|
| 63 |
|
| 64 |
-
if not
|
| 65 |
return {
|
| 66 |
"success": False,
|
| 67 |
"number": mobile,
|
|
@@ -71,6 +54,15 @@ async def search_mobile(mobile: str = Query(..., description="Enter mobile numbe
|
|
| 71 |
"credits": CREDITS
|
| 72 |
}
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
results = []
|
| 75 |
for row in result:
|
| 76 |
results.append({
|
|
|
|
| 12 |
"channel": "@API_WALLAH"
|
| 13 |
}
|
| 14 |
|
|
|
|
| 15 |
@asynccontextmanager
|
| 16 |
async def lifespan(app: FastAPI):
|
| 17 |
+
print("🟢 DuckDB extensions load ho rahi hain...")
|
| 18 |
con.execute("INSTALL httpfs; LOAD httpfs;")
|
|
|
|
|
|
|
| 19 |
con.execute("PRAGMA memory_limit='12GB'")
|
| 20 |
con.execute("PRAGMA threads=4")
|
| 21 |
con.execute("SET enable_object_cache=true")
|
| 22 |
|
|
|
|
| 23 |
PARQUET_URL = "https://huggingface.co/datasets/kzropx/icmr-parquet/resolve/main/icmr.parquet"
|
| 24 |
|
| 25 |
+
# 🔥 VIEW use karo (startup fast)
|
| 26 |
+
con.execute(f"CREATE OR REPLACE VIEW users_view AS SELECT * FROM read_parquet('{PARQUET_URL}')")
|
|
|
|
| 27 |
|
| 28 |
+
# Warm-up: sirf metadata load
|
| 29 |
+
con.execute("SELECT 1 FROM users_view LIMIT 1")
|
| 30 |
+
print("🟢 VIEW ready! API fast hai.")
|
| 31 |
yield
|
| 32 |
con.close()
|
| 33 |
|
|
|
|
| 35 |
|
| 36 |
@app.get("/")
|
| 37 |
def home():
|
| 38 |
+
return {"status": "alive", "endpoints": {"search": "/search?mobile=NUMBER"}, "credits": CREDITS}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
@app.get("/search")
|
| 41 |
async def search_mobile(mobile: str = Query(..., description="Enter mobile number to search")):
|
| 42 |
try:
|
| 43 |
+
# 🔥 STEP 1: EXISTS check (LIMIT 1) — Missing number ke liye fast
|
| 44 |
+
exists_query = "SELECT 1 FROM users_view WHERE phoneNumber = ? LIMIT 1"
|
| 45 |
+
exists = con.execute(exists_query, [str(mobile)]).fetchone()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
if not exists:
|
| 48 |
return {
|
| 49 |
"success": False,
|
| 50 |
"number": mobile,
|
|
|
|
| 54 |
"credits": CREDITS
|
| 55 |
}
|
| 56 |
|
| 57 |
+
# 🔥 STEP 2: Full data fetch (ab local metadata cache se fast)
|
| 58 |
+
query = """
|
| 59 |
+
SELECT name, fathersName, phoneNumber, aadharNumber, address, district, pincode, state, town
|
| 60 |
+
FROM users_view
|
| 61 |
+
WHERE phoneNumber = ?
|
| 62 |
+
LIMIT 100
|
| 63 |
+
"""
|
| 64 |
+
result = con.execute(query, [str(mobile)]).fetchall()
|
| 65 |
+
|
| 66 |
results = []
|
| 67 |
for row in result:
|
| 68 |
results.append({
|