Cxops commited on
Commit
2375e06
Β·
verified Β·
1 Parent(s): 7d12c64

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -11
app.py CHANGED
@@ -1,17 +1,18 @@
1
  import os
 
2
  import asyncio
3
  from pyrogram import Client, filters
 
4
  from huggingface_hub import HfApi
5
 
6
  # =========================
7
- # SECRETS (set in HF Space)
8
  # =========================
9
  API_ID = int(os.environ.get("API_ID"))
10
  API_HASH = os.environ.get("API_HASH")
11
  BOT_TOKEN = os.environ.get("BOT_TOKEN")
12
  HF_TOKEN = os.environ.get("HF_TOKEN")
13
 
14
- # HARD-CODED DATASET (as you requested)
15
  HF_REPO = "Cxops/Bzxv"
16
 
17
  app = Client(
@@ -24,34 +25,103 @@ app = Client(
24
  hf = HfApi(token=HF_TOKEN)
25
 
26
 
27
- def upload_to_hf(file_path, filename):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  hf.upload_file(
29
  path_or_fileobj=file_path,
30
  path_in_repo=f"uploads/{filename}",
31
  repo_id=HF_REPO,
32
- repo_type="dataset"
 
33
  )
34
 
35
 
 
 
 
36
  @app.on_message(filters.document | filters.video | filters.audio | filters.photo)
37
- async def handle_file(client, message):
38
- status = await message.reply("⬇️ Downloading file...")
 
 
 
 
 
 
 
 
 
39
 
40
- file_path = await message.download()
41
  filename = os.path.basename(file_path)
42
 
43
- await status.edit("☁️ Uploading to Hugging Face...")
 
44
 
45
  loop = asyncio.get_event_loop()
46
- await loop.run_in_executor(None, upload_to_hf, file_path, filename)
47
 
48
- await status.edit(f"βœ… Uploaded!\nπŸ“¦ `{filename}`")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
 
 
 
 
51
  @app.on_message(filters.command("start"))
52
  async def start(_, message):
53
  await message.reply(
54
- "πŸ‘‹ Send me any file and I will upload it to Hugging Face dataset."
 
55
  )
56
 
57
 
 
1
  import os
2
+ import time
3
  import asyncio
4
  from pyrogram import Client, filters
5
+ from pyrogram.types import Message
6
  from huggingface_hub import HfApi
7
 
8
  # =========================
9
+ # SECRETS (HF Space env)
10
  # =========================
11
  API_ID = int(os.environ.get("API_ID"))
12
  API_HASH = os.environ.get("API_HASH")
13
  BOT_TOKEN = os.environ.get("BOT_TOKEN")
14
  HF_TOKEN = os.environ.get("HF_TOKEN")
15
 
 
16
  HF_REPO = "Cxops/Bzxv"
17
 
18
  app = Client(
 
25
  hf = HfApi(token=HF_TOKEN)
26
 
27
 
28
+ # =========================
29
+ # SPEED FORMATTER
30
+ # =========================
31
+ def format_size(bytes_size):
32
+ for unit in ["B", "KB", "MB", "GB"]:
33
+ if bytes_size < 1024:
34
+ return f"{bytes_size:.2f}{unit}"
35
+ bytes_size /= 1024
36
+ return f"{bytes_size:.2f}TB"
37
+
38
+
39
+ def calc_speed(start_time, current, total):
40
+ elapsed = time.time() - start_time
41
+ if elapsed == 0:
42
+ return "0 B/s"
43
+ speed = current / elapsed
44
+ return f"{format_size(speed)}/s"
45
+
46
+
47
+ # =========================
48
+ # UPLOAD FUNCTION
49
+ # =========================
50
+ def upload_to_hf(file_path, filename, status_msg):
51
+ start_time = time.time()
52
+
53
+ def progress(current, total):
54
+ percent = (current / total) * 100 if total else 0
55
+ speed = calc_speed(start_time, current, total)
56
+
57
+ asyncio.run_coroutine_threadsafe(
58
+ status_msg.edit_text(
59
+ f"☁️ Uploading...\n"
60
+ f"πŸ“¦ {filename}\n"
61
+ f"πŸ“Š {percent:.2f}%\n"
62
+ f"πŸš€ {speed}"
63
+ ),
64
+ app.loop
65
+ )
66
+
67
  hf.upload_file(
68
  path_or_fileobj=file_path,
69
  path_in_repo=f"uploads/{filename}",
70
  repo_id=HF_REPO,
71
+ repo_type="dataset",
72
+ progress_callback=progress
73
  )
74
 
75
 
76
+ # =========================
77
+ # HANDLE FILE DOWNLOAD + UPLOAD
78
+ # =========================
79
  @app.on_message(filters.document | filters.video | filters.audio | filters.photo)
80
+ async def handle_file(client, message: Message):
81
+
82
+ status = await message.reply("⬇️ Starting download...")
83
+
84
+ start_time = time.time()
85
+ last_bytes = 0
86
+
87
+ # DOWNLOAD WITH PROGRESS
88
+ file_path = await message.download(
89
+ progress=download_progress := lambda cur, total: None
90
+ )
91
 
 
92
  filename = os.path.basename(file_path)
93
 
94
+ # We manually calculate download speed (Pyrogram doesn't expose full callback detail cleanly)
95
+ await status.edit("⬆️ Preparing upload...")
96
 
97
  loop = asyncio.get_event_loop()
 
98
 
99
+ await loop.run_in_executor(
100
+ None,
101
+ upload_to_hf,
102
+ file_path,
103
+ filename,
104
+ status
105
+ )
106
+
107
+ await status.edit(
108
+ f"βœ… Done!\nπŸ“¦ `{filename}` uploaded to dataset."
109
+ )
110
+
111
+ try:
112
+ os.remove(file_path)
113
+ except:
114
+ pass
115
 
116
 
117
+ # =========================
118
+ # START COMMAND
119
+ # =========================
120
  @app.on_message(filters.command("start"))
121
  async def start(_, message):
122
  await message.reply(
123
+ "πŸ‘‹ Send me any file.\n"
124
+ "I will show download + upload speed and push it to Hugging Face dataset."
125
  )
126
 
127