Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,33 +1,42 @@
|
|
| 1 |
from fastapi import FastAPI, HTTPException, Security, Query
|
| 2 |
from fastapi.security.api_key import APIKeyHeader
|
|
|
|
| 3 |
import pandas as pd
|
| 4 |
import os
|
| 5 |
|
| 6 |
-
app = FastAPI(title="
|
| 7 |
|
| 8 |
-
# Updated API Key to demo as requested
|
| 9 |
API_KEY = "demo"
|
| 10 |
API_KEY_NAME = "x-api-key"
|
| 11 |
api_key_header = APIKeyHeader(name=API_KEY_NAME, auto_error=False)
|
| 12 |
|
| 13 |
df = None
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
@app.on_event("startup")
|
| 17 |
def load_data():
|
| 18 |
global df
|
| 19 |
-
|
| 20 |
-
print("
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
df = df.astype(str).fillna("")
|
| 23 |
-
print("Database loaded successfully!")
|
| 24 |
-
|
| 25 |
-
print("Error
|
| 26 |
|
| 27 |
@app.get("/")
|
| 28 |
def read_root():
|
| 29 |
return {
|
| 30 |
-
"message": "API is running 24/7
|
| 31 |
"developer": "coder petro"
|
| 32 |
}
|
| 33 |
|
|
|
|
| 1 |
from fastapi import FastAPI, HTTPException, Security, Query
|
| 2 |
from fastapi.security.api_key import APIKeyHeader
|
| 3 |
+
from huggingface_hub import hf_hub_download
|
| 4 |
import pandas as pd
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
app = FastAPI(title="Telegram Database API", description="Optimized by coder petro")
|
| 8 |
|
|
|
|
| 9 |
API_KEY = "demo"
|
| 10 |
API_KEY_NAME = "x-api-key"
|
| 11 |
api_key_header = APIKeyHeader(name=API_KEY_NAME, auto_error=False)
|
| 12 |
|
| 13 |
df = None
|
| 14 |
+
REPO_ID = "coderpetro/telegram-data"
|
| 15 |
+
FILENAME = "Telegram_27.csv"
|
| 16 |
|
| 17 |
@app.on_event("startup")
|
| 18 |
def load_data():
|
| 19 |
global df
|
| 20 |
+
try:
|
| 21 |
+
print("Downloading database from private dataset...")
|
| 22 |
+
token = os.environ.get("HF_TOKEN")
|
| 23 |
+
csv_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME, repo_type="dataset", token=token)
|
| 24 |
+
|
| 25 |
+
print("Loading only required columns into memory...")
|
| 26 |
+
# WARNING: Make sure 'name' and 'phone' match the exact column names in your CSV file (case-sensitive)
|
| 27 |
+
# If your CSV has 'Name' or 'Phone', change them accordingly below.
|
| 28 |
+
required_cols = ['name', 'phone']
|
| 29 |
+
|
| 30 |
+
df = pd.read_csv(csv_path, usecols=required_cols, low_memory=False)
|
| 31 |
df = df.astype(str).fillna("")
|
| 32 |
+
print("Database optimized and loaded successfully!")
|
| 33 |
+
except Exception as e:
|
| 34 |
+
print(f"Error loading database: {str(e)}")
|
| 35 |
|
| 36 |
@app.get("/")
|
| 37 |
def read_root():
|
| 38 |
return {
|
| 39 |
+
"message": "Optimized API is running 24/7!",
|
| 40 |
"developer": "coder petro"
|
| 41 |
}
|
| 42 |
|