Cxops commited on
Commit
8c0b5d9
Β·
verified Β·
1 Parent(s): 8a77e1d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -57
app.py CHANGED
@@ -1,8 +1,8 @@
1
  import os
 
2
  import tempfile
3
  import shutil
4
  import threading
5
- import queue
6
 
7
  from flask import Flask
8
 
@@ -23,14 +23,14 @@ HF_TOKEN = os.environ["HF_TOKEN"]
23
  DATASET_REPO = "Cxops/Bzxv"
24
 
25
  # =========================
26
- # FLASK (HEALTH CHECK)
27
  # =========================
28
 
29
  web = Flask(__name__)
30
 
31
  @web.route("/")
32
  def home():
33
- return "OK - Ultra Fast Bot Running"
34
 
35
  def run_web():
36
  web.run(host="0.0.0.0", port=7860)
@@ -42,47 +42,11 @@ def run_web():
42
  hf = HfApi(token=HF_TOKEN)
43
 
44
  # =========================
45
- # UPLOAD QUEUE SYSTEM (FAST)
46
- # =========================
47
-
48
- upload_queue = queue.Queue()
49
-
50
- def worker():
51
- while True:
52
- try:
53
- local_path, repo_path, status = upload_queue.get()
54
-
55
- hf.upload_file(
56
- path_or_fileobj=local_path,
57
- path_in_repo=repo_path,
58
- repo_id=DATASET_REPO,
59
- repo_type="dataset",
60
- )
61
-
62
- try:
63
- status.edit_text("⚑ Upload Complete (Ultra Fast)")
64
- except:
65
- pass
66
-
67
- except Exception as e:
68
- try:
69
- status.edit_text(f"Upload Failed:\n{e}")
70
- except:
71
- pass
72
-
73
- finally:
74
- upload_queue.task_done()
75
-
76
- # start multiple workers (PARALLEL SPEED)
77
- for _ in range(3):
78
- threading.Thread(target=worker, daemon=True).start()
79
-
80
- # =========================
81
- # PYROGRAM
82
  # =========================
83
 
84
  app = Client(
85
- "ultra_fast_bot",
86
  api_id=API_ID,
87
  api_hash=API_HASH,
88
  bot_token=BOT_TOKEN
@@ -98,6 +62,31 @@ def cleanup(path):
98
  except:
99
  pass
100
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  # =========================
102
  # START
103
  # =========================
@@ -105,8 +94,8 @@ def cleanup(path):
105
  @app.on_message(filters.command("start"))
106
  async def start(_, m):
107
  await m.reply_text(
108
- "⚑ ULTRA FAST STORAGE BOT\n\n"
109
- "Send file β†’ stored in Hugging Face\n"
110
  "/files β†’ list files"
111
  )
112
 
@@ -135,7 +124,7 @@ async def files(_, m):
135
  ]
136
 
137
  await m.reply_text(
138
- "Your files:",
139
  reply_markup=InlineKeyboardMarkup(buttons)
140
  )
141
 
@@ -149,11 +138,10 @@ async def files(_, m):
149
  @app.on_callback_query(filters.regex("^get:"))
150
  async def get_file(_, q):
151
  path = q.data.split(":", 1)[1]
152
-
153
  tmp = tempfile.mkdtemp()
154
 
155
  try:
156
- await q.message.edit_text("⚑ Downloading...")
157
 
158
  file_path = hf_hub_download(
159
  repo_id=DATASET_REPO,
@@ -164,7 +152,6 @@ async def get_file(_, q):
164
  )
165
 
166
  await q.message.reply_document(file_path)
167
-
168
  await q.message.delete()
169
 
170
  except Exception as e:
@@ -174,7 +161,7 @@ async def get_file(_, q):
174
  cleanup(tmp)
175
 
176
  # =========================
177
- # UPLOAD FROM TELEGRAM (FAST)
178
  # =========================
179
 
