File size: 4,401 Bytes
5d02468
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# -*- coding: utf-8 -*-
import os
from huggingface_hub import hf_hub_download
API_KEY = os.environ["GOOGLE_API_KEY"]  # 環境変数から取得

import google.generativeai as genai
import torch
from pathlib import Path
from style_bert_vits2.nlp import bert_models
from style_bert_vits2.constants import Languages
from style_bert_vits2.tts_model import TTSModel
# ローカル実行時のみ有効化してください
# import sounddevice as sd
# import pytchat
import time

# --- Google Gemini API の設定 ---
genai.configure(api_key=API_KEY)
generation_config = {
    "temperature": 1,
    "top_p": 0.95,
    "top_k": 40,
    "max_output_tokens": 8192,
    "response_mime_type": "text/plain",
}
safety_settings = [
    {"category": "HARM_CATEGORY_HARASSMENT",     "threshold": "BLOCK_NONE"},
    {"category": "HARM_CATEGORY_HATE_SPEECH",    "threshold": "BLOCK_NONE"},
    {"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE"},
    {"category": "HARM_CATEGORY_DANGEROUS_CONTENT","threshold": "BLOCK_NONE"},
]
model = genai.GenerativeModel(
    model_name="gemini-2.0-flash-exp",
    generation_config=generation_config,
    safety_settings=safety_settings
)
chat_session = model.start_chat(history=[
    {"role":"user","parts":["今からあなたは明るい女の子です!"]},
    {"role":"model","parts":["こんにちは!"]}
])

# --- BERT モデルのロード ---
bert_models.load_model(Languages.JP, "ku-nlp/deberta-v2-large-japanese-char-wwm")
bert_models.load_tokenizer(Languages.JP, "ku-nlp/deberta-v2-large-japanese-char-wwm")

# --- TTS モデル用ファイルパス(Hugging Face Hubからダウンロード) ---
model_file = hf_hub_download(
    repo_id="buchi-stdesign/3DAItuber-model",
    filename="Anneli_e116_s32000.safetensors",
    repo_type="model",
    token=os.environ.get("HUGGINGFACE_TOKEN")  # トークンを明示的に指定
)
config_file = hf_hub_download(
    repo_id="buchi-stdesign/3DAItuber-model",
    filename="config.json",
    repo_type="model",
    token=os.environ.get("HUGGINGFACE_TOKEN")  # トークンを明示的に指定
)
style_file = hf_hub_download(
    repo_id="buchi-stdesign/3DAItuber-model",
    filename="style_vectors.npy",
    repo_type="model",
    token=os.environ.get("HUGGINGFACE_TOKEN")  # トークンを明示的に指定
)

# デバイス設定 (CUDA 未サポート時は CPU にフォールバック)
device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"[INFO] Using device: {device}")

# --- YouTube LiveChat 取得準備 ---
# ローカル実行時のみ有効化してください
# import pytchat
# livechat = pytchat.create(video_id="MYLhogwYrY4")

# --- オーディオ再生用ユーティリティ関数 ---
# ローカル実行時のみ有効化してください
# device_id = 10  # お使いの環境に合わせて変更してください
# def play_tts(text: str):
#     """
#     テキスト → 音声 → 再生 を行う関数(ローカル用)
#     """
#     tts = TTSModel(
#         model_path=model_file,
#         config_path=config_file,
#         style_vec_path=style_file,
#         device=device,
#     )
#     sr, wav = tts.infer(text=text, length=0.85)
#     sd.play(wav, sr, device=device_id)
#     sd.wait()

# クラウド・Streamlit用:音声データを返す関数
def tts_to_wav(text: str):
    """
    テキスト → 音声データ(wav配列, サンプリングレート)を返す(クラウド用)
    """
    tts = TTSModel(
        model_path=model_file,
        config_path=config_file,
        style_vec_path=style_file,
        device=device,
    )
    sr, wav = tts.infer(text=text, length=0.85)
    return sr, wav

# --- ライブチャットに応答して音声再生ループ ---
# ローカル実行時のみ有効化してください
# while livechat.is_alive():
#     chatdata = livechat.get()
#     for c in chatdata.items:
#         user_msg = f"{c.datetime} {c.author.name} {c.message} {c.amountString}"
#         print(user_msg)
#         resp = chat_session.send_message(user_msg)
#         print(resp.text)
#         play_tts(resp.text)
#     time.sleep(1)

# --- コンソール入力にも対応 ---
# ローカル実行時のみ有効化してください
# while True:
#     user_input = input("You: ")
#     resp = chat_session.send_message(user_input)
#     print("Bot:", resp.text)
#     play_tts(resp.text)