Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,17 +13,13 @@ 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 |
-
#
|
| 17 |
-
con.execute(f"""
|
| 18 |
-
CREATE SECRET IF NOT EXISTS hf_token (
|
| 19 |
-
TYPE HTTP,
|
| 20 |
-
HEADER 'Authorization' 'Bearer {HF_TOKEN}'
|
| 21 |
-
);
|
| 22 |
-
""")
|
| 23 |
-
|
| 24 |
-
# Parquet URL (without ?token)
|
| 25 |
PARQUET_URL = "https://huggingface.co/datasets/tfqdeadlo/Inddatainone/resolve/main/users_data.parquet"
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
@app.get("/")
|
| 28 |
def home():
|
| 29 |
return {"message": "API is running. Use /search?mobile=NUMBER"}
|
|
@@ -31,23 +27,24 @@ def home():
|
|
| 31 |
@app.get("/search")
|
| 32 |
async def search_mobile(mobile: str = Query(..., description="Mobile number to search")):
|
| 33 |
try:
|
| 34 |
-
#
|
|
|
|
| 35 |
query = f"""
|
| 36 |
SELECT mobile, name, fname, address, alt, circle, id, email
|
| 37 |
-
FROM read_parquet('{PARQUET_URL}')
|
| 38 |
WHERE mobile = ?
|
| 39 |
LIMIT 1
|
| 40 |
"""
|
| 41 |
result = con.execute(query, [str(mobile)]).fetchall()
|
| 42 |
|
| 43 |
if not result:
|
| 44 |
-
return {"status": "error", "message": f"Mobile {mobile} nahi mila."}
|
| 45 |
|
| 46 |
columns = [desc[0] for desc in con.description]
|
| 47 |
row_dict = dict(zip(columns, result[0]))
|
| 48 |
return {"status": "success", "data": row_dict}
|
| 49 |
|
| 50 |
except Exception as e:
|
| 51 |
-
# Token
|
| 52 |
-
error_msg = str(e).replace(HF_TOKEN, "[
|
| 53 |
raise HTTPException(status_code=500, detail=f"Error: {error_msg}")
|
|
|
|
| 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():
|
| 25 |
return {"message": "API is running. Use /search?mobile=NUMBER"}
|
|
|
|
| 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}")
|