tfqdeadlo commited on
Commit
72c0ebf
·
verified ·
1 Parent(s): 002a8d0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -12
app.py CHANGED
@@ -1,4 +1,4 @@
1
- From fastapi import FastAPI, HTTPException, Query
2
  import duckdb
3
  from contextlib import asynccontextmanager
4
 
@@ -9,17 +9,22 @@ con = duckdb.connect()
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
 
@@ -30,7 +35,7 @@ def home():
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
@@ -56,6 +61,7 @@ async def search_mobile(mobile: str = Query(..., description="Enter mobile numbe
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
 
 
1
+ from fastapi import FastAPI, HTTPException, Query
2
  import duckdb
3
  from contextlib import asynccontextmanager
4
 
 
9
  @asynccontextmanager
10
  async def lifespan(app: FastAPI):
11
  print("DuckDB extensions load ho rahi hain aur View ban raha hai...")
12
+ try:
13
+ con.execute("INSTALL httpfs; LOAD httpfs;")
14
+
15
+ # Parquet file ka correct URL
16
+ PARQUET_URL = "https://huggingface.co/datasets/tfqdeadlo/Inddatainonefile/resolve/main/users_data.parquet"
17
+
18
+ # Startup par hi Ek baar View bana kar metadata cache kar lete hain
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
+ except Exception as e:
22
+ print(f"❌ Startup Initialization me error aaya: {e}")
23
  yield
24
+ try:
25
+ con.close()
26
+ except Exception:
27
+ pass
28
 
29
  app = FastAPI(title="Super Fast Search API", lifespan=lifespan)
30
 
 
35
  @app.get("/search")
36
  async def search_mobile(mobile: str = Query(..., description="Enter mobile number to search")):
37
  try:
38
+ # Cache kiye hue 'users_view' se data nikalenge
39
  query = """
40
  SELECT mobile, name, fname, address, alt, circle, id, email
41
  FROM users_view
 
61
  }
62
 
63
  except Exception as e:
64
+ print(f"❌ Query execute karne me error: {e}")
65
  # Internal error ko secure rakhne ke liye mask kiya hai
66
  raise HTTPException(status_code=500, detail="Query timeout ya process nahi ho payi.")
67