Flix010 commited on
Commit
2c859f7
·
verified ·
1 Parent(s): 888314a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -0
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import duckdb
3
+ import uvicorn
4
+ from fastapi import FastAPI
5
+ from huggingface_hub import hf_hub_download
6
+
7
+ app = FastAPI()
8
+
9
+ # 🚀 Global Path taaki baar-baar download na ho
10
+ DB_PATH = None
11
+
12
+ def get_db():
13
+ global DB_PATH
14
+ if DB_PATH is None or not os.path.exists(DB_PATH):
15
+ token = os.getenv("HF_TOKEN")
16
+ if token is None:
17
+ raise ValueError("HF_TOKEN environment variable not set")
18
+ DB_PATH = hf_hub_download(
19
+ repo_id="sheeturt/Telegram",
20
+ filename="Midnight_Master_v2.db",
21
+ repo_type="dataset",
22
+ token=token
23
+ )
24
+ return DB_PATH
25
+
26
+ @app.get("/search")
27
+ @app.post("/search")
28
+ async def search(q: str = None):
29
+ if not q:
30
+ return {"status": "error", "message": "Query 'q' missing"}
31
+
32
+ try:
33
+ path = get_db()
34
+ con = duckdb.connect(database=':memory:')
35
+ con.execute("INSTALL sqlite;")
36
+ con.execute("LOAD sqlite;")
37
+
38
+ query = f"""
39
+ SELECT user_id
40
+ FROM sqlite_scan('{path}', 'telegram_users')
41
+ WHERE user_id LIKE '{q};%'
42
+ LIMIT 1
43
+ """
44
+ res = con.execute(query).fetchone()
45
+
46
+ results = []
47
+ if res:
48
+ p = res[0].split(';')
49
+ results.append({
50
+ "id": p[0],
51
+ "t": p[1] if len(p) > 1 else "N/A",
52
+ "u": p[2] if len(p) > 2 else "N/A",
53
+ "n": p[3] if len(p) > 3 else "N/A"
54
+ })
55
+
56
+ return {"status": "success", "results": results}
57
+
58
+ except Exception as e:
59
+ return {"status": "error", "details": str(e), "type": type(e).__name__}
60
+
61
+ if __name__ == "__main__":
62
+ uvicorn.run(app, host="0.0.0.0", port=7860)