Skip to content

Commit

Permalink
Added seed to node
Browse files Browse the repository at this point in the history
  • Loading branch information
niknah committed Nov 7, 2024
1 parent 62e4d7b commit 88d4ca8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
37 changes: 28 additions & 9 deletions F5TTS.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from .Install import Install
import subprocess
import wave
import torch
import torchaudio
import hashlib
import folder_paths
Expand All @@ -27,6 +28,7 @@

class F5TTSCreate:
voice_reg = re.compile(r"\{(\w+)\}")
tooltip_seed = "Seed. -1 = random"

def is_voice_name(self, word):
return self.voice_reg.match(word.strip())
Expand Down Expand Up @@ -68,7 +70,12 @@ def load_model(self):
ema_model = load_model(model_cls, model_cfg, ckpt_file, vocab_file)
return ema_model

def generate_audio(self, voices, model_obj, chunks):
def generate_audio(self, voices, model_obj, chunks, seed):
if seed >= 0:
torch.manual_seed(seed)
else:
torch.random.seed()

frame_rate = 44100
generated_audio_segments = []
pbar = ProgressBar(len(chunks))
Expand Down Expand Up @@ -110,9 +117,9 @@ def generate_audio(self, voices, model_obj, chunks):
os.unlink(wave_file.name)
return audio

def create(self, voices, chunks):
def create(self, voices, chunks, seed=-1):
model_obj = self.load_model()
return self.generate_audio(voices, model_obj, chunks)
return self.generate_audio(voices, model_obj, chunks, seed)


class F5TTSAudioInputs:
Expand All @@ -129,6 +136,11 @@ def INPUT_TYPES(s):
"multiline": True,
"default": "This is what I want to say"
}),
"seed": ("INT", {
"display": "number", "step": 1,
"default": 1, "min": -1,
"tooltip": F5TTSCreate.tooltip_seed,
}),
},
}

Expand Down Expand Up @@ -162,7 +174,7 @@ def remove_wave_file(self):
print("F5TTS: Cannot remove? "+self.wave_file.name)
print(e)

def create(self, sample_audio, sample_text, speech):
def create(self, sample_audio, sample_text, speech, seed=-1):
try:
main_voice = self.load_voice_from_input(sample_audio, sample_text)

Expand All @@ -172,17 +184,18 @@ def create(self, sample_audio, sample_text, speech):
chunks = f5ttsCreate.split_text(speech)
voices['main'] = main_voice

audio = f5ttsCreate.create(voices, chunks)
audio = f5ttsCreate.create(voices, chunks, seed)
finally:
self.remove_wave_file()
return (audio, )

@classmethod
def IS_CHANGED(s, sample_audio, sample_text, speech):
def IS_CHANGED(s, sample_audio, sample_text, speech, seed):
m = hashlib.sha256()
m.update(sample_text)
m.update(sample_audio)
m.update(speech)
m.update(seed)
return m.digest().hex()


Expand Down Expand Up @@ -215,6 +228,11 @@ def INPUT_TYPES(s):
"multiline": True,
"default": "This is what I want to say"
}),
"seed": ("INT", {
"display": "number", "step": 1,
"default": 1, "min": -1,
"tooltip": F5TTSCreate.tooltip_seed,
}),
}
}

Expand Down Expand Up @@ -271,7 +289,7 @@ def load_voices_from_files(self, sample, voice_names):
voices[voice_name] = self.load_voice_from_file(sample_file)
return voices

def create(self, sample, speech):
def create(self, sample, speech, seed=-1):
# Install.check_install()
main_voice = self.load_voice_from_file(sample)

Expand All @@ -291,11 +309,11 @@ def create(self, sample, speech):
voices = self.load_voices_from_files(sample, voice_names)
voices['main'] = main_voice

audio = f5ttsCreate.create(voices, chunks)
audio = f5ttsCreate.create(voices, chunks, seed)
return (audio, )

@classmethod
def IS_CHANGED(s, sample, speech):
def IS_CHANGED(s, sample, speech, seed):
m = hashlib.sha256()
audio_path = folder_paths.get_annotated_filepath(sample)
audio_txt_path = F5TTSAudio.get_txt_file_path(audio_path)
Expand All @@ -305,4 +323,5 @@ def IS_CHANGED(s, sample, speech):
m.update(str(last_modified_timestamp))
m.update(str(txt_last_modified_timestamp))
m.update(speech)
m.update(seed)
return m.digest().hex()
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[project]
name = "comfyui-f5-tts"
description = "Text to speech with F5-TTS"
version = "1.0.3"
version = "1.0.4"
license = {text = "MIT License"}

[project.urls]
Expand Down

0 comments on commit 88d4ca8

Please sign in to comment.