Download app.py from ZephrexXx/hitech: direct link, hf CLI and curl.
- Browser
- Download file 3.15 kB
-
https://huggingface.co/spaces/ZephrexXx/hitech/resolve/main/app.py
- Command line
-
hf download hf://spaces/ZephrexXx/hitech/app.py
-
curl -L -o app.py https://huggingface.co/spaces/ZephrexXx/hitech/resolve/main/app.py
3.15 kB
| from fastapi import FastAPI, Query, Request | |
| from fastapi.responses import JSONResponse | |
| from starlette.exceptions import HTTPException as StarletteHTTPException | |
| import duckdb | |
| import math | |
| import asyncio | |
| import threading | |
| import requests as http | |
| from concurrent.futures import ThreadPoolExecutor | |
| from pathlib import Path | |
| import gradio as gr | |
| app = FastAPI(docs_url=None, redoc_url=None) | |
| BASE = "https://huggingface.co/datasets/MRSHREY197/Hitekdatabase/resolve/main" | |
| CACHE_DIR = Path("/tmp/parquet_cache") | |
| CACHE_DIR.mkdir(exist_ok=True) | |
| _executor = ThreadPoolExecutor(max_workers=8) | |
| _thread_local = threading.local() | |
| _dl_lock = threading.Lock() | |
| _dl_done = set() | |
| def get_con(): | |
| if not hasattr(_thread_local, "con"): | |
| _thread_local.con = duckdb.connect() | |
| return _thread_local.con | |
| def ensure_cached(filename: str) -> str: | |
| local = CACHE_DIR / filename | |
| if str(local) in _dl_done: | |
| return str(local) | |
| with _dl_lock: | |
| if local.exists(): | |
| _dl_done.add(str(local)) | |
| return str(local) | |
| r = http.get(f"{BASE}/{filename}", stream=True, timeout=60) | |
| r.raise_for_status() | |
| with open(local, "wb") as f: | |
| for chunk in r.iter_content(chunk_size=1024 * 256): | |
| f.write(chunk) | |
| _dl_done.add(str(local)) | |
| return str(local) | |
| def clean_nan(obj): | |
| if isinstance(obj, float) and (math.isnan(obj) or math.isinf(obj)): | |
| return None | |
| if isinstance(obj, dict): | |
| return {k: clean_nan(v) for k, v in obj.items()} | |
| if isinstance(obj, list): | |
| return [clean_nan(v) for v in obj] | |
| return obj | |
| def run_query(number: str): | |
| d = number[-1] | |
| primary = ensure_cached(f"final_master_shard_{d}.parquet") | |
| alt = ensure_cached(f"alt_master_shard_{d}.parquet") | |
| query = f""" | |
| SELECT * FROM read_parquet('{primary}') WHERE mobile = '{number}' | |
| UNION ALL | |
| SELECT * FROM read_parquet('{alt}') WHERE alt = '{number}' | |
| """ | |
| return get_con().execute(query).df() | |
| def root(): | |
| return {"status": "API is running"} | |
| async def fetch_data(Number: str = Query(None)): | |
| if not Number or not Number.isdigit() or len(Number) < 10 or len(Number) > 15: | |
| return JSONResponse(status_code=400, content=[{"status": "invalid number"}]) | |
| try: | |
| loop = asyncio.get_running_loop() | |
| df = await loop.run_in_executor(_executor, run_query, Number) | |
| if df.empty: | |
| return JSONResponse(status_code=404, content=[{"status": "no data found"}]) | |
| results = clean_nan(df.to_dict(orient="records")) | |
| return JSONResponse(content=results) | |
| except Exception as e: | |
| return JSONResponse(status_code=500, content=[{"status": "error", "message": str(e)}]) | |
| async def custom_404(request: Request, exc: StarletteHTTPException): | |
| return JSONResponse(status_code=exc.status_code, content=[{"status": "invalid endpoint"}]) | |
| demo = gr.Interface(fn=lambda x: x, inputs="text", outputs="text") | |
| app = gr.mount_gradio_app(app, demo, path="/ui") |