Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,72 +1,27 @@
|
|
1 |
-
|
2 |
|
3 |
import os
|
4 |
import asyncio
|
5 |
import json
|
6 |
import logging
|
7 |
-
import sys
|
8 |
|
9 |
# --- Detailed Logging Setup ---
|
10 |
-
|
11 |
-
logging.
|
12 |
-
|
13 |
-
format='%(asctime)s - %(name)s - %(levelname)s - %(module)s:%(lineno)d - %(message)s',
|
14 |
-
stream=sys.stdout, # Ensure logs go to standard output for Hugging Face
|
15 |
-
force=True # In case logging was already configured by another lib (unlikely here)
|
16 |
-
)
|
17 |
-
logger = logging.getLogger(__name__) # Get a logger for this module
|
18 |
-
logging.getLogger('telethon').setLevel(logging.DEBUG) # Max verbosity for Telethon
|
19 |
-
# logging.getLogger('PIL').setLevel(logging.DEBUG) # If Pillow issues arise
|
20 |
-
print("DEBUG: Logging setup complete.")
|
21 |
|
22 |
logger.debug("DEBUG: Importing Telethon...")
|
23 |
-
from telethon import TelegramClient, events
|
|
|
24 |
from telethon.tl import types # For DocumentAttributeFilename
|
|
|
25 |
logger.debug("DEBUG: Telethon imported.")
|
26 |
|
27 |
-
|
28 |
-
from PIL import Image, ImageDraw, ImageFont
|
29 |
-
logger.debug("DEBUG: Pillow imported.")
|
30 |
|
31 |
-
|
32 |
-
#
|
33 |
-
logger.info("DEBUG: Loading environment variables...")
|
34 |
-
try:
|
35 |
-
logger.debug("DEBUG: Attempting to import dotenv...")
|
36 |
-
from dotenv import load_dotenv
|
37 |
-
logger.debug("DEBUG: dotenv imported. Attempting to load .env file...")
|
38 |
-
if load_dotenv():
|
39 |
-
logger.info("DEBUG: .env file loaded successfully.")
|
40 |
-
else:
|
41 |
-
logger.info("DEBUG: No .env file found or it's empty, relying on pre-set environment variables.")
|
42 |
-
except ImportError:
|
43 |
-
logger.info("DEBUG: python-dotenv not installed, relying on pre-set environment variables.")
|
44 |
-
except Exception as e:
|
45 |
-
logger.error(f"DEBUG: Error loading .env using dotenv: {e}")
|
46 |
-
|
47 |
-
|
48 |
-
API_ID = os.environ.get('API_ID')
|
49 |
-
API_HASH = os.environ.get('API_HASH')
|
50 |
-
BOT_TOKEN = os.environ.get('BOT_TOKEN')
|
51 |
-
|
52 |
-
logger.info(f"DEBUG: API_ID loaded: {'Set' if API_ID else 'NOT SET'}")
|
53 |
-
logger.info(f"DEBUG: API_HASH loaded: {'Set' if API_HASH else 'NOT SET'}")
|
54 |
-
logger.info(f"DEBUG: BOT_TOKEN loaded: {'Set' if BOT_TOKEN else 'NOT SET'}")
|
55 |
-
|
56 |
-
if not all([API_ID, API_HASH, BOT_TOKEN]):
|
57 |
-
logger.critical("CRITICAL ERROR: API_ID, API_HASH, and BOT_TOKEN environment variables must be set in Hugging Face Secrets.")
|
58 |
-
logger.critical("Bot cannot start. Please set these secrets in your Space settings.")
|
59 |
-
sys.exit(1) # Exit script if critical env vars are missing
|
60 |
-
|
61 |
-
try:
|
62 |
-
API_ID = int(API_ID)
|
63 |
-
logger.info(f"DEBUG: API_ID successfully converted to int: {str(API_ID)[:4]}... (masked for log)")
|
64 |
-
except ValueError:
|
65 |
-
logger.critical(f"CRITICAL ERROR: API_ID ('{os.environ.get('API_ID')}') must be an integer.")
|
66 |
-
sys.exit(1)
|
67 |
-
|
68 |
-
# --- Session Name (Change this to force a new session if connection hangs) ---
|
69 |
-
SESSION_VERSION = "v1" # Increment this (e.g., "v2", "v3") if you suspect session file issues
|
70 |
SESSION_NAME = f'session/image_bot_session_{SESSION_VERSION}'
|
71 |
logger.info(f"DEBUG: Using session name: {SESSION_NAME}")
|
72 |
|
@@ -78,6 +33,9 @@ except Exception as e:
|
|
78 |
logger.critical(f"CRITICAL ERROR: Failed to initialize TelegramClient: {e}", exc_info=True)
|
79 |
sys.exit(1)
|
80 |
|
|
|
|
|
|
|
81 |
|
82 |
# --- Paths and Directory Setup ---
|
83 |
logger.info("DEBUG: Defining paths and ensuring directories...")
|
|
|
1 |
+
# app.py (Partial - showing key changes)
|
2 |
|
3 |
import os
|
4 |
import asyncio
|
5 |
import json
|
6 |
import logging
|
7 |
+
import sys
|
8 |
|
9 |
# --- Detailed Logging Setup ---
|
10 |
+
# ... (keep the detailed logging setup as before) ...
|
11 |
+
logger = logging.getLogger(__name__)
|
12 |
+
logging.getLogger('telethon').setLevel(logging.DEBUG)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
logger.debug("DEBUG: Importing Telethon...")
|
15 |
+
from telethon import TelegramClient, events # type: ignore
|
16 |
+
import telethon # For version checking
|
17 |
from telethon.tl import types # For DocumentAttributeFilename
|
18 |
+
logger.info(f"DEBUG: Using Telethon version: {telethon.__version__}") # Log the version
|
19 |
logger.debug("DEBUG: Telethon imported.")
|
20 |
|
21 |
+
# ... (rest of imports, env var loading) ...
|
|
|
|
|
22 |
|
23 |
+
# --- Session Name (CHANGE THIS OFTEN FOR DEBUGGING CONNECTION HANGS) ---
|
24 |
+
SESSION_VERSION = "v3_debug_run" # <<<<<<< CHANGE THIS TO A NEW VALUE
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
SESSION_NAME = f'session/image_bot_session_{SESSION_VERSION}'
|
26 |
logger.info(f"DEBUG: Using session name: {SESSION_NAME}")
|
27 |
|
|
|
33 |
logger.critical(f"CRITICAL ERROR: Failed to initialize TelegramClient: {e}", exc_info=True)
|
34 |
sys.exit(1)
|
35 |
|
36 |
+
# ... (rest of the script, including the main() function) ...
|
37 |
+
|
38 |
+
|
39 |
|
40 |
# --- Paths and Directory Setup ---
|
41 |
logger.info("DEBUG: Defining paths and ensuring directories...")
|