slim-S commited on
Commit
312cf15
·
1 Parent(s): c7a470b

fix: errors in the skip and validation features

Browse files
Files changed (1) hide show
  1. app.py +405 -314
app.py CHANGED
@@ -6,38 +6,31 @@ import asyncio
6
  import signal
7
  import traceback
8
  import stat
9
- from telegram import Update, ReplyKeyboardMarkup, ReplyKeyboardRemove
10
- from telegram.ext import (
11
- Application,
12
- CommandHandler,
13
- MessageHandler,
14
- filters,
15
- ContextTypes,
16
- ConversationHandler,
17
- )
18
  from typing import Dict, Set, Tuple, Optional
19
 
20
  # Enable more detailed logging
21
  logging.basicConfig(
22
  format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
23
- level=logging.INFO # Changed to INFO for production, DEBUG for verbose
24
  )
25
  logger = logging.getLogger(__name__)
26
 
27
  # Add specific logger for telegram
28
  telegram_logger = logging.getLogger('telegram')
29
- telegram_logger.setLevel(logging.INFO) # Changed to INFO
30
 
31
  # Define conversation states
32
- AWAITING_CORRECTION, AWAITING_CONFIRMATION = range(2)
 
33
 
34
- # Store active tasks per user (DEPRECATED in favor of context.user_data for conversation state)
35
- # user_tasks = {} # {user_id: (voice_filename, text_filename, seq_num)}
36
- # We will use context.user_data to store task-specific info like {'current_chunk_info': (voice, text, seq), 'proposed_correction': 'text'}
37
 
38
- # Track chunks being processed globally
39
- # set of sequence numbers that are currently being processed
40
- chunks_in_process: Set[int] = set()
41
 
42
  # Directory paths
43
  DATA_DIR = os.environ.get('DATA_DIR', 'data')
@@ -45,7 +38,6 @@ PENDING_DIR = os.path.join(DATA_DIR, 'pending')
45
  PENDING_VOICE_DIR = os.path.join(PENDING_DIR, 'voice')
46
  PENDING_TEXT_DIR = os.path.join(PENDING_DIR, 'text')
47
  PROCESSED_DIR = os.path.join(DATA_DIR, 'processed')
48
- # Although not used for saving, kept for structure
49
  PROCESSED_VOICE_DIR = os.path.join(PROCESSED_DIR, 'voice')
50
  PROCESSED_TEXT_DIR = os.path.join(PROCESSED_DIR, 'text')
51
 
@@ -53,18 +45,21 @@ PROCESSED_TEXT_DIR = os.path.join(PROCESSED_DIR, 'text')
53
 
54
 
55
  def ensure_directory_with_permissions(directory):
 
56
  try:
57
  if not os.path.exists(directory):
58
  os.makedirs(directory, exist_ok=True)
