Spaces:
Paused
Paused
Update main.py
Browse files
main.py
CHANGED
|
@@ -7,65 +7,51 @@ from contextlib import asynccontextmanager
|
|
| 7 |
|
| 8 |
DB_PATH = "insta_hitech.duckdb"
|
| 9 |
DB_URL = "https://huggingface.co/datasets/Watchhrr/Insta17m/resolve/main/insta_hitech.duckdb"
|
|
|
|
| 10 |
|
| 11 |
-
# Yahan humne secret variable ka naam use kiya hai jo aapne Settings mein dala tha
|
| 12 |
-
TOKEN = os.getenv("MY_HF_TOKEN")
|
| 13 |
-
|
| 14 |
-
# 1. Download Function
|
| 15 |
def download_db():
|
| 16 |
if not os.path.exists(DB_PATH):
|
| 17 |
-
print("📥 Database missing.
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
with
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
f.write(chunk)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
print("✅ Download Complete!")
|
| 30 |
-
|
| 31 |
-
print(f"❌ Download Failed: {
|
| 32 |
else:
|
| 33 |
-
print(f"✅
|
| 34 |
|
| 35 |
-
# 2. Lifespan Handler
|
| 36 |
@asynccontextmanager
|
| 37 |
async def lifespan(app: FastAPI):
|
| 38 |
download_db()
|
| 39 |
yield
|
| 40 |
-
print("Shutting down...")
|
| 41 |
|
| 42 |
app = FastAPI(lifespan=lifespan)
|
| 43 |
|
| 44 |
@app.get("/search")
|
| 45 |
async def search(q: str = Query(None)):
|
| 46 |
-
if not q:
|
| 47 |
-
|
| 48 |
|
| 49 |
-
if not os.path.exists(DB_PATH):
|
| 50 |
-
return {"error": "Database file not ready yet. Please wait a few minutes."}
|
| 51 |
-
|
| 52 |
conn = duckdb.connect(DB_PATH, read_only=True)
|
| 53 |
try:
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
if res.empty:
|
| 59 |
-
return {"status": "error", "message": "No Record Found"}
|
| 60 |
-
|
| 61 |
-
data = res.rename(columns={
|
| 62 |
-
't':'phone', 'u':'username', 'e':'email',
|
| 63 |
-
'n':'name', 'id':'user_id', 'a':'extra'
|
| 64 |
-
}).to_dict(orient='records')
|
| 65 |
-
|
| 66 |
return {"status": "success", "data": data}
|
| 67 |
-
except Exception as e:
|
| 68 |
-
return {"status": "error", "message": str(e)}
|
| 69 |
finally:
|
| 70 |
conn.close()
|
| 71 |
|
|
|
|
| 7 |
|
| 8 |
DB_PATH = "insta_hitech.duckdb"
|
| 9 |
DB_URL = "https://huggingface.co/datasets/Watchhrr/Insta17m/resolve/main/insta_hitech.duckdb"
|
| 10 |
+
TOKEN = os.getenv("MY_HF_TOKEN")
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
def download_db():
|
| 13 |
if not os.path.exists(DB_PATH):
|
| 14 |
+
print("📥 Database missing. Starting download...")
|
| 15 |
+
headers = {"Authorization": f"Bearer {TOKEN}"} if TOKEN else {}
|
| 16 |
+
|
| 17 |
+
response = requests.get(DB_URL, headers=headers, stream=True)
|
| 18 |
+
total_size = int(response.headers.get('content-length', 0))
|
| 19 |
+
|
| 20 |
+
if response.status_code == 200:
|
| 21 |
+
with open(DB_PATH, 'wb') as f:
|
| 22 |
+
downloaded = 0
|
| 23 |
+
for chunk in response.iter_content(chunk_size=1024*1024): # 1MB chunks
|
| 24 |
+
if chunk:
|
| 25 |
f.write(chunk)
|
| 26 |
+
downloaded += len(chunk)
|
| 27 |
+
# Har 50MB par log dikhayega
|
| 28 |
+
if (downloaded // (50*1024*1024)) > ((downloaded - len(chunk)) // (50*1024*1024)):
|
| 29 |
+
done = int(50 * downloaded / total_size)
|
| 30 |
+
print(f"⏳ Progress: [{'=' * done}{' ' * (50-done)}] {downloaded/(1024*1024):.1f}MB / {total_size/(1024*1024):.1f}MB")
|
| 31 |
print("✅ Download Complete!")
|
| 32 |
+
else:
|
| 33 |
+
print(f"❌ Download Failed! Status: {response.status_code}. Check your Token/Secret.")
|
| 34 |
else:
|
| 35 |
+
print(f"✅ DB found. Size: {os.path.getsize(DB_PATH)/(1024*1024):.1f} MB")
|
| 36 |
|
|
|
|
| 37 |
@asynccontextmanager
|
| 38 |
async def lifespan(app: FastAPI):
|
| 39 |
download_db()
|
| 40 |
yield
|
|
|
|
| 41 |
|
| 42 |
app = FastAPI(lifespan=lifespan)
|
| 43 |
|
| 44 |
@app.get("/search")
|
| 45 |
async def search(q: str = Query(None)):
|
| 46 |
+
if not q: return {"error": "Query 'q' missing"}
|
| 47 |
+
if not os.path.exists(DB_PATH): return {"error": "DB still downloading..."}
|
| 48 |
|
|
|
|
|
|
|
|
|
|
| 49 |
conn = duckdb.connect(DB_PATH, read_only=True)
|
| 50 |
try:
|
| 51 |
+
res = conn.execute("SELECT * FROM users WHERE u = ? OR id = ? OR t = ?", [q, q, q]).df()
|
| 52 |
+
if res.empty: return {"status": "error", "message": "No Record Found"}
|
| 53 |
+
data = res.rename(columns={'t':'phone', 'u':'username', 'e':'email', 'n':'name', 'id':'user_id'}).to_dict(orient='records')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
return {"status": "success", "data": data}
|
|
|
|
|
|
|
| 55 |
finally:
|
| 56 |
conn.close()
|
| 57 |
|