Watchhrr commited on
Commit
6ff2003
Β·
verified Β·
1 Parent(s): 1899f06

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -44
app.py CHANGED
@@ -1,64 +1,33 @@
1
  import duckdb
2
  from fastapi import FastAPI, HTTPException, Security, Depends
3
  from fastapi.security.api_key import APIKeyHeader
4
- import os
5
 
6
- app = FastAPI(title="Hitech Professional API - Owner Swapnil")
7
 
8
- # --- πŸ” KEY MANAGEMENT ---
9
  API_KEY_NAME = "X-API-KEY"
10
  api_key_header = APIKeyHeader(name=API_KEY_NAME, auto_error=False)
 
11
 
12
- # Yahan Swapnil bhai apni keys manage karenge
13
- VALID_KEYS = {
14
- "Satuu5": "Master_Admin", # Teri Personal Key
15
- "DEMO_USER_123": "Active", # Kisi ko test karwane ke liye
16
- }
17
-
18
- # --- πŸ›οΈ DATABASE CONFIG ---
19
- # Direct streaming from 36-part zip logic
20
  DATA_URL = "https://huggingface.co/datasets/Watchhrr/HITECH_DB/resolve/main/Hi-Tek-DB.zip.001"
21
 
22
  async def get_api_key(api_header: str = Security(api_key_header)):
23
- if api_header in VALID_KEYS:
24
- return api_header
25
- raise HTTPException(
26
- status_code=403,
27
- detail="❌ Access Denied: Invalid Key. Contact Admin for Access."
28
- )
29
-
30
- @app.get("/")
31
- def home():
32
- return {
33
- "status": "Online",
34
- "owner": "Swapnil",
35
- "message": "Hitech Search Engine is Ready for Business"
36
- }
37
 
38
  @app.get("/search")
39
  def search(query: str, type: str = "phone", key: str = Depends(get_api_key)):
40
- """
41
- Search by 'phone', 'aadhar', or 'name'
42
- """
43
  con = duckdb.connect()
44
  try:
45
- # SQL Injection safe query with '?'
46
- # DuckDB handles the 36 parts automatically via the .001 reference
47
  sql = f"SELECT * FROM read_csv_auto('{DATA_URL}') WHERE {type} = ? LIMIT 5"
48
-
49
  result = con.execute(sql, [query]).df().to_dict(orient="records")
50
 
51
- # Log entry (Sirf Swapnil dekh sakta hai Space Logs mein)
52
- print(f"πŸ’° BUSINESS LOG: Key [{key}] fetched data for {type}: {query}")
53
-
54
- return {
55
- "status": "success",
56
- "results": result
57
- }
58
  except Exception as e:
59
- return {"status": "error", "message": "Record not found or Database processing."}
60
-
61
- if __name__ == "__main__":
62
- import uvicorn
63
- uvicorn.run(app, host="0.0.0.0", port=7860)
64
-
 
1
  import duckdb
2
  from fastapi import FastAPI, HTTPException, Security, Depends
3
  from fastapi.security.api_key import APIKeyHeader
 
4
 
5
+ app = FastAPI(title="Hitech DB - Swapnil Edition")
6
 
7
+ # --- πŸ” SECURITY ---
8
  API_KEY_NAME = "X-API-KEY"
9
  api_key_header = APIKeyHeader(name=API_KEY_NAME, auto_error=False)
10
+ VALID_KEYS = {"Satuu5": "Master_Admin"}
11
 
12
+ # --- πŸ›οΈ DATA PATH ---
13
+ # Hum direct main zip ko target kar rahe hain
 
 
 
 
 
 
14
  DATA_URL = "https://huggingface.co/datasets/Watchhrr/HITECH_DB/resolve/main/Hi-Tek-DB.zip.001"
15
 
16
  async def get_api_key(api_header: str = Security(api_key_header)):
17
+ if api_header in VALID_KEYS: return api_header
18
+ raise HTTPException(status_code=403, detail="Invalid Key.")
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  @app.get("/search")
21
  def search(query: str, type: str = "phone", key: str = Depends(get_api_key)):
22
+ # ⚑ Yahan shuru hoti hai indexing aur database conversion
 
 
23
  con = duckdb.connect()
24
  try:
25
+ # Ye command on-the-fly index banayegi
26
+ # SQL Injection safe query
27
  sql = f"SELECT * FROM read_csv_auto('{DATA_URL}') WHERE {type} = ? LIMIT 5"
 
28
  result = con.execute(sql, [query]).df().to_dict(orient="records")
29
 
30
+ return {"status": "success", "results": result}
 
 
 
 
 
 
31
  except Exception as e:
32
+ return {"status": "error", "message": "Indexing in progress or Data not found."}
33
+