59
- os.chmod(directory, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
 
 
60
  logger.info(f"Directory ensured with permissions: {directory}")
61
  except Exception as e:
62
  logger.error(f"Error setting permissions for {directory}: {str(e)}")
63
 
64
 
 
65
  ensure_directory_with_permissions(PENDING_VOICE_DIR)
66
  ensure_directory_with_permissions(PENDING_TEXT_DIR)
67
- # Ensure it exists even if not saving voice files here
68
  ensure_directory_with_permissions(PROCESSED_VOICE_DIR)
69
  ensure_directory_with_permissions(PROCESSED_TEXT_DIR)
70
 
@@ -73,16 +68,19 @@ application = None
73
 
74
 
75
  def extract_sequence_number(filename: str) -> Optional[int]:
 
76
  match = re.search(r'(\d+)', filename)
77
  if match:
78
  return int(match.group(1))
79
  return None
80
 
81
 
82
- def get_files_by_sequence_number() -> Tuple[Dict[int, str], Dict[int, str]]:
83
- voice_files: Dict[int, str] = {}
84
- text_files: Dict[int, str] = {}
 
85
 
 
86
  if os.path.exists(PENDING_VOICE_DIR):
87
  for filename in os.listdir(PENDING_VOICE_DIR):
88
  if filename.endswith('.wav'):
@@ -90,353 +88,400 @@ def get_files_by_sequence_number() -> Tuple[Dict[int, str], Dict[int, str]]:
90
  if seq_num is not None:
91
  voice_files[seq_num] = filename
92
 
 
93
  if os.path.exists(PENDING_TEXT_DIR):
94
  for filename in os.listdir(PENDING_TEXT_DIR):
95
  if filename.endswith('.txt'):
96
  seq_num = extract_sequence_number(filename)
97
  if seq_num is not None:
98
  text_files[seq_num] = filename
 
99
  return voice_files, text_files
100
 
101
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
  async def get_next_available_chunk() -> Tuple[Optional[str], Optional[str], Optional[int]]:
 
 
103
  voice_files, text_files = get_files_by_sequence_number()
104
- common_seq_nums = set(voice_files.keys()).intersection(text_files.keys())
105
- available_seq_nums = sorted([
106
- seq_num for seq_num in common_seq_nums if seq_num not in chunks_in_process
107
- ])
 
 
 
 
 
108
  logger.debug(f"Available sequence numbers: {available_seq_nums}")
 
109
  if available_seq_nums:
110
- seq_num = available_seq_nums[0] # Get the smallest available
 
111
  return voice_files[seq_num], text_files[seq_num], seq_num
 
112
  logger.debug("No available chunks found")
113
  return None, None, None
114
 
115
 
116
- async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
117
- logger.info(f"Start command received from user {update.effective_user.id}")
118
- await update.message.reply_text(
119
- "Welcome to the Algerian Darija Transcription Correction Bot!\n\n"
120
- "Use /correct to start correcting a transcription.\n"
121
- "During correction, you can use /skip to get a new audio chunk.\n"
122
- "Use /cancel to stop the current correction task at any time."
123
- )
124
-
125
-
126
- async def send_new_chunk_to_user(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
127
- """Fetches a new chunk and sends it to the user."""
128
- user_id = update.effective_user.id
129
- voice_filename, text_filename, seq_num = await get_next_available_chunk()
130
-
131
- if not voice_filename or not text_filename or seq_num is None:
132
- await update.message.reply_text("No pending transcriptions available at the moment. Please try again later.", reply_markup=ReplyKeyboardRemove())
133
- return ConversationHandler.END
134
 
135
- chunks_in_process.add(seq_num)
136
- context.user_data['current_chunk_info'] = (
137
- voice_filename, text_filename, seq_num)
138
- # No need for user_tasks anymore if we manage state via ConversationHandler and user_data
 
139
 
140
- audio_path = os.path.join(PENDING_VOICE_DIR, voice_filename)
141
  text_path = os.path.join(PENDING_TEXT_DIR, text_filename)
142
-
143
- if not os.path.exists(audio_path) or not os.path.exists(text_path):
144
- logger.error(
145
- f"Audio or text file not found for seq {seq_num}. Audio: {audio_path}, Text: {text_path}")
146
- await update.message.reply_text("Error: Essential files for the chunk are missing. Skipping this chunk.", reply_markup=ReplyKeyboardRemove())
147
- chunks_in_process.remove(seq_num)
148
- if 'current_chunk_info' in context.user_data:
149
- del context.user_data['current_chunk_info']
150
- # Try to send another chunk
151
- return await send_new_chunk_to_user(update, context)
152
 
153
  try:
154
  with open(text_path, 'r', encoding='utf-8') as f:
155
  original_text = f.read().strip()
156
  except Exception as e:
157
- logger.error(f"Error reading text file {text_path}: {e}")
158
- await update.message.reply_text("Error reading transcription file. Please try again.", reply_markup=ReplyKeyboardRemove())
159
- chunks_in_process.remove(seq_num)
160
- if 'current_chunk_info' in context.user_data:
161
- del context.user_data['current_chunk_info']
162
- return ConversationHandler.END # Or try to send another
163
 
 
164
  try:
165
  with open(audio_path, 'rb') as audio_file:
166
  await update.message.reply_voice(voice=audio_file)
 
167
  await update.message.reply_text(
168
  f"Please listen to the audio and correct the transcription.\n\n"
169
  f"Original transcription:\n{original_text}\n\n"
170
- f"Send your corrected version. Use /skip if you can't correct this one, or /cancel to stop.",
171
- reply_markup=ReplyKeyboardRemove() # Remove any previous keyboards
172
  )
173
- logger.info(
174
- f"Successfully sent audio (seq: {seq_num}) and text to user {user_id}")
175
- return AWAITING_CORRECTION
176
  except Exception as e:
177
- logger.error(f"Error sending audio or text for seq {seq_num}: {e}")
178
- await update.message.reply_text("Error sending audio. Please try again.", reply_markup=ReplyKeyboardRemove())
179
- chunks_in_process.remove(seq_num)
180
- if 'current_chunk_info' in context.user_data:
181
- del context.user_data['current_chunk_info']
182
- return ConversationHandler.END
183
 
184
 
185
- async def correct_entry_point(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
186
- """Starts the correction process or informs user if already active."""
 
 
187
  user_id = update.effective_user.id
188
- logger.info(f"Correct command received from user {user_id}")
189
-
190
- # Check if user is already in a conversation (which means they have a task)
191
- # This check might be redundant if ConversationHandler handles re-entry properly,
192
- # but good for explicit control.
193
- if 'current_chunk_info' in context.user_data:
194
- # Resend current chunk info if they are stuck or re-entered
195
- voice_filename, text_filename, seq_num = context.user_data['current_chunk_info']
196
- audio_path = os.path.join(PENDING_VOICE_DIR, voice_filename)
197
- text_path = os.path.join(PENDING_TEXT_DIR, text_filename)
198
- try:
199
- with open(text_path, 'r', encoding='utf-8') as f:
200
- original_text = f.read().strip()
201
- with open(audio_path, 'rb') as audio_file:
202
- await update.message.reply_voice(voice=audio_file)
203
- await update.message.reply_text(
204
- f"You are currently working on this audio. Please provide the correction.\n\n"
205
- f"Original transcription:\n{original_text}\n\n"
206
- f"Use /skip or /cancel if needed.",
207
- reply_markup=ReplyKeyboardRemove()
208
- )
209
- return AWAITING_CORRECTION # Stay in or return to this state
210
- except Exception as e:
211
- logger.error(
212
- f"Error re-sending current chunk to user {user_id}: {e}")
213
- await update.message.reply_text("There was an error with your current task. Let's try getting a new one.", reply_markup=ReplyKeyboardRemove())
214
- # Clean up potentially corrupted current task
215
- if seq_num in chunks_in_process:
216
- chunks_in_process.remove(seq_num)
217
- if 'current_chunk_info' in context.user_data:
218
- del context.user_data['current_chunk_info']
219
- if 'proposed_correction' in context.user_data:
220
- del context.user_data['proposed_correction']
221
-
222
- # Ensure directories exist
223
  ensure_directory_with_permissions(PENDING_VOICE_DIR)
224
  ensure_directory_with_permissions(PENDING_TEXT_DIR)
225
 
226
- return await send_new_chunk_to_user(update, context)
 
 
 
227
 
 
 
 
 
 
 
228
 
229
- async def handle_correction_submission(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
230
- """Handles the user's submitted correction and asks for confirmation."""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
231
  user_id = update.effective_user.id
232
- if 'current_chunk_info' not in context.user_data:
233
- await update.message.reply_text("It seems your task expired or was cancelled. Please use /correct to start a new one.", reply_markup=ReplyKeyboardRemove())
 
234
  return ConversationHandler.END
235
 
 
236
  corrected_text = update.message.text
237
- context.user_data['proposed_correction'] = corrected_text
238
 
239
- logger.info(
240
- f"User {user_id} submitted correction: '{corrected_text[:50]}...'")
 
 
 
 
 
 
 
 
 
 
 
 
241
 
242
- reply_keyboard = [['Yes', 'No']]
243
  await update.message.reply_text(
244
- "Thank you for the correction. Are you confident with this submission?\n\n"
245
  f"Your correction:\n{corrected_text}\n\n"
246
- "Please reply with 'Yes' or 'No'.",
247
- reply_markup=ReplyKeyboardMarkup(
248
- reply_keyboard, one_time_keyboard=True, resize_keyboard=True),
249
  )
250
- return AWAITING_CONFIRMATION
251
 
 
252
 
253
- async def confirm_yes(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
254
- """User confirmed 'Yes'. Save the correction."""
255
- user_id = update.effective_user.id
256
- if 'current_chunk_info' not in context.user_data or 'proposed_correction' not in context.user_data:
257
- await update.message.reply_text("Something went wrong, your session might have expired. Use /correct to start again.", reply_markup=ReplyKeyboardRemove())
 
 
 
 
 
258
  return ConversationHandler.END
259
 
260
- voice_filename, text_filename, seq_num = context.user_data['current_chunk_info']
261
- corrected_text = context.user_data['proposed_correction']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
262
 
 
 
 
 
263
  original_txt_path = os.path.join(PENDING_TEXT_DIR, text_filename)
264
  original_wav_path = os.path.join(PENDING_VOICE_DIR, voice_filename)
265
 
 
266
  user_processed_text_dir = os.path.join(
267
  PROCESSED_TEXT_DIR, f"user_{user_id}")
268
- ensure_directory_with_permissions(user_processed_text_dir)
269
- processed_txt_path = os.path.join(user_processed_text_dir, text_filename)
 
270
 
271
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
272
  with open(processed_txt_path, 'w', encoding='utf-8') as f:
273
  f.write(corrected_text)
 
274
  os.chmod(processed_txt_path, stat.S_IRUSR | stat.S_IWUSR |
275
- stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH | stat.S_IWOTH) # 0666
 
 
 
 
276
 
 
277
  if os.path.exists(original_txt_path):
 
278
  os.remove(original_txt_path)
279
  else:
280
  logger.warning(
281
- f"Pending text file {original_txt_path} not found for removal.")
282
 
283
  if os.path.exists(original_wav_path):
 
284
  os.remove(original_wav_path)
285
  else:
286
  logger.warning(
287
- f"Pending voice file {original_wav_path} not found for removal.")
288
-
289
- logger.info(
290
- f"Correction for seq {seq_num} by user {user_id} saved to {processed_txt_path}")
291
- await update.message.reply_text(
292
- "Thank you! Your correction has been saved.\n"
293
- "Use /correct to receive another transcription task.",
294
- reply_markup=ReplyKeyboardRemove()
295
- )
296
- except Exception as e:
297
- logger.error(
298
- f"Error saving confirmed correction for seq {seq_num} by user {user_id}: {e}\n{traceback.format_exc()}")
299
- await update.message.reply_text(f"An error occurred while saving: {e}. Please try /correct again.", reply_markup=ReplyKeyboardRemove())
300
- finally:
301
- if seq_num in chunks_in_process:
302
- chunks_in_process.remove(seq_num)
303
- context.user_data.pop('current_chunk_info', None)
304
- context.user_data.pop('proposed_correction', None)
305
-
306
- return ConversationHandler.END
307
-
308
 
309
- async def confirm_no(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
310
- """User confirmed 'No'. Resend the same chunk."""
311
- user_id = update.effective_user.id
312
- if 'current_chunk_info' not in context.user_data:
313
- await update.message.reply_text("It seems your task expired. Please use /correct to start a new one.", reply_markup=ReplyKeyboardRemove())
314
- return ConversationHandler.END
315
-
316
- # seq_num is not strictly needed here but good for logging
317
- voice_filename, text_filename, seq_num = context.user_data['current_chunk_info']
318
- logger.info(
319
- f"User {user_id} is not confident with correction for seq {seq_num}. Re-sending chunk.")
320
 
321
- # Remove the previous proposal
322
- context.user_data.pop('proposed_correction', None)
323
 
324
- audio_path = os.path.join(PENDING_VOICE_DIR, voice_filename)
325
- text_path = os.path.join(PENDING_TEXT_DIR, text_filename)
326
-
327
- try:
328
- with open(text_path, 'r', encoding='utf-8') as f:
329
- original_text = f.read().strip()
330
- except Exception as e:
331
- logger.error(f"Error reading text file {text_path} on confirm_no: {e}")
332
- await update.message.reply_text("Error re-loading transcription file. Please try /correct for a new task.", reply_markup=ReplyKeyboardRemove())
333
- if seq_num in chunks_in_process:
334
- chunks_in_process.remove(seq_num)
335
- context.user_data.pop('current_chunk_info', None)
336
- return ConversationHandler.END
337
 
338
- try:
339
- with open(audio_path, 'rb') as audio_file:
340
- await update.message.reply_voice(voice=audio_file)
341
- await update.message.reply_text(
342
- "Okay, let's try that again. Please listen to the audio and provide your correction.\n\n"
343
- f"Original transcription:\n{original_text}\n\n"
344
- "Send your corrected version. Use /skip or /cancel if needed.",
345
- reply_markup=ReplyKeyboardRemove()
346
- )
347
- return AWAITING_CORRECTION
348
  except Exception as e:
349
- logger.error(
350
- f"Error re-sending audio/text for seq {seq_num} on confirm_no: {e}")
351
- await update.message.reply_text("Error re-sending audio. Please try /correct for a new task.", reply_markup=ReplyKeyboardRemove())
352
- if seq_num in chunks_in_process:
353
- chunks_in_process.remove(seq_num)
354
- context.user_data.pop('current_chunk_info', None)
355
- return ConversationHandler.END
356
-
357
-
358
- async def invalid_confirmation_reply(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
359
- """Handles invalid input during confirmation state."""
360
- reply_keyboard = [['Yes', 'No']]
361
- await update.message.reply_text(
362
- "Invalid response. Please use the buttons or type 'Yes' or 'No'.",
363
- reply_markup=ReplyKeyboardMarkup(
364
- reply_keyboard, one_time_keyboard=True, resize_keyboard=True),
365
- )
366
- return AWAITING_CONFIRMATION # Stay in this state
367
-
368
-
369
- async def skip_chunk(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
370
- """Skips the current chunk and tries to offer a new one."""
371
- user_id = update.effective_user.id
372
- if 'current_chunk_info' not in context.user_data:
373
- await update.message.reply_text("You don't have an active chunk to skip. Use /correct to get one.", reply_markup=ReplyKeyboardRemove())
374
- return ConversationHandler.END # Or whatever state is appropriate if not in conv
375
-
376
- _, _, seq_num = context.user_data['current_chunk_info']
377
-
378
- logger.info(f"User {user_id} initiated /skip for chunk seq {seq_num}.")
379
-
380
- if seq_num in chunks_in_process:
381
- chunks_in_process.remove(seq_num)
382
-
383
- # Clear data related to the skipped chunk
384
- context.user_data.pop('current_chunk_info', None)
385
- context.user_data.pop('proposed_correction', None)
386
-
387
- await update.message.reply_text("Chunk skipped. Let me fetch a new one for you...", reply_markup=ReplyKeyboardRemove())
388
 
389
- # Attempt to send a new chunk
390
- return await send_new_chunk_to_user(update, context)
391
 
392
 
393
  async def cancel(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
394
  """Cancel the current correction task."""
395
  user_id = update.effective_user.id
396
- logger.info(f"User {user_id} initiated /cancel.")
397
 
398
- if 'current_chunk_info' in context.user_data:
399
- _, _, seq_num = context.user_data['current_chunk_info']
400
- if seq_num in chunks_in_process:
401
- chunks_in_process.remove(seq_num)
402
- logger.info(f"Cancelled task for user {user_id}, seq {seq_num}.")
403
- else:
404
- logger.info(
405
- f"User {user_id} tried to cancel but had no active task in context.")
406
 
407
- context.user_data.clear() # Clear all user-specific data for this conversation
 
 
408
 
409
- await update.message.reply_text(
410
- "Task cancelled. Use /correct to start a new one.",
411
- reply_markup=ReplyKeyboardRemove()
412
- )
413
  return ConversationHandler.END
414
 
415
 
416
  async def status(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
 
417
  logger.info(
418
  f"Status command received from user {update.effective_user.id}")
419
- pending_voice_count = len([f for f in os.listdir(PENDING_VOICE_DIR) if f.endswith(
420
- '.wav')]) if os.path.exists(PENDING_VOICE_DIR) else 0
421
- pending_text_count = len([f for f in os.listdir(PENDING_TEXT_DIR) if f.endswith(
422
- '.txt')]) if os.path.exists(PENDING_TEXT_DIR) else 0
423
 
 
 
 
 
 
 
 
424
  voice_files, text_files = get_files_by_sequence_number()
425
  matching_pairs = len(
426
- set(voice_files.keys()).intersection(text_files.keys()))
427
 
 
428
  processed_text_count = 0
429
- if os.path.exists(PROCESSED_TEXT_DIR):
430
- for root, _, files in os.walk(PROCESSED_TEXT_DIR):
431
- processed_text_count += len(
432
- [f for f in files if f.endswith('.txt')])
433
 
434
- active_chunks_count = len(chunks_in_process)
 
 
 
 
435
 
 
436
  permissions_info = ""
437
  try:
 
438
  can_write_text = os.access(PROCESSED_TEXT_DIR, os.W_OK)
439
- permissions_info = f"\n• Write permissions to PROCESSED_TEXT_DIR: {'Yes' if can_write_text else 'No'}"
440
  except Exception as e:
441
  permissions_info = f"\n• Error checking permissions: {str(e)}"
442
 
@@ -444,105 +489,142 @@ async def status(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
444
  f"📊 Transcription Status:\n"
445
  f"• Pending voice files: {pending_voice_count}\n"
446
  f"• Pending text files: {pending_text_count}\n"
447
- f"• Matching pending pairs (available for processing): {matching_pairs - active_chunks_count}\n"
448
  f"• Processed text files: {processed_text_count}\n"
449
- f"• Chunks currently being processed by users: {active_chunks_count}{permissions_info}"
 
450
  )
451
 
452
 
453
  async def debug_info(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
454
- """Command to show debug information."""
455
- debug_messages = [
 
 
 
456
  "📁 Directory Check:",
457
  f"• DATA_DIR: {DATA_DIR}, exists: {os.path.exists(DATA_DIR)}",
 
458
  f"• PENDING_VOICE_DIR: {PENDING_VOICE_DIR}, exists: {os.path.exists(PENDING_VOICE_DIR)}",
459
  f"• PENDING_TEXT_DIR: {PENDING_TEXT_DIR}, exists: {os.path.exists(PENDING_TEXT_DIR)}",
 
460
  f"• PROCESSED_TEXT_DIR: {PROCESSED_TEXT_DIR}, exists: {os.path.exists(PROCESSED_TEXT_DIR)}",
461
- "\n🔑 Permission Check:",
462
- f"• PENDING_VOICE_DIR writable: {os.access(PENDING_VOICE_DIR, os.W_OK)}",
463
- f"• PENDING_TEXT_DIR writable: {os.access(PENDING_TEXT_DIR, os.W_OK)}",
464
- f"• PROCESSED_TEXT_DIR writable: {os.access(PROCESSED_TEXT_DIR, os.W_OK)}",
465
- "\n📋 Active Chunks:",
466
- f"• Chunks in process (seq_nums): {chunks_in_process}",
467
  ]
468
- await update.message.reply_text("\n".join(debug_messages))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
469
 
470
 
471
  async def test_handler(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
 
472
  logger.info(f"Test command received from user {update.effective_user.id}")
473
  await update.message.reply_text("Bot is working! ✅")
474
 
475
 
476
  async def setup_application():
 
477
  global application
 
 
478
  token = os.environ.get("TELEGRAM_TOKEN")
479
  if not token:
480
  logger.error("TELEGRAM_TOKEN environment variable not set!")
481
  return None
 
482
  logger.info(f"Setting up bot with token: {token[:10]}...")
 
 
483
  application = Application.builder().token(token).build()
484
 
 
 
 
 
485
  conv_handler = ConversationHandler(
486
- entry_points=[CommandHandler("correct", correct_entry_point)],
487
  states={
488
  AWAITING_CORRECTION: [
489
- CommandHandler("skip", skip_chunk), # Allow skip in this state
490
  MessageHandler(filters.TEXT & ~filters.COMMAND,
491
- handle_correction_submission),
492
- ],
493
- AWAITING_CONFIRMATION: [
494
- # Allow skip in this state too
495
  CommandHandler("skip", skip_chunk),
496
- MessageHandler(filters.Regex(
497
- r'^(Yes|نعم|Oui)$/i'), confirm_yes),
498
- MessageHandler(filters.Regex(r'^(No|لا|Non)$/i'), confirm_no),
499
- # Catchall for bad confirm
500
- MessageHandler(filters.TEXT & ~filters.COMMAND,
501
- invalid_confirmation_reply),
502
  ],
503
  },
504
- fallbacks=[CommandHandler("cancel", cancel), CommandHandler(
505
- "correct", correct_entry_point)], # correct as fallback to restart if stuck
 
 
506
  name="correction_conversation",
507
- # Set to True if you want to use PicklePersistence for states across bot restarts
508
- persistent=False,
509
- allow_reentry=True,
510
  )
511
 
512
  application.add_handler(conv_handler)
 
 
513
  application.add_handler(CommandHandler("start", start))
514
  application.add_handler(CommandHandler("status", status))
515
- application.add_handler(CommandHandler("debug", debug_info))
516
- application.add_handler(CommandHandler("test", test_handler))
517
- # A global cancel command if user is not in a conversation or fallback doesn't catch
518
  application.add_handler(CommandHandler("cancel", cancel))
 
 
 
519
 
520
  logger.info("All handlers added successfully")
521
  return application
522
 
523
 
524
  async def start_bot():
 
525
  global application
 
526
  logger.info("Starting bot setup...")
 
 
527
  app = await setup_application()
528
  if not app:
529
  logger.error("Failed to create application")
530
  return
531
 
532
  logger.info("Application created successfully")
 
 
533
  try:
534
  await app.initialize()
535
  logger.info("Application initialized")
 
536
  await app.start()
537
  logger.info("Application started")
538
- # Drop updates from when bot was offline
539
- await app.updater.start_polling(drop_pending_updates=True)
 
540
  logger.info("Started polling for updates")
 
541
  except Exception as e:
542
  logger.error(f"Error starting bot: {e}")
543
  return
544
 
 
545
  loop = asyncio.get_event_loop()
 
546
  for signal_name in ('SIGINT', 'SIGTERM'):
547
  try:
548
  loop.add_signal_handler(
@@ -550,41 +632,50 @@ async def start_bot():
550
  lambda s=signal_name: asyncio.create_task(shutdown(s))
551
  )
552
  except NotImplementedError:
553
- logger.info(
554
- f"Signal handler for {signal_name} not supported on this platform (likely Windows)")
 
555
 
556
  try:
 
557
  logger.info("Bot is now running and waiting for messages...")
558
- await asyncio.Event().wait() # Keep running indefinitely
559
  finally:
560
- await shutdown("Manual or loop end")
 
561
 
562
 
563
  async def shutdown(signal_type):
 
564
  global application
565
- logger.info(f"Received exit signal {signal_type}... Shutting down.")
566
- if application and application.updater and application.updater.running:
567
- logger.info("Stopping updater...")
568
- await application.updater.stop()
569
  if application:
570
  logger.info("Stopping application...")
571
- await application.stop() # Gracefully stops the application dispatcher
572
- logger.info("Shutting down application...")
573
- await application.shutdown() # Cleans up application resources
574
 
575
- # Cancel all other asyncio tasks
576
  tasks = [t for t in asyncio.all_tasks() if t is not asyncio.current_task()]
 
 
 
 
577
  if tasks:
578
- logger.info(f"Cancelling {len(tasks)} outstanding tasks...")
579
- for task in tasks:
580
- task.cancel()
581
  await asyncio.gather(*tasks, return_exceptions=True)
582
- logger.info("Outstanding tasks cancelled.")
583
- logger.info("Application shutdown complete.")
584
 
585
 
586
  async def main():
 
 
587
  await start_bot()
588
 
 
589
  if __name__ == "__main__":
590
  asyncio.run(main())
 
6
  import signal
7
  import traceback
8
  import stat
9
+ from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
10
+ from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes, ConversationHandler, CallbackQueryHandler
 
 
 
 
 
 
 
11
  from typing import Dict, Set, Tuple, Optional
12
 
13
  # Enable more detailed logging
14
  logging.basicConfig(
15
  format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
16
+ level=logging.DEBUG # Changed to DEBUG for more details
17
  )
18
  logger = logging.getLogger(__name__)
19
 
20
  # Add specific logger for telegram
21
  telegram_logger = logging.getLogger('telegram')
22
+ telegram_logger.setLevel(logging.DEBUG)
23
 
24
  # Define conversation states
25
+ AWAITING_CORRECTION = 1
26
+ AWAITING_VALIDATION = 2
27
 
28
+ # Store active tasks per user
29
+ # {user_id: (voice_filename, text_filename, seq_num, corrected_text)}
30
+ user_tasks = {}
31
 
32
+ # Track chunks being processed
33
+ chunks_in_process = set() # set of sequence numbers that are currently being processed
 
34
 
35
  # Directory paths
36
  DATA_DIR = os.environ.get('DATA_DIR', 'data')
 
38
  PENDING_VOICE_DIR = os.path.join(PENDING_DIR, 'voice')
39
  PENDING_TEXT_DIR = os.path.join(PENDING_DIR, 'text')
40
  PROCESSED_DIR = os.path.join(DATA_DIR, 'processed')
 
41
  PROCESSED_VOICE_DIR = os.path.join(PROCESSED_DIR, 'voice')
42
  PROCESSED_TEXT_DIR = os.path.join(PROCESSED_DIR, 'text')
43
 
 
45
 
46
 
47
  def ensure_directory_with_permissions(directory):
48
+ """Create directory if it doesn't exist and set permissions."""
49
  try:
50
  if not os.path.exists(directory):
51
  os.makedirs(directory, exist_ok=True)
52
+ # Set permissions: read/write/execute for everyone
53
+ os.chmod(directory, stat.S_IRWXU | stat.S_IRWXG |
54
+ stat.S_IRWXO) # 0777 permissions
55
  logger.info(f"Directory ensured with permissions: {directory}")
56
  except Exception as e:
57
  logger.error(f"Error setting permissions for {directory}: {str(e)}")
58
 
59
 
60
+ # Initialize directories with proper permissions
61
  ensure_directory_with_permissions(PENDING_VOICE_DIR)
62
  ensure_directory_with_permissions(PENDING_TEXT_DIR)
 
63
  ensure_directory_with_permissions(PROCESSED_VOICE_DIR)
64
  ensure_directory_with_permissions(PROCESSED_TEXT_DIR)
65
 
 
68
 
69
 
70
  def extract_sequence_number(filename: str) -> Optional[int]:
71
+ """Extract sequence number from filename, e.g., 'Sound 100.wav' -> 100."""
72
  match = re.search(r'(\d+)', filename)
73
  if match:
74
  return int(match.group(1))
75
  return None
76
 
77
 
78
+ def get_files_by_sequence_number():
79
+ """Create dictionaries mapping sequence numbers to filenames."""
80
+ voice_files = {}
81
+ text_files = {}
82
 
83
+ # Map voice files to sequence numbers
84
  if os.path.exists(PENDING_VOICE_DIR):
85
  for filename in os.listdir(PENDING_VOICE_DIR):
86
  if filename.endswith('.wav'):
 
88
  if seq_num is not None:
89
  voice_files[seq_num] = filename
90
 
91
+ # Map text files to sequence numbers
92
  if os.path.exists(PENDING_TEXT_DIR):
93
  for filename in os.listdir(PENDING_TEXT_DIR):
94
  if filename.endswith('.txt'):
95
  seq_num = extract_sequence_number(filename)
96
  if seq_num is not None:
97
  text_files[seq_num] = filename
98
+
99
  return voice_files, text_files
100
 
101
 
102
+ async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
103
+ """Send a welcome message when the /start command is issued."""
104
+ logger.info(f"Start command received from user {update.effective_user.id}")
105
+ try:
106
+ await update.message.reply_text(
107
+ "Welcome to the Algerian Darija Transcription Correction Bot!\n\n"
108
+ "Use /correct to start correcting a transcription.\n"
109
+ "Use /skip to skip the current chunk (if you have one).\n"
110
+ "Use /cancel to cancel the current correction task."
111
+ )
112
+ logger.info("Start message sent successfully")
113
+ except Exception as e:
114
+ logger.error(f"Error sending start message: {e}")
115
+
116
+
117
  async def get_next_available_chunk() -> Tuple[Optional[str], Optional[str], Optional[int]]:
118
+ """Find the next available audio chunk that isn't being processed.
119
+ Returns (voice_filename, text_filename, sequence_number) or (None, None, None)"""
120
  voice_files, text_files = get_files_by_sequence_number()
121
+
122
+ # Find sequence numbers that exist in both voice and text files
123
+ common_seq_nums = set(voice_files.keys()).intersection(
124
+ set(text_files.keys()))
125
+
126
+ # Filter out sequence numbers that are already being processed
127
+ available_seq_nums = [
128
+ seq_num for seq_num in common_seq_nums if seq_num not in chunks_in_process]
129
+
130
  logger.debug(f"Available sequence numbers: {available_seq_nums}")
131
+
132
  if available_seq_nums:
133
+ # Get the first available sequence number
134
+ seq_num = min(available_seq_nums)
135
  return voice_files[seq_num], text_files[seq_num], seq_num
136
+
137
  logger.debug("No available chunks found")
138
  return None, None, None
139
 
140
 
141
+ async def send_chunk_to_user(update: Update, user_id: int, voice_filename: str, text_filename: str, seq_num: int) -> bool:
142
+ """Send audio chunk and text to user. Returns True if successful, False otherwise."""
143
+ # Send audio file
144
+ audio_path = os.path.join(PENDING_VOICE_DIR, voice_filename)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
 
146
+ # Check if audio file exists
147
+ if not os.path.exists(audio_path):
148
+ logger.error(f"Audio file not found: {audio_path}")
149
+ await update.message.reply_text("Error: Audio file not found. Please try again.")
150
+ return False
151
 
152
+ # Read the original transcription to send as reference
153
  text_path = os.path.join(PENDING_TEXT_DIR, text_filename)
154
+ if not os.path.exists(text_path):
155
+ logger.error(f"Text file not found: {text_path}")
156
+ await update.message.reply_text("Error: Text file not found. Please try again.")
157
+ return False
 
 
 
 
 
 
158
 
159
  try:
160
  with open(text_path, 'r', encoding='utf-8') as f:
161
  original_text = f.read().strip()
162
  except Exception as e:
163
+ logger.error(f"Error reading text file: {e}")
164
+ await update.message.reply_text(f"Error reading transcription file. Please try again.")
165
+ return False
 
 
 
166
 
167
+ # Send audio and original text
168
  try:
169
  with open(audio_path, 'rb') as audio_file:
170
  await update.message.reply_voice(voice=audio_file)
171
+
172
  await update.message.reply_text(
173
  f"Please listen to the audio and correct the transcription.\n\n"
174
  f"Original transcription:\n{original_text}\n\n"
175
+ f"Please send your corrected version, or use /skip to skip this chunk."
 
176
  )
177
+
178
+ logger.info(f"Successfully sent audio and text to user {user_id}")
179
+ return True
180
  except Exception as e:
181
+ logger.error(f"Error sending audio or text: {e}")
182
+ await update.message.reply_text("Error sending audio. Please try again.")
183
+ return False
 
 
 
184
 
185
 
186
+ async def correct(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
187
+ """Start the correction process."""
188
+ logger.info(
189
+ f"Correct command received from user {update.effective_user.id}")
190
  user_id = update.effective_user.id
191
+
192
+ # Check if user already has an active task
193
+ if user_id in user_tasks:
194
+ await update.message.reply_text("You already have an active correction task. Please finish it or use /cancel.")
195
+ current_state = AWAITING_VALIDATION if len(
196
+ user_tasks[user_id]) > 3 else AWAITING_CORRECTION
197
+ return current_state
198
+
199
+ # Ensure directory paths exist with proper permissions
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
200
  ensure_directory_with_permissions(PENDING_VOICE_DIR)
201
  ensure_directory_with_permissions(PENDING_TEXT_DIR)
202
 
203
+ # Log directory contents for debugging
204
+ logger.debug(
205
+ f"PENDING_VOICE_DIR contents: {os.listdir(PENDING_VOICE_DIR)}")
206
+ logger.debug(f"PENDING_TEXT_DIR contents: {os.listdir(PENDING_TEXT_DIR)}")
207
 
208
+ # Get next available chunk
209
+ voice_filename, text_filename, seq_num = await get_next_available_chunk()
210
+
211
+ if not voice_filename or not text_filename:
212
+ await update.message.reply_text("No pending transcriptions available at the moment. Please try again later.")
213
+ return ConversationHandler.END
214
 
215
+ # Mark chunk as being processed
216
+ chunks_in_process.add(seq_num)
217
+ user_tasks[user_id] = (voice_filename, text_filename, seq_num)
218
+
219
+ # Send chunk to user
220
+ success = await send_chunk_to_user(update, user_id, voice_filename, text_filename, seq_num)
221
+
222
+ if not success:
223
+ # Clean up on failure
224
+ chunks_in_process.remove(seq_num)
225
+ del user_tasks[user_id]
226
+ return ConversationHandler.END
227
+
228
+ return AWAITING_CORRECTION
229
+
230
+
231
+ async def skip_chunk(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
232
+ """Skip the current chunk and get a new one."""
233
+ user_id = update.effective_user.id
234
+
235
+ if user_id not in user_tasks:
236
+ await update.message.reply_text("You don't have an active correction task. Use /correct to start one.")
237
+ return ConversationHandler.END
238
+
239
+ # Get current task info
240
+ current_task = user_tasks[user_id]
241
+ if len(current_task) >= 3: # Has at least voice, text, seq_num
242
+ _, _, current_seq_num = current_task[:3]
243
+
244
+ # Remove current chunk from processing (making it available for others)
245
+ chunks_in_process.discard(current_seq_num)
246
+ logger.info(f"User {user_id} skipped chunk {current_seq_num}")
247
+
248
+ # Remove current task
249
+ del user_tasks[user_id]
250
+
251
+ await update.message.reply_text("Chunk skipped! Getting you a new one...")
252
+
253
+ # Get next available chunk
254
+ voice_filename, text_filename, seq_num = await get_next_available_chunk()
255
+
256
+ if not voice_filename or not text_filename:
257
+ await update.message.reply_text("No more pending transcriptions available at the moment. Use /correct to try again later.")
258
+ return ConversationHandler.END
259
+
260
+ # Mark new chunk as being processed
261
+ chunks_in_process.add(seq_num)
262
+ user_tasks[user_id] = (voice_filename, text_filename, seq_num)
263
+
264
+ # Send new chunk to user
265
+ success = await send_chunk_to_user(update, user_id, voice_filename, text_filename, seq_num)
266
+
267
+ if not success:
268
+ # Clean up on failure
269
+ chunks_in_process.remove(seq_num)
270
+ del user_tasks[user_id]
271
+ return ConversationHandler.END
272
+
273
+ return AWAITING_CORRECTION
274
+
275
+
276
+ async def receive_correction(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
277
+ """Receive the user's correction and ask for validation."""
278
  user_id = update.effective_user.id
279
+
280
+ if user_id not in user_tasks:
281
+ await update.message.reply_text("You don't have an active correction task. Use /correct to start one.")
282
  return ConversationHandler.END
283
 
284
+ voice_filename, text_filename, seq_num = user_tasks[user_id]
285
  corrected_text = update.message.text
 
286
 
287
+ # Update user task with corrected text
288
+ user_tasks[user_id] = (voice_filename, text_filename,
289
+ seq_num, corrected_text)
290
+
291
+ # Create validation buttons
292
+ keyboard = [
293
+ [
294
+ InlineKeyboardButton("✅ Yes, I'm confident",
295
+ callback_data="validate_yes"),
296
+ InlineKeyboardButton("❌ No, let me correct again",
297
+ callback_data="validate_no")
298
+ ]
299
+ ]
300
+ reply_markup = InlineKeyboardMarkup(keyboard)
301
 
 
302
  await update.message.reply_text(
 
303
  f"Your correction:\n{corrected_text}\n\n"
304
+ f"Are you confident with this correction?",
305
+ reply_markup=reply_markup
 
306
  )
 
307
 
308
+ return AWAITING_VALIDATION
309
 
310
+
311
+ async def handle_validation(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
312
+ """Handle the validation response."""
313
+ query = update.callback_query
314
+ await query.answer()
315
+
316
+ user_id = query.from_user.id
317
+
318
+ if user_id not in user_tasks or len(user_tasks[user_id]) < 4:
319
+ await query.edit_message_text("You don't have an active correction task. Use /correct to start one.")
320
  return ConversationHandler.END
321
 
322
+ voice_filename, text_filename, seq_num, corrected_text = user_tasks[user_id]
323
+
324
+ if query.data == "validate_yes":
325
+ # User is confident - save the correction
326
+ success = await save_final_correction(query, user_id, voice_filename, text_filename, seq_num, corrected_text)
327
+ if success:
328
+ await query.edit_message_text(
329
+ "Thank you! Your correction has been saved.\n"
330
+ "Use /correct to receive another transcription task."
331
+ )
332
+ return ConversationHandler.END
333
+ else:
334
+ # If saving failed, go back to correction state
335
+ # Remove corrected text
336
+ user_tasks[user_id] = (voice_filename, text_filename, seq_num)
337
+ await query.edit_message_text(
338
+ "There was an error saving your correction. Please try correcting again."
339
+ )
340
+ return AWAITING_CORRECTION
341
+
342
+ elif query.data == "validate_no":
343
+ # User wants to correct again - remove corrected text and go back to correction state
344
+ user_tasks[user_id] = (voice_filename, text_filename, seq_num)
345
+ await query.edit_message_text(
346
+ "Please send your corrected transcription again."
347
+ )
348
+ return AWAITING_CORRECTION
349
+
350
+ return AWAITING_VALIDATION
351
 
352
+
353
+ async def save_final_correction(query, user_id: int, voice_filename: str, text_filename: str, seq_num: int, corrected_text: str) -> bool:
354
+ """Save the final validated correction."""
355
+ # Get original file paths
356
  original_txt_path = os.path.join(PENDING_TEXT_DIR, text_filename)
357
  original_wav_path = os.path.join(PENDING_VOICE_DIR, voice_filename)
358
 
359
+ # Create user-specific directory in processed TEXT folder only
360
  user_processed_text_dir = os.path.join(
361
  PROCESSED_TEXT_DIR, f"user_{user_id}")
362
+
363
+ # Log the directory path for debugging
364
+ logger.debug(f"Creating text directory: {user_processed_text_dir}")
365
 
366
  try:
367
+ # Ensure text directory with proper permissions
368
+ ensure_directory_with_permissions(user_processed_text_dir)
369
+
370
+ # Save corrected text to processed directory
371
+ processed_txt_path = os.path.join(
372
+ user_processed_text_dir, text_filename)
373
+
374
+ # Log file paths for debugging
375
+ logger.debug(
376
+ f"Original text path: {original_txt_path}, exists: {os.path.exists(original_txt_path)}")
377
+ logger.debug(
378
+ f"Original wav path: {original_wav_path}, exists: {os.path.exists(original_wav_path)}")
379
+ logger.debug(f"Processed text path: {processed_txt_path}")
380
+
381
+ # Save corrected text
382
+ logger.debug(f"Saving corrected text to {processed_txt_path}")
383
  with open(processed_txt_path, 'w', encoding='utf-8') as f:
384
  f.write(corrected_text)
385
+ # Set file permissions
386
  os.chmod(processed_txt_path, stat.S_IRUSR | stat.S_IWUSR |
387
+ stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH | stat.S_IWOTH)
388
+
389
+ # Verify processed text file exists
390
+ logger.debug(
391
+ f"Verifying processed text file exists: {os.path.exists(processed_txt_path)}")
392
 
393
+ # Remove both files from pending directory (text and voice)
394
  if os.path.exists(original_txt_path):
395
+ logger.debug(f"Removing original text file at {original_txt_path}")
396
  os.remove(original_txt_path)
397
  else:
398
  logger.warning(
399
+ f"Could not remove original text file as it doesn't exist at {original_txt_path}")
400
 
401
  if os.path.exists(original_wav_path):
402
+ logger.debug(f"Removing original wav file at {original_wav_path}")
403
  os.remove(original_wav_path)
404
  else:
405
  logger.warning(
406
+ f"Could not remove original wav file as it doesn't exist at {original_wav_path}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
407
 
408
+ # Remove from active tasks
409
+ logger.debug(f"Removing seq_num {seq_num} from chunks_in_process")
410
+ chunks_in_process.remove(seq_num)
 
 
 
 
 
 
 
 
411
 
412
+ logger.debug(f"Removing user {user_id} from user_tasks")
413
+ del user_tasks[user_id]
414
 
415
+ logger.info(
416
+ f"Successfully saved validated correction for user {user_id}")
417
+ return True
 
 
 
 
 
 
 
 
 
 
418
 
 
 
 
 
 
 
 
 
 
 
419
  except Exception as e:
420
+ # Enhanced error logging with stack trace
421
+ error_msg = f"Error saving correction: {str(e)}"
422
+ logger.error(error_msg)
423
+ logger.error(traceback.format_exc())
424
+
425
+ # Try to provide more specific error messages
426
+ if "Permission denied" in str(e):
427
+ await query.edit_message_text("Error: Permission denied while saving files. Please contact the administrator.")
428
+ elif "No such file or directory" in str(e):
429
+ await query.edit_message_text("Error: File not found. The system couldn't find one of the files.")
430
+ else:
431
+ await query.edit_message_text(f"Error saving your correction: {str(e)}. Please try again.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
432
 
433
+ return False
 
434
 
435
 
436
  async def cancel(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
437
  """Cancel the current correction task."""
438
  user_id = update.effective_user.id
 
439
 
440
+ if user_id in user_tasks:
441
+ task_info = user_tasks[user_id]
442
+ if len(task_info) >= 3: # Has at least voice, text, seq_num
443
+ seq_num = task_info[2]
444
+ chunks_in_process.discard(seq_num)
445
+ del user_tasks[user_id]
 
 
446
 
447
+ await update.message.reply_text("Task cancelled. Use /correct to start a new one.")
448
+ else:
449
+ await update.message.reply_text("You don't have an active task to cancel.")
450
 
 
 
 
 
451
  return ConversationHandler.END
452
 
453
 
454
  async def status(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
455
+ """Show status of pending and processed chunks."""
456
  logger.info(
457
  f"Status command received from user {update.effective_user.id}")
 
 
 
 
458
 
459
+ # Count pending files
460
+ pending_voice_count = len(
461
+ [f for f in os.listdir(PENDING_VOICE_DIR) if f.endswith('.wav')])
462
+ pending_text_count = len(
463
+ [f for f in os.listdir(PENDING_TEXT_DIR) if f.endswith('.txt')])
464
+
465
+ # Get sequence numbers
466
  voice_files, text_files = get_files_by_sequence_number()
467
  matching_pairs = len(
468
+ set(voice_files.keys()).intersection(set(text_files.keys())))
469
 
470
+ # Count processed files (only text files now)
471
  processed_text_count = 0
 
 
 
 
472
 
473
+ # Count text files
474
+ for root, dirs, files in os.walk(PROCESSED_TEXT_DIR):
475
+ processed_text_count += len([f for f in files if f.endswith('.txt')])
476
+
477
+ active_tasks = len(user_tasks)
478
 
479
+ # Check file permissions
480
  permissions_info = ""
481
  try:
482
+ # Check if we can write to the processed text directory
483
  can_write_text = os.access(PROCESSED_TEXT_DIR, os.W_OK)
484
+ permissions_info = f"\n• Write permissions: Text: {can_write_text}"
485
  except Exception as e:
486
  permissions_info = f"\n• Error checking permissions: {str(e)}"
487
 
 
489
  f"📊 Transcription Status:\n"
490
  f"• Pending voice files: {pending_voice_count}\n"
491
  f"• Pending text files: {pending_text_count}\n"
492
+ f"• Matching pending pairs: {matching_pairs}\n"
493
  f"• Processed text files: {processed_text_count}\n"
494
+ f"• Active tasks: {active_tasks}{permissions_info}\n"
495
+ f"• Note: Only corrected text files are saved, voice files are not copied to processed folder"
496
  )
497
 
498
 
499
  async def debug_info(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
500
+ """Command to show debug information about directories and permissions."""
501
+ user_id = update.effective_user.id
502
+
503
+ # Check directory structure
504
+ debug_info = [
505
  "📁 Directory Check:",
506
  f"• DATA_DIR: {DATA_DIR}, exists: {os.path.exists(DATA_DIR)}",
507
+ f"• PENDING_DIR: {PENDING_DIR}, exists: {os.path.exists(PENDING_DIR)}",
508
  f"• PENDING_VOICE_DIR: {PENDING_VOICE_DIR}, exists: {os.path.exists(PENDING_VOICE_DIR)}",
509
  f"• PENDING_TEXT_DIR: {PENDING_TEXT_DIR}, exists: {os.path.exists(PENDING_TEXT_DIR)}",
510
+ f"• PROCESSED_DIR: {PROCESSED_DIR}, exists: {os.path.exists(PROCESSED_DIR)}",
511
  f"• PROCESSED_TEXT_DIR: {PROCESSED_TEXT_DIR}, exists: {os.path.exists(PROCESSED_TEXT_DIR)}",
 
 
 
 
 
 
512
  ]
513
+
514
+ # Check permissions
515
+ try:
516
+ permission_info = [
517
+ "🔑 Permission Check:",
518
+ f"• PENDING_VOICE_DIR writable: {os.access(PENDING_VOICE_DIR, os.W_OK)}",
519
+ f"• PENDING_TEXT_DIR writable: {os.access(PENDING_TEXT_DIR, os.W_OK)}",
520
+ f"• PROCESSED_TEXT_DIR writable: {os.access(PROCESSED_TEXT_DIR, os.W_OK)}",
521
+ ]
522
+ debug_info.extend(permission_info)
523
+ except Exception as e:
524
+ debug_info.append(f"Error checking permissions: {str(e)}")
525
+
526
+ # Check active tasks
527
+ task_info = [
528
+ "📋 Active Tasks:",
529
+ f"• Number of active users: {len(user_tasks)}",
530
+ f"• Active chunks: {len(chunks_in_process)}",
531
+ ]
532
+ debug_info.extend(task_info)
533
+
534
+ await update.message.reply_text("\n".join(debug_info))
535
 
536
 
537
  async def test_handler(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
538
+ """Test handler to verify bot is receiving messages."""
539
  logger.info(f"Test command received from user {update.effective_user.id}")
540
  await update.message.reply_text("Bot is working! ✅")
541
 
542
 
543
  async def setup_application():
544
+ """Setup the Application with all handlers."""
545
  global application
546
+
547
+ # Get the token from environment variable
548
  token = os.environ.get("TELEGRAM_TOKEN")
549
  if not token:
550
  logger.error("TELEGRAM_TOKEN environment variable not set!")
551
  return None
552
+
553
  logger.info(f"Setting up bot with token: {token[:10]}...")
554
+
555
+ # Create the Application with additional debugging
556
  application = Application.builder().token(token).build()
557
 
558
+ # Add a test handler first
559
+ application.add_handler(CommandHandler("test", test_handler))
560
+
561
+ # Add conversation handler for correction workflow
562
  conv_handler = ConversationHandler(
563
+ entry_points=[CommandHandler("correct", correct)],
564
  states={
565
  AWAITING_CORRECTION: [
 
566
  MessageHandler(filters.TEXT & ~filters.COMMAND,
567
+ receive_correction),
 
 
 
568
  CommandHandler("skip", skip_chunk),
569
+ ],
570
+ AWAITING_VALIDATION: [
571
+ CallbackQueryHandler(handle_validation, pattern="^validate_"),
 
 
 
572
  ],
573
  },
574
+ fallbacks=[
575
+ CommandHandler("cancel", cancel),
576
+ CommandHandler("skip", skip_chunk),
577
+ ],
578
  name="correction_conversation",
 
 
 
579
  )
580
 
581
  application.add_handler(conv_handler)
582
+
583
+ # Add command handlers
584
  application.add_handler(CommandHandler("start", start))
585
  application.add_handler(CommandHandler("status", status))
 
 
 
586
  application.add_handler(CommandHandler("cancel", cancel))
587
+ application.add_handler(CommandHandler(
588
+ "skip", skip_chunk)) # Global skip handler
589
+ application.add_handler(CommandHandler("debug", debug_info))
590
 
591
  logger.info("All handlers added successfully")
592
  return application
593
 
594
 
595
  async def start_bot():
596
+ """Start the bot with proper signal handling."""
597
  global application
598
+
599
  logger.info("Starting bot setup...")
600
+
601
+ # Create and configure the bot
602
  app = await setup_application()
603
  if not app:
604
  logger.error("Failed to create application")
605
  return
606
 
607
  logger.info("Application created successfully")
608
+
609
+ # Start the Bot
610
  try:
611
  await app.initialize()
612
  logger.info("Application initialized")
613
+
614
  await app.start()
615
  logger.info("Application started")
616
+
617
+ # Start polling for updates
618
+ await app.updater.start_polling(drop_pending_updates=False)
619
  logger.info("Started polling for updates")
620
+
621
  except Exception as e:
622
  logger.error(f"Error starting bot: {e}")
623
  return
624
 
625
+ # Setup signal handlers for graceful shutdown
626
  loop = asyncio.get_event_loop()
627
+
628
  for signal_name in ('SIGINT', 'SIGTERM'):
629
  try:
630
  loop.add_signal_handler(
 
632
  lambda s=signal_name: asyncio.create_task(shutdown(s))
633
  )
634
  except NotImplementedError:
635
+ # Windows doesn't support this
636
+ logger.info("Signal handlers not supported on this platform")
637
+ pass
638
 
639
  try:
640
+ # Just run forever until interrupted
641
  logger.info("Bot is now running and waiting for messages...")
642
+ await asyncio.Event().wait()
643
  finally:
644
+ # Ensure the bot is properly shut down
645
+ await shutdown("Manual")
646
 
647
 
648
  async def shutdown(signal_type):
649
+ """Cleanup tasks tied to the service's shutdown."""
650
  global application
651
+
652
+ logger.info(f"Received exit signal {signal_type}...")
653
+
654
+ # Stop the bot
655
  if application:
656
  logger.info("Stopping application...")
657
+ await application.updater.stop()
658
+ await application.stop()
659
+ await application.shutdown()
660
 
661
+ # Cancel all running tasks
662
  tasks = [t for t in asyncio.all_tasks() if t is not asyncio.current_task()]
663
+ for task in tasks:
664
+ task.cancel()
665
+
666
+ # Wait for all tasks to be cancelled
667
  if tasks:
668
+ logger.info(f"Waiting for {len(tasks)} tasks to complete...")
 
 
669
  await asyncio.gather(*tasks, return_exceptions=True)
670
+
671
+ logger.info("Application shutdown complete")
672
 
673
 
674
  async def main():
675
+ """Main function to setup and run the bot."""
676
+ # Setup and start the bot
677
  await start_bot()
678
 
679
+ # This is the entry point when run as the main process
680
  if __name__ == "__main__":
681
  asyncio.run(main())