CodeXDevloper commited on
Commit
0d7facc
·
verified ·
1 Parent(s): 24809cc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -20
app.py CHANGED
@@ -2,7 +2,6 @@ from fastapi import FastAPI, Query
2
  from fastapi.middleware.cors import CORSMiddleware
3
  from datasets import load_dataset
4
  import os
5
- import time
6
 
7
  os.environ["HF_HOME"] = "/tmp/hf"
8
 
@@ -26,24 +25,50 @@ def root():
26
  }
27
 
28
  @app.get("/search")
29
- def search_uid(uid: str = Query(..., description="Telegram UID")):
30
- """Poore dataset mein UID dhoondho (25 second timeout ke saath)"""
 
 
 
 
 
 
 
 
 
31
  try:
32
  ds = load_dataset(DATASET, split="train", streaming=True)
33
 
34
- start_time = time.time()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  scanned = 0
36
- max_seconds = 25 # HF Space timeout se pehle ruk jao
37
 
38
- for row in ds:
39
  scanned += 1
40
-
41
- # UID match karo
42
  if uid == str(row.get("user_id", "")).strip():
43
  return {
44
  "found": True,
45
  "uid": uid,
 
46
  "scanned": scanned,
 
47
  "data": {
48
  "user_id": row.get("user_id"),
49
  "username": row.get("username"),
@@ -58,23 +83,16 @@ def search_uid(uid: str = Query(..., description="Telegram UID")):
58
  }
59
  }
60
 
61
- # Har 5000 rows ke baad time check
62
- if scanned % 5000 == 0:
63
- if time.time() - start_time > max_seconds:
64
- return {
65
- "found": False,
66
- "uid": uid,
67
- "scanned": scanned,
68
- "time_taken": round(time.time() - start_time, 1),
69
- "message": f"{scanned} rows scan kiye, UID nahi mila. Phir try karo."
70
- }
71
 
72
  return {
73
  "found": False,
74
  "uid": uid,
 
75
  "scanned": scanned,
76
- "time_taken": round(time.time() - start_time, 1),
77
- "message": "Poore dataset mein nahi mila"
78
  }
79
 
80
  except Exception as e:
 
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
 
 
25
  }
26
 
27
  @app.get("/search")
28
+ def search_uid(
29
+ uid: str = Query(..., description="Telegram UID"),
30
+ offset: int = Query(0, ge=0, description="Kahan se start karna hai (default 0)")
31
+ ):
32
+ """
33
+ Poore dataset mein UID dhoondho.
34
+ - offset=0 se start karo
35
+ - Agar nahi mila toh next_offset milega
36
+ - Woh next_offset daal ke dobara call karo
37
+ - Aise karte karte poora dataset cover hoga
38
+ """
39
  try:
40
  ds = load_dataset(DATASET, split="train", streaming=True)
41
 
42
+ # Offset tak skip karo
43
+ iterator = iter(ds)
44
+ skipped = 0
45
+ for _ in range(offset):
46
+ try:
47
+ next(iterator)
48
+ skipped += 1
49
+ except StopIteration:
50
+ return {
51
+ "found": False,
52
+ "uid": uid,
53
+ "offset": offset,
54
+ "scanned": 0,
55
+ "message": "End of dataset",
56
+ "next_offset": None
57
+ }
58
+
59
+ # Ab 5 lakh rows scan karo (HF Space 30 second timeout ke liye safe)
60
  scanned = 0
61
+ max_scan = 500000
62
 
63
+ for row in iterator:
64
  scanned += 1
 
 
65
  if uid == str(row.get("user_id", "")).strip():
66
  return {
67
  "found": True,
68
  "uid": uid,
69
+ "offset": offset,
70
  "scanned": scanned,
71
+ "total_scanned": offset + scanned,
72
  "data": {
73
  "user_id": row.get("user_id"),
74
  "username": row.get("username"),
 
83
  }
84
  }
85
 
86
+ if scanned >= max_scan:
87
+ break
 
 
 
 
 
 
 
 
88
 
89
  return {
90
  "found": False,
91
  "uid": uid,
92
+ "offset": offset,
93
  "scanned": scanned,
94
+ "next_offset": offset + scanned,
95
+ "message": f"Is chunk mein nahi mila. Ab offset={offset + scanned} try karo."
96
  }
97
 
98
  except Exception as e: