Cxops commited on
Commit
3243602
·
verified ·
1 Parent(s): 84e557a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -19
app.py CHANGED
@@ -198,14 +198,30 @@ def format_size(bytes_val: int) -> str:
198
  def format_speed(bytes_per_sec: float) -> str:
199
  return f"{format_size(bytes_per_sec)}/s"
200
 
201
- def get_memory_usage() -> str:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
202
  try:
203
- with open("/proc/self/status") as f:
204
  for line in f:
205
- if line.startswith("VmRSS:"):
206
- return format_size(int(line.split()[1]) * 1024)
207
  except Exception:
208
- return "unknown"
 
 
209
 
210
  def extract_file_size(msg: Message) -> Optional[int]:
211
  """Try to get the file size from a message object."""
@@ -648,12 +664,7 @@ async def process_album_group(media_group_id: str):
648
  )
649
  return
650
 
651
- # Handle duplicates – for simplicity, we'll prompt for each valid file individually
652
- # but that would be messy. Instead, we'll collect files that are not duplicates,
653
- # and for those that are, prompt individually.
654
- # For album upload, we can still do a combined prompt only for non-duplicates.
655
- # We'll check each, and if any duplicate found, send a separate duplicate prompt,
656
- # removing it from the batch.
657
  non_dup = []
658
  for msg_id, filename, file_id, size in valid:
659
  token, data = build_duplicate_prompt(user_id, chat_id, None, filename, size, file_id)
@@ -800,16 +811,10 @@ async def cmd_status(_, message: Message):
800
  uptime_sec = int(time.time() - bot_start_time)
801
  uptime_text = f"⏱️ Uptime: {uptime_sec//3600}h {(uptime_sec%3600)//60}m {uptime_sec%60}s"
802
 
803
- mem_text = f"🧠 Memory: {get_memory_usage()}"
804
  cpu_text = f"🖥️ CPU cores: {os.cpu_count()}"
805
 
806
- try:
807
- r = requests.head("https://huggingface.co", timeout=5)
808
- hf_ping = "OK" if r.status_code < 400 else f"HTTP {r.status_code}"
809
- except Exception:
810
- hf_ping = "unreachable"
811
-
812
- status_msg = f"{disk_text}\n{dataset_text}\n{uptime_text}\n{mem_text}\n{cpu_text}\n🌐 Hugging Face: {hf_ping}"
813
  await message.reply_text(status_msg)
814
 
815
  @app.on_message(filters.command("files"))
 
198
  def format_speed(bytes_per_sec: float) -> str:
199
  return f"{format_size(bytes_per_sec)}/s"
200
 
201
+ def get_total_memory() -> int:
202
+ """
203
+ Returns the total memory available to the container (in bytes).
204
+ Tries cgroup v1 limit first, then falls back to /proc/meminfo MemTotal.
205
+ """
206
+ # cgroup v1 memory limit (common in Docker)
207
+ try:
208
+ with open("/sys/fs/cgroup/memory/memory.limit_in_bytes") as f:
209
+ limit = int(f.read().strip())
210
+ if limit < 9_223_372_036_854_771_200: # less than practically infinite
211
+ return limit
212
+ except Exception:
213
+ pass
214
+
215
+ # /proc/meminfo fallback
216
  try:
217
+ with open("/proc/meminfo") as f:
218
  for line in f:
219
+ if line.startswith("MemTotal:"):
220
+ return int(line.split()[1]) * 1024 # kB → bytes
221
  except Exception:
222
+ pass
223
+
224
+ return 0
225
 
226
  def extract_file_size(msg: Message) -> Optional[int]:
227
  """Try to get the file size from a message object."""
 
664
  )
665
  return
666
 
667
+ # Handle duplicates
 
 
 
 
 
668
  non_dup = []
669
  for msg_id, filename, file_id, size in valid:
670
  token, data = build_duplicate_prompt(user_id, chat_id, None, filename, size, file_id)
 
811
  uptime_sec = int(time.time() - bot_start_time)
812
  uptime_text = f"⏱️ Uptime: {uptime_sec//3600}h {(uptime_sec%3600)//60}m {uptime_sec%60}s"
813
 
814
+ mem_text = f"🧠 Memory: {format_size(get_total_memory())}"
815
  cpu_text = f"🖥️ CPU cores: {os.cpu_count()}"
816
 
817
+ status_msg = f"{disk_text}\n{dataset_text}\n{uptime_text}\n{mem_text}\n{cpu_text}"
 
 
 
 
 
 
818
  await message.reply_text(status_msg)
819
 
820
  @app.on_message(filters.command("files"))