Spaces:
Paused
Paused
Upload AIvtuber.py
Browse files- AIvtuber.py +124 -121
AIvtuber.py
CHANGED
|
@@ -1,121 +1,124 @@
|
|
| 1 |
-
# -*- coding: utf-8 -*-
|
| 2 |
-
import os
|
| 3 |
-
from huggingface_hub import hf_hub_download
|
| 4 |
-
API_KEY = os.environ["GOOGLE_API_KEY"] # 環境変数から取得
|
| 5 |
-
|
| 6 |
-
import google.generativeai as genai
|
| 7 |
-
import torch
|
| 8 |
-
from pathlib import Path
|
| 9 |
-
from style_bert_vits2.nlp import bert_models
|
| 10 |
-
from style_bert_vits2.constants import Languages
|
| 11 |
-
from style_bert_vits2.tts_model import TTSModel
|
| 12 |
-
# ローカル実行時のみ有効化してください
|
| 13 |
-
# import sounddevice as sd
|
| 14 |
-
# import pytchat
|
| 15 |
-
import time
|
| 16 |
-
|
| 17 |
-
# --- Google Gemini API の設定 ---
|
| 18 |
-
genai.configure(api_key=API_KEY)
|
| 19 |
-
generation_config = {
|
| 20 |
-
"temperature": 1,
|
| 21 |
-
"top_p": 0.95,
|
| 22 |
-
"top_k": 40,
|
| 23 |
-
"max_output_tokens": 8192,
|
| 24 |
-
"response_mime_type": "text/plain",
|
| 25 |
-
}
|
| 26 |
-
safety_settings = [
|
| 27 |
-
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
|
| 28 |
-
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"},
|
| 29 |
-
{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE"},
|
| 30 |
-
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT","threshold": "BLOCK_NONE"},
|
| 31 |
-
]
|
| 32 |
-
model = genai.GenerativeModel(
|
| 33 |
-
model_name="gemini-2.0-flash-exp",
|
| 34 |
-
generation_config=generation_config,
|
| 35 |
-
safety_settings=safety_settings
|
| 36 |
-
)
|
| 37 |
-
chat_session = model.start_chat(history=[
|
| 38 |
-
{"role":"user","parts":["今からあなたは明るい女の子です!"]},
|
| 39 |
-
{"role":"model","parts":["こんにちは!"]}
|
| 40 |
-
])
|
| 41 |
-
|
| 42 |
-
# --- BERT モデルのロード ---
|
| 43 |
-
bert_models.load_model(Languages.JP, "ku-nlp/deberta-v2-large-japanese-char-wwm")
|
| 44 |
-
bert_models.load_tokenizer(Languages.JP, "ku-nlp/deberta-v2-large-japanese-char-wwm")
|
| 45 |
-
|
| 46 |
-
# --- TTS モデル用ファイルパス(Hugging Face Hubからダウンロード) ---
|
| 47 |
-
model_file = hf_hub_download(
|
| 48 |
-
repo_id="buchi-stdesign/3DAItuber-model",
|
| 49 |
-
filename="Anneli_e116_s32000.safetensors",
|
| 50 |
-
repo_type="model"
|
| 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 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
import os
|
| 3 |
+
from huggingface_hub import hf_hub_download
|
| 4 |
+
API_KEY = os.environ["GOOGLE_API_KEY"] # 環境変数から取得
|
| 5 |
+
|
| 6 |
+
import google.generativeai as genai
|
| 7 |
+
import torch
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
from style_bert_vits2.nlp import bert_models
|
| 10 |
+
from style_bert_vits2.constants import Languages
|
| 11 |
+
from style_bert_vits2.tts_model import TTSModel
|
| 12 |
+
# ローカル実行時のみ有効化してください
|
| 13 |
+
# import sounddevice as sd
|
| 14 |
+
# import pytchat
|
| 15 |
+
import time
|
| 16 |
+
|
| 17 |
+
# --- Google Gemini API の設定 ---
|
| 18 |
+
genai.configure(api_key=API_KEY)
|
| 19 |
+
generation_config = {
|
| 20 |
+
"temperature": 1,
|
| 21 |
+
"top_p": 0.95,
|
| 22 |
+
"top_k": 40,
|
| 23 |
+
"max_output_tokens": 8192,
|
| 24 |
+
"response_mime_type": "text/plain",
|
| 25 |
+
}
|
| 26 |
+
safety_settings = [
|
| 27 |
+
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
|
| 28 |
+
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"},
|
| 29 |
+
{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE"},
|
| 30 |
+
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT","threshold": "BLOCK_NONE"},
|
| 31 |
+
]
|
| 32 |
+
model = genai.GenerativeModel(
|
| 33 |
+
model_name="gemini-2.0-flash-exp",
|
| 34 |
+
generation_config=generation_config,
|
| 35 |
+
safety_settings=safety_settings
|
| 36 |
+
)
|
| 37 |
+
chat_session = model.start_chat(history=[
|
| 38 |
+
{"role":"user","parts":["今からあなたは明るい女の子です!"]},
|
| 39 |
+
{"role":"model","parts":["こんにちは!"]}
|
| 40 |
+
])
|
| 41 |
+
|
| 42 |
+
# --- BERT モデルのロード ---
|
| 43 |
+
bert_models.load_model(Languages.JP, "ku-nlp/deberta-v2-large-japanese-char-wwm")
|
| 44 |
+
bert_models.load_tokenizer(Languages.JP, "ku-nlp/deberta-v2-large-japanese-char-wwm")
|
| 45 |
+
|
| 46 |
+
# --- TTS モデル用ファイルパス(Hugging Face Hubからダウンロード) ---
|
| 47 |
+
model_file = hf_hub_download(
|
| 48 |
+
repo_id="buchi-stdesign/3DAItuber-model",
|
| 49 |
+
filename="Anneli_e116_s32000.safetensors",
|
| 50 |
+
repo_type="model",
|
| 51 |
+
token=os.environ.get("HUGGINGFACE_TOKEN") # トークンを明示的に指定
|
| 52 |
+
)
|
| 53 |
+
config_file = hf_hub_download(
|
| 54 |
+
repo_id="buchi-stdesign/3DAItuber-model",
|
| 55 |
+
filename="config.json",
|
| 56 |
+
repo_type="model",
|
| 57 |
+
token=os.environ.get("HUGGINGFACE_TOKEN") # トークンを明示的に指定
|
| 58 |
+
)
|
| 59 |
+
style_file = hf_hub_download(
|
| 60 |
+
repo_id="buchi-stdesign/3DAItuber-model",
|
| 61 |
+
filename="style_vectors.npy",
|
| 62 |
+
repo_type="model",
|
| 63 |
+
token=os.environ.get("HUGGINGFACE_TOKEN") # トークンを明示的に指定
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
# デバイス設定 (CUDA 未サポート時は CPU にフォールバック)
|
| 67 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 68 |
+
print(f"[INFO] Using device: {device}")
|
| 69 |
+
|
| 70 |
+
# --- YouTube LiveChat 取得準備 ---
|
| 71 |
+
# ローカル実行時のみ有効化してください
|
| 72 |
+
# import pytchat
|
| 73 |
+
# livechat = pytchat.create(video_id="MYLhogwYrY4")
|
| 74 |
+
|
| 75 |
+
# --- オーディオ再生用ユーティリティ関数 ---
|
| 76 |
+
# ローカル実行時のみ有効化してください
|
| 77 |
+
# device_id = 10 # お使いの環境に合わせて変更してください
|
| 78 |
+
# def play_tts(text: str):
|
| 79 |
+
# """
|
| 80 |
+
# テキスト → 音声 → 再生 を行う関数(ローカル用)
|
| 81 |
+
# """
|
| 82 |
+
# tts = TTSModel(
|
| 83 |
+
# model_path=model_file,
|
| 84 |
+
# config_path=config_file,
|
| 85 |
+
# style_vec_path=style_file,
|
| 86 |
+
# device=device,
|
| 87 |
+
# )
|
| 88 |
+
# sr, wav = tts.infer(text=text, length=0.85)
|
| 89 |
+
# sd.play(wav, sr, device=device_id)
|
| 90 |
+
# sd.wait()
|
| 91 |
+
|
| 92 |
+
# クラウド・Streamlit用:音声データを返す関数
|
| 93 |
+
def tts_to_wav(text: str):
|
| 94 |
+
"""
|
| 95 |
+
テキスト → 音声データ(wav配列, サンプリングレート)を返す(クラウド用)
|
| 96 |
+
"""
|
| 97 |
+
tts = TTSModel(
|
| 98 |
+
model_path=model_file,
|
| 99 |
+
config_path=config_file,
|
| 100 |
+
style_vec_path=style_file,
|
| 101 |
+
device=device,
|
| 102 |
+
)
|
| 103 |
+
sr, wav = tts.infer(text=text, length=0.85)
|
| 104 |
+
return sr, wav
|
| 105 |
+
|
| 106 |
+
# --- ライブチャットに応答して音声再生ループ ---
|
| 107 |
+
# ローカル実行時のみ有効化してください
|
| 108 |
+
# while livechat.is_alive():
|
| 109 |
+
# chatdata = livechat.get()
|
| 110 |
+
# for c in chatdata.items:
|
| 111 |
+
# user_msg = f"{c.datetime} {c.author.name} {c.message} {c.amountString}"
|
| 112 |
+
# print(user_msg)
|
| 113 |
+
# resp = chat_session.send_message(user_msg)
|
| 114 |
+
# print(resp.text)
|
| 115 |
+
# play_tts(resp.text)
|
| 116 |
+
# time.sleep(1)
|
| 117 |
+
|
| 118 |
+
# --- コンソール入力にも対応 ---
|
| 119 |
+
# ローカル実行時のみ有効化してください
|
| 120 |
+
# while True:
|
| 121 |
+
# user_input = input("You: ")
|
| 122 |
+
# resp = chat_session.send_message(user_input)
|
| 123 |
+
# print("Bot:", resp.text)
|
| 124 |
+
# play_tts(resp.text)
|