Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Query
|
| 2 |
+
import duckdb
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import numpy as np
|
| 5 |
+
import traceback
|
| 6 |
+
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
|
| 9 |
+
HF_FILE = "hf://datasets/warmifans/HiTeckGroupp@main/users_data.parquet"
|
| 10 |
+
|
| 11 |
+
con = duckdb.connect()
|
| 12 |
+
|
| 13 |
+
@app.on_event("startup")
|
| 14 |
+
def startup():
|
| 15 |
+
con.execute("INSTALL httpfs;")
|
| 16 |
+
con.execute("LOAD httpfs;")
|
| 17 |
+
|
| 18 |
+
@app.get("/")
|
| 19 |
+
def home():
|
| 20 |
+
return {
|
| 21 |
+
"status": "API Running"
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
@app.get("/id")
|
| 25 |
+
@app.get("/id/")
|
| 26 |
+
def search_id(id: str = Query(..., min_length=1)):
|
| 27 |
+
try:
|
| 28 |
+
query = """
|
| 29 |
+
SELECT *
|
| 30 |
+
FROM read_parquet(?)
|
| 31 |
+
WHERE id = ?
|
| 32 |
+
LIMIT 10
|
| 33 |
+
"""
|
| 34 |
+
|
| 35 |
+
df = con.execute(
|
| 36 |
+
query,
|
| 37 |
+
[HF_FILE, id.strip()]
|
| 38 |
+
).fetchdf()
|
| 39 |
+
|
| 40 |
+
if df is None or df.empty:
|
| 41 |
+
return {
|
| 42 |
+
"found": 0,
|
| 43 |
+
"data": []
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
df = df.drop_duplicates()
|
| 47 |
+
|
| 48 |
+
df = df.replace([np.inf, -np.inf], np.nan)
|
| 49 |
+
df = df.where(pd.notnull(df), None)
|
| 50 |
+
|
| 51 |
+
return {
|
| 52 |
+
"found": len(df),
|
| 53 |
+
"data": df.to_dict(orient="records")
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
except Exception:
|
| 57 |
+
traceback.print_exc()
|
| 58 |
+
return {
|
| 59 |
+
"error": traceback.format_exc()
|
| 60 |
+
}
|