Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,13 @@
|
|
| 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 |
-
#
|
| 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("/")
|
|
@@ -31,9 +15,8 @@ def home():
|
|
| 31 |
return {"message": "API is running. Use /search?mobile=NUMBER"}
|
| 32 |
|
| 33 |
@app.get("/search")
|
| 34 |
-
async def search_mobile(mobile: str = Query(...
|
| 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}')
|
|
@@ -41,14 +24,10 @@ async def search_mobile(mobile: str = Query(..., description="Mobile number to s
|
|
| 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 |
-
|
| 54 |
-
raise HTTPException(status_code=500, detail=f"Error: {error_msg}")
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
from fastapi import FastAPI, HTTPException, Query
|
| 3 |
import duckdb
|
| 4 |
|
| 5 |
app = FastAPI(title="Fast Mobile Search API")
|
| 6 |
|
|
|
|
| 7 |
con = duckdb.connect()
|
| 8 |
con.execute("INSTALL httpfs; LOAD httpfs;")
|
| 9 |
|
| 10 |
+
# Direct URL - bina token ke (public dataset)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
PARQUET_URL = "https://huggingface.co/datasets/tfqdeadlo/Inddatainone/resolve/main/users_data.parquet"
|
| 12 |
|
| 13 |
@app.get("/")
|
|
|
|
| 15 |
return {"message": "API is running. Use /search?mobile=NUMBER"}
|
| 16 |
|
| 17 |
@app.get("/search")
|
| 18 |
+
async def search_mobile(mobile: str = Query(...)):
|
| 19 |
try:
|
|
|
|
| 20 |
query = f"""
|
| 21 |
SELECT mobile, name, fname, address, alt, circle, id, email
|
| 22 |
FROM read_parquet('{PARQUET_URL}')
|
|
|
|
| 24 |
LIMIT 1
|
| 25 |
"""
|
| 26 |
result = con.execute(query, [str(mobile)]).fetchall()
|
|
|
|
| 27 |
if not result:
|
| 28 |
return {"status": "error", "message": f"Mobile {mobile} nahi mila."}
|
|
|
|
| 29 |
columns = [desc[0] for desc in con.description]
|
| 30 |
row_dict = dict(zip(columns, result[0]))
|
| 31 |
return {"status": "success", "data": row_dict}
|
|
|
|
| 32 |
except Exception as e:
|
| 33 |
+
raise HTTPException(status_code=500, detail=str(e))
|
|
|