| from fastapi import FastAPI, HTTPException, Query |
| import duckdb |
| from contextlib import asynccontextmanager |
|
|
| |
| con = duckdb.connect() |
|
|
| |
| @asynccontextmanager |
| async def lifespan(app: FastAPI): |
| print("DuckDB extensions load ho rahi hain aur View ban raha hai...") |
| con.execute("INSTALL httpfs; LOAD httpfs;") |
| |
| |
| PARQUET_URL = "https://huggingface.co/datasets/tfqdeadlo/Inddatainonefile/resolve/main/users_data.parquet" |
| |
| |
| |
| con.execute(f"CREATE OR REPLACE VIEW users_view AS SELECT * FROM read_parquet('{PARQUET_URL}')") |
| print("View taiyar hai! API requests ke liye taiyar hai.") |
| yield |
| con.close() |
|
|
| app = FastAPI(title="Super Fast Search API", lifespan=lifespan) |
|
|
| @app.get("/") |
| def home(): |
| return {"message": "Fast API online hai! Use /search?mobile=YOUR_NUMBER"} |
|
|
| @app.get("/search") |
| async def search_mobile(mobile: str = Query(..., description="Enter mobile number to search")): |
| try: |
| |
| query = """ |
| SELECT mobile, name, fname, address, alt, circle, id, email |
| FROM users_view |
| WHERE mobile = ? |
| LIMIT 1 |
| """ |
| |
| result = con.execute(query, [str(mobile)]).fetchall() |
| |
| if not result: |
| return { |
| "status": "error", |
| "message": f"Mobile number {mobile} nahi mila." |
| } |
| |
| |
| columns = [desc[0] for desc in con.description] |
| row_dict = dict(zip(columns, result[0])) |
| |
| return { |
| "status": "success", |
| "data": row_dict |
| } |
| |
| except Exception as e: |
| |
| raise HTTPException(status_code=500, detail="Query timeout ya process nahi ho payi.") |