Spaces:
Runtime error
Runtime error
File size: 1,975 Bytes
1899f06 1e54171 1899f06 1e54171 fb79491 6ff2003 3536edd 1899f06 fb79491 1899f06 2644c3c 1e54171 2644c3c fb79491 1e54171 1899f06 ccbf139 3536edd fb79491 2644c3c fb79491 2644c3c fb79491 1899f06 fb79491 1899f06 fb79491 e0fce5e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | import duckdb
from fastapi import FastAPI, HTTPException, Query
import os
app = FastAPI()
# --- 🔐 CONFIG ---
VALID_KEYS = {"Satuu5": "Master_Admin"}
HF_TOKEN = os.getenv("HF_TOKEN")
DATA_URL = "https://huggingface.co/datasets/Watchhrr/HITECH_DB/resolve/main/Hi-Tek-DB.zip.001"
@app.get("/")
def home():
status = "READY" if HF_TOKEN else "TOKEN_MISSING_IN_SETTINGS"
return {"status": "Online", "auth": status, "Owner": "Swapnil"}
@app.get("/search")
def search(query: str, key: str = None):
if key not in VALID_KEYS:
raise HTTPException(status_code=403, detail="Invalid Key!")
if not HF_TOKEN:
return {"error": "Bhai, Settings mein HF_TOKEN secret dalo pehle!"}
con = duckdb.connect()
try:
con.execute("INSTALL httpfs; LOAD httpfs;")
# --- FIX: Sabse stable method for HTTP Authentication ---
con.execute(f"SET http_headers = 'Authorization: Bearer {HF_TOKEN}';")
# Data scanning (Automatic Indexing)
# Pehli bar scan karne mein 30-40 sec lag sakte hain
sql_check = f"SELECT * FROM read_csv_auto('{DATA_URL}', all_varchar=True) LIMIT 1"
df_sample = con.execute(sql_check).df()
cols = df_sample.columns
# Phone/Mobile column detect karna
search_col = next((c for c in cols if any(x in c.lower() for x in ['phone', 'mobile', 'number', 'contact'])), cols[0])
# Final Search Query
final_sql = f"SELECT * FROM read_csv_auto('{DATA_URL}', all_varchar=True) WHERE \"{search_col}\" LIKE '%{query}%' LIMIT 5"
result = con.execute(final_sql).df().to_dict(orient="records")
return {
"status": "success",
"column_searched": search_col,
"results": result
}
except Exception as e:
# Agar connection timeout ho jaye
return {"status": "error", "message": "Database is heavy, retrying...", "details": str(e)}
|