Compare commits

..

2 Commits

Author SHA1 Message Date
yumoqing
a5a33d8d1e bugfix 2024-08-04 14:28:28 +08:00
yumoqing
1f39d86557 bugfix 2024-08-01 13:46:05 +08:00
7 changed files with 83 additions and 6 deletions

View File

@ -38,6 +38,12 @@ bricks.AudioPlayer = class extends bricks.JsWidget {
this.audio.src = url;
bricks.debug(this.audio.src,' new src seted');
}
set_source_from_response(resp){
// 将服务器返回的数据设置为音频源
this.audio.src = URL.createObjectURL(new Blob([response.body]));
// 播放音频
this.audio.play();
}
play(){
this.audio.play();
}

View File

@ -6,6 +6,7 @@ SOURCES=" page_data_loader.js factory.js uitypesdef.js utils.js uitype.js \
tree.js multiple_state_image.js dynamiccolumn.js form.js message.js conform.js \
paging.js datagrid.js iframe.js cols.js echartsext.js \
floaticonbar.js miniform.js wterm.js dynamicaccordion.js \
binstreaming.js streaming_audio.js \
llm_dialog.js llm.js websocket.js datarow.js tabular.js \
line.js pie.js bar.js gobang.js "
echo ${SOURCES}

View File

@ -75,6 +75,7 @@ bricks.Button = class extends bricks.Layout {
var txt = new bricks.Text(opts);
this.add_widget(txt);
txt.bind('click', this.target_clicked.bind(this));
this.text_w = txt;
}
}
target_clicked(event){

View File

@ -7,9 +7,6 @@
<link rel="stylesheet" href="/bricks/3parties/xterm.css" />
<link href="/bricks/3parties/video-js.css" rel="stylesheet" />
<link rel="stylesheet" href="/bricks/css/bricks.css">
<!-- support IE8 (for Video.js versions prior to v7) -->
<script src="{{entire_url('/bricks/3parties/videojs-ie8.min.js')}}"></script>
</head>
<body>
<!--
@ -17,6 +14,9 @@
<script>eruda.init();</script>
-->
<script type="text/javascript" src="https://registry.npmmirror.com/echarts/5.5.1/files/dist/echarts.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/onnxruntime-web/dist/ort.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@ricky0123/vad-web@0.0.7/dist/bundle.min.js"></script>
<script src="/bricks/3parties/marked.min.js"></script>
<script src="/bricks/3parties/xterm.js"></script>
<script src="/bricks/3parties/video.min.js"></script>

View File

@ -28,6 +28,9 @@ bricks.Image = class extends bricks.JsWidget {
this.dom_element.style.height = this.height;
}
}
removeBase64Header(base64String) {
return base64String.replace(/^data:[^;]*;base64,/, '');
}
base64(){
const image = this.dom_element;
// 创建一个画布来绘制图像
@ -42,7 +45,8 @@ bricks.Image = class extends bricks.JsWidget {
ctx.drawImage(image, 0, 0);
// 获取画布数据并转换为 base64
const dataURL = canvas.toDataURL('image/png'); // 可以根据需要修改图像格式
var dataURL = canvas.toDataURL('image/png'); // 可以根据需要修改图像格式
dataURL = this.removeBase64Header(dataURL);
console.log(dataURL);
return dataURL;
}

View File

@ -1,5 +1,46 @@
bricks = window.bricks || {}
bricks.LlmMsgAudio = class extends bricks.UpStreaming {
constructor(opts){
super(opts);
this.olddata = '';
this.data = '';
this.cn_p = ["。","","","","\n"];
this.other_p = [".",",","!","?","\n"];
this.audio = AudioPlayer({})
}
detectLanguage(text) {
try {
const detector = new Intl.LocaleDetector();
const locale = detector.detectLocaleOf(text);
return locale.language;
} catch (error) {
console.error('无法检测语言:', error);
return '未知';
}
}
send(data){
var newdata = data.slice(this.olddata.length);
this.olddata = data;
this.data += newdata;
var lang = detectLaguage(this.data);
var parts;
if (lang='zh'){
parts = this.data.split(this.cn_p).filter(part => part.trim()!== '');
} else {
parts = this.data.split(this.oter_p).filter(part => part.trim()!== '');
}
for(var i=0;i<parts.length - 1; i++){
super.send(parts[i]);
}
this.data = parts[parts.length - 1];
}
async go(){
var resp = await super.go();
this.audio.set_source_from_response(resp);
return resp;
}
}
bricks.ModelOutput = class extends bricks.HBox {
/* {
icon:
@ -64,8 +105,16 @@ bricks.ModelOutput = class extends bricks.HBox {
if (this.run) {
this.run.stop_timepass();
this.remove_widget(this.run);
if(this.textvoice){
this.upstreaming = new bricks.UpStreaming({
url:this.tts_url
});
this.upstreaming.go();
}
}
if (this.upstreaming){
this.upstreaming.send(data.content);
}
console.log('update_data():data=', data);
this.run = null;
var w = await bricks.widgetBuild(this.output_view, this.llmio, data);
w.set_css('llm_msg');
@ -79,6 +128,11 @@ bricks.ModelOutput = class extends bricks.HBox {
}
}
}
finish(){
if (this.upstreaming){
this.upstreaming.finish();
}
}
}
bricks.LlmModel = class extends bricks.JsWidget {
@ -95,6 +149,8 @@ bricks.LlmModel = class extends bricks.JsWidget {
llm_message_format:
use_session:
input_from:
textvoice:
tts_url:
response_mode:stream, sync or async
}
*/
@ -119,6 +175,8 @@ bricks.LlmModel = class extends bricks.JsWidget {
}
async model_inputed(data){
var mout = new bricks.ModelOutput({
textvoice:this.textvoice,
tts_url:this.tts_url,
icon:this.opts.icon,
estimate_url:this.llmio.estimate_url,
output_view:this.opts.output_view});
@ -225,8 +283,15 @@ bricks.LlmIO = class extends bricks.VBox {
this.o_w = new bricks.Filler({overflow:'auto'});
this.add_widget(this.title_w);
this.add_widget(this.o_w);
if (this.models.length < 2 && this.tts_url){
this.textvoice = true;
}
this.add_widget(this.i_w);
this.models.forEach( m =>{
if (this.textvoice){
m.textvoice = true;
m.tts_url = this.tts_url;
}
var lm = new bricks.LlmModel(this, m);
this.llmmodels.push(lm);
var tw = lm.render_title();

View File

@ -309,7 +309,7 @@ bricks.TextBase = class extends bricks.JsWidget {
if (this.i18n) {
text = this._i18n._(this.otext);
} else {
text = otext;
text = this.otext;
}
this.set_text(text);
}