File size: 2,274 Bytes
b4ad851
e9dacc4
76271f7
ce34ac3
 
fa548a8
 
b4ad851
76271f7
 
 
 
6e51f6e
76271f7
 
6e51f6e
76271f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e51f6e
76271f7
 
 
 
6e51f6e
76271f7
 
e9dacc4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import gradio as gr
import torch
from transformers import AutoTokenizer, GPT2LMHeadModel

# モデルとトークナイザーをロード
tokenizer = AutoTokenizer.from_pretrained("buchi-stdesign/style-bert-vits2-demo")
model = GPT2LMHeadModel.from_pretrained("buchi-stdesign/style-bert-vits2-demo")

# 音声生成関数
def text_to_speech(text, speaker, emotion):
    if not text:
        return "Error: 入力テキストが空です"
    
    # テキストをトークナイズして、パディングとトランケートを行い attention_mask を生成
    inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
    
    input_ids = inputs['input_ids']
    attention_mask = inputs['attention_mask']  # Attention maskを取得
    
    # モデルの generate メソッドに attention_mask を渡す
    try:
        generated_output = model.generate(
            input_ids=input_ids,              # トークナイズされたテキスト
            attention_mask=attention_mask,    # 明示的に attention mask を渡す
            max_length=50,                    # 最大長
            num_return_sequences=1,           # 生成するシーケンス数
            do_sample=True,                   # サンプリングを使用
            top_k=50,                         # トークンの上位50個からサンプリング
            top_p=0.95                        # トークンの累積確率0.95までのトークンを使用
        )
    except Exception as e:
        return f"Error in model generation: {str(e)}"
    
    # 生成されたトークンをデコード
    try:
        if len(generated_output) > 0:
            generated_text = tokenizer.decode(generated_output[0], skip_special_tokens=True)
        else:
            return "Error: モデルからの出力がありません"
    except Exception as e:
        return f"Error in decoding: {str(e)}"
    
    return f"Generated text: {generated_text}"

# Gradioインターフェース
demo = gr.Interface(fn=text_to_speech,
                    inputs=["text", gr.Dropdown(["Anneli", "Amitaro"]), gr.Dropdown(["Neutral", "Happy", "Sad", "Angry"])],
                    outputs="text")

# Gradioアプリを起動
demo.launch()  # share=True を削除