tfqdeadlo commited on
Commit
478cd9e
·
verified ·
1 Parent(s): 3b04732

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -13
app.py CHANGED
@@ -1,26 +1,39 @@
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
  """
@@ -33,7 +46,7 @@ async def search_mobile(mobile: str = Query(..., description="Enter mobile numbe
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
 
@@ -43,6 +56,6 @@ async def search_mobile(mobile: str = Query(..., description="Enter mobile numbe
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
 
 
1
  from fastapi import FastAPI, HTTPException, Query
2
  import duckdb
3
+ from contextlib import asynccontextmanager
4
 
5
+ # Global connection object
 
 
6
  con = duckdb.connect()
 
7
 
8
+ # Lifespan use karke app start hote hi heavy initialization ek baar karenge
9
+ @asynccontextmanager
10
+ async def lifespan(app: FastAPI):
11
+ print("DuckDB extensions load ho rahi hain aur View ban raha hai...")
12
+ con.execute("INSTALL httpfs; LOAD httpfs;")
13
+
14
+ # Parquet file ka correct URL
15
+ PARQUET_URL = "https://huggingface.co/datasets/tfqdeadlo/Inddatainonefile/resolve/main/users_data.parquet"
16
+
17
+ # Startup par hi Ek baar View bana kar metadata cache kar lete hain
18
+ # Isse har API request par network par load nahi padega
19
+ con.execute(f"CREATE OR REPLACE VIEW users_view AS SELECT * FROM read_parquet('{PARQUET_URL}')")
20
+ print("View taiyar hai! API requests ke liye taiyar hai.")
21
+ yield
22
+ con.close()
23
+
24
+ app = FastAPI(title="Super Fast Search API", lifespan=lifespan)
25
 
26
  @app.get("/")
27
  def home():
28
+ return {"message": "Fast API online hai! Use /search?mobile=YOUR_NUMBER"}
29
 
30
  @app.get("/search")
31
  async def search_mobile(mobile: str = Query(..., description="Enter mobile number to search")):
32
  try:
33
+ # Ab hum direct cache kiye hue 'users_view' se data nikalenge
34
+ query = """
35
  SELECT mobile, name, fname, address, alt, circle, id, email
36
+ FROM users_view
37
  WHERE mobile = ?
38
  LIMIT 1
39
  """
 
46
  "message": f"Mobile number {mobile} nahi mila."
47
  }
48
 
49
+ # Data mapping
50
  columns = [desc[0] for desc in con.description]
51
  row_dict = dict(zip(columns, result[0]))
52
 
 
56
  }
57
 
58
  except Exception as e:
59
+ # Internal error ko secure rakhne ke liye mask kiya hai
60
+ raise HTTPException(status_code=500, detail="Query timeout ya process nahi ho payi.")
61