Deploy Free Gradio-FastAPI app (app.py)
Browse files
app.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import duckdb
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from fastapi import FastAPI, Query, HTTPException
|
| 5 |
+
from huggingface_hub import HfFileSystem
|
| 6 |
+
import uvicorn
|
| 7 |
+
|
| 8 |
+
app = FastAPI(title="ICMR Dataset API")
|
| 9 |
+
|
| 10 |
+
BUCKET_PATH = "hf://buckets/Watchhrr/num/**/*.parquet"
|
| 11 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 12 |
+
|
| 13 |
+
def get_db_connection():
|
| 14 |
+
conn = duckdb.connect(database=":memory:")
|
| 15 |
+
conn.execute("PRAGMA threads=2;")
|
| 16 |
+
conn.execute("PRAGMA memory_limit='10GB';")
|
| 17 |
+
|
| 18 |
+
fs = HfFileSystem(token=HF_TOKEN)
|
| 19 |
+
conn.register_filesystem(fs)
|
| 20 |
+
return conn
|
| 21 |
+
|
| 22 |
+
conn = get_db_connection()
|
| 23 |
+
|
| 24 |
+
@app.get("/api_status")
|
| 25 |
+
def root():
|
| 26 |
+
return {
|
| 27 |
+
"status": "online",
|
| 28 |
+
"service": "ICMR REST API",
|
| 29 |
+
"bucket": BUCKET_PATH,
|
| 30 |
+
"token_configured": bool(HF_TOKEN),
|
| 31 |
+
"endpoints": {
|
| 32 |
+
"demo": "/demo",
|
| 33 |
+
"search": "/api?query=9999999891"
|
| 34 |
+
}
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
@app.get("/demo")
|
| 38 |
+
def demo():
|
| 39 |
+
try:
|
| 40 |
+
sql = f"SELECT * FROM read_parquet('{BUCKET_PATH}') LIMIT 10"
|
| 41 |
+
res = conn.execute(sql).fetchdf()
|
| 42 |
+
return {
|
| 43 |
+
"status": "success",
|
| 44 |
+
"count": len(res),
|
| 45 |
+
"data": res.fillna("").to_dict(orient="records")
|
| 46 |
+
}
|
| 47 |
+
except Exception as e:
|
| 48 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 49 |
+
|
| 50 |
+
@app.get("/api")
|
| 51 |
+
def search_db(query: str = Query(..., min_length=3)):
|
| 52 |
+
clean_q = query.strip()
|
| 53 |
+
try:
|
| 54 |
+
sql = f"""
|
| 55 |
+
SELECT * FROM read_parquet('{BUCKET_PATH}')
|
| 56 |
+
WHERE TRIM(CAST(mobile AS VARCHAR)) = ?
|
| 57 |
+
OR TRIM(CAST(alt AS VARCHAR)) = ?
|
| 58 |
+
OR TRIM(CAST(id AS VARCHAR)) = ?
|
| 59 |
+
LIMIT 50
|
| 60 |
+
"""
|
| 61 |
+
res = conn.execute(sql, [clean_q, clean_q, clean_q]).fetchdf()
|
| 62 |
+
records = res.fillna("").to_dict(orient="records")
|
| 63 |
+
return {
|
| 64 |
+
"status": "success",
|
| 65 |
+
"query": clean_q,
|
| 66 |
+
"count": len(records),
|
| 67 |
+
"data": records
|
| 68 |
+
}
|
| 69 |
+
except Exception as e:
|
| 70 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 71 |
+
|
| 72 |
+
# Gradio Dashboard UI for the web interface
|
| 73 |
+
with gr.Blocks(title="ICMR Search API") as demo_ui:
|
| 74 |
+
gr.Markdown("# 🚀 ICMR Dataset REST API (Free Tier)")
|
| 75 |
+
gr.Markdown("Server status: **RUNNING**")
|
| 76 |
+
gr.Markdown("### Available REST Endpoints:")
|
| 77 |
+
gr.Markdown("- Search API: `/api?query=NUMBER`")
|
| 78 |
+
gr.Markdown("- Demo Data: `/demo`")
|
| 79 |
+
gr.Markdown("- System Status: `/api_status`")
|
| 80 |
+
|
| 81 |
+
# Mount Gradio safely into FastAPI app
|
| 82 |
+
app = gr.mount_gradio_app(app, demo_ui, path="/")
|
| 83 |
+
|
| 84 |
+
if __name__ == "__main__":
|
| 85 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|