Skip to content

Commit

Permalink
v0.14.6 - fixed queue processing
Browse files Browse the repository at this point in the history
  • Loading branch information
FlyingFathead committed Jun 14, 2024
1 parent 20568ea commit bb3cde5
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 90 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ After launching the bot, you can interact with it via Telegram:
- `/language` - set the model's transcription language (`auto` = autodetect); if you know the language spoken in the audio, setting the transcription language manually with this command may improve both transcription speed and accuracy.

## Changes
- v0.14.6 - fixed occasional queue hangs with sent audio files (wav/mp3)
- v0.14.5 - fixed following the "keep/don't keep audio files" config rule
- v0.14.4 - added the `/info` command for viewing current settings & queue status
- v0.14.3 - Whisper model language selection via `/language` command
Expand Down
187 changes: 97 additions & 90 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# openai-whisper transcriber-bot for Telegram

# version of this program
version_number = "0.14.5"
version_number = "0.14.6"

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# https://github.com/FlyingFathead/whisper-transcriber-telegram-bot/
Expand Down Expand Up @@ -119,97 +119,104 @@ async def process_queue(self):
logger.info(f"Processing task for user ID {user_id}: {task}")

async with TranscriberBot.processing_lock: # Use the class-level lock
model = get_whisper_model(user_id)
language = get_whisper_language(user_id)

if language == "auto":
language = None

if isinstance(task, str) and task.startswith('http'):
logger.info(f"Processing URL: {task}")
await process_url_message(task, bot, update, model, language)
elif task.endswith('.wav') or task.endswith('.mp3'):
logger.info(f"Processing audio file: {task}")

# Notify the user about the model and GPU
if language:
await bot.send_message(chat_id=update.effective_chat.id, text=f"Starting transcription with model: {model} and language: {language}")
else:
await bot.send_message(chat_id=update.effective_chat.id, text=f"Starting transcription with model: {model} and language autodetection")

