Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,39 @@
|
|
| 1 |
from fastapi import FastAPI, HTTPException, Query
|
| 2 |
import duckdb
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
# DuckDB connection aur httpfs extension load karo
|
| 7 |
con = duckdb.connect()
|
| 8 |
-
con.execute("INSTALL httpfs; LOAD httpfs;")
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
#
|
| 21 |
-
query =
|
| 22 |
SELECT mobile, name, fname, address, alt, circle, id, email
|
| 23 |
-
FROM
|
| 24 |
WHERE mobile = ?
|
| 25 |
LIMIT 1
|
| 26 |
"""
|
|
@@ -33,7 +46,7 @@ async def search_mobile(mobile: str = Query(..., description="Enter mobile numbe
|
|
| 33 |
"message": f"Mobile number {mobile} nahi mila."
|
| 34 |
}
|
| 35 |
|
| 36 |
-
# Data
|
| 37 |
columns = [desc[0] for desc in con.description]
|
| 38 |
row_dict = dict(zip(columns, result[0]))
|
| 39 |
|
|
@@ -43,6 +56,6 @@ async def search_mobile(mobile: str = Query(..., description="Enter mobile numbe
|
|
| 43 |
}
|
| 44 |
|
| 45 |
except Exception as e:
|
| 46 |
-
#
|
| 47 |
-
raise HTTPException(status_code=500, detail="
|
| 48 |
|
|
|
|
| 1 |
from fastapi import FastAPI, HTTPException, Query
|
| 2 |
import duckdb
|
| 3 |
+
from contextlib import asynccontextmanager
|
| 4 |
|
| 5 |
+
# Global connection object
|
|
|
|
|
|
|
| 6 |
con = duckdb.connect()
|
|
|
|
| 7 |
|
| 8 |
+
# Lifespan use karke app start hote hi heavy initialization ek baar karenge
|
| 9 |
+
@asynccontextmanager
|
| 10 |
+
async def lifespan(app: FastAPI):
|
| 11 |
+
print("DuckDB extensions load ho rahi hain aur View ban raha hai...")
|
| 12 |
+
con.execute("INSTALL httpfs; LOAD httpfs;")
|
| 13 |
+
|
| 14 |
+
# Parquet file ka correct URL
|
| 15 |
+
PARQUET_URL = "https://huggingface.co/datasets/tfqdeadlo/Inddatainonefile/resolve/main/users_data.parquet"
|
| 16 |
+
|
| 17 |
+
# Startup par hi Ek baar View bana kar metadata cache kar lete hain
|
| 18 |
+
# Isse har API request par network par load nahi padega
|
| 19 |
+
con.execute(f"CREATE OR REPLACE VIEW users_view AS SELECT * FROM read_parquet('{PARQUET_URL}')")
|
| 20 |
+
print("View taiyar hai! API requests ke liye taiyar hai.")
|
| 21 |
+
yield
|
| 22 |
+
con.close()
|
| 23 |
+
|
| 24 |
+
app = FastAPI(title="Super Fast Search API", lifespan=lifespan)
|
| 25 |
|
| 26 |
@app.get("/")
|
| 27 |
def home():
|
| 28 |
+
return {"message": "Fast API online hai! Use /search?mobile=YOUR_NUMBER"}
|
| 29 |
|
| 30 |
@app.get("/search")
|
| 31 |
async def search_mobile(mobile: str = Query(..., description="Enter mobile number to search")):
|
| 32 |
try:
|
| 33 |
+
# Ab hum direct cache kiye hue 'users_view' se data nikalenge
|
| 34 |
+
query = """
|
| 35 |
SELECT mobile, name, fname, address, alt, circle, id, email
|
| 36 |
+
FROM users_view
|
| 37 |
WHERE mobile = ?
|
| 38 |
LIMIT 1
|
| 39 |
"""
|
|
|
|
| 46 |
"message": f"Mobile number {mobile} nahi mila."
|
| 47 |
}
|
| 48 |
|
| 49 |
+
# Data mapping
|
| 50 |
columns = [desc[0] for desc in con.description]
|
| 51 |
row_dict = dict(zip(columns, result[0]))
|
| 52 |
|
|
|
|
| 56 |
}
|
| 57 |
|
| 58 |
except Exception as e:
|
| 59 |
+
# Internal error ko secure rakhne ke liye mask kiya hai
|
| 60 |
+
raise HTTPException(status_code=500, detail="Query timeout ya process nahi ho payi.")
|
| 61 |
|