Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,8 @@ import os
|
|
| 2 |
import tempfile
|
| 3 |
import shutil
|
| 4 |
import threading
|
|
|
|
|
|
|
| 5 |
|
| 6 |
from flask import Flask
|
| 7 |
|
|
@@ -21,6 +23,10 @@ HF_TOKEN = os.environ["HF_TOKEN"]
|
|
| 21 |
|
| 22 |
DATASET_REPO = "Cxops/Bzxv"
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
# =========================
|
| 25 |
# FLASK HEALTH SERVER
|
| 26 |
# =========================
|
|
@@ -34,6 +40,24 @@ def home():
|
|
| 34 |
def run_web():
|
| 35 |
web.run(host="0.0.0.0", port=7860)
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
# =========================
|
| 38 |
# HUGGINGFACE
|
| 39 |
# =========================
|
|
@@ -128,9 +152,7 @@ async def get_file(_, query):
|
|
| 128 |
temp_dir = tempfile.mkdtemp()
|
| 129 |
|
| 130 |
try:
|
| 131 |
-
await query.message.edit_text(
|
| 132 |
-
"Downloading from dataset..."
|
| 133 |
-
)
|
| 134 |
|
| 135 |
local_file = hf_hub_download(
|
| 136 |
repo_id=DATASET_REPO,
|
|
@@ -151,9 +173,7 @@ async def get_file(_, query):
|
|
| 151 |
pass
|
| 152 |
|
| 153 |
except Exception as e:
|
| 154 |
-
await query.message.reply_text(
|
| 155 |
-
f"Error:\n{e}"
|
| 156 |
-
)
|
| 157 |
|
| 158 |
finally:
|
| 159 |
cleanup(temp_dir)
|
|
@@ -173,9 +193,7 @@ async def upload_file(_, message):
|
|
| 173 |
temp_dir = tempfile.mkdtemp()
|
| 174 |
|
| 175 |
try:
|
| 176 |
-
status = await message.reply_text(
|
| 177 |
-
"Downloading file..."
|
| 178 |
-
)
|
| 179 |
|
| 180 |
if message.document:
|
| 181 |
media = message.document
|
|
@@ -183,36 +201,23 @@ async def upload_file(_, message):
|
|
| 183 |
|
| 184 |
elif message.video:
|
| 185 |
media = message.video
|
| 186 |
-
filename =
|
| 187 |
-
media.file_name
|
| 188 |
-
or f"{media.file_unique_id}.mp4"
|
| 189 |
-
)
|
| 190 |
|
| 191 |
elif message.audio:
|
| 192 |
media = message.audio
|
| 193 |
-
filename =
|
| 194 |
-
media.file_name
|
| 195 |
-
or f"{media.file_unique_id}.mp3"
|
| 196 |
-
)
|
| 197 |
|
| 198 |
else:
|
| 199 |
media = message.photo[-1]
|
| 200 |
filename = f"{media.file_unique_id}.jpg"
|
| 201 |
|
| 202 |
local_path = await message.download(
|
| 203 |
-
file_name=os.path.join(
|
| 204 |
-
temp_dir,
|
| 205 |
-
filename
|
| 206 |
-
)
|
| 207 |
)
|
| 208 |
|
| 209 |
-
await status.edit_text(
|
| 210 |
-
"Uploading to Hugging Face..."
|
| 211 |
-
)
|
| 212 |
|
| 213 |
-
repo_path =
|
| 214 |
-
f"{message.from_user.id}/{filename}"
|
| 215 |
-
)
|
| 216 |
|
| 217 |
hf.upload_file(
|
| 218 |
path_or_fileobj=local_path,
|
|
@@ -221,14 +226,10 @@ async def upload_file(_, message):
|
|
| 221 |
repo_type="dataset"
|
| 222 |
)
|
| 223 |
|
| 224 |
-
await status.edit_text(
|
| 225 |
-
f"Uploaded successfully.\n\n{filename}"
|
| 226 |
-
)
|
| 227 |
|
| 228 |
except Exception as e:
|
| 229 |
-
await message.reply_text(
|
| 230 |
-
f"Upload failed:\n{e}"
|
| 231 |
-
)
|
| 232 |
|
| 233 |
finally:
|
| 234 |
cleanup(temp_dir)
|
|
@@ -245,6 +246,13 @@ if __name__ == "__main__":
|
|
| 245 |
daemon=True
|
| 246 |
).start()
|
| 247 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
print("Starting Telegram bot...")
|
| 249 |
|
| 250 |
app.run()
|
|
|
|
| 2 |
import tempfile
|
| 3 |
import shutil
|
| 4 |
import threading
|
| 5 |
+
import time
|
| 6 |
+
import requests
|
| 7 |
|
| 8 |
from flask import Flask
|
| 9 |
|
|
|
|
| 23 |
|
| 24 |
DATASET_REPO = "Cxops/Bzxv"
|
| 25 |
|
| 26 |
+
# Hugging Face Space to keep alive
|
| 27 |
+
KEEP_ALIVE_URL = "https://huggingface.co/spaces/Cxops/Push"
|
| 28 |
+
PING_INTERVAL = 300 # 5 minutes
|
| 29 |
+
|
| 30 |
# =========================
|
| 31 |
# FLASK HEALTH SERVER
|
| 32 |
# =========================
|
|
|
|
| 40 |
def run_web():
|
| 41 |
web.run(host="0.0.0.0", port=7860)
|
| 42 |
|
| 43 |
+
# =========================
|
| 44 |
+
# KEEP ALIVE PINGER (INTEGRATED)
|
| 45 |
+
# =========================
|
| 46 |
+
|
| 47 |
+
def keep_alive():
|
| 48 |
+
headers = {
|
| 49 |
+
"User-Agent": "Mozilla/5.0 (KeepAliveBot)"
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
while True:
|
| 53 |
+
try:
|
| 54 |
+
r = requests.get(KEEP_ALIVE_URL, headers=headers, timeout=20)
|
| 55 |
+
print(f"[KEEPALIVE] Pinged {KEEP_ALIVE_URL} | Status: {r.status_code}")
|
| 56 |
+
except Exception as e:
|
| 57 |
+
print(f"[KEEPALIVE ERROR] {e}")
|
| 58 |
+
|
| 59 |
+
time.sleep(PING_INTERVAL)
|
| 60 |
+
|
| 61 |
# =========================
|
| 62 |
# HUGGINGFACE
|
| 63 |
# =========================
|
|
|
|
| 152 |
temp_dir = tempfile.mkdtemp()
|
| 153 |
|
| 154 |
try:
|
| 155 |
+
await query.message.edit_text("Downloading from dataset...")
|
|
|
|
|
|
|
| 156 |
|
| 157 |
local_file = hf_hub_download(
|
| 158 |
repo_id=DATASET_REPO,
|
|
|
|
| 173 |
pass
|
| 174 |
|
| 175 |
except Exception as e:
|
| 176 |
+
await query.message.reply_text(f"Error:\n{e}")
|
|
|
|
|
|
|
| 177 |
|
| 178 |
finally:
|
| 179 |
cleanup(temp_dir)
|
|
|
|
| 193 |
temp_dir = tempfile.mkdtemp()
|
| 194 |
|
| 195 |
try:
|
| 196 |
+
status = await message.reply_text("Downloading file...")
|
|
|
|
|
|
|
| 197 |
|
| 198 |
if message.document:
|
| 199 |
media = message.document
|
|
|
|
| 201 |
|
| 202 |
elif message.video:
|
| 203 |
media = message.video
|
| 204 |
+
filename = media.file_name or f"{media.file_unique_id}.mp4"
|
|
|
|
|
|
|
|
|
|
| 205 |
|
| 206 |
elif message.audio:
|
| 207 |
media = message.audio
|
| 208 |
+
filename = media.file_name or f"{media.file_unique_id}.mp3"
|
|
|
|
|
|
|
|
|
|
| 209 |
|
| 210 |
else:
|
| 211 |
media = message.photo[-1]
|
| 212 |
filename = f"{media.file_unique_id}.jpg"
|
| 213 |
|
| 214 |
local_path = await message.download(
|
| 215 |
+
file_name=os.path.join(temp_dir, filename)
|
|
|
|
|
|
|
|
|
|
| 216 |
)
|
| 217 |
|
| 218 |
+
await status.edit_text("Uploading to Hugging Face...")
|
|
|
|
|
|
|
| 219 |
|
| 220 |
+
repo_path = f"{message.from_user.id}/{filename}"
|
|
|
|
|
|
|
| 221 |
|
| 222 |
hf.upload_file(
|
| 223 |
path_or_fileobj=local_path,
|
|
|
|
| 226 |
repo_type="dataset"
|
| 227 |
)
|
| 228 |
|
| 229 |
+
await status.edit_text(f"Uploaded successfully.\n\n{filename}")
|
|
|
|
|
|
|
| 230 |
|
| 231 |
except Exception as e:
|
| 232 |
+
await message.reply_text(f"Upload failed:\n{e}")
|
|
|
|
|
|
|
| 233 |
|
| 234 |
finally:
|
| 235 |
cleanup(temp_dir)
|
|
|
|
| 246 |
daemon=True
|
| 247 |
).start()
|
| 248 |
|
| 249 |
+
print("Starting Keep-Alive Pinger...")
|
| 250 |
+
|
| 251 |
+
threading.Thread(
|
| 252 |
+
target=keep_alive,
|
| 253 |
+
daemon=True
|
| 254 |
+
).start()
|
| 255 |
+
|
| 256 |
print("Starting Telegram bot...")
|
| 257 |
|
| 258 |
app.run()
|