Update app.py
Browse files1 unmodified line23456713 unmodified lines2122232425267 unmodified lines34353637383988 unmodified lines12812913013113213313413513614 unmodified lines15115215315415515615715815913 unmodified lines1731741751761771781791801811 unmodified line1831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182 unmodified lines22122222322422522622722822923023123223323410 unmodified lines2452462472482492501 unmodified lineimport tempfileimport shutilimport threadingfrom flask import Flask13 unmodified linesDATASET_REPO = "Cxops/Bzxv"# =========================# FLASK HEALTH SERVER# =========================7 unmodified linesdef run_web():web.run(host="0.0.0.0", port=7860)# =========================# HUGGINGFACE# =========================88 unmodified linestemp_dir = tempfile.mkdtemp()try:await query.message.edit_text("Downloading from dataset...")local_file = hf_hub_download(repo_id=DATASET_REPO,14 unmodified linespassexcept Exception as e:await query.message.reply_text(f"Error:\n{e}")finally:cleanup(temp_dir)13 unmodified linestemp_dir = tempfile.mkdtemp()try:status = await message.reply_text("Downloading file...")if message.document:media = message.document1 unmodified lineelif message.video:media = message.videofilename = (media.file_nameor f"{media.file_unique_id}.mp4")elif message.audio:media = message.audiofilename = (media.file_nameor f"{media.file_unique_id}.mp3")else:media = message.photo[-1]filename = f"{media.file_unique_id}.jpg"local_path = await message.download(file_name=os.path.join(temp_dir,filename))await status.edit_text("Uploading to Hugging Face...")repo_path = (f"{message.from_user.id}/{filename}")hf.upload_file(path_or_fileobj=local_path,2 unmodified linesrepo_type="dataset")await status.edit_text(f"Uploaded successfully.\n\n{filename}")except Exception as e:await message.reply_text(f"Upload failed:\n{e}")finally:cleanup(temp_dir)10 unmodified linesdaemon=True).start()print("Starting Telegram bot...")app.run()1 unmodified line2345678913 unmodified lines232425262728293031327 unmodified lines40414243444546474849505152535455565758596061626388 unmodified lines15215315415515615715814 unmodified lines17317417517617717817913 unmodified lines1931941951961971981991 unmodified line2012022032042052062072082092102112122132142152162172182192202212222232 unmodified lines22622722822923023123223323423510 unmodified lines2462472482492502512522532542552562572581 unmodified lineimport tempfileimport shutilimport threadingimport timeimport requestsfrom flask import Flask13 unmodified linesDATASET_REPO = "Cxops/Bzxv"# Hugging Face Space to keep aliveKEEP_ALIVE_URL = "https://huggingface.co/spaces/Cxops/Push"PING_INTERVAL = 300 # 5 minutes# =========================# FLASK HEALTH SERVER# =========================7 unmodified linesdef run_web():web.run(host="0.0.0.0", port=7860)# =========================# KEEP ALIVE PINGER (INTEGRATED)# =========================def keep_alive():headers = {"User-Agent": "Mozilla/5.0 (KeepAliveBot)"}while True:try:r = requests.get(KEEP_ALIVE_URL, headers=headers, timeout=20)print(f"[KEEPALIVE] Pinged {KEEP_ALIVE_URL} | Status: {r.status_code}")except Exception as e:print(f"[KEEPALIVE ERROR] {e}")time.sleep(PING_INTERVAL)# =========================# HUGGINGFACE# =========================88 unmodified linestemp_dir = tempfile.mkdtemp()try:await query.message.edit_text("Downloading from dataset...")local_file = hf_hub_download(repo_id=DATASET_REPO,14 unmodified linespassexcept Exception as e:await query.message.reply_text(f"Error:\n{e}")finally:cleanup(temp_dir)13 unmodified linestemp_dir = tempfile.mkdtemp()try:status = await message.reply_text("Downloading file...")if message.document:media = message.document1 unmodified lineelif message.video:media = message.videofilename = media.file_name or f"{media.file_unique_id}.mp4"elif message.audio:media = message.audiofilename = media.file_name or f"{media.file_unique_id}.mp3"else:media = message.photo[-1]filename = f"{media.file_unique_id}.jpg"local_path = await message.download(file_name=os.path.join(temp_dir, filename))await status.edit_text("Uploading to Hugging Face...")repo_path = f"{message.from_user.id}/{filename}"hf.upload_file(path_or_fileobj=local_path,2 unmodified linesrepo_type="dataset")await status.edit_text(f"Uploaded successfully.\n\n{filename}")except Exception as e:await message.reply_text(f"Upload failed:\n{e}")finally:cleanup(temp_dir)10 unmodified linesdaemon=True).start()print("Starting Keep-Alive Pinger...")threading.Thread(target=keep_alive,daemon=True).start()print("Starting Telegram bot...")app.run()
app.py+41-33