Update app.py
Browse files
app.py
CHANGED
|
@@ -14,7 +14,7 @@ import requests
|
|
| 14 |
from flask import Flask
|
| 15 |
from pyrogram import Client, filters
|
| 16 |
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton
|
| 17 |
-
from huggingface_hub import HfApi, get_hf_file_metadata
|
| 18 |
from cryptography.hazmat.primitives.ciphers.aead import AESSIV
|
| 19 |
|
| 20 |
# --------------- CONFIG ---------------
|
|
@@ -285,12 +285,16 @@ async def do_download_job(chat_id: int, enc_path: str, orig_name: str,
|
|
| 285 |
temp_dir = tempfile.mkdtemp()
|
| 286 |
try:
|
| 287 |
loop = asyncio.get_event_loop()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 288 |
metadata = await loop.run_in_executor(
|
| 289 |
None,
|
| 290 |
-
lambda: get_hf_file_metadata(
|
| 291 |
-
url=hf.get_hf_file_metadata_url(repo_id=DATASET_REPO, filename=enc_path, repo_type="dataset"),
|
| 292 |
-
token=HF_TOKEN
|
| 293 |
-
)
|
| 294 |
)
|
| 295 |
total_size = metadata.size
|
| 296 |
|
|
@@ -304,11 +308,8 @@ async def do_download_job(chat_id: int, enc_path: str, orig_name: str,
|
|
| 304 |
download_done = loop.create_future()
|
| 305 |
|
| 306 |
def sync_download_with_progress():
|
| 307 |
-
url = hf.get_hf_file_metadata_url(
|
| 308 |
-
repo_id=DATASET_REPO, filename=enc_path, repo_type="dataset"
|
| 309 |
-
)
|
| 310 |
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 311 |
-
resp = requests.get(
|
| 312 |
resp.raise_for_status()
|
| 313 |
with open(local_file, "wb") as f:
|
| 314 |
downloaded = 0
|
|
@@ -417,10 +418,12 @@ async def list_files(_, message):
|
|
| 417 |
except Exception:
|
| 418 |
orig_name = enc_name
|
| 419 |
try:
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
|
|
|
| 423 |
)
|
|
|
|
| 424 |
size = meta.size
|
| 425 |
except Exception:
|
| 426 |
size = 0
|
|
@@ -434,7 +437,6 @@ async def list_files(_, message):
|
|
| 434 |
|
| 435 |
buttons = []
|
| 436 |
for enc_path, orig_name, _ in user_files:
|
| 437 |
-
# Generate a short token instead of passing the whole path
|
| 438 |
token = make_token()
|
| 439 |
file_callback_map[token] = enc_path
|
| 440 |
buttons.append([InlineKeyboardButton(orig_name[:60], callback_data=f"get:{token}")])
|
|
@@ -530,7 +532,6 @@ async def on_file(_, message):
|
|
| 530 |
else:
|
| 531 |
filename = f"{message.photo.file_unique_id}.jpg"
|
| 532 |
|
| 533 |
-
# Use short token to avoid long callback data
|
| 534 |
token = make_token()
|
| 535 |
upload_prompt_map[token] = (message.from_user.id, message.id, filename)
|
| 536 |
|
|
|
|
| 14 |
from flask import Flask
|
| 15 |
from pyrogram import Client, filters
|
| 16 |
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton
|
| 17 |
+
from huggingface_hub import HfApi, get_hf_file_metadata, hf_hub_url
|
| 18 |
from cryptography.hazmat.primitives.ciphers.aead import AESSIV
|
| 19 |
|
| 20 |
# --------------- CONFIG ---------------
|
|
|
|
| 285 |
temp_dir = tempfile.mkdtemp()
|
| 286 |
try:
|
| 287 |
loop = asyncio.get_event_loop()
|
| 288 |
+
|
| 289 |
+
# Correct way to get file size: build URL with hf_hub_url
|
| 290 |
+
file_url = hf_hub_url(
|
| 291 |
+
repo_id=DATASET_REPO,
|
| 292 |
+
filename=enc_path,
|
| 293 |
+
repo_type="dataset"
|
| 294 |
+
)
|
| 295 |
metadata = await loop.run_in_executor(
|
| 296 |
None,
|
| 297 |
+
lambda: get_hf_file_metadata(url=file_url, token=HF_TOKEN)
|
|
|
|
|
|
|
|
|
|
| 298 |
)
|
| 299 |
total_size = metadata.size
|
| 300 |
|
|
|
|
| 308 |
download_done = loop.create_future()
|
| 309 |
|
| 310 |
def sync_download_with_progress():
|
|
|
|
|
|
|
|
|
|
| 311 |
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 312 |
+
resp = requests.get(file_url, stream=True, headers=headers)
|
| 313 |
resp.raise_for_status()
|
| 314 |
with open(local_file, "wb") as f:
|
| 315 |
downloaded = 0
|
|
|
|
| 418 |
except Exception:
|
| 419 |
orig_name = enc_name
|
| 420 |
try:
|
| 421 |
+
file_url = hf_hub_url(
|
| 422 |
+
repo_id=DATASET_REPO,
|
| 423 |
+
filename=path,
|
| 424 |
+
repo_type="dataset"
|
| 425 |
)
|
| 426 |
+
meta = get_hf_file_metadata(url=file_url, token=HF_TOKEN)
|
| 427 |
size = meta.size
|
| 428 |
except Exception:
|
| 429 |
size = 0
|
|
|
|
| 437 |
|
| 438 |
buttons = []
|
| 439 |
for enc_path, orig_name, _ in user_files:
|
|
|
|
| 440 |
token = make_token()
|
| 441 |
file_callback_map[token] = enc_path
|
| 442 |
buttons.append([InlineKeyboardButton(orig_name[:60], callback_data=f"get:{token}")])
|
|
|
|
| 532 |
else:
|
| 533 |
filename = f"{message.photo.file_unique_id}.jpg"
|
| 534 |
|
|
|
|
| 535 |
token = make_token()
|
| 536 |
upload_prompt_map[token] = (message.from_user.id, message.id, filename)
|
| 537 |
|