coderpetro commited on
Commit
6f5a5d1
·
verified ·
1 Parent(s): f41da16

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +9 -9
main.py CHANGED
@@ -4,7 +4,7 @@ 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"
@@ -22,21 +22,19 @@ def load_data():
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
 
@@ -54,6 +52,8 @@ def search_data(
54
  raise HTTPException(status_code=500, detail="Database is not loaded yet. Please try again in a few moments.")
55
 
56
  search_lower = query.lower()
 
 
57
  mask = df.apply(lambda row: row.str.lower().str.contains(search_lower)).any(axis=1)
58
  results = df[mask].to_dict(orient="records")
59
 
 
4
  import pandas as pd
5
  import os
6
 
7
+ app = FastAPI(title="Telegram Database API", description="Optimized Delimiter by coder petro")
8
 
9
  API_KEY = "demo"
10
  API_KEY_NAME = "x-api-key"
 
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 database with Pipe (|) separator...")
26
+ # Since your file uses '|' as separator, we specify sep='|'
27
+ # engine='c' provides the maximum loading speed for large files
28
+ df = pd.read_csv(csv_path, sep='|', low_memory=False, engine='c')
 
 
29
  df = df.astype(str).fillna("")
30
+ print("Database loaded successfully with optimizations!")
31
  except Exception as e:
32
  print(f"Error loading database: {str(e)}")
33
 
34
  @app.get("/")
35
  def read_root():
36
  return {
37
+ "message": "Super Fast Optimized API is running 24/7!",
38
  "developer": "coder petro"
39
  }
40
 
 
52
  raise HTTPException(status_code=500, detail="Database is not loaded yet. Please try again in a few moments.")
53
 
54
  search_lower = query.lower()
55
+
56
+ # Fast row-filtering using modern pandas optimization
57
  mask = df.apply(lambda row: row.str.lower().str.contains(search_lower)).any(axis=1)
58
  results = df[mask].to_dict(orient="records")
59