Spaces:
Sleeping
Sleeping
File size: 20,399 Bytes
739e03a d08b063 739e03a 2d211fd 99b4d91 593568b 2d211fd 593568b 739e03a e9864b6 739e03a f80ae18 739e03a 2333484 739e03a f372391 cb4c490 f80ae18 739e03a 2333484 f265869 2333484 739e03a f80ae18 739e03a f80ae18 739e03a f80ae18 739e03a f80ae18 94a3dee 739e03a 094a8be 739e03a 967ac9e 94a3dee dca43e8 e608697 dca43e8 739e03a 9cf8841 739e03a 094a8be 739e03a 4175811 359d252 739e03a abf0c98 2333484 e841e68 2333484 abf0c98 9254785 94a3dee abf0c98 e841e68 abf0c98 739e03a e841e68 739e03a |
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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 |
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Sample Chat AI VTuber</title>
<link rel="stylesheet" href="aivtuber.css">
<script src="./aivtuber.js"></script>
<title>Live2Dサンプル</title>
<!-- Live2D -->
<script src="./live2dcubismcore.js"></script>
<script src="//cdn.jsdelivr.net/gh/dylanNew/live2d/webgl/Live2D/lib/live2d.min.js"></script>
<!-- PixiJS -->
<script src="//cdnjs.cloudflare.com/ajax/libs/pixi.js/5.1.3/pixi.min.js"></script>
<script src="//cdn.jsdelivr.net/npm/pixi-live2d-display/dist/index.min.js"></script>
<!-- Kalidokit -->
<script src="//cdn.jsdelivr.net/npm/kalidokit@1.1/dist/kalidokit.umd.js"></script>
</head>
<body>
<video id="my-video" autoplay></video>
<script>
let _debug = 0;
'use strict';
function speaks(input) {
var text = input;//document.getElementById("text").value;
var speech = new SpeechSynthesisUtterance(text);
speech.lang = "ja-JP"; // 日本語に設定
window.speechSynthesis.speak(speech);
}
function speak(input) {
var text = input;//document.getElementById("text").value;
var speech = new SpeechSynthesisUtterance(text);
// 声のリストから男性の声を選択
var voices = window.speechSynthesis.getVoices();
for (var i = 0; i < voices.length; i++) {
if (voices[i].name.includes("Google 日本語") || voices[i].name.includes("Male")) {
speech.voice = voices[i];
break;
}
}
// 音声の設定
speech.lang = "ja-JP"; // 日本語の設定
speech.pitch = 1; // ピッチ(声の高さ)
speech.rate = 1; // 読み上げ速度
speech.volume = 1; // 音量
// 音声の再生
window.speechSynthesis.speak(speech);
}
class MiiboAvatar {
constructor(config) {
this.container = config.container;
this.speechToTextOptions = config.option.speech_to_text;
this.miiboOptions = config.option.miibo;
this.didOptions = config.option.d_id;
this.initialize();
}
initialize() {
//alert("38");
const RTCPeerConnection = window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection;
this.videoElement = document.getElementById(this.container);
this.videoElement.setAttribute('playsinline', '');
this.playIdleVideo();
this.createNewStream();
this.rec = new webkitSpeechRecognition()
this.speaching = false;
this.processing = false;
this.streams = [];
}
async createNewStream() {
if(_debug==1)
alert("53")
try {
this.stopAllStreams();
this.closePC();
let presenter = {"source_url": this.didOptions.presenter.image_url}
const sessionResponse = await this.fetchWithRetries(`https://api.d-id.com/${this.didOptions.service}/streams`, {
method: 'POST',
headers: {
Authorization: `Basic ${this.didOptions.key}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(presenter),
});
const { id: newStreamId, offer, ice_servers: iceServers, session_id: newSessionId } = await sessionResponse.json();
this.streamId = newStreamId;
this.sessionId = newSessionId;
try {
this.sessionClientAnswer = await this.createPeerConnection(offer, iceServers);
} catch (e) {
console.log('Error creating peer connection:', e);
this.stopAllStreams();
this.closePC();
return;
}
const sdpResponse = await fetch(`https://api.d-id.com/${this.didOptions.service}/streams/${this.streamId}/sdp`, {
method: 'POST',
headers: {
Authorization: `Basic ${this.didOptions.key}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
answer: this.sessionClientAnswer,
session_id: this.sessionId,
}),
});
// Handle sdpResponse if needed
} catch (error) {
console.log('Error creating new stream:', error);
// Handle error
}
}
speechRecogInit() {
this.audioContext = new (window.AudioContext || window.webkitAudioContext)()
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia({audio: true}).then((stream) => {
var input = this.audioContext.createMediaStreamSource(stream)
this.audioContext.resume()
this.recorder = new Recorder(input)
})
}
}
startRecording() {
this.recorder && this.recorder.record();
}
stopRecording() {
this.playLoadingVideo();
this.recorder && this.recorder.stop();
this.audioRecognize();
this.recorder.clear();
}
audioRecognize() {
this.recorder && this.recorder.exportWAV((blob) => {
let reader = new FileReader()
reader.onload = () => {
let result = new Uint8Array(reader.result)
let data = {
"config": {
"encoding": "LINEAR16",
"languageCode": "ja-JP",
"alternativeLanguageCodes": ["en-US"],//,"cmn-CN","ko-KR"],
"audio_channel_count": 2
},
"audio": {
"content": this.arrayBufferToBase64(result)
}
}
fetch('https://speech.googleapis.com/v1/speech:recognize?key=' + this.speechToTextOptions.api_key, {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8'
},
body: JSON.stringify(data)
}).then((response) => {
return response.text()
}).then((text) => {
let result_json = JSON.parse(text)
text = result_json.results[0].alternatives[0].transcript;
this.languageCode = result_json.results[0].languageCode;
//alert("154 ask")
this.ask(text)
})
}
reader.readAsArrayBuffer(blob)
})
}
// Chrome Only
autoRecognizeMessage(message) {
//handleLiveComment("test",message)
//live2d
if(_debug==1)
alert("start word test")
this.rec.continuous = false
this.rec.interimResults = false
this.rec.lang = 'ja-JP'
this.processing = true
this.playLoadingVideo();
this.rec.stop()
//alert("172 start================================= "+message)
let transcript = message
//alert("transcript"+transcript)
this.ask(transcript);
//for (var i = e.resultIndex; i < e.results.length; i++) {
// if (!e.results[i].isFinal) continue
//
// const { transcript } = e.results[i][0]
// this.ask(transcript);
//}
//this.rec.onresult = (e) => {
//}
this.rec.onend = () => { this.autoRecognizeRestart() }
this.rec.start()
}
// Chrome Only
autoRecognize() {
this.rec.continuous = false
this.rec.interimResults = false
this.rec.lang = 'ja-JP'
this.rec.onresult = (e) => {
this.processing = true
this.playLoadingVideo();
this.rec.stop()
if(_debug==1)
alert("start")
for (var i = e.resultIndex; i < e.results.length; i++) {
if (!e.results[i].isFinal) continue
const { transcript } = e.results[i][0]
this.ask(transcript);
}
}
this.rec.onend = () => { this.autoRecognizeRestart() }
this.rec.start()
}
autoRecognizeRestart() {
if (this.processing) {
setTimeout(() => {this.autoRecognizeRestart()}, 1000)
} else {
this.rec.start()
}
}
arrayBufferToBase64(buffer) {
let binary = ''
let bytes = new Uint8Array(buffer);
let len = bytes.byteLength
for (let i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i])
}
return window.btoa(binary)
}
ask(message) {
//youtubeからの質問
if(_debug==1)
alert("203 ask ========================== "+message);
//ask(start);
this.getMiiboResponse(message);
}
async getMiiboResponse(utterance) {
if(_debug==1)
alert("209 getMiiboResponse"+utterance)
const params = {
api_key: this.miiboOptions.api_key,
agent_id: this.miiboOptions.agent_id,
uid: this.miiboOptions.user_id,
stream: true,
utterance: utterance
};
try {
const res = await fetch("https://api-mebo.dev/api", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(params),
});
const reader = res.body.getReader();
const decoder = new TextDecoder();
let output = "";
let sentences = [];
let current_index = 0;
const read = async () => {
const { done, value } = await reader.read();
if (done) return;
let dataString = decoder.decode(value).split("\n").filter(x => x != "");
try {
let responseData = JSON.parse(dataString[dataString.length - 1]);
output = responseData.bestResponse.utterance.split("\n").filter(x => x.trim() != "").join("\n");
sentences = output.replace(/[。、\.\,\!\?!?]/,".").split(".")
if (this.didOptions.priority == "speed" && current_index == 0 && current_index + 1 < sentences.length) {
this.startTalk(sentences[current_index++])
}
} catch(e) {
console.log(e);
}
return read();
};
await read();
reader.releaseLock();
this.startTalk(sentences.slice(current_index).join("。"));
} catch(error) {
console.log("Error fetching AI response: ", error);
}
}
async startTalk(input) {
if(_debug==1)
alert("start streaming id"+input)
if (this.peerConnection?.signalingState === 'stable' || this.peerConnection?.iceConnectionState === 'connected') {
const gender = this.didOptions.presenter.gender;
let voice_id = this.didOptions.presenter.voice_id || "";
if (voice_id == "") {
switch (this.languageCode) {
case "en-us":
voice_id = gender == "male" ? "en-US-GuyNeural" : "en-US-AriaNeural"
break;
case "ko-kr":
voice_id = gender == "male" ? "ko-KR-InJoonNeural" : "ko-KR-YuJinNeural"
break;
case "cmn-CN":
voice_id = gender == "male" ? "zh-CN-YunjianNeural" : "zh-CN-XiaohanNeural"
break;
default:
voice_id = gender == "male" ? "ja-JP-KeitaNeural" : "ja-JP-NanamiNeural"
}
}
console.log("start tolak 330")
//speak(input)
const requestOptions = {
method: 'POST',
headers: {
Authorization: `Basic ${this.didOptions.key}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
script: {
type: "text",
subtitles: false,
provider: {
type: "microsoft",
voice_id: voice_id
},
ssml: false,
input: input
},
config: {
fluent: false,
pad_audio: 0,
align_driver: false,
stitch: false,
auto_match: false,
sharpen: false,
normalization_factor: 0
},
session_id: this.sessionId,
}),
};
if (this.didOptions.service === 'clips') {
requestOptions.body.background = { color: '#FFFFFF' };
}
try {
const playResponse = await this.fetchWithRetries(`https://api.d-id.com/${this.didOptions.service}/streams/${this.streamId}`, requestOptions);
startTyping({
el: "#aiResponseUtterance",
string: input,
speed: 50
});
// Handle response if needed
} catch (error) {
console.error('Error starting talk:', error);
// Handle error
}
}
}
async destoryTalk(input) {
try {
await fetch(`https://api.d-id.com/${this.didOptions.service}/streams/${this.streamId}`, {
method: 'DELETE',
headers: {
Authorization: `Basic ${this.didOptions.key}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({ session_id: this.sessionId }),
});
await this.stopAllStreams();
await this.closePC();
} catch (error) {
console.error('Error destroying talk:', error);
// Handle error
}
}
onIceCandidate(event) {
if (event.candidate) {
const { candidate, sdpMid, sdpMLineIndex } = event.candidate;
fetch(`https://api.d-id.com/${this.didOptions.service}/streams/${this.streamId}/ice`, {
method: 'POST',
headers: {
Authorization: `Basic ${this.didOptions.key}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
candidate,
sdpMid,
sdpMLineIndex,
session_id: this.sessionId,
}),
}).catch((error) => {
console.error('Error sending ICE candidate:', error);
// Handle error
});
}
}
onIceConnectionStateChange() {
if (this.peerConnection.iceConnectionState === 'failed' || this.peerConnection.iceConnectionState === 'closed') {
this.stopAllStreams();
this.closePC();
}
}
onTrack(event) {
if (!event.track || !event.streams || event.streams.length === 0) return;
this.statsIntervalId = setInterval(async () => {
const stats = await this.peerConnection.getStats(event.track);
stats.forEach((report) => {
if (report.type === 'inbound-rtp' && report.mediaType === 'video') {
const videoStatusChanged = this.videoIsPlaying !== report.bytesReceived > this.lastBytesReceived;
if (videoStatusChanged) {
this.videoIsPlaying = report.bytesReceived > this.lastBytesReceived;
this.onVideoStatusChange(this.videoIsPlaying, event.streams[0]);
}
this.lastBytesReceived = report.bytesReceived;
}
});
}, 100);
}
onVideoStatusChange(videoIsPlaying, stream) {
let status;
if (videoIsPlaying) {
status = 'streaming';
const remoteStream = stream;
this.streams.push(remoteStream);
this.checkSpeaching();
} else {
status = 'empty';
this.speaching = false;
this.processing = false;
this.playIdleVideo();
}
}
checkSpeaching() {
if (this.speaching) {
setTimeout(() => {this.checkSpeaching()}, 20)
} else {
this.setVideoElement(this.streams.shift());
}
}
async createPeerConnection(offer, iceServers) {
if (!this.peerConnection) {
this.peerConnection = new RTCPeerConnection({ iceServers });
this.peerConnection.addEventListener('icecandidate', this.onIceCandidate.bind(this), true);
this.peerConnection.addEventListener('iceconnectionstatechange', this.onIceConnectionStateChange.bind(this), true);
this.peerConnection.addEventListener('track', this.onTrack.bind(this), true);
}
await this.peerConnection.setRemoteDescription(offer);
const sessionClientAnswer = await this.peerConnection.createAnswer();
await this.peerConnection.setLocalDescription(sessionClientAnswer);
return sessionClientAnswer;
}
setVideoElement(stream) {
if (!stream) return;
this.videoElement.srcObject = stream;
this.videoElement.loop = false;
// safari hotfix
if (this.videoElement.paused) {
this.videoElement
.play()
.then((_) => {})
.catch((e) => {});
}
}
playIdleVideo() {
this.videoElement.srcObject = undefined;
this.videoElement.src = this.didOptions.presenter.idle_movie;
this.videoElement.loop = true;
}
playLoadingVideo() {
this.videoElement.srcObject = undefined;
this.videoElement.src = this.didOptions.presenter.loading_movie;
this.videoElement.loop = false;
}
stopAllStreams() {
if (this.videoElement.srcObject) {
this.videoElement.srcObject.getTracks().forEach((track) => track.stop());
this.videoElement.srcObject = null;
}
}
closePC(pc = this.peerConnection) {
if (!pc) return;
pc.close();
pc.removeEventListener('icecandidate', this.onIceCandidate.bind(this), true);
pc.removeEventListener('iceconnectionstatechange', this.onIceConnectionStateChange.bind(this), true);
pc.removeEventListener('track', this.onTrack.bind(this), true);
clearInterval(this.statsIntervalId);
if (pc === this.peerConnection) {
this.peerConnection = null;
}
}
async fetchWithRetries(url, options, retries = 1) {
const maxRetryCount = 3;
const maxDelaySec = 4;
try {
return await fetch(url, options);
} catch (err) {
if (retries <= maxRetryCount) {
const delay = Math.min(Math.pow(2, retries) / 4 + Math.random(), maxDelaySec) * 1000;
await new Promise((resolve) => setTimeout(resolve, delay));
return this.fetchWithRetries(url, options, retries + 1);
} else {
throw new Error(`Max retries exceeded. error: ${err}`);
}
}
}
}
const miiboAvatar = new MiiboAvatar({
container: "my-video",
option: {
miibo: {
api_key: "9206bb9d-be50-46b9-8ba2-4441fee3fe7b190cb511a0c78",
agent_id: "3cfb2749-a1fa-4a5b-a2c8-eb17aee77808190cb500dd1357",
user_id: "user",
},
d_id: {
key: "c3VnaUB1cmxvdW5nZS5jby5qcA:XSxcahMhg6JQY7RMB_wGu",
service: "talks",
priority: "cost",
presenter:{
gender: "male",
image_url: "https://kenken999-fastapi-django-main-live.hf.space/static/sugi.jpg",
idle_movie: "https://kenken999-fastapi-django-main-live.hf.space/static/miiboi.mp4",
loading_movie: "https://kenken999-fastapi-django-main-live.hf.space/static/miibo.mp4",
}
}
}
})
miiboAvatar.autoRecognize();
const onClickSends = () => {
//alert("start send ============================ ")
let utterance = document.querySelector("#utterances");
//alert(utterances.value)
miiboAvatar.autoRecognizeMessage(utterance.value)
//case voice box
//handleLiveComment(utterance.value, '匿名');
//onClickSend
utterances.value = "";
}
</script>
<canvas id="my-live2d"></canvas>
<script src="live2d.js"></script>
<script src="index.js"></script>
<div id="vtubers">
<!--
<iframe width="auto" height="315" src="https://www.youtube.com/embed/9BSgttQDyOw?si=ax2F4oAi2h9f9eGT" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
/-->
<!--<img id="charaImg" src="chara.png" width="auto" height="400" />/-->
</div>
<div id="aiResponse" class="aiResponseBox">
<p class="ai-response" id="aiResponseUtterance"></p>
</div>
<div class="bottomBox">
<p id="userComment"></p>
<button id="startLiveButton" onclick="startLive();">LIVE開始</button>
<div id="submit_forms">
<input type="text" id="utterances" />
<button id="sendButton" onclick="onClickSends();">送信</button>
</div>
</div>
</body>
</html> |