Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,32 +2,33 @@ import os
|
|
| 2 |
from fastapi import FastAPI, HTTPException, Query
|
| 3 |
import duckdb
|
| 4 |
|
| 5 |
-
app = FastAPI(
|
| 6 |
-
|
| 7 |
con = duckdb.connect()
|
| 8 |
con.execute("INSTALL httpfs; LOAD httpfs;")
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
return {"message": "API is running. Use /search?mobile=NUMBER"}
|
| 16 |
|
| 17 |
@app.get("/search")
|
| 18 |
async def search_mobile(mobile: str = Query(...)):
|
| 19 |
try:
|
|
|
|
| 20 |
query = f"""
|
| 21 |
-
SELECT mobile, name, fname, address, alt, circle, id, email
|
| 22 |
-
FROM read_parquet('{
|
| 23 |
-
WHERE mobile = ?
|
| 24 |
LIMIT 1
|
| 25 |
"""
|
| 26 |
-
|
|
|
|
| 27 |
if not result:
|
| 28 |
-
return {"status": "error", "message":
|
| 29 |
columns = [desc[0] for desc in con.description]
|
| 30 |
-
|
| 31 |
-
return {"status": "success", "data": row_dict}
|
| 32 |
except Exception as e:
|
| 33 |
-
raise HTTPException(
|
|
|
|
| 2 |
from fastapi import FastAPI, HTTPException, Query
|
| 3 |
import duckdb
|
| 4 |
|
| 5 |
+
app = FastAPI()
|
|
|
|
| 6 |
con = duckdb.connect()
|
| 7 |
con.execute("INSTALL httpfs; LOAD httpfs;")
|
| 8 |
|
| 9 |
+
# Performance settings
|
| 10 |
+
con.execute("SET memory_limit = '2GB'")
|
| 11 |
+
con.execute("SET enable_progress_bar = false")
|
| 12 |
+
con.execute("PRAGMA threads=4")
|
| 13 |
|
| 14 |
+
# Sahi path (sorted file ka)
|
| 15 |
+
HF_PATH = "hf://datasets/tfqdeadlo/Inddatainonefile@refs/convert/parquet/default/train/0000.parquet"
|
|
|
|
| 16 |
|
| 17 |
@app.get("/search")
|
| 18 |
async def search_mobile(mobile: str = Query(...)):
|
| 19 |
try:
|
| 20 |
+
# Sirf needed columns + DuckDB ko sorted column hint dena (optional)
|
| 21 |
query = f"""
|
| 22 |
+
SELECT mobile, name, fname, address, alt, circle, id, email
|
| 23 |
+
FROM read_parquet('{HF_PATH}')
|
| 24 |
+
WHERE mobile = ?
|
| 25 |
LIMIT 1
|
| 26 |
"""
|
| 27 |
+
# DuckDB apne aap row group skip kar lega kyunki data sorted hai
|
| 28 |
+
result = con.execute(query, [mobile]).fetchall()
|
| 29 |
if not result:
|
| 30 |
+
return {"status": "error", "message": "Not found"}
|
| 31 |
columns = [desc[0] for desc in con.description]
|
| 32 |
+
return {"status": "success", "data": dict(zip(columns, result[0]))}
|
|
|
|
| 33 |
except Exception as e:
|
| 34 |
+
raise HTTPException(500, detail=str(e))
|