Watchhrr commited on
Commit
2644c3c
·
verified ·
1 Parent(s): e0fce5e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -15
app.py CHANGED
@@ -3,35 +3,34 @@ from fastapi import FastAPI, HTTPException, Query
3
  import os
4
 
5
  app = FastAPI()
6
-
7
- # --- 🔐 SECURITY & CONFIG ---
8
  VALID_KEYS = {"Satuu5": "Master_Admin"}
9
  HF_TOKEN = os.getenv("HF_TOKEN")
10
  DATA_URL = "https://huggingface.co/datasets/Watchhrr/HITECH_DB/resolve/main/Hi-Tek-DB.zip.001"
11
 
12
- @app.get("/")
13
- def home():
14
- return {"status": "Online", "Owner": "Swapnil", "DB_Type": "Virtual_DuckDB"}
15
-
16
  @app.get("/search")
17
- def search(query: str, type: str = "phone", key: str = None):
18
  if key not in VALID_KEYS:
19
- raise HTTPException(status_code=403, detail="Invalid API Key!")
20
 
21
  con = duckdb.connect()
22
  try:
23
- # Har version ke liye fix configuration
24
  con.execute("INSTALL httpfs; LOAD httpfs;")
25
  if HF_TOKEN:
26
- # Ye latest DuckDB secret method hai jo kabhi fail nahi hota
27
  con.execute(f"CREATE OR REPLACE SECRET (TYPE HTTP, KEY_ID 'Authorization', SECRET 'Bearer {HF_TOKEN}');")
28
 
29
- # SQL query jo 100GB ke zip parts ko handle karegi
30
- # 'all_varchar=True' taaki phone numbers galat read na hon
31
- sql = f"SELECT * FROM read_csv_auto('{DATA_URL}', all_varchar=True) WHERE {type} LIKE '%{query}%' LIMIT 5"
 
 
 
 
 
 
 
32
  result = con.execute(sql).df().to_dict(orient="records")
33
 
34
- return {"status": "success", "data": result}
35
  except Exception as e:
36
- return {"status": "error", "message": "Database scanning... try again in 30 seconds."}
37
 
 
3
  import os
4
 
5
  app = FastAPI()
 
 
6
  VALID_KEYS = {"Satuu5": "Master_Admin"}
7
  HF_TOKEN = os.getenv("HF_TOKEN")
8
  DATA_URL = "https://huggingface.co/datasets/Watchhrr/HITECH_DB/resolve/main/Hi-Tek-DB.zip.001"
9
 
 
 
 
 
10
  @app.get("/search")
11
+ def search(query: str, key: str = None):
12
  if key not in VALID_KEYS:
13
+ raise HTTPException(status_code=403, detail="Invalid Key!")
14
 
15
  con = duckdb.connect()
16
  try:
 
17
  con.execute("INSTALL httpfs; LOAD httpfs;")
18
  if HF_TOKEN:
 
19
  con.execute(f"CREATE OR REPLACE SECRET (TYPE HTTP, KEY_ID 'Authorization', SECRET 'Bearer {HF_TOKEN}');")
20
 
21
+ # --- SMART SEARCH ---
22
+ # Ye pehle headers check karega automatically
23
+ df_sample = con.execute(f"SELECT * FROM read_csv_auto('{DATA_URL}') LIMIT 1").df()
24
+ columns = df_sample.columns
25
+
26
+ # Phone jaisa dikhne wala column dhoond raha hu
27
+ target_col = [c for c in columns if 'phone' in c.lower() or 'mobile' in c.lower() or 'number' in c.lower()]
28
+ search_col = target_col[0] if target_col else columns[0] # Agar kuch na mile toh pehla column
29
+
30
+ sql = f"SELECT * FROM read_csv_auto('{DATA_URL}', all_varchar=True) WHERE \"{search_col}\" LIKE '%{query}%' LIMIT 5"
31
  result = con.execute(sql).df().to_dict(orient="records")
32
 
33
+ return {"status": "success", "searching_in_column": search_col, "data": result}
34
  except Exception as e:
35
+ return {"status": "error", "message": "Still loading data... wait 1 minute.", "raw_error": str(e)}
36