Spaces:
Paused
Paused
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,60 +1,40 @@
|
|
| 1 |
import os
|
| 2 |
-
import requests
|
| 3 |
import uvicorn
|
| 4 |
import duckdb
|
| 5 |
from fastapi import FastAPI, Query
|
| 6 |
-
from contextlib import asynccontextmanager
|
| 7 |
|
| 8 |
-
|
| 9 |
-
DB_URL = "https://huggingface.co/datasets/Watchhrr/Insta17m/resolve/main/insta_hitech.duckdb"
|
| 10 |
-
TOKEN = os.getenv("MY_HF_TOKEN")
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 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:
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
conn = duckdb.connect(DB_PATH, read_only=True)
|
| 50 |
try:
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
return {"status": "success", "data": data}
|
| 55 |
finally:
|
| 56 |
conn.close()
|
| 57 |
|
| 58 |
if __name__ == "__main__":
|
| 59 |
-
uvicorn.run(
|
| 60 |
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import uvicorn
|
| 3 |
import duckdb
|
| 4 |
from fastapi import FastAPI, Query
|
|
|
|
| 5 |
|
| 6 |
+
app = FastAPI()
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# Mount hone ke baad file is path par milegi
|
| 9 |
+
# /data/ aapka dataset folder ban jayega
|
| 10 |
+
DB_PATH = "/data/insta_hitech.duckdb"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
@app.get("/search")
|
| 13 |
async def search(q: str = Query(None)):
|
| 14 |
+
if not q:
|
| 15 |
+
return {"error": "Query parameter 'q' missing"}
|
| 16 |
+
|
| 17 |
+
if not os.path.exists(DB_PATH):
|
| 18 |
+
return {"error": f"Database not found at {DB_PATH}. Check if Dataset is mounted."}
|
| 19 |
|
| 20 |
conn = duckdb.connect(DB_PATH, read_only=True)
|
| 21 |
try:
|
| 22 |
+
# Smart search across Username (u), ID, and Phone (t)
|
| 23 |
+
query = "SELECT * FROM users WHERE u = ? OR id = ? OR t = ?"
|
| 24 |
+
res = conn.execute(query, [q, q, q]).df()
|
| 25 |
+
|
| 26 |
+
if res.empty:
|
| 27 |
+
return {"status": "error", "message": "No Record Found"}
|
| 28 |
+
|
| 29 |
+
data = res.rename(columns={
|
| 30 |
+
't':'phone', 'u':'username', 'e':'email',
|
| 31 |
+
'n':'name', 'id':'user_id'
|
| 32 |
+
}).to_dict(orient='records')
|
| 33 |
+
|
| 34 |
return {"status": "success", "data": data}
|
| 35 |
finally:
|
| 36 |
conn.close()
|
| 37 |
|
| 38 |
if __name__ == "__main__":
|
| 39 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
| 40 |
|