Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ from fastapi.middleware.cors import CORSMiddleware
|
|
| 3 |
from datasets import load_dataset
|
| 4 |
from huggingface_hub import HfApi, login
|
| 5 |
import os
|
|
|
|
| 6 |
|
| 7 |
os.environ["HF_HOME"] = "/tmp/hf"
|
| 8 |
|
|
@@ -11,7 +12,7 @@ HF_TOKEN = os.environ.get("HF_TOKEN")
|
|
| 11 |
if HF_TOKEN:
|
| 12 |
login(token=HF_TOKEN)
|
| 13 |
|
| 14 |
-
app = FastAPI()
|
| 15 |
|
| 16 |
app.add_middleware(
|
| 17 |
CORSMiddleware,
|
|
@@ -21,62 +22,87 @@ app.add_middleware(
|
|
| 21 |
)
|
| 22 |
|
| 23 |
DATASET = "CodeXDevloper/MergedTgDataset"
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
def
|
| 26 |
api = HfApi()
|
| 27 |
files = api.list_repo_files(DATASET, repo_type="dataset")
|
| 28 |
-
return [f for f in files if f.endswith(".parquet")]
|
| 29 |
|
| 30 |
@app.get("/")
|
| 31 |
def root():
|
| 32 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
@app.get("/search")
|
| 35 |
-
def search_uid(
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
try:
|
| 40 |
-
files =
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
ds = load_dataset(
|
| 43 |
DATASET,
|
| 44 |
-
data_files={"train": files
|
| 45 |
split="train",
|
| 46 |
streaming=True
|
| 47 |
)
|
| 48 |
|
| 49 |
-
|
| 50 |
-
iterator = iter(ds)
|
| 51 |
-
for _ in range(offset):
|
| 52 |
-
try:
|
| 53 |
-
next(iterator)
|
| 54 |
-
except StopIteration:
|
| 55 |
-
return {"found": False, "uid": uid, "message": "End", "next_offset": None}
|
| 56 |
-
|
| 57 |
-
# 2 lakh rows scan
|
| 58 |
scanned = 0
|
| 59 |
-
max_scan = 200000
|
| 60 |
|
| 61 |
-
for row in
|
| 62 |
scanned += 1
|
|
|
|
|
|
|
| 63 |
if uid == str(row.get("user_id", "")).strip():
|
| 64 |
return {
|
| 65 |
"found": True,
|
| 66 |
"uid": uid,
|
| 67 |
-
"offset": offset,
|
| 68 |
"scanned": scanned,
|
| 69 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
}
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
return {
|
| 75 |
"found": False,
|
| 76 |
"uid": uid,
|
| 77 |
-
"offset": offset,
|
| 78 |
"scanned": scanned,
|
| 79 |
-
"
|
|
|
|
| 80 |
}
|
|
|
|
| 81 |
except Exception as e:
|
| 82 |
-
return {"error": str(e)}
|
|
|
|
| 3 |
from datasets import load_dataset
|
| 4 |
from huggingface_hub import HfApi, login
|
| 5 |
import os
|
| 6 |
+
import time
|
| 7 |
|
| 8 |
os.environ["HF_HOME"] = "/tmp/hf"
|
| 9 |
|
|
|
|
| 12 |
if HF_TOKEN:
|
| 13 |
login(token=HF_TOKEN)
|
| 14 |
|
| 15 |
+
app = FastAPI(title="Telegram UID Search API")
|
| 16 |
|
| 17 |
app.add_middleware(
|
| 18 |
CORSMiddleware,
|
|
|
|
| 22 |
)
|
| 23 |
|
| 24 |
DATASET = "CodeXDevloper/MergedTgDataset"
|
| 25 |
+
PART_SIZE = 100000 # har part mein 1 lakh rows
|
| 26 |
+
MAX_PARTS = 50 # ek request mein max 50 parts
|
| 27 |
+
TIME_LIMIT = 25 # 25 second ke andar ruk jao
|
| 28 |
|
| 29 |
+
def get_all_files():
|
| 30 |
api = HfApi()
|
| 31 |
files = api.list_repo_files(DATASET, repo_type="dataset")
|
| 32 |
+
return sorted([f for f in files if f.endswith(".parquet")])
|
| 33 |
|
| 34 |
@app.get("/")
|
| 35 |
def root():
|
| 36 |
+
return {
|
| 37 |
+
"status": "running",
|
| 38 |
+
"usage": "/search?uid=385167100",
|
| 39 |
+
"note": "Poore dataset mein search (parts mein divided)"
|
| 40 |
+
}
|
| 41 |
|
| 42 |
@app.get("/search")
|
| 43 |
+
def search_uid(uid: str = Query(..., description="Telegram UID")):
|
| 44 |
+
"""
|
| 45 |
+
Poore dataset mein UID dhoondho - parts mein divided, fast.
|
| 46 |
+
"""
|
| 47 |
try:
|
| 48 |
+
files = get_all_files()
|
| 49 |
|
| 50 |
+
if not files:
|
| 51 |
+
return {"error": "Koi parquet file nahi mili"}
|
| 52 |
+
|
| 53 |
+
# Saari files ko parts mein load karo
|
| 54 |
ds = load_dataset(
|
| 55 |
DATASET,
|
| 56 |
+
data_files={"train": files},
|
| 57 |
split="train",
|
| 58 |
streaming=True
|
| 59 |
)
|
| 60 |
|
| 61 |
+
start_time = time.time()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
scanned = 0
|
|
|
|
| 63 |
|
| 64 |
+
for row in ds:
|
| 65 |
scanned += 1
|
| 66 |
+
|
| 67 |
+
# UID match karo
|
| 68 |
if uid == str(row.get("user_id", "")).strip():
|
| 69 |
return {
|
| 70 |
"found": True,
|
| 71 |
"uid": uid,
|
|
|
|
| 72 |
"scanned": scanned,
|
| 73 |
+
"time_taken": round(time.time() - start_time, 2),
|
| 74 |
+
"data": {
|
| 75 |
+
"user_id": row.get("user_id"),
|
| 76 |
+
"username": row.get("username"),
|
| 77 |
+
"first_name": row.get("first_name"),
|
| 78 |
+
"last_name": row.get("last_name"),
|
| 79 |
+
"phone": row.get("phone"),
|
| 80 |
+
"email": row.get("email"),
|
| 81 |
+
"status": row.get("status"),
|
| 82 |
+
"linked_id": row.get("linked_id"),
|
| 83 |
+
"linked_name": row.get("linked_name"),
|
| 84 |
+
"linked_handle": row.get("linked_handle"),
|
| 85 |
+
}
|
| 86 |
}
|
| 87 |
+
|
| 88 |
+
# Time limit check (har 5000 rows)
|
| 89 |
+
if scanned % 5000 == 0:
|
| 90 |
+
if time.time() - start_time > TIME_LIMIT:
|
| 91 |
+
return {
|
| 92 |
+
"found": False,
|
| 93 |
+
"uid": uid,
|
| 94 |
+
"scanned": scanned,
|
| 95 |
+
"time_taken": round(time.time() - start_time, 2),
|
| 96 |
+
"message": f"{TIME_LIMIT} second mein nahi mila. Phir try karo."
|
| 97 |
+
}
|
| 98 |
|
| 99 |
return {
|
| 100 |
"found": False,
|
| 101 |
"uid": uid,
|
|
|
|
| 102 |
"scanned": scanned,
|
| 103 |
+
"time_taken": round(time.time() - start_time, 2),
|
| 104 |
+
"message": "Poore dataset mein nahi mila"
|
| 105 |
}
|
| 106 |
+
|
| 107 |
except Exception as e:
|
| 108 |
+
return {"error": str(e), "uid": uid}
|