添加tts功能代码

main
gitee 6 days ago
parent d38d33f578
commit 4376afabe5

@ -5,9 +5,12 @@ import com.supervision.domain.LivetalkingChatDTO;
import com.supervision.dto.DigitalHumanDTO;
import com.supervision.dto.DigitalHumanVoiceDTO;
import com.supervision.dto.R;
import com.supervision.service.TTsService;
import com.supervision.service.danmaku.DigitalHumanManageService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
/**
*
@ -19,6 +22,8 @@ public class DigitalHumanController {
private final DigitalHumanManageService digitalHumanManageService;
private final TTsService tTsService;
/**
*
* @param page
@ -49,4 +54,9 @@ public class DigitalHumanController {
digitalHumanManageService.chatCallBack(digitalHumanVoiceDTO);
return R.ok();
}
@PostMapping("/livetalking/asr")
public R<String> tts(@RequestPart("file") MultipartFile file) throws IOException {
String stt = tTsService.stt(file.getOriginalFilename(), file.getBytes());
return R.ok(stt);
}
}

@ -0,0 +1,6 @@
package com.supervision.service;
public interface TTsService {
String stt(String model,byte[] audioData);
}

@ -0,0 +1,38 @@
package com.supervision.service.impl;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.supervision.service.TTsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class TTsServiceImpl implements TTsService {
@Value("${livetalking.tts.url}")
private String ttsUrl;
@Value("${livetalking.tts.model}")
private String model;
@Override
public String stt(String fileName, byte[] audioData) {
String url = ttsUrl + "/v1/audio/transcriptions";
HttpRequest post = HttpUtil.createPost(url);
post.form("model", model);
post.form("file", audioData, fileName);
try (var response = post.execute()) {
if (response.isOk()) {
String body = response.body();
JSONObject entries = JSONUtil.parseObj(body);
return entries.getStr("text");
}
throw new RuntimeException("STT request failed: " + response.getStatus() + " - " + response.body());
}
}
}

@ -43,6 +43,9 @@ livetalking:
url: http://192.168.10.96:8010
session:
id: 0
tts:
url: http://192.168.10.96:9997
model: whisper-large-v3
jwt:
secret: "DlHaPUePiN6MyvpMpsMq/t6swzMHqtrRFd2YnofKz4k=" # JWT密钥 使用官方推荐方式生成 Base64.getEncoder().encodeToString(Keys.secretKeyFor(SignatureAlgorithm.HS256).getEncoded());
expiration: 86400000 # 1小时3600000 1天86400000

@ -1,13 +1,18 @@
package com.supervision;
import cn.hutool.core.io.FileUtil;
import com.hankcs.hanlp.seg.common.Term;
import com.hankcs.hanlp.tokenizer.StandardTokenizer;
import com.supervision.domain.SysByteArray;
import com.supervision.service.SysByteArrayService;
import com.supervision.service.TTsService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
@Slf4j
@SpringBootTest
@ -36,6 +41,17 @@ public class PlatformApplicationTest {
boolean isQuestion = isQuestion(sentence);
System.out.println("是否是问句: " + isQuestion);
}
@Autowired
private TTsService tTsService;
@Test
public void ttsTest() throws IOException {
File file = FileUtil.file("C:\\Users\\Administrator\\Desktop\\doubao-man.wav_0000181120_0000313600.wav");
FileInputStream inputStream = new FileInputStream(file);
byte[] bytes = inputStream.readAllBytes();
String stt = tTsService.stt("doubao-man.wav_0000181120_0000313600.wav", bytes);
System.out.println(stt);
}
private boolean isQuestion(String sentence) {
String[] questionWords = {"什么", "为什么", "如何", "哪", "谁", "多少", "是否", "能否", "是不是"};
String[] questionEndings = {"吗", "呢", "", "?"};

Loading…
Cancel
Save