Skip to content
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
13 changes: 8 additions & 5 deletions ChatTTS/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,15 @@ def download_models(
) -> Optional[str]:
if source == "local":
download_path = os.getcwd()
if not check_all_assets(Path(download_path), self.sha256_map, update=True) or force_redownload:
if (
not check_all_assets(Path(download_path), self.sha256_map, update=True)
or force_redownload
):
with tempfile.TemporaryDirectory() as tmp:
download_all_assets(tmpdir=tmp)
if not check_all_assets(Path(download_path), self.sha256_map, update=False):
if not check_all_assets(
Path(download_path), self.sha256_map, update=False
):
self.logger.error(
"download to local path %s failed.", download_path
)
Expand Down Expand Up @@ -107,9 +112,7 @@ def download_models(
elif source == "custom":
self.logger.log(logging.INFO, f"try to load from local: {custom_path}")
if not check_all_assets(Path(custom_path), self.sha256_map, update=False):
self.logger.error(
"check models in custom path %s failed.", custom_path
)
self.logger.error("check models in custom path %s failed.", custom_path)
return None
download_path = custom_path

Expand Down
16 changes: 12 additions & 4 deletions ChatTTS/model/gpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,9 @@ def _prepare_generation_inputs(
# 2 - If the past_length is smaller than input_ids', then input_ids holds all input tokens. We can discard
# input_ids based on the past_length.
elif past_length < input_ids.shape[1]:
input_ids = input_ids.narrow(1, past_length, input_ids.size(1)-past_length)
input_ids = input_ids.narrow(
1, past_length, input_ids.size(1) - past_length
)
# 3 - Otherwise (past_length >= input_ids.shape[1]), let's assume input_ids only has unprocessed tokens.

# If we are about to go beyond the maximum cache length, we need to crop the input attention mask.
Expand All @@ -235,14 +237,18 @@ def _prepare_generation_inputs(
and attention_mask is not None
and cache_length + input_ids.shape[1] > max_cache_length
):
attention_mask = attention_mask.narrow(1, -max_cache_length, max_cache_length)
attention_mask = attention_mask.narrow(
1, -max_cache_length, max_cache_length
)

if attention_mask is not None and position_ids is None:
# create position_ids on the fly for batch generation
position_ids = attention_mask.long().cumsum(-1) - 1
position_ids.masked_fill_(attention_mask == 0, 1)
if past_key_values:
position_ids = position_ids.narrow(1, -input_ids.shape[1], input_ids.shape[1])
position_ids = position_ids.narrow(
1, -input_ids.shape[1], input_ids.shape[1]
)

input_length = (
position_ids.shape[-1] if position_ids is not None else input_ids.shape[-1]
Expand Down Expand Up @@ -360,7 +366,9 @@ def generate(
device=inputs_ids.device,
)
if attention_mask is not None:
attention_mask_cache.narrow(1, 0, attention_mask.shape[1]).copy_(attention_mask)
attention_mask_cache.narrow(1, 0, attention_mask.shape[1]).copy_(
attention_mask
)

with tqdm(
total=max_new_token,
Expand Down
5 changes: 4 additions & 1 deletion ChatTTS/model/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ def __call__(
input_ids = input_ids.narrow(1, -self.past_window, self.past_window)
freq = F.one_hot(input_ids, scores.size(1)).sum(1)
if freq.size(0) > self.max_input_ids:
freq.narrow(0, self.max_input_ids, freq.size(0)-self.max_input_ids).zero_()
freq.narrow(
0, self.max_input_ids, freq.size(0) - self.max_input_ids
).zero_()
alpha = torch.pow(self.penalty, freq)
scores = scores.contiguous()
inp = scores.multiply(alpha)
Expand All @@ -32,6 +34,7 @@ def __call__(
del inp, oth, scores, con, alpha
return out


def gen_logits(
num_code: int,
top_P=0.7,
Expand Down
2 changes: 1 addition & 1 deletion examples/web/ex.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ex=[
ex = [
[
"四川美食确实以辣闻名,但也有不辣的选择。比如甜水面、赖汤圆、蛋烘糕、叶儿粑等,这些小吃口味温和,甜而不腻,也很受欢迎。",
0.3,
Expand Down
2 changes: 1 addition & 1 deletion examples/web/funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def refine_text(
has_interrupted = False

if not refine_text_flag:
sleep(1) # to skip fast answer of loading mark
sleep(1) # to skip fast answer of loading mark
return text, *set_generate_buttons(
generate_button, interrupt_button, is_reset=True
)
Expand Down