tfqdeadlo commited on
Commit
c7134fa
·
verified ·
1 Parent(s): a9de8bd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -13
app.py CHANGED
@@ -1,24 +1,30 @@
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 with httpfs extension
8
  con = duckdb.connect()
9
  con.execute("INSTALL httpfs; LOAD httpfs;")
10
 
11
- # Hugging Face token from Space secrets
12
  HF_TOKEN = os.environ.get("HF_TOKEN", "")
13
  if not HF_TOKEN:
14
  raise Exception("HF_TOKEN environment variable not set. Add it in Space secrets.")
15
 
16
- # Remote parquet URL
17
- PARQUET_URL = "https://huggingface.co/datasets/tfqdeadlo/Inddatainone/resolve/main/users_data.parquet"
 
 
 
 
 
 
18
 
19
- # DuckDB map syntax for HTTP headers
20
- # Isse token automatically authorisation header mein bheja jayega
21
- HEADERS_MAP = f"map({{'Authorization': 'Bearer {HF_TOKEN}'}})"
22
 
23
  @app.get("/")
24
  def home():
@@ -27,24 +33,22 @@ def home():
27
  @app.get("/search")
28
  async def search_mobile(mobile: str = Query(..., description="Mobile number to search")):
29
  try:
30
- # Direct remote parquet read with authentication headers
31
- # DuckDB sirf matching rows fetch karega, poori file nahi
32
  query = f"""
33
  SELECT mobile, name, fname, address, alt, circle, id, email
34
- FROM read_parquet('{PARQUET_URL}', http_headers={HEADERS_MAP})
35
  WHERE mobile = ?
36
  LIMIT 1
37
  """
38
  result = con.execute(query, [str(mobile)]).fetchall()
39
 
40
  if not result:
41
- return {"status": "error", "message": f"Mobile number {mobile} nahi mila."}
42
 
43
  columns = [desc[0] for desc in con.description]
44
  row_dict = dict(zip(columns, result[0]))
45
  return {"status": "success", "data": row_dict}
46
 
47
  except Exception as e:
48
- # Token ko error message se hata do
49
- error_msg = str(e).replace(HF_TOKEN, "[HIDDEN_TOKEN]")
50
  raise HTTPException(status_code=500, detail=f"Error: {error_msg}")
 
1
  import os
2
+ import json
3
  from fastapi import FastAPI, HTTPException, Query
4
  import duckdb
5
 
6
  app = FastAPI(title="Fast Mobile Search API")
7
 
8
+ # DuckDB connection
9
  con = duckdb.connect()
10
  con.execute("INSTALL httpfs; LOAD httpfs;")
11
 
12
+ # Hugging Face token
13
  HF_TOKEN = os.environ.get("HF_TOKEN", "")
14
  if not HF_TOKEN:
15
  raise Exception("HF_TOKEN environment variable not set. Add it in Space secrets.")
16
 
17
+ # Create secret with proper JSON headers
18
+ headers_json = json.dumps({"Authorization": f"Bearer {HF_TOKEN}"})
19
+ con.execute(f"""
20
+ CREATE OR REPLACE SECRET hf_token (
21
+ TYPE HTTP,
22
+ HEADERS '{headers_json}'
23
+ );
24
+ """)
25
 
26
+ # Parquet URL (without ?token)
27
+ PARQUET_URL = "https://huggingface.co/datasets/tfqdeadlo/Inddatainone/resolve/main/users_data.parquet"
 
28
 
29
  @app.get("/")
30
  def home():
 
33
  @app.get("/search")
34
  async def search_mobile(mobile: str = Query(..., description="Mobile number to search")):
35
  try:
36
+ # Secret automatically adds auth header to any httpfs request
 
37
  query = f"""
38
  SELECT mobile, name, fname, address, alt, circle, id, email
39
+ FROM read_parquet('{PARQUET_URL}')
40
  WHERE mobile = ?
41
  LIMIT 1
42
  """
43
  result = con.execute(query, [str(mobile)]).fetchall()
44
 
45
  if not result:
46
+ return {"status": "error", "message": f"Mobile {mobile} nahi mila."}
47
 
48
  columns = [desc[0] for desc in con.description]
49
  row_dict = dict(zip(columns, result[0]))
50
  return {"status": "success", "data": row_dict}
51
 
52
  except Exception as e:
53
+ error_msg = str(e).replace(HF_TOKEN, "[HIDDEN]")
 
54
  raise HTTPException(status_code=500, detail=f"Error: {error_msg}")