Spaces:
Paused
Paused
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,90 +0,0 @@
|
|
| 1 |
-
import json
|
| 2 |
-
import os
|
| 3 |
-
import threading
|
| 4 |
-
import requests
|
| 5 |
-
from fastapi import FastAPI, Response
|
| 6 |
-
import duckdb
|
| 7 |
-
|
| 8 |
-
app = FastAPI()
|
| 9 |
-
|
| 10 |
-
DATASET_URL = "https://huggingface.co/datasets/warmifans/Sushant/resolve/main/data.parquet?download=true"
|
| 11 |
-
LOCAL_PATH = "data.parquet"
|
| 12 |
-
|
| 13 |
-
# Background mein file download karne ka function
|
| 14 |
-
def download_dataset():
|
| 15 |
-
if not os.path.exists(LOCAL_PATH):
|
| 16 |
-
print("Background download start ho gaya hai...")
|
| 17 |
-
try:
|
| 18 |
-
with requests.get(DATASET_URL, stream=True) as r:
|
| 19 |
-
r.raise_for_status()
|
| 20 |
-
with open(LOCAL_PATH, 'wb') as f:
|
| 21 |
-
for chunk in r.iter_content(chunk_size=8192):
|
| 22 |
-
f.write(chunk)
|
| 23 |
-
print("Download Complete! Data ready hai.")
|
| 24 |
-
except Exception as e:
|
| 25 |
-
print("Download failed:", e)
|
| 26 |
-
|
| 27 |
-
# App start hote hi background thread chalu ho jayega (API block nahi hogi)
|
| 28 |
-
threading.Thread(target=download_dataset, daemon=True).start()
|
| 29 |
-
|
| 30 |
-
con = duckdb.connect()
|
| 31 |
-
|
| 32 |
-
def pretty_json(data):
|
| 33 |
-
formatted_data = json.dumps(data, indent=4)
|
| 34 |
-
return Response(content=formatted_data, media_type="application/json")
|
| 35 |
-
|
| 36 |
-
@app.get("/")
|
| 37 |
-
def read_root():
|
| 38 |
-
return pretty_json({
|
| 39 |
-
"status": "API is running!",
|
| 40 |
-
"message": "Agar data nahi mil raha, toh background me download chal raha hoga."
|
| 41 |
-
})
|
| 42 |
-
|
| 43 |
-
@app.get("/search/")
|
| 44 |
-
def search_number(number: str = None):
|
| 45 |
-
if not number or not number.strip():
|
| 46 |
-
return pretty_json({
|
| 47 |
-
"error": "Number parameter is required.",
|
| 48 |
-
"Chnl": "@HiTeckGroup",
|
| 49 |
-
"Dev": "dmforbans"
|
| 50 |
-
})
|
| 51 |
-
|
| 52 |
-
# Agar abhi background me download chal raha hai
|
| 53 |
-
if not os.path.exists(LOCAL_PATH):
|
| 54 |
-
return pretty_json({
|
| 55 |
-
"error": "Dataset abhi background me download ho raha hai. Kripya 3 se 4 minute baad dubara try karein.",
|
| 56 |
-
"Chnl": "@HiTeckGroup",
|
| 57 |
-
"Dev": "dmforbans"
|
| 58 |
-
})
|
| 59 |
-
|
| 60 |
-
try:
|
| 61 |
-
query = f"""
|
| 62 |
-
SELECT * FROM '{LOCAL_PATH}'
|
| 63 |
-
WHERE Number = '{number.strip()}'
|
| 64 |
-
LIMIT 10
|
| 65 |
-
"""
|
| 66 |
-
|
| 67 |
-
result_df = con.execute(query).df()
|
| 68 |
-
|
| 69 |
-
if result_df.empty:
|
| 70 |
-
return pretty_json({
|
| 71 |
-
"message": "Data not found for this number.",
|
| 72 |
-
"Chnl": "@HiTeckGroup",
|
| 73 |
-
"Dev": "dmforbans"
|
| 74 |
-
})
|
| 75 |
-
|
| 76 |
-
records = result_df.to_dict(orient="records")
|
| 77 |
-
|
| 78 |
-
return pretty_json({
|
| 79 |
-
"result": records,
|
| 80 |
-
"Chnl": "@HiTeckGroup",
|
| 81 |
-
"Dev": "dmforbans"
|
| 82 |
-
})
|
| 83 |
-
|
| 84 |
-
except Exception as e:
|
| 85 |
-
return pretty_json({
|
| 86 |
-
"error": f"Internal server error: {str(e)}",
|
| 87 |
-
"Chnl": "@HiTeckGroup",
|
| 88 |
-
"Dev": "dmforbans"
|
| 89 |
-
})
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|