Update app.py
Browse files
app.py
CHANGED
|
@@ -23,14 +23,21 @@ HF_TOKEN = os.environ["HF_TOKEN"]
|
|
| 23 |
DATASET_REPO = "Cxops/Bzxv"
|
| 24 |
|
| 25 |
# =========================
|
| 26 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
# =========================
|
| 28 |
|
| 29 |
web = Flask(__name__)
|
| 30 |
|
| 31 |
@web.route("/")
|
| 32 |
def home():
|
| 33 |
-
return "OK
|
| 34 |
|
| 35 |
def run_web():
|
| 36 |
web.run(host="0.0.0.0", port=7860)
|
|
@@ -42,7 +49,7 @@ def run_web():
|
|
| 42 |
hf = HfApi(token=HF_TOKEN)
|
| 43 |
|
| 44 |
# =========================
|
| 45 |
-
# PYROGRAM
|
| 46 |
# =========================
|
| 47 |
|
| 48 |
app = Client(
|
|
@@ -66,25 +73,24 @@ def cleanup(path):
|
|
| 66 |
# PROGRESS BAR
|
| 67 |
# =========================
|
| 68 |
|
| 69 |
-
def
|
| 70 |
-
for
|
| 71 |
if size < 1024:
|
| 72 |
-
return f"{size:.2f}{
|
| 73 |
size /= 1024
|
| 74 |
return f"{size:.2f}TB"
|
| 75 |
|
| 76 |
|
| 77 |
-
def
|
| 78 |
-
elapsed = time.time() -
|
| 79 |
-
speed =
|
| 80 |
-
percent = (
|
| 81 |
-
|
| 82 |
-
bar = "β" * int(percent // 10) + "β" * (10 - int(percent // 10))
|
| 83 |
|
|
|
|
| 84 |
return (
|
| 85 |
-
|
| 86 |
f"{percent:.2f}%\n"
|
| 87 |
-
f"{
|
| 88 |
)
|
| 89 |
|
| 90 |
# =========================
|
|
@@ -94,8 +100,8 @@ def progress_bar(current, total, start_time):
|
|
| 94 |
@app.on_message(filters.command("start"))
|
| 95 |
async def start(_, m):
|
| 96 |
await m.reply_text(
|
| 97 |
-
"β‘
|
| 98 |
-
"Send file β upload
|
| 99 |
"/files β list files"
|
| 100 |
)
|
| 101 |
|
|
@@ -124,7 +130,7 @@ async def files(_, m):
|
|
| 124 |
]
|
| 125 |
|
| 126 |
await m.reply_text(
|
| 127 |
-
"
|
| 128 |
reply_markup=InlineKeyboardMarkup(buttons)
|
| 129 |
)
|
| 130 |
|
|
@@ -132,7 +138,7 @@ async def files(_, m):
|
|
| 132 |
await m.reply_text(str(e))
|
| 133 |
|
| 134 |
# =========================
|
| 135 |
-
#
|
| 136 |
# =========================
|
| 137 |
|
| 138 |
@app.on_callback_query(filters.regex("^get:"))
|
|
@@ -141,7 +147,26 @@ async def get_file(_, q):
|
|
| 141 |
tmp = tempfile.mkdtemp()
|
| 142 |
|
| 143 |
try:
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
file_path = hf_hub_download(
|
| 147 |
repo_id=DATASET_REPO,
|
|
@@ -151,17 +176,35 @@ async def get_file(_, q):
|
|
| 151 |
local_dir=tmp
|
| 152 |
)
|
| 153 |
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
await q.message.delete()
|
| 156 |
|
|
|
|
|
|
|
| 157 |
except Exception as e:
|
|
|
|
| 158 |
await q.message.reply_text(str(e))
|
| 159 |
|
| 160 |
finally:
|
| 161 |
cleanup(tmp)
|
| 162 |
|
| 163 |
# =========================
|
| 164 |
-
#
|
| 165 |
# =========================
|
| 166 |
|
| 167 |
@app.on_message(
|
|
@@ -175,89 +218,79 @@ async def upload(_, m):
|
|
| 175 |
tmp = tempfile.mkdtemp()
|
| 176 |
|
| 177 |
try:
|
| 178 |
-
status = await m.reply_text("Starting
|
| 179 |
|
| 180 |
-
# detect file
|
| 181 |
if m.document:
|
| 182 |
name = m.document.file_name
|
| 183 |
-
|
| 184 |
elif m.video:
|
| 185 |
name = m.video.file_name or f"{m.video.file_unique_id}.mp4"
|
| 186 |
-
|
| 187 |
elif m.audio:
|
| 188 |
name = m.audio.file_name or f"{m.audio.file_unique_id}.mp3"
|
| 189 |
-
|
| 190 |
else:
|
| 191 |
name = f"{m.photo[-1].file_unique_id}.jpg"
|
| 192 |
|
| 193 |
-
|
| 194 |
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
# =========================
|
| 198 |
|
| 199 |
-
|
| 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 |
-
|
| 214 |
)
|
| 215 |
except:
|
| 216 |
pass
|
| 217 |
|
| 218 |
-
|
| 219 |
file_name=f"{tmp}/{name}",
|
| 220 |
progress=dl_progress
|
| 221 |
)
|
| 222 |
|
| 223 |
-
|
| 224 |
|
| 225 |
-
|
| 226 |
|
| 227 |
-
|
| 228 |
-
# BACKGROUND HF UPLOAD
|
| 229 |
-
# =========================
|
| 230 |
|
|
|
|
| 231 |
def hf_upload():
|
| 232 |
try:
|
|
|
|
| 233 |
hf.upload_file(
|
| 234 |
-
path_or_fileobj=
|
| 235 |
path_in_repo=repo_path,
|
| 236 |
repo_id=DATASET_REPO,
|
| 237 |
repo_type="dataset",
|
| 238 |
)
|
| 239 |
-
|
|
|
|
| 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 |
-
|
|
|
|
| 247 |
|
| 248 |
finally:
|
| 249 |
-
|
| 250 |
-
time.sleep(20)
|
| 251 |
-
cleanup(tmp)
|
| 252 |
-
|
| 253 |
-
threading.Thread(target=delayed_cleanup, daemon=True).start()
|
| 254 |
|
| 255 |
# =========================
|
| 256 |
-
# RUN
|
| 257 |
# =========================
|
| 258 |
|
| 259 |
if __name__ == "__main__":
|
| 260 |
-
|
| 261 |
|
| 262 |
threading.Thread(target=run_web, daemon=True).start()
|
| 263 |
|
|
|
|
| 23 |
DATASET_REPO = "Cxops/Bzxv"
|
| 24 |
|
| 25 |
# =========================
|
| 26 |
+
# LOG HELPER
|
| 27 |
+
# =========================
|
| 28 |
+
|
| 29 |
+
def log(msg):
|
| 30 |
+
print(f"[BOT LOG] {msg}", flush=True)
|
| 31 |
+
|
| 32 |
+
# =========================
|
| 33 |
+
# FLASK (HF SPACE KEEP ALIVE)
|
| 34 |
# =========================
|
| 35 |
|
| 36 |
web = Flask(__name__)
|
| 37 |
|
| 38 |
@web.route("/")
|
| 39 |
def home():
|
| 40 |
+
return "OK"
|
| 41 |
|
| 42 |
def run_web():
|
| 43 |
web.run(host="0.0.0.0", port=7860)
|
|
|
|
| 49 |
hf = HfApi(token=HF_TOKEN)
|
| 50 |
|
| 51 |
# =========================
|
| 52 |
+
# PYROGRAM
|
| 53 |
# =========================
|
| 54 |
|
| 55 |
app = Client(
|
|
|
|
| 73 |
# PROGRESS BAR
|
| 74 |
# =========================
|
| 75 |
|
| 76 |
+
def human(size):
|
| 77 |
+
for u in ["B", "KB", "MB", "GB"]:
|
| 78 |
if size < 1024:
|
| 79 |
+
return f"{size:.2f}{u}"
|
| 80 |
size /= 1024
|
| 81 |
return f"{size:.2f}TB"
|
| 82 |
|
| 83 |
|
| 84 |
+
def bar(cur, total, start):
|
| 85 |
+
elapsed = time.time() - start
|
| 86 |
+
speed = cur / elapsed if elapsed else 0
|
| 87 |
+
percent = (cur * 100 / total) if total else 0
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
fill = int(percent // 10)
|
| 90 |
return (
|
| 91 |
+
"β" * fill + "β" * (10 - fill) + "\n"
|
| 92 |
f"{percent:.2f}%\n"
|
| 93 |
+
f"{human(speed)}/s"
|
| 94 |
)
|
| 95 |
|
| 96 |
# =========================
|
|
|
|
| 100 |
@app.on_message(filters.command("start"))
|
| 101 |
async def start(_, m):
|
| 102 |
await m.reply_text(
|
| 103 |
+
"β‘ HF Storage Bot\n\n"
|
| 104 |
+
"Send file β upload\n"
|
| 105 |
"/files β list files"
|
| 106 |
)
|
| 107 |
|
|
|
|
| 130 |
]
|
| 131 |
|
| 132 |
await m.reply_text(
|
| 133 |
+
"Select file:",
|
| 134 |
reply_markup=InlineKeyboardMarkup(buttons)
|
| 135 |
)
|
| 136 |
|
|
|
|
| 138 |
await m.reply_text(str(e))
|
| 139 |
|
| 140 |
# =========================
|
| 141 |
+
# HF β TELEGRAM (WITH FULL PROGRESS)
|
| 142 |
# =========================
|
| 143 |
|
| 144 |
@app.on_callback_query(filters.regex("^get:"))
|
|
|
|
| 147 |
tmp = tempfile.mkdtemp()
|
| 148 |
|
| 149 |
try:
|
| 150 |
+
log(f"Downloading from HF: {path}")
|
| 151 |
+
|
| 152 |
+
await q.message.edit_text("π₯ Downloading from Hugging Face...")
|
| 153 |
+
|
| 154 |
+
start = time.time()
|
| 155 |
+
last = [0]
|
| 156 |
+
|
| 157 |
+
def progress(cur, total):
|
| 158 |
+
now = time.time()
|
| 159 |
+
if now - last[0] < 3:
|
| 160 |
+
return
|
| 161 |
+
last[0] = now
|
| 162 |
+
|
| 163 |
+
try:
|
| 164 |
+
q.message.edit_text(
|
| 165 |
+
"π₯ Downloading from HF...\n\n" +
|
| 166 |
+
bar(cur, total, start)
|
| 167 |
+
)
|
| 168 |
+
except:
|
| 169 |
+
pass
|
| 170 |
|
| 171 |
file_path = hf_hub_download(
|
| 172 |
repo_id=DATASET_REPO,
|
|
|
|
| 176 |
local_dir=tmp
|
| 177 |
)
|
| 178 |
|
| 179 |
+
log(f"HF download complete: {file_path}")
|
| 180 |
+
|
| 181 |
+
await q.message.edit_text("π€ Uploading to Telegram...")
|
| 182 |
+
|
| 183 |
+
tg_start = time.time()
|
| 184 |
+
tg_last = [0]
|
| 185 |
+
|
| 186 |
+
# Telegram upload with progress
|
| 187 |
+
await q.message.reply_document(
|
| 188 |
+
file_path,
|
| 189 |
+
progress=lambda cur, tot: (
|
| 190 |
+
setattr(tg_last, 0, time.time())
|
| 191 |
+
if time.time() - tg_last[0] < 3 else None
|
| 192 |
+
)
|
| 193 |
+
)
|
| 194 |
+
|
| 195 |
await q.message.delete()
|
| 196 |
|
| 197 |
+
log("Sent to Telegram user successfully")
|
| 198 |
+
|
| 199 |
except Exception as e:
|
| 200 |
+
log(f"ERROR HF->TG: {e}")
|
| 201 |
await q.message.reply_text(str(e))
|
| 202 |
|
| 203 |
finally:
|
| 204 |
cleanup(tmp)
|
| 205 |
|
| 206 |
# =========================
|
| 207 |
+
# TELEGRAM β HF (FULL PROGRESS + LOGS)
|
| 208 |
# =========================
|
| 209 |
|
| 210 |
@app.on_message(
|
|
|
|
| 218 |
tmp = tempfile.mkdtemp()
|
| 219 |
|
| 220 |
try:
|
| 221 |
+
status = await m.reply_text("Starting...")
|
| 222 |
|
|
|
|
| 223 |
if m.document:
|
| 224 |
name = m.document.file_name
|
|
|
|
| 225 |
elif m.video:
|
| 226 |
name = m.video.file_name or f"{m.video.file_unique_id}.mp4"
|
|
|
|
| 227 |
elif m.audio:
|
| 228 |
name = m.audio.file_name or f"{m.audio.file_unique_id}.mp3"
|
|
|
|
| 229 |
else:
|
| 230 |
name = f"{m.photo[-1].file_unique_id}.jpg"
|
| 231 |
|
| 232 |
+
log(f"Incoming file: {name}")
|
| 233 |
|
| 234 |
+
start = time.time()
|
| 235 |
+
last = [0]
|
|
|
|
| 236 |
|
| 237 |
+
# DOWNLOAD PROGRESS
|
| 238 |
+
def dl_progress(cur, total):
|
|
|
|
| 239 |
now = time.time()
|
| 240 |
+
if now - last[0] < 3:
|
|
|
|
|
|
|
| 241 |
return
|
| 242 |
+
last[0] = now
|
|
|
|
| 243 |
|
| 244 |
try:
|
| 245 |
status.edit_text(
|
| 246 |
+
"π₯ Downloading from Telegram...\n\n" +
|
| 247 |
+
bar(cur, total, start)
|
| 248 |
)
|
| 249 |
except:
|
| 250 |
pass
|
| 251 |
|
| 252 |
+
local = await m.download(
|
| 253 |
file_name=f"{tmp}/{name}",
|
| 254 |
progress=dl_progress
|
| 255 |
)
|
| 256 |
|
| 257 |
+
log("Telegram download done")
|
| 258 |
|
| 259 |
+
await status.edit_text("β Uploading to Hugging Face...")
|
| 260 |
|
| 261 |
+
repo_path = f"{m.from_user.id}/{name}"
|
|
|
|
|
|
|
| 262 |
|
| 263 |
+
# HF upload thread (with logs)
|
| 264 |
def hf_upload():
|
| 265 |
try:
|
| 266 |
+
log("Uploading to HF...")
|
| 267 |
hf.upload_file(
|
| 268 |
+
path_or_fileobj=local,
|
| 269 |
path_in_repo=repo_path,
|
| 270 |
repo_id=DATASET_REPO,
|
| 271 |
repo_type="dataset",
|
| 272 |
)
|
| 273 |
+
log("HF upload done")
|
| 274 |
+
status.edit_text("β‘ Upload Complete to HF")
|
| 275 |
except Exception as e:
|
| 276 |
+
log(f"HF ERROR: {e}")
|
| 277 |
status.edit_text(f"HF Error:\n{e}")
|
| 278 |
|
| 279 |
threading.Thread(target=hf_upload, daemon=True).start()
|
| 280 |
|
| 281 |
except Exception as e:
|
| 282 |
+
log(f"UPLOAD ERROR: {e}")
|
| 283 |
+
await m.reply_text(str(e))
|
| 284 |
|
| 285 |
finally:
|
| 286 |
+
threading.Thread(target=lambda: (time.sleep(20), cleanup(tmp)), daemon=True).start()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 287 |
|
| 288 |
# =========================
|
| 289 |
+
# RUN
|
| 290 |
# =========================
|
| 291 |
|
| 292 |
if __name__ == "__main__":
|
| 293 |
+
log("Bot starting...")
|
| 294 |
|
| 295 |
threading.Thread(target=run_web, daemon=True).start()
|
| 296 |
|