Spaces:
Running
Running
Create app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
"""FastAPI backend over DuckDB -
|
| 2 |
import json
|
| 3 |
import os
|
| 4 |
import threading
|
|
@@ -10,8 +10,8 @@ import numpy as np
|
|
| 10 |
import pandas as pd
|
| 11 |
from fastapi import FastAPI, Query, Response
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
HF_FILE = "hf://datasets/HiTeckGroup/
|
| 15 |
HF_TOKEN = os.environ.get("HF_TOKEN", "")
|
| 16 |
|
| 17 |
# π API KEY
|
|
@@ -20,7 +20,7 @@ VALID_KEYS = {}
|
|
| 20 |
if API_KEY:
|
| 21 |
VALID_KEYS[API_KEY] = {}
|
| 22 |
|
| 23 |
-
app = FastAPI(title="
|
| 24 |
|
| 25 |
_conn = None
|
| 26 |
_conn_lock = threading.Lock()
|
|
@@ -38,19 +38,19 @@ def _get_conn() -> duckdb.DuckDBPyConnection:
|
|
| 38 |
if HF_TOKEN:
|
| 39 |
con.execute(f"SET http_headers = {{'Authorization': 'Bearer {HF_TOKEN}'}};")
|
| 40 |
|
| 41 |
-
# Speed settings
|
| 42 |
con.execute("SET enable_http_metadata_cache = true;")
|
| 43 |
con.execute("SET http_keep_alive = true;")
|
| 44 |
con.execute("SET http_timeout = 600000;")
|
| 45 |
con.execute("SET threads = 4;")
|
| 46 |
|
| 47 |
_conn = con
|
| 48 |
-
print("π DuckDB ready (
|
| 49 |
return con
|
| 50 |
|
| 51 |
def _check_key(key: str | None) -> tuple[bool, dict | None]:
|
| 52 |
if not key or key not in VALID_KEYS:
|
| 53 |
-
return False, {"message": "Provide correct key", "contact": "@
|
| 54 |
return True, VALID_KEYS[key]
|
| 55 |
|
| 56 |
def _pretty(data: dict[str, Any], pretty: bool = True) -> Response:
|
|
@@ -67,49 +67,48 @@ async def startup_event():
|
|
| 67 |
|
| 68 |
@app.get("/")
|
| 69 |
def home():
|
| 70 |
-
return {"app": "
|
| 71 |
|
| 72 |
@app.get("/search")
|
| 73 |
async def search(
|
| 74 |
key: str = Query(..., description="API Key"),
|
| 75 |
-
|
| 76 |
-
email: str = Query(None, description="Search by Email"),
|
| 77 |
pretty: bool = Query(True)
|
| 78 |
):
|
|
|
|
| 79 |
valid, err = _check_key(key)
|
| 80 |
if not valid:
|
| 81 |
return Response(content=json.dumps(err, ensure_ascii=False), status_code=403, media_type="application/json")
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
if phone and (not phone.isdigit() or len(phone) != 10):
|
| 87 |
-
return Response(content=json.dumps({"message": "Provide valid 10 digit phone", "contact": "@dmforbans"}), status_code=400, media_type="application/json")
|
| 88 |
|
| 89 |
try:
|
| 90 |
con = _get_conn()
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
if email:
|
| 98 |
-
query += " AND CAST(CustomerEmail AS VARCHAR) ILIKE ?"
|
| 99 |
-
params.append(f"%{email}%")
|
| 100 |
-
|
| 101 |
-
query += " LIMIT 50"
|
| 102 |
-
df = con.execute(query, params).fetchdf()
|
| 103 |
|
| 104 |
if df is None or df.empty:
|
| 105 |
-
return _pretty({"found": 0, "data": [], "developer": "@
|
| 106 |
|
|
|
|
| 107 |
df = df.replace([np.inf, -np.inf], np.nan)
|
| 108 |
df = df.where(pd.notnull(df), None)
|
| 109 |
records = df.to_dict(orient="records")
|
| 110 |
|
| 111 |
-
return _pretty({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
except Exception:
|
| 114 |
traceback.print_exc()
|
| 115 |
-
return Response(content=json.dumps({"
|
|
|
|
| 1 |
+
"""FastAPI backend over DuckDB - HiTeckNuMinfo Email Search."""
|
| 2 |
import json
|
| 3 |
import os
|
| 4 |
import threading
|
|
|
|
| 10 |
import pandas as pd
|
| 11 |
from fastapi import FastAPI, Query, Response
|
| 12 |
|
| 13 |
+
# π₯ NuMinfo Dataset Path
|
| 14 |
+
HF_FILE = "hf://datasets/HiTeckGroup/HiTeckNuMinfo@main/users_data.parquet"
|
| 15 |
HF_TOKEN = os.environ.get("HF_TOKEN", "")
|
| 16 |
|
| 17 |
# π API KEY
|
|
|
|
| 20 |
if API_KEY:
|
| 21 |
VALID_KEYS[API_KEY] = {}
|
| 22 |
|
| 23 |
+
app = FastAPI(title="NuMinfo Email Search API")
|
| 24 |
|
| 25 |
_conn = None
|
| 26 |
_conn_lock = threading.Lock()
|
|
|
|
| 38 |
if HF_TOKEN:
|
| 39 |
con.execute(f"SET http_headers = {{'Authorization': 'Bearer {HF_TOKEN}'}};")
|
| 40 |
|
| 41 |
+
# π Speed settings (Metadata Cache)
|
| 42 |
con.execute("SET enable_http_metadata_cache = true;")
|
| 43 |
con.execute("SET http_keep_alive = true;")
|
| 44 |
con.execute("SET http_timeout = 600000;")
|
| 45 |
con.execute("SET threads = 4;")
|
| 46 |
|
| 47 |
_conn = con
|
| 48 |
+
print("π DuckDB ready (NuMinfo API)", flush=True)
|
| 49 |
return con
|
| 50 |
|
| 51 |
def _check_key(key: str | None) -> tuple[bool, dict | None]:
|
| 52 |
if not key or key not in VALID_KEYS:
|
| 53 |
+
return False, {"message": "Provide correct key", "contact": "@Endexed"}
|
| 54 |
return True, VALID_KEYS[key]
|
| 55 |
|
| 56 |
def _pretty(data: dict[str, Any], pretty: bool = True) -> Response:
|
|
|
|
| 67 |
|
| 68 |
@app.get("/")
|
| 69 |
def home():
|
| 70 |
+
return {"app": "NuMinfo Email Search API", "status": "active", "contact": "@Endexed"}
|
| 71 |
|
| 72 |
@app.get("/search")
|
| 73 |
async def search(
|
| 74 |
key: str = Query(..., description="API Key"),
|
| 75 |
+
email: str = Query(..., description="Search by Email (exact match)"),
|
|
|
|
| 76 |
pretty: bool = Query(True)
|
| 77 |
):
|
| 78 |
+
# π Key Check
|
| 79 |
valid, err = _check_key(key)
|
| 80 |
if not valid:
|
| 81 |
return Response(content=json.dumps(err, ensure_ascii=False), status_code=403, media_type="application/json")
|
| 82 |
|
| 83 |
+
# π Email Check
|
| 84 |
+
if not email or "@" not in email:
|
| 85 |
+
return Response(content=json.dumps({"message": "Provide valid email address", "contact": "@Endexed"}), status_code=400, media_type="application/json")
|
|
|
|
|
|
|
| 86 |
|
| 87 |
try:
|
| 88 |
con = _get_conn()
|
| 89 |
+
|
| 90 |
+
# 93GB file me fast search ke liye seedha email match karenge
|
| 91 |
+
query = "SELECT * FROM read_parquet(?) WHERE email = ? LIMIT 50"
|
| 92 |
+
|
| 93 |
+
# Email ko lowercase me convert kar rahe hain taaki case-insensitive match ho
|
| 94 |
+
df = con.execute(query, [HF_FILE, email.lower().strip()]).fetchdf()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
if df is None or df.empty:
|
| 97 |
+
return _pretty({"found": 0, "data": [], "developer": "@Endexed"}, pretty)
|
| 98 |
|
| 99 |
+
# NaN values ko null kar do
|
| 100 |
df = df.replace([np.inf, -np.inf], np.nan)
|
| 101 |
df = df.where(pd.notnull(df), None)
|
| 102 |
records = df.to_dict(orient="records")
|
| 103 |
|
| 104 |
+
return _pretty({
|
| 105 |
+
"status": "success",
|
| 106 |
+
"developer": "@Endexed",
|
| 107 |
+
"found": len(records),
|
| 108 |
+
"data": records,
|
| 109 |
+
"contact": "@Endexed"
|
| 110 |
+
}, pretty)
|
| 111 |
|
| 112 |
except Exception:
|
| 113 |
traceback.print_exc()
|
| 114 |
+
return Response(content=json.dumps({"status": "error", "message": "Data not found", "contact": "@Endexed"}), status_code=500, media_type="application/json")
|