CodeXDevloper commited on
Commit
eabe32b
·
verified ·
1 Parent(s): e1b2ba3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -71
app.py CHANGED
@@ -1,76 +1,36 @@
1
- from flask import Flask, request
2
- import pyarrow.parquet as pq
 
 
3
 
4
- app = Flask(__name__)
5
- PARQUET_FILE = "mera_data.parquet"
6
 
7
- def search_data(query):
8
- query = str(query).strip().lower()
9
- pf = pq.ParquetFile(PARQUET_FILE)
10
- for batch in pf.iter_batches(batch_size=10000):
11
- for i in range(batch.num_rows):
12
- tg_id = str(batch.column("TG_ID")[i].as_py() or "").lower()
13
- tg_username = str(batch.column("TG_USERNAME")[i].as_py() or "").lower()
14
- if query == tg_id or query == tg_username:
15
- return {
16
- "TG_ID": batch.column("TG_ID")[i].as_py(),
17
- "TG_USERNAME": batch.column("TG_USERNAME")[i].as_py(),
18
- "FIRST_NAME": batch.column("FIRST_NAME")[i].as_py(),
19
- "PHONE": batch.column("PHONE")[i].as_py(),
20
- }
21
- return None
22
 
23
- @app.route("/search", methods=["GET"])
24
- def search():
25
- query = request.args.get("search", "").strip()
26
- if not query:
27
- r = '{"status":"error","message":"Search value required!","example":"/search?search=Kudesner77"}'
28
- return app.response_class(response=r, status=400, mimetype="application/json")
29
- result = search_data(query)
30
- if result:
31
- r = (
32
- "{\n"
33
- ' "status": "found",\n'
34
- f' "query": "{query}",\n'
35
- ' "data": {\n'
36
- f' "TG_ID": "{result["TG_ID"]}",\n'
37
- f' "TG_USERNAME": "{result["TG_USERNAME"]}",\n'
38
- f' "FIRST_NAME": "{result["FIRST_NAME"]}",\n'
39
- f' "PHONE": "{result["PHONE"]}"\n'
40
- " },\n"
41
- "\n\n\n\n"
42
- ' "owner": "Code X",\n'
43
- ' "username": "@C4DX1"\n'
44
- "}"
45
- )
46
- return app.response_class(response=r, status=200, mimetype="application/json")
47
- else:
48
- r = (
49
- "{\n"
50
- ' "status": "not found",\n'
51
- f' "query": "{query}",\n'
52
- ' "message": "No data found!",\n'
53
- "\n\n\n\n"
54
- ' "owner": "Code X",\n'
55
- ' "username": "@C4DX1"\n'
56
- "}"
57
- )
58
- return app.response_class(response=r, status=404, mimetype="application/json")
59
 
60
- @app.route("/", methods=["GET"])
61
- def home():
62
- r = (
63
- "{\n"
64
- ' "status": "online",\n'
65
- ' "name": "Code X API",\n'
66
- ' "usage": "/search?search=TG_USERNAME or TG_ID",\n'
67
- ' "example": "/search?search=Kudesner77",\n'
68
- "\n\n\n\n"
69
- ' "owner": "Code X",\n'
70
- ' "username": "@C4DX1"\n'
71
- "}"
72
- )
73
- return app.response_class(response=r, status=200, mimetype="application/json")
74
 
75
- if __name__ == "__main__":
76
- app.run(host="0.0.0.0", port=7860)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Query
2
+ from fastapi.middleware.cors import CORSMiddleware
3
+ from datasets import load_dataset
4
+ import os
5
 
6
+ os.environ["HF_HOME"] = "/tmp/hf"
 
7
 
8
+ app = FastAPI()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
+ app.add_middleware(
11
+ CORSMiddleware,
12
+ allow_origins=["*"],
13
+ allow_methods=["*"],
14
+ allow_headers=["*"],
15
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
+ DATASET = "CodeXDevloper/MergedTgDataset"
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
+ @app.get("/")
20
+ def root():
21
+ return {"status": "running", "usage": "/search?uid=385167100"}
22
+
23
+ @app.get("/search")
24
+ def search_uid(uid: str = Query(...)):
25
+ try:
26
+ ds = load_dataset(DATASET, split="train", streaming=True)
27
+ scanned = 0
28
+ for row in ds:
29
+ scanned += 1
30
+ if uid == str(row.get("user_id", "")).strip():
31
+ return {"found": True, "uid": uid, "data": row}
32
+ if scanned >= 5000000:
33
+ break
34
+ return {"found": False, "uid": uid, "scanned": scanned}
35
+ except Exception as e:
36
+ return {"error": str(e)}