best_gpu = get_best_gpu()
if best_gpu:
device = f'cuda:{best_gpu.id}'
gpu_message = (
f"Using GPU {best_gpu.id}: {best_gpu.name}\n"
f"Free Memory: {best_gpu.memoryFree} MB\n"
f"Load: {best_gpu.load * 100:.1f}%"
try:
model = get_whisper_model(user_id)
language = get_whisper_language(user_id)

if language == "auto":
language = None

if isinstance(task, str) and task.startswith('http'):
logger.info(f"Processing URL: {task}")
await process_url_message(task, bot, update, model, language)
elif task.endswith('.wav') or task.endswith('.mp3'):
logger.info(f"Processing audio file: {task}")

# # Notify the user about the model and GPU
# if language:
# await bot.send_message(chat_id=update.effective_chat.id, text=f"Starting transcription with model: {model} and language: {language}")
# else:
# await bot.send_message(chat_id=update.effective_chat.id, text=f"Starting transcription with model: {model} and language autodetection")

best_gpu = get_best_gpu()
if best_gpu:
device = f'cuda:{best_gpu.id}'
gpu_message = (
f"Using GPU {best_gpu.id}: {best_gpu.name}\n"
f"Free Memory: {best_gpu.memoryFree} MB\n"
f"Load: {best_gpu.load * 100:.1f}%"
)
else:
device = 'cpu'
gpu_message = "No GPU available, using CPU for transcription."

# Log and send the GPU information to the user
logger.info(gpu_message)
await bot.send_message(chat_id=update.effective_chat.id, text=gpu_message)

# Inform the user about the estimated time for transcription
audio_duration = get_audio_duration(task)
if audio_duration is None:
await bot.send_message(chat_id=update.effective_chat.id, text="Invalid audio file. Please upload or link to a valid audio file.")
if os.path.exists(task):
os.remove(task)
continue

estimated_time = estimate_transcription_time(model, audio_duration)
estimated_minutes = estimated_time / 60 # Convert to minutes for user-friendly display

# Calculate estimated finish time
current_time = datetime.now()
estimated_finish_time = current_time + timedelta(seconds=estimated_time)

# Format messages for start and estimated finish time
time_now_str = current_time.strftime('%Y-%m-%d %H:%M:%S')
estimated_finish_time_str = estimated_finish_time.strftime('%Y-%m-%d %H:%M:%S')

formatted_audio_duration = format_duration(audio_duration)
language_setting = language if language else "autodetection"
detailed_message = (
f"Audio file length:\n{formatted_audio_duration}\n\n"
f"Whisper model in use:\n{model}\n\n"
f"Model language set to:\n{language_setting}\n\n"
f"Estimated transcription time:\n{estimated_minutes:.1f} minutes.\n\n"
f"Time now:\n{time_now_str}\n\n"
f"Time when finished (estimate):\n{estimated_finish_time_str}\n\n"
"Transcribing audio..."
)
else:
device = 'cpu'
gpu_message = "No GPU available, using CPU for transcription."

# Log and send the GPU information to the user
logger.info(gpu_message)
await bot.send_message(chat_id=update.effective_chat.id, text=gpu_message)

# Inform the user about the estimated time for transcription
audio_duration = get_audio_duration(task)
if audio_duration is None:
await bot.send_message(chat_id=update.effective_chat.id, text="Invalid audio file. Please upload or link to a valid audio file.")
if os.path.exists(task):
os.remove(task)
continue

estimated_time = estimate_transcription_time(model, audio_duration)
estimated_minutes = estimated_time / 60 # Convert to minutes for user-friendly display

# Calculate estimated finish time
current_time = datetime.now()
estimated_finish_time = current_time + timedelta(seconds=estimated_time)

# Format messages for start and estimated finish time
time_now_str = current_time.strftime('%Y-%m-%d %H:%M:%S')
estimated_finish_time_str = estimated_finish_time.strftime('%Y-%m-%d %H:%M:%S')

formatted_audio_duration = format_duration(audio_duration)
language_setting = language if language else "autodetection"
detailed_message = (
f"Audio file length:\n{formatted_audio_duration}\n\n"
f"Whisper model in use:\n{model}\n\n"
f"Model language set to:\n{language_setting}\n\n"
f"Estimated transcription time:\n{estimated_minutes:.1f} minutes.\n\n"
f"Time now:\n{time_now_str}\n\n"
f"Time when finished (estimate):\n{estimated_finish_time_str}\n\n"
"Transcribing audio..."
)
logger.info(detailed_message)
await bot.send_message(chat_id=update.effective_chat.id, text=detailed_message)

# Now start the transcription process
transcription_paths = await transcribe_audio(task, self.output_dir, "", "", self.config.getboolean('TranscriptionSettings', 'includeheaderintranscription'), model, device, language)
if not transcription_paths:
# Notify if transcription fails
await bot.send_message(chat_id=update.effective_chat.id, text="Failed to transcribe audio.")
if os.path.exists(task):
os.remove(task)
continue

# Send transcription files and finalize the process
for fmt, path in transcription_paths.items():
try:
await bot.send_document(chat_id=update.effective_chat.id, document=open(path, 'rb'))
logger.info(f"Sent {fmt} file to user {user_id}: {path}")
except Exception as e:
logger.error(f"Failed to send {fmt} file to user {user_id}: {path}, error: {e}")

if not self.config.getboolean('TranscriptionSettings', 'keepaudiofiles'):
os.remove(task)

# Send the "Have a nice day!" message
logger.info(detailed_message)
await bot.send_message(chat_id=update.effective_chat.id, text=detailed_message)

# Now start the transcription process
transcription_paths = await transcribe_audio(task, self.output_dir, "", "", self.config.getboolean('TranscriptionSettings', 'includeheaderintranscription'), model, device, language)
if not transcription_paths:
# Notify if transcription fails
await bot.send_message(chat_id=update.effective_chat.id, text="Failed to transcribe audio.")
if os.path.exists(task):
os.remove(task)
continue

# Send transcription files and finalize the process
for fmt, path in transcription_paths.items():
try:
await bot.send_document(chat_id=update.effective_chat.id, document=open(path, 'rb'))
logger.info(f"Sent {fmt} file to user {user_id}: {path}")
except Exception as e:
logger.error(f"Failed to send {fmt} file to user {user_id}: {path}, error: {e}")

if not self.config.getboolean('TranscriptionSettings', 'keepaudiofiles'):
try:
os.remove(task)
logger.info(f"Deleted audio file: {task}")
except Exception as e:
logger.error(f"Failed to delete audio file {task}: {e}")

except Exception as e:
logger.error(f"An error occurred while processing the task: {e}")
finally:
self.task_queue.task_done()
await bot.send_message(chat_id=update.effective_chat.id, text="Transcription complete. Have a nice day!")
self.task_queue.task_done()
logger.info(f"Task completed for user ID {user_id}: {task}")
logger.info(f"Task completed for user ID {user_id}: {task}")

async def shutdown(self, signal, loop):
"""Cleanup tasks tied to the service's shutdown."""
Expand Down

0 comments on commit bb3cde5

Please sign in to comment.