tfqdeadlo commited on
Commit
67fa32f
·
verified ·
1 Parent(s): 6fbe1b1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -15
app.py CHANGED
@@ -2,32 +2,33 @@ import os
2
  from fastapi import FastAPI, HTTPException, Query
3
  import duckdb
4
 
5
- app = FastAPI(title="Fast Mobile Search API")
6
-
7
  con = duckdb.connect()
8
  con.execute("INSTALL httpfs; LOAD httpfs;")
9
 
10
- # Direct URL - bina token ke (public dataset)
11
- PARQUET_URL = "https://huggingface.co/datasets/tfqdeadlo/Inddatainonefile/resolve/main/users_data.parquet"
 
 
12
 
13
- @app.get("/")
14
- def home():
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('{PARQUET_URL}')
23
- WHERE mobile = ?
24
  LIMIT 1
25
  """
26
- result = con.execute(query, [str(mobile)]).fetchall()
 
27
  if not result:
28
- return {"status": "error", "message": f"Mobile {mobile} nahi mila."}
29
  columns = [desc[0] for desc in con.description]
30
- row_dict = dict(zip(columns, result[0]))
31
- return {"status": "success", "data": row_dict}
32
  except Exception as e:
33
- raise HTTPException(status_code=500, detail=str(e))
 
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))