tfqdeadlo commited on
Commit
3b04732
·
verified ·
1 Parent(s): 67fa32f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -18
app.py CHANGED
@@ -1,34 +1,48 @@
1
- import os
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))
 
 
 
 
1
  from fastapi import FastAPI, HTTPException, Query
2
  import duckdb
3
 
4
+ app = FastAPI(title="Fast Search API")
5
+
6
+ # DuckDB connection aur httpfs extension load karo
7
  con = duckdb.connect()
8
  con.execute("INSTALL httpfs; LOAD httpfs;")
9
 
10
+ # Public Parquet URL (Isme koi token ya header nahi chahiye)
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 online hai! Use /search?mobile=YOUR_NUMBER"}
16
 
17
  @app.get("/search")
18
+ async def search_mobile(mobile: str = Query(..., description="Enter mobile number to search")):
19
  try:
20
+ # Clean SQL Query bina kisi extra headers ke
21
  query = f"""
22
+ SELECT mobile, name, fname, address, alt, circle, id, email
23
+ FROM read_parquet('{PARQUET_URL}')
24
+ WHERE mobile = ?
25
  LIMIT 1
26
  """
27
+
28
+ result = con.execute(query, [str(mobile)]).fetchall()
29
+
30
  if not result:
31
+ return {
32
+ "status": "error",
33
+ "message": f"Mobile number {mobile} nahi mila."
34
+ }
35
+
36
+ # Data ko dictionary format me convert karo
37
  columns = [desc[0] for desc in con.description]
38
+ row_dict = dict(zip(columns, result[0]))
39
+
40
+ return {
41
+ "status": "success",
42
+ "data": row_dict
43
+ }
44
+
45
  except Exception as e:
46
+ # Safe logging: Taki koi internal server details public na hon
47
+ raise HTTPException(status_code=500, detail="Server query process nahi kar paya.")
48
+