180
  @app.on_message(
@@ -188,8 +175,9 @@ async def upload(_, m):
188
  tmp = tempfile.mkdtemp()
189
 
190
  try:
191
- status = await m.reply_text("⚑ Downloading...")
192
 
 
193
  if m.document:
194
  name = m.document.file_name
195
 
@@ -202,32 +190,74 @@ async def upload(_, m):
202
  else:
203
  name = f"{m.photo[-1].file_unique_id}.jpg"
204
 
205
- local_path = await m.download(file_name=f"{tmp}/{name}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
206
 
207
  repo_path = f"{m.from_user.id}/{name}"
208
 
209
- # πŸ”₯ push into queue (INSTANT RETURN)
210
- upload_queue.put((local_path, repo_path, status))
 
211
 
212
- await status.edit_text("⚑ Queued for ultra-fast upload...")
 
 
 
 
 
 
 
 
 
 
 
 
213
 
214
  except Exception as e:
215
  await m.reply_text(f"Error:\n{e}")
216
 
217
  finally:
218
  def delayed_cleanup():
219
- import time
220
- time.sleep(15)
221
  cleanup(tmp)
222
 
223
  threading.Thread(target=delayed_cleanup, daemon=True).start()
224
 
225
  # =========================
226
- # RUN
227
  # =========================
228
 
229
  if __name__ == "__main__":
230
- print("πŸš€ Ultra Fast Bot Starting...")
231
 
232
  threading.Thread(target=run_web, daemon=True).start()
233
 
 
1
  import os
2
+ import time
3
  import tempfile
4
  import shutil
5
  import threading
 
6
 
7
  from flask import Flask
8
 
 
23
  DATASET_REPO = "Cxops/Bzxv"
24
 
25
  # =========================
26
+ # FLASK SERVER (HF SPACE FIX)
27
  # =========================
28
 
29
  web = Flask(__name__)
30
 
31
  @web.route("/")
32
  def home():
33
+ return "OK - Bot Running"
34
 
35
  def run_web():
36
  web.run(host="0.0.0.0", port=7860)
 
42
  hf = HfApi(token=HF_TOKEN)
43
 
44
  # =========================
45
+ # PYROGRAM CLIENT
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  # =========================
47
 
48
  app = Client(
49
+ "progress_bot",
50
  api_id=API_ID,
51
  api_hash=API_HASH,
52
  bot_token=BOT_TOKEN
 
62
  except:
63
  pass
64
 
65
+ # =========================
66
+ # PROGRESS BAR
67
+ # =========================
68
+
69
+ def human_size(size):
70
+ for unit in ["B", "KB", "MB", "GB"]:
71
+ if size < 1024:
72
+ return f"{size:.2f}{unit}"
73
+ size /= 1024
74
+ return f"{size:.2f}TB"
75
+
76
+
77
+ def progress_bar(current, total, start_time):
78
+ elapsed = time.time() - start_time
79
+ speed = current / elapsed if elapsed > 0 else 0
80
+ percent = (current * 100 / total) if total else 0
81
+
82
+ bar = "β–ˆ" * int(percent // 10) + "β–‘" * (10 - int(percent // 10))
83
+
84
+ return (
85
+ f"{bar}\n"
86
+ f"{percent:.2f}%\n"
87
+ f"{human_size(speed)}/s"
88
+ )
89
+
90
  # =========================
91
  # START
92
  # =========================
 
94
  @app.on_message(filters.command("start"))
95
  async def start(_, m):
96
  await m.reply_text(
97
+ "⚑ FAST HF STORAGE BOT\n\n"
98
+ "Send file β†’ upload to Hugging Face\n"
99
  "/files β†’ list files"
100
  )
101
 
 
124
  ]
125
 
126
  await m.reply_text(
127
+ "Your Files:",
128
  reply_markup=InlineKeyboardMarkup(buttons)
129
  )
130
 
 
138
  @app.on_callback_query(filters.regex("^get:"))
139
  async def get_file(_, q):
140
  path = q.data.split(":", 1)[1]
 
141
  tmp = tempfile.mkdtemp()
142
 
143
  try:
144
+ await q.message.edit_text("πŸ“₯ Downloading...")
145
 
146
  file_path = hf_hub_download(
147
  repo_id=DATASET_REPO,
 
152
  )
153
 
154
  await q.message.reply_document(file_path)
 
155
  await q.message.delete()
156
 
157
  except Exception as e:
 
161
  cleanup(tmp)
162
 
163
  # =========================
164
+ # UPLOAD (FAST + 3 SEC PROGRESS UPDATE)
165
  # =========================
166
 
167
  @app.on_message(
 
175
  tmp = tempfile.mkdtemp()
176
 
177
  try:
178
+ status = await m.reply_text("Starting download...")
179
 
180
+ # detect file
181
  if m.document:
182
  name = m.document.file_name
183
 
 
190
  else:
191
  name = f"{m.photo[-1].file_unique_id}.jpg"
192
 
193
+ start_time = time.time()
194
+
195
+ # =========================
196
+ # PROGRESS (FIXED 3 SEC LIMIT)
197
+ # =========================
198
+
199
+ last_update = [0]
200
+
201
+ def dl_progress(current, total):
202
+ now = time.time()
203
+
204
+ # UPDATE EVERY 3 SECONDS ONLY
205
+ if now - last_update[0] < 3:
206
+ return
207
+
208
+ last_update[0] = now
209
+
210
+ try:
211
+ status.edit_text(
212
+ "πŸ“₯ Downloading...\n\n" +
213
+ progress_bar(current, total, start_time)
214
+ )
215
+ except:
216
+ pass
217
+
218
+ local_path = await m.download(
219
+ file_name=f"{tmp}/{name}",
220
+ progress=dl_progress
221
+ )
222
+
223
+ await status.edit_text("⚑ Uploading to Hugging Face...")
224
 
225
  repo_path = f"{m.from_user.id}/{name}"
226
 
227
+ # =========================
228
+ # BACKGROUND HF UPLOAD
229
+ # =========================
230
 
231
+ def hf_upload():
232
+ try:
233
+ hf.upload_file(
234
+ path_or_fileobj=local_path,
235
+ path_in_repo=repo_path,
236
+ repo_id=DATASET_REPO,
237
+ repo_type="dataset",
238
+ )
239
+ status.edit_text("⚑ Upload Complete!")
240
+ except Exception as e:
241
+ status.edit_text(f"HF Error:\n{e}")
242
+
243
+ threading.Thread(target=hf_upload, daemon=True).start()
244
 
245
  except Exception as e:
246
  await m.reply_text(f"Error:\n{e}")
247
 
248
  finally:
249
  def delayed_cleanup():
250
+ time.sleep(20)
 
251
  cleanup(tmp)
252
 
253
  threading.Thread(target=delayed_cleanup, daemon=True).start()
254
 
255
  # =========================
256
+ # RUN BOT
257
  # =========================
258
 
259
  if __name__ == "__main__":
260
+ print("πŸš€ Bot Running")
261
 
262
  threading.Thread(target=run_web, daemon=True).start()
263