tfqdeadlo commited on
Commit
15a932a
·
verified ·
1 Parent(s): f9458a2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -7
app.py CHANGED
@@ -1,24 +1,31 @@
 
1
  from fastapi import FastAPI, HTTPException, Query
2
  import duckdb
3
 
4
  app = FastAPI(title="Fast Mobile Search API")
5
 
6
- # Global DuckDB connection setup karo aur HTTPFS extension load karo remote files read karne ke liye
7
  con = duckdb.connect()
8
  con.execute("INSTALL httpfs; LOAD httpfs;")
9
 
10
- # Tumhari Hugging Face Parquet file ka direct URL
 
 
 
11
  PARQUET_URL = "https://huggingface.co/datasets/tfqdeadlo/Inddatainone/resolve/main/users_data.parquet"
12
 
 
 
 
 
13
  @app.get("/")
14
  def home():
15
- return {"message": "API is running. Use /search?mobile=YOUR_NUMBER to find data."}
16
 
17
  @app.get("/search")
18
  async def search_mobile(mobile: str = Query(..., description="Enter the mobile number to search")):
19
  try:
20
- # Parameterized query use kar rahe hain taaki SQL injection na ho
21
- # read_parquet() seedhe URL se data fetch karega bina download kiye
22
  query = f"""
23
  SELECT mobile, name, fname, address, alt, circle, id, email
24
  FROM read_parquet('{PARQUET_URL}')
@@ -26,7 +33,7 @@ async def search_mobile(mobile: str = Query(..., description="Enter the mobile n
26
  LIMIT 1
27
  """
28
 
29
- # Query execute karo
30
  result = con.execute(query, [str(mobile)]).fetchall()
31
 
32
  if not result:
@@ -35,7 +42,7 @@ async def search_mobile(mobile: str = Query(..., description="Enter the mobile n
35
  "message": f"Mobile number {mobile} nahi mila."
36
  }
37
 
38
- # Column names nikalne ke liye desc ka use karenge
39
  columns = [desc[0] for desc in con.description]
40
  row_dict = dict(zip(columns, result[0]))
41
 
 
1
+ import os
2
  from fastapi import FastAPI, HTTPException, Query
3
  import duckdb
4
 
5
  app = FastAPI(title="Fast Mobile Search API")
6
 
7
+ # DuckDB connection setup aur httpfs extension load karo
8
  con = duckdb.connect()
9
  con.execute("INSTALL httpfs; LOAD httpfs;")
10
 
11
+ # Hugging Face Space ke Secret se token nikalna
12
+ HF_TOKEN = os.environ.get("HF_TOKEN", "")
13
+
14
+ # Base Parquet URL
15
  PARQUET_URL = "https://huggingface.co/datasets/tfqdeadlo/Inddatainone/resolve/main/users_data.parquet"
16
 
17
+ # Agar token maujood hai, toh URL ke sath authenticated request ke liye append karo
18
+ if HF_TOKEN:
19
+ PARQUET_URL += f"?token={HF_TOKEN}"
20
+
21
  @app.get("/")
22
  def home():
23
+ return {"message": "API is running perfectly. Use /search?mobile=YOUR_NUMBER"}
24
 
25
  @app.get("/search")
26
  async def search_mobile(mobile: str = Query(..., description="Enter the mobile number to search")):
27
  try:
28
+ # DuckDB query jo seedhe authenticated URL se data fetch karegi
 
29
  query = f"""
30
  SELECT mobile, name, fname, address, alt, circle, id, email
31
  FROM read_parquet('{PARQUET_URL}')
 
33
  LIMIT 1
34
  """
35
 
36
+ # Query execute karo aur result fetch karo
37
  result = con.execute(query, [str(mobile)]).fetchall()
38
 
39
  if not result:
 
42
  "message": f"Mobile number {mobile} nahi mila."
43
  }
44
 
45
+ # Columns names nikal kar dictionary format me data convert karo
46
  columns = [desc[0] for desc in con.description]
47
  row_dict = dict(zip(columns, result[0]))
48