buchi-stdesign commited on
Commit
5d02468
·
verified ·
1 Parent(s): 35585d5

Upload AIvtuber.py

Browse files
Files changed (1) hide show
  1. 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
- config_file = hf_hub_download(
53
- repo_id="buchi-stdesign/3DAItuber-model",
54
- filename="config.json",
55
- repo_type="model"
56
- )
57
- style_file = hf_hub_download(
58
- repo_id="buchi-stdesign/3DAItuber-model",
59
- filename="style_vectors.npy",
60
- repo_type="model"
61
- )
62
-
63
- # デバイス設定 (CUDA 未サポー時は CPU にフォルバッ)
64
- device = "cuda" if torch.cuda.is_available() else "cpu"
65
- print(f"[INFO] Using device: {device}")
66
-
67
- # --- YouTube LiveChat 取得準備 ---
68
- # ローカル実行時のみ有効化してください
69
- # import pytchat
70
- # livechat = pytchat.create(video_id="MYLhogwYrY4")
71
-
72
- # --- オーディオ再生用ユーティリティ関数 ---
73
- # ローカル実行時のみ有効化してください
74
- # device_id = 10 # お使いの環境に合わせて変更してください
75
- # def play_tts(text: str):
76
- # """
77
- # テキスト 音声 再生 を行う関数(ローカル用)
78
- # """
79
- # tts = TTSModel(
80
- # model_path=model_file,
81
- # config_path=config_file,
82
- # style_vec_path=style_file,
83
- # device=device,
84
- # )
85
- # sr, wav = tts.infer(text=text, length=0.85)
86
- # sd.play(wav, sr, device=device_id)
87
- # sd.wait()
88
-
89
- # クラウド・Streamlit用:音声データを返す関数
90
- def tts_to_wav(text: str):
91
- """
92
- テキスト 音声データ(wav配列, サンプリングレート)を返す(クラウド用)
93
- """
94
- tts = TTSModel(
95
- model_path=model_file,
96
- config_path=config_file,
97
- style_vec_path=style_file,
98
- device=device,
99
- )
100
- sr, wav = tts.infer(text=text, length=0.85)
101
- return sr, wav
102
-
103
- # --- ライブチャットに応答して音声再生ループ ---
104
- # ローカル実行時のみ有効化してください
105
- # while livechat.is_alive():
106
- # chatdata = livechat.get()
107
- # for c in chatdata.items:
108
- # user_msg = f"{c.datetime} {c.author.name} {c.message} {c.amountString}"
109
- # print(user_msg)
110
- # resp = chat_session.send_message(user_msg)
111
- # print(resp.text)
112
- # play_tts(resp.text)
113
- # time.sleep(1)
114
-
115
- # --- コンソール入力にも対応 ---
116
- # ローカル実行時のみ有効化してください
117
- # while True:
118
- # user_input = input("You: ")
119
- # resp = chat_session.send_message(user_input)
120
- # print("Bot:", resp.text)
121
- # play_tts(resp.text)
 
 
 
 
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)