Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,48 @@
|
|
| 1 |
-
import os
|
| 2 |
from fastapi import FastAPI, HTTPException, Query
|
| 3 |
import duckdb
|
| 4 |
|
| 5 |
-
app = FastAPI()
|
|
|
|
|
|
|
| 6 |
con = duckdb.connect()
|
| 7 |
con.execute("INSTALL httpfs; LOAD httpfs;")
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
con.execute("SET enable_progress_bar = false")
|
| 12 |
-
con.execute("PRAGMA threads=4")
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
|
|
|
| 16 |
|
| 17 |
@app.get("/search")
|
| 18 |
-
async def search_mobile(mobile: str = Query(...)):
|
| 19 |
try:
|
| 20 |
-
#
|
| 21 |
query = f"""
|
| 22 |
-
SELECT mobile, name, fname, address, alt, circle, id, email
|
| 23 |
-
FROM read_parquet('{
|
| 24 |
-
WHERE mobile = ?
|
| 25 |
LIMIT 1
|
| 26 |
"""
|
| 27 |
-
|
| 28 |
-
result = con.execute(query, [mobile]).fetchall()
|
|
|
|
| 29 |
if not result:
|
| 30 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
columns = [desc[0] for desc in con.description]
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
except Exception as e:
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI, HTTPException, Query
|
| 2 |
import duckdb
|
| 3 |
|
| 4 |
+
app = FastAPI(title="Fast Search API")
|
| 5 |
+
|
| 6 |
+
# DuckDB connection aur httpfs extension load karo
|
| 7 |
con = duckdb.connect()
|
| 8 |
con.execute("INSTALL httpfs; LOAD httpfs;")
|
| 9 |
|
| 10 |
+
# Public Parquet URL (Isme koi token ya header nahi chahiye)
|
| 11 |
+
PARQUET_URL = "https://huggingface.co/datasets/tfqdeadlo/Inddatainonefile/resolve/main/users_data.parquet"
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
@app.get("/")
|
| 14 |
+
def home():
|
| 15 |
+
return {"message": "API online hai! Use /search?mobile=YOUR_NUMBER"}
|
| 16 |
|
| 17 |
@app.get("/search")
|
| 18 |
+
async def search_mobile(mobile: str = Query(..., description="Enter mobile number to search")):
|
| 19 |
try:
|
| 20 |
+
# Clean SQL Query bina kisi extra headers ke
|
| 21 |
query = f"""
|
| 22 |
+
SELECT mobile, name, fname, address, alt, circle, id, email
|
| 23 |
+
FROM read_parquet('{PARQUET_URL}')
|
| 24 |
+
WHERE mobile = ?
|
| 25 |
LIMIT 1
|
| 26 |
"""
|
| 27 |
+
|
| 28 |
+
result = con.execute(query, [str(mobile)]).fetchall()
|
| 29 |
+
|
| 30 |
if not result:
|
| 31 |
+
return {
|
| 32 |
+
"status": "error",
|
| 33 |
+
"message": f"Mobile number {mobile} nahi mila."
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
# Data ko dictionary format me convert karo
|
| 37 |
columns = [desc[0] for desc in con.description]
|
| 38 |
+
row_dict = dict(zip(columns, result[0]))
|
| 39 |
+
|
| 40 |
+
return {
|
| 41 |
+
"status": "success",
|
| 42 |
+
"data": row_dict
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
except Exception as e:
|
| 46 |
+
# Safe logging: Taki koi internal server details public na hon
|
| 47 |
+
raise HTTPException(status_code=500, detail="Server query process nahi kar paya.")
|
| 48 |
+
|