Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,33 +7,32 @@ app = FastAPI()
|
|
| 7 |
# --- 🔐 SECURITY ---
|
| 8 |
VALID_KEYS = {"Satuu5": "Master_Admin"}
|
| 9 |
|
| 10 |
-
#
|
|
|
|
| 11 |
DATA_URL = "https://huggingface.co/datasets/Watchhrr/HITECH_DB/resolve/main/Hi-Tek-DB.zip.001"
|
| 12 |
|
| 13 |
@app.get("/")
|
| 14 |
def home():
|
| 15 |
-
return {"
|
| 16 |
|
| 17 |
@app.get("/search")
|
| 18 |
-
def search(
|
| 19 |
-
|
| 20 |
-
type: str = Query("phone", description="Type: phone, aadhar, or name"),
|
| 21 |
-
key: str = Query(..., description="Your API Key")
|
| 22 |
-
):
|
| 23 |
-
# 1. Key Verification
|
| 24 |
if key not in VALID_KEYS:
|
| 25 |
raise HTTPException(status_code=403, detail="Invalid API Key!")
|
| 26 |
|
| 27 |
-
# 2. Database Search
|
| 28 |
con = duckdb.connect()
|
| 29 |
try:
|
| 30 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
sql = f"SELECT * FROM read_csv_auto('{DATA_URL}') WHERE {type} = ? LIMIT 5"
|
| 32 |
result = con.execute(sql, [query]).df().to_dict(orient="records")
|
| 33 |
|
| 34 |
-
if not result:
|
| 35 |
-
return {"status": "success", "message": "No record found", "data": []}
|
| 36 |
-
|
| 37 |
return {"status": "success", "data": result}
|
| 38 |
except Exception as e:
|
| 39 |
return {"status": "error", "message": str(e)}
|
|
|
|
| 7 |
# --- 🔐 SECURITY ---
|
| 8 |
VALID_KEYS = {"Satuu5": "Master_Admin"}
|
| 9 |
|
| 10 |
+
# Hugging Face Settings se Secret uthayega
|
| 11 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 12 |
DATA_URL = "https://huggingface.co/datasets/Watchhrr/HITECH_DB/resolve/main/Hi-Tek-DB.zip.001"
|
| 13 |
|
| 14 |
@app.get("/")
|
| 15 |
def home():
|
| 16 |
+
return {"status": "Online", "Owner": "Swapnil"}
|
| 17 |
|
| 18 |
@app.get("/search")
|
| 19 |
+
def search(query: str, type: str = "phone", key: str = None):
|
| 20 |
+
# 1. Key Check
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
if key not in VALID_KEYS:
|
| 22 |
raise HTTPException(status_code=403, detail="Invalid API Key!")
|
| 23 |
|
|
|
|
| 24 |
con = duckdb.connect()
|
| 25 |
try:
|
| 26 |
+
# Authentication using Environment Variable
|
| 27 |
+
con.execute(f"SET http_keep_alive=false;")
|
| 28 |
+
con.execute(f"INSTALL httpfs; LOAD httpfs;")
|
| 29 |
+
if HF_TOKEN:
|
| 30 |
+
con.execute(f"SET http_header = 'Authorization: Bearer {HF_TOKEN}';")
|
| 31 |
+
|
| 32 |
+
# Database Search
|
| 33 |
sql = f"SELECT * FROM read_csv_auto('{DATA_URL}') WHERE {type} = ? LIMIT 5"
|
| 34 |
result = con.execute(sql, [query]).df().to_dict(orient="records")
|
| 35 |
|
|
|
|
|
|
|
|
|
|
| 36 |
return {"status": "success", "data": result}
|
| 37 |
except Exception as e:
|
| 38 |
return {"status": "error", "message": str(e)}
|