Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,6 @@ from telethon.errors import SessionPasswordNeededError, PhoneCodeInvalidError, A
|
|
| 6 |
from huggingface_hub import upload_file
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
from flask import Flask, request, render_template, jsonify
|
| 9 |
-
import threading
|
| 10 |
|
| 11 |
# === Load secrets from .env ===
|
| 12 |
load_dotenv()
|
|
@@ -16,7 +15,7 @@ API_HASH = os.getenv("API_HASH")
|
|
| 16 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 17 |
CHANNEL = os.getenv("CHANNEL_USERNAME")
|
| 18 |
REPO_ID = os.getenv("DATASET_REPO")
|
| 19 |
-
DATA_PATH = "
|
| 20 |
|
| 21 |
# === Logging setup ===
|
| 22 |
logging.basicConfig(
|
|
@@ -73,17 +72,12 @@ async def process_filenames(name_input):
|
|
| 73 |
await client.connect()
|
| 74 |
|
| 75 |
if not await client.is_user_authorized():
|
| 76 |
-
# Attempt to re-authorize if not authorized
|
| 77 |
-
# This part needs user interaction, which is not possible in this setup.
|
| 78 |
-
# For a web app, you'd redirect to a login page.
|
| 79 |
-
# For now, we'll just return an error.
|
| 80 |
return "β Error: Telegram client not authorized. Please ensure your session is valid and authenticated."
|
| 81 |
|
| 82 |
filenames = [name.strip().lower() for name in name_input.replace(",", "\n").splitlines() if name.strip()]
|
| 83 |
results = []
|
| 84 |
found = set()
|
| 85 |
|
| 86 |
-
# Use a more conservative approach to message iteration
|
| 87 |
try:
|
| 88 |
messages = []
|
| 89 |
async for msg in client.iter_messages(CHANNEL, limit=30000):
|
|
@@ -133,10 +127,6 @@ async def process_filenames(name_input):
|
|
| 133 |
logging.error(f"Error in process_filenames: {e}")
|
| 134 |
return f"β Error: {str(e)}"
|
| 135 |
|
| 136 |
-
# === Global event loop for the Telegram client ===
|
| 137 |
-
# This ensures the client uses a single, persistent event loop.
|
| 138 |
-
loop = asyncio.get_event_loop()
|
| 139 |
-
|
| 140 |
# === Flask App with explicit template and static folder paths ===
|
| 141 |
# Get the directory where this script is located
|
| 142 |
basedir = os.path.abspath(os.path.dirname(__file__))
|
|
@@ -150,18 +140,17 @@ def index():
|
|
| 150 |
return render_template('index.html')
|
| 151 |
|
| 152 |
@app.route('/upload', methods=['POST'])
|
| 153 |
-
def upload():
|
| 154 |
try:
|
| 155 |
filenames_input = request.form.get('filenames', '').strip()
|
| 156 |
if not filenames_input:
|
| 157 |
return "β Error: No filenames provided", 400
|
| 158 |
|
| 159 |
-
# Check if credentials are configured
|
| 160 |
if not client:
|
| 161 |
return "β Error: Application not configured. Please set up your environment variables with API credentials.", 500
|
| 162 |
|
| 163 |
-
#
|
| 164 |
-
results =
|
| 165 |
return results
|
| 166 |
|
| 167 |
except Exception as e:
|
|
@@ -226,7 +215,6 @@ def session_info():
|
|
| 226 |
return jsonify({"error": "Client not initialized"})
|
| 227 |
|
| 228 |
try:
|
| 229 |
-
# This is a synchronous check
|
| 230 |
session_status = {
|
| 231 |
"session_file_exists": os.path.exists('my_session.session'),
|
| 232 |
"client_initialized": bool(client),
|
|
|
|
| 6 |
from huggingface_hub import upload_file
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
from flask import Flask, request, render_template, jsonify
|
|
|
|
| 9 |
|
| 10 |
# === Load secrets from .env ===
|
| 11 |
load_dotenv()
|
|
|
|
| 15 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 16 |
CHANNEL = os.getenv("CHANNEL_USERNAME")
|
| 17 |
REPO_ID = os.getenv("DATASET_REPO")
|
| 18 |
+
DATA_PATH = "telegram_uploads"
|
| 19 |
|
| 20 |
# === Logging setup ===
|
| 21 |
logging.basicConfig(
|
|
|
|
| 72 |
await client.connect()
|
| 73 |
|
| 74 |
if not await client.is_user_authorized():
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
return "β Error: Telegram client not authorized. Please ensure your session is valid and authenticated."
|
| 76 |
|
| 77 |
filenames = [name.strip().lower() for name in name_input.replace(",", "\n").splitlines() if name.strip()]
|
| 78 |
results = []
|
| 79 |
found = set()
|
| 80 |
|
|
|
|
| 81 |
try:
|
| 82 |
messages = []
|
| 83 |
async for msg in client.iter_messages(CHANNEL, limit=30000):
|
|
|
|
| 127 |
logging.error(f"Error in process_filenames: {e}")
|
| 128 |
return f"β Error: {str(e)}"
|
| 129 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
# === Flask App with explicit template and static folder paths ===
|
| 131 |
# Get the directory where this script is located
|
| 132 |
basedir = os.path.abspath(os.path.dirname(__file__))
|
|
|
|
| 140 |
return render_template('index.html')
|
| 141 |
|
| 142 |
@app.route('/upload', methods=['POST'])
|
| 143 |
+
async def upload(): # Mark the route as async
|
| 144 |
try:
|
| 145 |
filenames_input = request.form.get('filenames', '').strip()
|
| 146 |
if not filenames_input:
|
| 147 |
return "β Error: No filenames provided", 400
|
| 148 |
|
|
|
|
| 149 |
if not client:
|
| 150 |
return "β Error: Application not configured. Please set up your environment variables with API credentials.", 500
|
| 151 |
|
| 152 |
+
# Await the async function directly
|
| 153 |
+
results = await process_filenames(filenames_input)
|
| 154 |
return results
|
| 155 |
|
| 156 |
except Exception as e:
|
|
|
|
| 215 |
return jsonify({"error": "Client not initialized"})
|
| 216 |
|
| 217 |
try:
|
|
|
|
| 218 |
session_status = {
|
| 219 |
"session_file_exists": os.path.exists('my_session.session'),
|
| 220 |
"client_initialized": bool(client),
|