Watchhrr commited on
Commit
4512925
Β·
verified Β·
1 Parent(s): 0904ab1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -25
app.py CHANGED
@@ -4,8 +4,11 @@ import pandas as pd
4
  import os
5
  from huggingface_hub import hf_hub_download
6
 
7
- # --- CONFIGURATION (SECURE) ---
8
- # Ab hum secret se token uthayenge
 
 
 
9
  TOKEN = os.getenv("HF_TOKEN")
10
  REPO = "Watchhrr/Telegram"
11
  DB1_NAME = "Telegram_Master_DB.db"
@@ -14,36 +17,38 @@ DB2_NAME = "Telegram_Part2.db"
14
  # --- DOWNLOAD DATABASES ---
15
  def load_data():
16
  if not TOKEN:
17
- raise ValueError("❌ HF_TOKEN secret nahi mila! Settings mein jake add karein.")
18
 
19
- print("⏳ Loading Databases from Hugging Face...")
20
- # Ye files ko cache mein save karega
21
- path1 = hf_hub_download(repo_id=REPO, filename=DB1_NAME, repo_type="dataset", token=TOKEN)
22
- path2 = hf_hub_download(repo_id=REPO, filename=DB2_NAME, repo_type="dataset", token=TOKEN)
23
- return path1, path2
 
 
 
 
24
 
25
- # Initialize paths
26
- try:
27
- db1_path, db2_path = load_data()
28
- except Exception as e:
29
- print(f"Startup Error: {e}")
30
- db1_path, db2_path = None, None
31
 
32
  def hitech_search(search_query):
33
  if not db1_path or not db2_path:
34
- return "❌ Database files load nahi ho payi. Token check karein."
35
 
36
  if not search_query or len(search_query) < 3:
37
  return "⚠️ Please enter at least 3 characters."
38
 
39
  try:
40
- # Connect and Attach
41
  conn = sqlite3.connect(db1_path)
 
42
  conn.execute(f"ATTACH DATABASE '{db2_path}' AS db2")
43
 
 
44
  clean_phone = ''.join(filter(str.isdigit, str(search_query)))
45
 
46
- # Search Query
47
  sql = """
48
  SELECT 'Master' as source, user_id, phone, username, first_name, last_name FROM users
49
  WHERE phone = ? OR user_id = ? OR username = ?
@@ -57,22 +62,31 @@ def hitech_search(search_query):
57
  df = pd.read_sql_query(sql, conn, params=params)
58
  conn.close()
59
 
60
- return df if not df.empty else "❌ No records found."
 
 
 
61
  except Exception as e:
62
  return f"❌ Search Error: {str(e)}"
63
 
64
- # --- GRADIO INTERFACE ---
65
- with gr.Blocks(theme=gr.themes.Default()) as demo:
66
- gr.Markdown("# πŸš€ Hitech Telegram Master Search")
67
- gr.Markdown("Connected to 251 Million Records.")
68
 
69
  with gr.Row():
70
- input_text = gr.Textbox(label="Search (Phone/ID/User)", placeholder="Enter here...")
71
- search_btn = gr.Button("πŸ” Search", variant="primary")
 
 
 
72
 
73
- output_table = gr.DataFrame()
 
74
 
 
75
  search_btn.click(fn=hitech_search, inputs=input_text, outputs=output_table)
 
76
 
77
  if __name__ == "__main__":
78
  demo.launch()
 
4
  import os
5
  from huggingface_hub import hf_hub_download
6
 
7
+ # Fast download enable karne ke liye
8
+ os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
9
+
10
+ # --- CONFIGURATION ---
11
+ # Token ko Space Settings > Secrets mein HF_TOKEN naam se save karna
12
  TOKEN = os.getenv("HF_TOKEN")
13
  REPO = "Watchhrr/Telegram"
14
  DB1_NAME = "Telegram_Master_DB.db"
 
17
  # --- DOWNLOAD DATABASES ---
18
  def load_data():
19
  if not TOKEN:
20
+ return None, None
21
 
22
+ print("⏳ Downloading Databases (Large Files)...")
23
+ try:
24
+ # Dono DB files ko download karke local path lena
25
+ path1 = hf_hub_download(repo_id=REPO, filename=DB1_NAME, repo_type="dataset", token=TOKEN)
26
+ path2 = hf_hub_download(repo_id=REPO, filename=DB2_NAME, repo_type="dataset", token=TOKEN)
27
+ return path1, path2
28
+ except Exception as e:
29
+ print(f"Download Error: {e}")
30
+ return None, None
31
 
32
+ # Initialize paths at startup
33
+ db1_path, db2_path = load_data()
 
 
 
 
34
 
35
  def hitech_search(search_query):
36
  if not db1_path or not db2_path:
37
+ return "❌ Error: Databases not loaded. Check HF_TOKEN in Secrets."
38
 
39
  if not search_query or len(search_query) < 3:
40
  return "⚠️ Please enter at least 3 characters."
41
 
42
  try:
43
+ # SQLite Connection
44
  conn = sqlite3.connect(db1_path)
45
+ # Dusre DB ko attach karna taaki ek sath search ho sake
46
  conn.execute(f"ATTACH DATABASE '{db2_path}' AS db2")
47
 
48
+ # Phone search ke liye sirf numbers extract karna
49
  clean_phone = ''.join(filter(str.isdigit, str(search_query)))
50
 
51
+ # Optimized SQL Query with UNION
52
  sql = """
53
  SELECT 'Master' as source, user_id, phone, username, first_name, last_name FROM users
54
  WHERE phone = ? OR user_id = ? OR username = ?
 
62
  df = pd.read_sql_query(sql, conn, params=params)
63
  conn.close()
64
 
65
+ if df.empty:
66
+ return "❌ No records found."
67
+ return df
68
+
69
  except Exception as e:
70
  return f"❌ Search Error: {str(e)}"
71
 
72
+ # --- GRADIO UI ---
73
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
74
+ gr.Markdown("# πŸš€ Hitech Telegram Search Engine")
75
+ gr.Markdown("Searching across **251 Million+** records in real-time.")
76
 
77
  with gr.Row():
78
+ input_text = gr.Textbox(
79
+ label="Phone / User ID / Username",
80
+ placeholder="Type here and press Enter...",
81
+ lines=1
82
+ )
83
 
84
+ search_btn = gr.Button("πŸ” Deep Search", variant="primary")
85
+ output_table = gr.DataFrame(label="Results")
86
 
87
+ # Click aur Enter dono pe kaam karega
88
  search_btn.click(fn=hitech_search, inputs=input_text, outputs=output_table)
89
+ input_text.submit(fn=hitech_search, inputs=input_text, outputs=output_table)
90
 
91
  if __name__ == "__main__":
92
  demo.launch()