Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds script to generate audio from a screenplay script #152

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,6 @@ tags
.ruff_cache

wandb
myenv
scripts/*
output/*
57 changes: 57 additions & 0 deletions generate_audio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import torch
from parler_tts import ParlerTTSForConditionalGeneration
from transformers import AutoTokenizer
import soundfile as sf
import os
import sys

# device = "mps" if torch.backends.mps.is_available() else torch.device("cpu")
device = "cuda" if torch.cuda.is_available() else "cpu"

# Load the model and tokenizer
model = ParlerTTSForConditionalGeneration.from_pretrained("parler-tts/parler-tts-mini-v1").to(device)
tokenizer = AutoTokenizer.from_pretrained("parler-tts/parler-tts-mini-v1")

# Function to load a script from the 'scripts' folder
def load_script(file_name):
script_path = os.path.join("scripts", file_name)
with open(script_path, "r") as file:
script = file.read().strip()
return script

# Get script name from command line argument
if len(sys.argv) < 2:
print("Please provide a script file name.")
sys.exit(1)

script_file = sys.argv[1] # First argument from the command line, e.g., 'your_script.txt'
script_name = os.path.splitext(script_file)[0] # Remove .txt extension

# Create a folder with the script name (if it doesn't exist)
output_folder = os.path.join("output", script_name)
os.makedirs(output_folder, exist_ok=True)

# Load the script
prompt = load_script(script_file)

# Define the description for the desired voice
description = ("A soothing, mesmerizing, and mystical Indian female voice with soft tones, "
"gentle delivery, and a captivating presence. The voice should evoke calmness "
"and wonder, perfect for spiritual or ethereal stories. High quality and clear.")

# Convert the description and prompt to input IDs
input_ids = tokenizer(description, return_tensors="pt").input_ids.to(device)
prompt_input_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(device)

# Create an attention mask
attention_mask = torch.ones(input_ids.shape, device=device)

# Generate the speech
generation = model.generate(input_ids=input_ids, prompt_input_ids=prompt_input_ids, attention_mask=attention_mask)
audio_arr = generation.cpu().numpy().squeeze()

# Save the output as a .wav file in the corresponding folder
output_file = os.path.join(output_folder, f"{script_name}.wav")
sf.write(output_file, audio_arr, model.config.sampling_rate)

print(f"Audio generated and saved as {output_file}")
15 changes: 15 additions & 0 deletions scripts/law_of_karma.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Once upon a time, in a peaceful village nestled between the mountains, there lived a wise old teacher. She often spoke about a powerful force that guided everything in life—this force was called ‘Karma.’

Karma, she explained, is like a mirror of our actions. Everything we do—whether it’s a kind gesture or an unkind one—comes back to us, like ripples in a pond. If we plant good seeds, we grow beautiful flowers. But if we plant bad seeds, we get weeds.

Imagine you help someone by giving them food when they’re hungry. That kindness is like planting a good seed, and soon, good things start happening to you. Maybe someone helps you when you need it most, or you just feel happier inside.

On the other hand, if you’re mean to someone or hurt them, that’s like planting a bad seed. Over time, those bad actions can grow into problems that come back into your life, causing stress or unhappiness.

The law of Karma teaches us that we are responsible for what we put into the world. Every action has a reaction—like a boomerang, it comes back to us.

But here’s the good news! Karma also gives us a chance to learn and grow. If we’ve made mistakes, we can choose to act with kindness, compassion, and love from today. By planting more good seeds, we can create a life full of joy and peace.

So remember, every small action matters. When we live with good intentions and help others, we make the world—and our own lives—a little brighter.

That is the simple beauty of the Law of Karma.