The issue you're encountering is that while you can clear the GPU memory using torch.cuda.empty_cache(), system RAM can still be occupied by Python objects, including intermediate results, data within the model, and various buffer allocations.
Here are a few ways to ensure that the system RAM is freed up after the transcribing task is done:
Step-by-Step Solution
-
Explicit Deletions: Ensure that you delete all large objects, including the Whisper model and any intermediate data.
-
Garbage Collection: Use Python's built-in garbage collection mechanism to free up memory.
-
Process Isolation: Encapsulate your task in a separate function and use machine learning model-inference libraries or subprocesses to ensure complete memory cleanup.
Here's a combination of methods packaged into a function:
import whisper
import os
import gc
import torch
from google.colab import files
def transcribe_and_save(audio_path, model_path="large-v2", prompt="Please transscribe the following content in Hungarian:"):
# Load model
model = whisper.load_model(model_path)
# Transcribe audio
result = model.transcribe(audio_path, prompt=prompt)
# Print result
print(result["text"])
# Get filename without extension
file_name = os.path.splitext(os.path.basename(audio_path))[0]
# Save result to a txt file and download
with open(f"{file_name}.txt", "w") as file:
file.write(result["text"])
files.download(f"{file_name}.txt")
# Cleanup
del model
gc.collect()
torch.cuda.empty_cache()
# Use the function
audio_path = "/content/asd2.mp3"
transcribe_and_save(audio_path)
To make sure that the memory is entirely released, you can use the subprocess module to run your transcription as an isolated process:
import subprocess
import os
def transcribe_as_subprocess(script_name):
# Run a separate process to execute the transcription.
subprocess.run(["python", script_name], check=True)
# Create a Python script with the transcription code
script_content = """
import whisper
import os
import gc
import torch
from google.colab import files
def transcribe_and_save(audio_path, model_path="large-v2", prompt="Please transscribe the following content in Hungarian:"):
# Load model
model = whisper.load_model(model_path)
# Transcribe audio
result = model.transcribe(audio_path, prompt=prompt)
# Print result
print(result["text"])
# Get filename without extension
file_name = os.path.splitext(os.path.basename(audio_path))[0]
# Save result to a txt file and download
with open(f"{file_name}.txt", "w") as file:
file.write(result["text"])
files.download(f"{file_name}.txt")
# Cleanup
del model
gc.collect()
torch.cuda.empty_cache()
# Use the function
audio_path = "/content/asd2.mp3"
transcribe_and_save(audio_path)
"""
# Write the script content to a file
script_name = "transcribe_script.py"
with open(script_name, "w") as f:
f.write(script_content)
# Execute the script in a new process
transcribe_as_subprocess(script_name)
# Optionally, delete the script after execution if needed
os.remove(script_name)
Summary
- Encase model loading, transcribing, and saving into a function to localize variable scope.
- Use garbage collection and explicit
del operations to clear memory within the function.
- Use subprocesses for complete memory isolation and ensure any residual memory usage is freed when the subprocess exits.