Inayatgaming commited on
Commit
43f01a7
·
verified ·
1 Parent(s): 66de7c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -20
app.py CHANGED
@@ -1,5 +1,7 @@
1
  import os
2
- from fastapi import FastAPI, Header, HTTPException
 
 
3
 
4
  app = FastAPI(
5
  title="InayatGaming Dataset API",
@@ -8,18 +10,33 @@ app = FastAPI(
8
 
9
  API_KEY = os.getenv("API_KEY")
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- def auth(key: str | None):
13
  if not API_KEY:
14
  raise HTTPException(
15
  status_code=500,
16
  detail="API_KEY is not configured"
17
  )
18
 
19
- if key != API_KEY:
20
  raise HTTPException(
21
  status_code=401,
22
- detail="Unauthorized"
23
  )
24
 
25
 
@@ -33,22 +50,61 @@ def root():
33
 
34
  @app.get("/health")
35
  def health():
36
- return {"status": "ok"}
 
 
37
 
38
 
39
- @app.get("/info")
40
- def info(x_api_key: str | None = Header(default=None)):
41
- auth(x_api_key)
 
 
 
42
 
43
- return {
44
- "dataset": "Inayatgaming/InayatGaming",
45
- "format": "Parquet",
46
- "files": [
47
- "part1.parquet",
48
- "part2a.parquet",
49
- "part2b_new.parquet"
50
- ],
51
- "rows": 2504793870,
52
- "approx_size": "103.8 GB",
53
- "query_engine": "DuckDB"
54
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
+ import duckdb
3
+
4
+ from fastapi import FastAPI, Query, HTTPException
5
 
6
  app = FastAPI(
7
  title="InayatGaming Dataset API",
 
10
 
11
  API_KEY = os.getenv("API_KEY")
12
 
13
+ PARQUET_FILES = [
14
+ "hf://datasets/Inayatgaming/InayatGaming/part1.parquet",
15
+ "hf://datasets/Inayatgaming/InayatGaming/part2a.parquet",
16
+ "hf://datasets/Inayatgaming/InayatGaming/part2b_new.parquet",
17
+ ]
18
+
19
+ # Only fields explicitly approved for API output.
20
+ PUBLIC_FIELDS = [
21
+ "district",
22
+ "pincode",
23
+ "state",
24
+ "town",
25
+ "source",
26
+ ]
27
+
28
 
29
+ def authenticate(api_key: str):
30
  if not API_KEY:
31
  raise HTTPException(
32
  status_code=500,
33
  detail="API_KEY is not configured"
34
  )
35
 
36
+ if api_key != API_KEY:
37
  raise HTTPException(
38
  status_code=401,
39
+ detail="Invalid API key"
40
  )
41
 
42
 
 
50
 
51
  @app.get("/health")
52
  def health():
53
+ return {
54
+ "status": "ok"
55
+ }
56
 
57
 
58
+ @app.get("/lookup")
59
+ def lookup(
60
+ api_key: str = Query(...),
61
+ number: str = Query(..., min_length=1)
62
+ ):
63
+ authenticate(api_key)
64
 
65
+ # Normalize the supplied identifier.
66
+ number = number.strip()
67
+
68
+ con = duckdb.connect()
69
+
70
+ try:
71
+ query = """
72
+ SELECT
73
+ district,
74
+ pincode,
75
+ state,
76
+ town,
77
+ source
78
+ FROM read_parquet(
79
+ ?,
80
+ union_by_name=true
81
+ )
82
+ WHERE phoneNumber = ?
83
+ LIMIT 1
84
+ """
85
+
86
+ # Query each source file rather than loading the dataset
87
+ # into Python memory.
88
+ for parquet_file in PARQUET_FILES:
89
+ result = con.execute(
90
+ query,
91
+ [parquet_file, number]
92
+ ).fetchone()
93
+
94
+ if result:
95
+ return {
96
+ "success": True,
97
+ "match": True,
98
+ "data": dict(
99
+ zip(PUBLIC_FIELDS, result)
100
+ )
101
+ }
102
+
103
+ return {
104
+ "success": True,
105
+ "match": False,
106
+ "data": None
107
+ }
108
+
109
+ finally:
110
+ con.close()