Spaces:
Sleeping
Sleeping
Download progress_tracker.py from Erog0291/Shazam_1: direct link, hf CLI and curl.
- Browser
- Download file 4.28 kB
-
https://huggingface.co/spaces/Erog0291/Shazam_1/resolve/main/progress_tracker.py
- Command line
-
hf download hf://spaces/Erog0291/Shazam_1/progress_tracker.py
-
curl -L -o progress_tracker.py https://huggingface.co/spaces/Erog0291/Shazam_1/resolve/main/progress_tracker.py
4.28 kB
| from typing import Optional, Dict, Any | |
| from aiogram import Bot | |
| from aiogram.types import Message | |
| from loguru import logger | |
| import asyncio | |
| import time | |
| class ProgressTracker: | |
| """ | |
| Tracks and sends progress updates to Telegram | |
| Makes the AI feel alive and transparent | |
| """ | |
| def __init__(self, bot: Bot, chat_id: int): | |
| self.bot = bot | |
| self.chat_id = chat_id | |
| self.current_message: Optional[Message] = None | |
| self.start_time = time.time() | |
| self.updates_sent = 0 | |
| logger.info(f"Progress tracker initialized for chat {chat_id}") | |
| async def send_update( | |
| self, | |
| message: str, | |
| progress: Optional[int] = None, | |
| edit: bool = True | |
| ): | |
| """Send progress update to Telegram""" | |
| self.updates_sent += 1 | |
| # Format message with emoji and progress | |
| formatted = self._format_message(message, progress) | |
| try: | |
| if edit and self.current_message: | |
| # Edit existing message | |
| await self.bot.edit_message_text( | |
| chat_id=self.chat_id, | |
| message_id=self.current_message.message_id, | |
| text=formatted, | |
| parse_mode="HTML" | |
| ) | |
| else: | |
| # Send new message | |
| self.current_message = await self.bot.send_message( | |
| chat_id=self.chat_id, | |
| text=formatted, | |
| parse_mode="HTML" | |
| ) except Exception as e: | |
| logger.error(f"Failed to send update: {e}") | |
| def _format_message(self, message: str, progress: Optional[int]) -> str: | |
| """Format message with progress bar""" | |
| elapsed = time.time() - self.start_time | |
| header = f"β‘ <b>Shazam AI</b> - Active\n" | |
| header += f"β± Runtime: {elapsed:.1f}s | Updates: {self.updates_sent}\n\n" | |
| if progress is not None: | |
| # Create progress bar | |
| bar_length = 20 | |
| filled = int(bar_length * progress / 100) | |
| bar = "β" * filled + "β" * (bar_length - filled) | |
| header += f"<code>[{bar}]</code> {progress}%\n\n" | |
| return header + message | |
| async def send_file( | |
| self, | |
| file_path: str, | |
| caption: str = "", | |
| file_type: str = "document" | |
| ): | |
| """Send file to Telegram""" | |
| try: | |
| if file_type == "photo": | |
| await self.bot.send_photo( | |
| chat_id=self.chat_id, | |
| photo=open(file_path, 'rb'), | |
| caption=caption | |
| ) | |
| elif file_type == "video": | |
| await self.bot.send_video( | |
| chat_id=self.chat_id, | |
| video=open(file_path, 'rb'), | |
| caption=caption | |
| ) | |
| else: | |
| await self.bot.send_document( | |
| chat_id=self.chat_id, | |
| document=open(file_path, 'rb'), | |
| caption=caption | |
| ) | |
| except Exception as e: | |
| logger.error(f"Failed to send file: {e}") | |
| async def send_thinking_message(self, action: str): | |
| """Send a thinking/action message""" thinking_messages = [ | |
| f"π€ Analyzing: {action}", | |
| f"π Processing: {action}", | |
| f"π§ Thinking through: {action}", | |
| f"βοΈ Working on: {action}", | |
| f"π¨ Creating: {action}" | |
| ] | |
| import random | |
| await self.send_update(random.choice(thinking_messages)) | |
| async def complete_task(self, message: str = "Task completed successfully!"): | |
| """Mark task as complete""" | |
| elapsed = time.time() - self.start_time | |
| final_message = ( | |
| f"β <b>Task Complete!</b>\n\n" | |
| f"β± Total time: {elapsed:.1f}s\n" | |
| f"π Updates sent: {self.updates_sent}\n\n" | |
| f"{message}" | |
| ) | |
| await self.send_update(final_message, progress=100) | |
| def reset(self): | |
| """Reset tracker for new task""" | |
| self.start_time = time.time() | |
| self.updates_sent = 0 | |
| self.current_message = None |