Update app.py
Browse files
app.py
CHANGED
|
@@ -23,24 +23,24 @@ HF_TOKEN = os.environ["HF_TOKEN"]
|
|
| 23 |
DATASET_REPO = "Cxops/Bzxv"
|
| 24 |
|
| 25 |
# =========================
|
| 26 |
-
#
|
| 27 |
# =========================
|
| 28 |
|
| 29 |
def log(msg):
|
| 30 |
-
print(f"[
|
| 31 |
|
| 32 |
# =========================
|
| 33 |
# FLASK (HF SPACE KEEP ALIVE)
|
| 34 |
# =========================
|
| 35 |
|
| 36 |
-
|
| 37 |
|
| 38 |
-
@
|
| 39 |
def home():
|
| 40 |
return "OK"
|
| 41 |
|
| 42 |
def run_web():
|
| 43 |
-
|
| 44 |
|
| 45 |
# =========================
|
| 46 |
# HF CLIENT
|
|
@@ -53,7 +53,7 @@ hf = HfApi(token=HF_TOKEN)
|
|
| 53 |
# =========================
|
| 54 |
|
| 55 |
app = Client(
|
| 56 |
-
"
|
| 57 |
api_id=API_ID,
|
| 58 |
api_hash=API_HASH,
|
| 59 |
bot_token=BOT_TOKEN
|
|
@@ -70,27 +70,20 @@ def cleanup(path):
|
|
| 70 |
pass
|
| 71 |
|
| 72 |
# =========================
|
| 73 |
-
# PROGRESS
|
| 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
|
|
|
|
| 87 |
percent = (cur * 100 / total) if total else 0
|
| 88 |
|
| 89 |
-
|
| 90 |
return (
|
| 91 |
-
"β" *
|
| 92 |
f"{percent:.2f}%\n"
|
| 93 |
-
f"{
|
| 94 |
)
|
| 95 |
|
| 96 |
# =========================
|
|
@@ -100,9 +93,9 @@ def bar(cur, total, start):
|
|
| 100 |
@app.on_message(filters.command("start"))
|
| 101 |
async def start(_, m):
|
| 102 |
await m.reply_text(
|
| 103 |
-
"β‘
|
| 104 |
-
"Send file β
|
| 105 |
-
"/files β list
|
| 106 |
)
|
| 107 |
|
| 108 |
# =========================
|
|
@@ -113,32 +106,28 @@ async def start(_, m):
|
|
| 113 |
async def files(_, m):
|
| 114 |
uid = str(m.from_user.id)
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
)
|
| 121 |
-
|
| 122 |
-
user_files = [f for f in all_files if f.startswith(uid + "/")]
|
| 123 |
|
| 124 |
-
|
| 125 |
-
return await m.reply_text("No files found.")
|
| 126 |
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
for f in user_files[:50]
|
| 130 |
-
]
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
|
| 137 |
-
|
| 138 |
-
|
|
|
|
|
|
|
| 139 |
|
| 140 |
# =========================
|
| 141 |
-
# HF β TELEGRAM (
|
| 142 |
# =========================
|
| 143 |
|
| 144 |
@app.on_callback_query(filters.regex("^get:"))
|
|
@@ -147,27 +136,11 @@ async def get_file(_, q):
|
|
| 147 |
tmp = tempfile.mkdtemp()
|
| 148 |
|
| 149 |
try:
|
| 150 |
-
|
| 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,
|
| 173 |
repo_type="dataset",
|
|
@@ -176,35 +149,43 @@ async def get_file(_, q):
|
|
| 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 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 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"
|
| 201 |
await q.message.reply_text(str(e))
|
| 202 |
|
| 203 |
finally:
|
| 204 |
cleanup(tmp)
|
| 205 |
|
| 206 |
# =========================
|
| 207 |
-
# TELEGRAM β HF (
|
| 208 |
# =========================
|
| 209 |
|
| 210 |
@app.on_message(
|
|
@@ -229,7 +210,7 @@ async def upload(_, m):
|
|
| 229 |
else:
|
| 230 |
name = f"{m.photo[-1].file_unique_id}.jpg"
|
| 231 |
|
| 232 |
-
|
| 233 |
|
| 234 |
start = time.time()
|
| 235 |
last = [0]
|
|
@@ -237,7 +218,8 @@ async def upload(_, m):
|
|
| 237 |
# DOWNLOAD PROGRESS
|
| 238 |
def dl_progress(cur, total):
|
| 239 |
now = time.time()
|
| 240 |
-
|
|
|
|
| 241 |
return
|
| 242 |
last[0] = now
|
| 243 |
|
|
@@ -254,36 +236,35 @@ async def upload(_, m):
|
|
| 254 |
progress=dl_progress
|
| 255 |
)
|
| 256 |
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
await status.edit_text("β Uploading to Hugging Face...")
|
| 260 |
|
| 261 |
-
repo_path = f"{
|
| 262 |
|
| 263 |
-
# HF upload
|
| 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(
|
|
|
|
|
|
|
|
|
|
| 287 |
|
| 288 |
# =========================
|
| 289 |
# RUN
|
|
|
|
| 23 |
DATASET_REPO = "Cxops/Bzxv"
|
| 24 |
|
| 25 |
# =========================
|
| 26 |
+
# LOGS
|
| 27 |
# =========================
|
| 28 |
|
| 29 |
def log(msg):
|
| 30 |
+
print(f"[LOG] {msg}", flush=True)
|
| 31 |
|
| 32 |
# =========================
|
| 33 |
# FLASK (HF SPACE KEEP ALIVE)
|
| 34 |
# =========================
|
| 35 |
|
| 36 |
+
app_web = Flask(__name__)
|
| 37 |
|
| 38 |
+
@app_web.route("/")
|
| 39 |
def home():
|
| 40 |
return "OK"
|
| 41 |
|
| 42 |
def run_web():
|
| 43 |
+
app_web.run(host="0.0.0.0", port=7860)
|
| 44 |
|
| 45 |
# =========================
|
| 46 |
# HF CLIENT
|
|
|
|
| 53 |
# =========================
|
| 54 |
|
| 55 |
app = Client(
|
| 56 |
+
"bot",
|
| 57 |
api_id=API_ID,
|
| 58 |
api_hash=API_HASH,
|
| 59 |
bot_token=BOT_TOKEN
|
|
|
|
| 70 |
pass
|
| 71 |
|
| 72 |
# =========================
|
| 73 |
+
# PROGRESS UI
|
| 74 |
# =========================
|
| 75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
def bar(cur, total, start):
|
| 77 |
elapsed = time.time() - start
|
| 78 |
+
speed = cur / (elapsed + 0.0001)
|
| 79 |
+
|
| 80 |
percent = (cur * 100 / total) if total else 0
|
| 81 |
|
| 82 |
+
blocks = int(percent // 10)
|
| 83 |
return (
|
| 84 |
+
"β" * blocks + "β" * (10 - blocks) + "\n"
|
| 85 |
f"{percent:.2f}%\n"
|
| 86 |
+
f"{speed/1024/1024:.2f} MB/s"
|
| 87 |
)
|
| 88 |
|
| 89 |
# =========================
|
|
|
|
| 93 |
@app.on_message(filters.command("start"))
|
| 94 |
async def start(_, m):
|
| 95 |
await m.reply_text(
|
| 96 |
+
"β‘ Bot Ready\n\n"
|
| 97 |
+
"Send file β HF storage\n"
|
| 98 |
+
"/files β list"
|
| 99 |
)
|
| 100 |
|
| 101 |
# =========================
|
|
|
|
| 106 |
async def files(_, m):
|
| 107 |
uid = str(m.from_user.id)
|
| 108 |
|
| 109 |
+
all_files = hf.list_repo_files(
|
| 110 |
+
repo_id=DATASET_REPO,
|
| 111 |
+
repo_type="dataset"
|
| 112 |
+
)
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
+
user_files = [f for f in all_files if f.startswith(uid + "/")]
|
|
|
|
| 115 |
|
| 116 |
+
if not user_files:
|
| 117 |
+
return await m.reply_text("No files found")
|
|
|
|
|
|
|
| 118 |
|
| 119 |
+
buttons = [
|
| 120 |
+
[InlineKeyboardButton(os.path.basename(f), callback_data=f"get:{f}")]
|
| 121 |
+
for f in user_files[:50]
|
| 122 |
+
]
|
| 123 |
|
| 124 |
+
await m.reply_text(
|
| 125 |
+
"Your files:",
|
| 126 |
+
reply_markup=InlineKeyboardMarkup(buttons)
|
| 127 |
+
)
|
| 128 |
|
| 129 |
# =========================
|
| 130 |
+
# HF β TELEGRAM (FIXED PROGRESS)
|
| 131 |
# =========================
|
| 132 |
|
| 133 |
@app.on_callback_query(filters.regex("^get:"))
|
|
|
|
| 136 |
tmp = tempfile.mkdtemp()
|
| 137 |
|
| 138 |
try:
|
| 139 |
+
await q.message.edit_text("π₯ Downloading from HF...")
|
|
|
|
|
|
|
| 140 |
|
| 141 |
start = time.time()
|
| 142 |
last = [0]
|
| 143 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
file_path = hf_hub_download(
|
| 145 |
repo_id=DATASET_REPO,
|
| 146 |
repo_type="dataset",
|
|
|
|
| 149 |
local_dir=tmp
|
| 150 |
)
|
| 151 |
|
|
|
|
|
|
|
| 152 |
await q.message.edit_text("π€ Uploading to Telegram...")
|
| 153 |
|
| 154 |
tg_start = time.time()
|
| 155 |
tg_last = [0]
|
| 156 |
|
| 157 |
+
def tg_progress(cur, total):
|
| 158 |
+
now = time.time()
|
| 159 |
+
|
| 160 |
+
if now - tg_last[0] < 2:
|
| 161 |
+
return
|
| 162 |
+
tg_last[0] = now
|
| 163 |
+
|
| 164 |
+
try:
|
| 165 |
+
q.message.edit_text(
|
| 166 |
+
"π€ Uploading to Telegram...\n\n" +
|
| 167 |
+
bar(cur, total, tg_start)
|
| 168 |
+
)
|
| 169 |
+
except:
|
| 170 |
+
pass
|
| 171 |
+
|
| 172 |
+
await app.send_document(
|
| 173 |
+
chat_id=q.message.chat.id,
|
| 174 |
+
document=file_path,
|
| 175 |
+
progress=tg_progress
|
| 176 |
)
|
| 177 |
|
| 178 |
await q.message.delete()
|
| 179 |
|
|
|
|
|
|
|
| 180 |
except Exception as e:
|
| 181 |
+
log(f"HF->TG error: {e}")
|
| 182 |
await q.message.reply_text(str(e))
|
| 183 |
|
| 184 |
finally:
|
| 185 |
cleanup(tmp)
|
| 186 |
|
| 187 |
# =========================
|
| 188 |
+
# TELEGRAM β HF (FIXED PROGRESS)
|
| 189 |
# =========================
|
| 190 |
|
| 191 |
@app.on_message(
|
|
|
|
| 210 |
else:
|
| 211 |
name = f"{m.photo[-1].file_unique_id}.jpg"
|
| 212 |
|
| 213 |
+
uid = str(m.from_user.id)
|
| 214 |
|
| 215 |
start = time.time()
|
| 216 |
last = [0]
|
|
|
|
| 218 |
# DOWNLOAD PROGRESS
|
| 219 |
def dl_progress(cur, total):
|
| 220 |
now = time.time()
|
| 221 |
+
|
| 222 |
+
if now - last[0] < 2:
|
| 223 |
return
|
| 224 |
last[0] = now
|
| 225 |
|
|
|
|
| 236 |
progress=dl_progress
|
| 237 |
)
|
| 238 |
|
| 239 |
+
await status.edit_text("β Uploading to HF...")
|
|
|
|
|
|
|
| 240 |
|
| 241 |
+
repo_path = f"{uid}/{name}"
|
| 242 |
|
| 243 |
+
# HF upload background
|
| 244 |
def hf_upload():
|
| 245 |
try:
|
|
|
|
| 246 |
hf.upload_file(
|
| 247 |
path_or_fileobj=local,
|
| 248 |
path_in_repo=repo_path,
|
| 249 |
repo_id=DATASET_REPO,
|
| 250 |
repo_type="dataset",
|
| 251 |
)
|
| 252 |
+
status.edit_text("β‘ Upload Complete")
|
| 253 |
log("HF upload done")
|
|
|
|
| 254 |
except Exception as e:
|
|
|
|
| 255 |
status.edit_text(f"HF Error:\n{e}")
|
| 256 |
+
log(f"HF error: {e}")
|
| 257 |
|
| 258 |
threading.Thread(target=hf_upload, daemon=True).start()
|
| 259 |
|
| 260 |
except Exception as e:
|
|
|
|
| 261 |
await m.reply_text(str(e))
|
| 262 |
|
| 263 |
finally:
|
| 264 |
+
threading.Thread(
|
| 265 |
+
target=lambda: (time.sleep(15), cleanup(tmp)),
|
| 266 |
+
daemon=True
|
| 267 |
+
).start()
|
| 268 |
|
| 269 |
# =========================
|
| 270 |
# RUN
|