添加tts功能代码
parent
d38d33f578
commit
4376afabe5
@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue