Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,80 +1,49 @@
|
|
| 1 |
import os
|
| 2 |
-
import tempfile
|
| 3 |
-
import requests
|
| 4 |
from fastapi import FastAPI, HTTPException, Query
|
| 5 |
import duckdb
|
| 6 |
|
| 7 |
app = FastAPI(title="Fast Mobile Search API")
|
| 8 |
|
| 9 |
-
# DuckDB connection
|
| 10 |
con = duckdb.connect()
|
|
|
|
| 11 |
|
| 12 |
-
# Hugging Face token
|
| 13 |
HF_TOKEN = os.environ.get("HF_TOKEN", "")
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
if temp_file_path and os.path.exists(temp_file_path):
|
| 22 |
-
return temp_file_path
|
| 23 |
-
|
| 24 |
-
if not HF_TOKEN:
|
| 25 |
-
raise Exception("HF_TOKEN environment variable not set. Please add your token in Space secrets.")
|
| 26 |
-
|
| 27 |
-
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 28 |
-
response = requests.get(PARQUET_URL, headers=headers, stream=True)
|
| 29 |
-
|
| 30 |
-
if response.status_code != 200:
|
| 31 |
-
raise Exception(f"Download failed with status {response.status_code}: {response.text[:200]}")
|
| 32 |
-
|
| 33 |
-
# Save to a temporary file
|
| 34 |
-
fd, path = tempfile.mkstemp(suffix=".parquet")
|
| 35 |
-
os.close(fd)
|
| 36 |
-
with open(path, "wb") as f:
|
| 37 |
-
for chunk in response.iter_content(chunk_size=8192):
|
| 38 |
-
f.write(chunk)
|
| 39 |
-
|
| 40 |
-
temp_file_path = path
|
| 41 |
-
return path
|
| 42 |
|
| 43 |
@app.get("/")
|
| 44 |
def home():
|
| 45 |
-
return {"message": "API is running
|
| 46 |
|
| 47 |
@app.get("/search")
|
| 48 |
-
async def search_mobile(mobile: str = Query(..., description="
|
| 49 |
try:
|
| 50 |
-
#
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
# DuckDB query on local file
|
| 54 |
query = f"""
|
| 55 |
SELECT mobile, name, fname, address, alt, circle, id, email
|
| 56 |
-
FROM read_parquet('{
|
| 57 |
WHERE mobile = ?
|
| 58 |
LIMIT 1
|
| 59 |
"""
|
| 60 |
-
|
| 61 |
result = con.execute(query, [str(mobile)]).fetchall()
|
| 62 |
|
| 63 |
if not result:
|
| 64 |
-
return {
|
| 65 |
-
"status": "error",
|
| 66 |
-
"message": f"Mobile number {mobile} nahi mila."
|
| 67 |
-
}
|
| 68 |
|
| 69 |
columns = [desc[0] for desc in con.description]
|
| 70 |
row_dict = dict(zip(columns, result[0]))
|
| 71 |
-
|
| 72 |
-
return {
|
| 73 |
-
"status": "success",
|
| 74 |
-
"data": row_dict
|
| 75 |
-
}
|
| 76 |
|
| 77 |
except Exception as e:
|
| 78 |
-
#
|
| 79 |
-
error_msg = str(e).replace(HF_TOKEN, "[
|
| 80 |
-
raise HTTPException(status_code=500, detail=f"
|
|
|
|
| 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 |
+
# Set HTTP headers for all outgoing httpfs requests
|
| 17 |
+
con.execute(f"SET http_headers = 'Authorization: Bearer {HF_TOKEN}'")
|
| 18 |
|
| 19 |
+
# Parquet URL (without ?token)
|
| 20 |
+
PARQUET_URL = "https://huggingface.co/datasets/tfqdeadlo/Inddatainone/resolve/main/users_data.parquet"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
@app.get("/")
|
| 23 |
def home():
|
| 24 |
+
return {"message": "API is running. Use /search?mobile=NUMBER"}
|
| 25 |
|
| 26 |
@app.get("/search")
|
| 27 |
+
async def search_mobile(mobile: str = Query(..., description="Mobile number to search")):
|
| 28 |
try:
|
| 29 |
+
# DuckDB will read remote parquet directly (no download)
|
| 30 |
+
# Predicate pushdown: sirf matching rows fetch hongi
|
|
|
|
|
|
|
| 31 |
query = f"""
|
| 32 |
SELECT mobile, name, fname, address, alt, circle, id, email
|
| 33 |
+
FROM read_parquet('{PARQUET_URL}')
|
| 34 |
WHERE mobile = ?
|
| 35 |
LIMIT 1
|
| 36 |
"""
|
|
|
|
| 37 |
result = con.execute(query, [str(mobile)]).fetchall()
|
| 38 |
|
| 39 |
if not result:
|
| 40 |
+
return {"status": "error", "message": f"Mobile {mobile} nahi mila."}
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
columns = [desc[0] for desc in con.description]
|
| 43 |
row_dict = dict(zip(columns, result[0]))
|
| 44 |
+
return {"status": "success", "data": row_dict}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
except Exception as e:
|
| 47 |
+
# Token hide karo error se
|
| 48 |
+
error_msg = str(e).replace(HF_TOKEN, "[HIDDEN]")
|
| 49 |
+
raise HTTPException(status_code=500, detail=f"Error: {error_msg}")
|