@@ -23,7 +23,13 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value ;
import org.springframework.stereotype.Service ;
import org.springframework.validation.annotation.Validated ;
import org.springframework.web.multipart.MultipartFile ;
import javax.sound.sampled.AudioInputStream ;
import javax.sound.sampled.AudioSystem ;
import java.io.InputStream ;
import java.net.HttpURLConnection ;
import java.net.URL ;
import java.time.LocalDateTime ;
import java.util.Comparator ;
import java.util.List ;
@@ -75,18 +81,22 @@ public class AiDialogServiceImpl extends ServiceImpl<AiDialogMapper, AiDialogDO>
if ( messageDO = = null ) {
messageDO = new AiDialogMessageDO ( ) ;
}
String fileName = StrUtil . isBlank ( messageDO . getFileName ( ) ) ? fileReqVo . getFile ( ) . getOriginalFilename ( ) : messageDO . getFileName ( ) ;
String fileName = StrUtil . isBlank ( messageDO . getFileName ( ) ) ? fileReqVo . getFile ( ) . getOriginalFilename ( ) : messageDO . getFileName ( ) ;
//上传文件获取文件地址
String fileUrl = StrUtil . isBlank ( messageDO . getContentText ( ) ) ? fileServer + fileApi . createFile ( fileName , " " , fileReqVo . getFile ( ) . getBytes ( ) ) : messageDO . getContentText ( ) ;
log . info ( fileUrl ) ;
String fileUrl = StrUtil . isBlank ( messageDO . getContentText ( ) ) ? fileServer + fileApi . createFile ( fileName , " " , fileReqVo . getFile ( ) . getBytes ( ) ) : messageDO . getContentText ( ) ;
//翻译结果
translate ( messageDO , fileUrl , fileName ) ;
translate ( messageDO , fileUrl , fileName ) ;
//创建消息 持久化消息
if ( messageDO . getId ( ) = = null ) {
if ( messageDO . getId ( ) = = null ) {
messageDO . setFileName ( fileName ) ;
messageDO . setFileType ( fileReqVo . getFile ( ) . getContentType ( ) ) ;
messageDO . setDialogId ( fileReqVo . getDialogId ( ) ) ;
messageDO . setContentDuration ( fileReqVo . getContentDuration ( ) ) ;
//前端无法转换时 后端进行转
messageDO . setContentDuration (
StrUtil . isBlank ( fileReqVo . getContentDuration ( ) ) | | " Infinity " . equals ( fileReqVo . getContentDuration ( ) )
? 0 : Long . parseLong ( fileReqVo . getContentDuration ( ) )
) ;
messageDO . setContentText ( fileUrl ) ;
messageDO . setContentType ( 2 ) ;
@@ -103,18 +113,23 @@ public class AiDialogServiceImpl extends ServiceImpl<AiDialogMapper, AiDialogDO>
messageDO . setMessageOrder ( aiDialogMessageDO = = null ? 1 : aiDialogMessageDO . getMessageOrder ( ) + 1 ) ;
messageDO . setCreateTime ( LocalDateTime . now ( ) ) ;
}
int i = messageDO . getId ( ) = = null ? aiDialogMessageMapper . insert ( messageDO ) : aiDialogMessageMapper . updateById ( messageDO ) ;
int i = messageDO . getId ( ) = = null ? aiDialogMessageMapper . insert ( messageDO ) : aiDialogMessageMapper . updateById ( messageDO ) ;
TranslateRespVo bean = BeanUtils . toBean ( messageDO , TranslateRespVo . class ) ;
bean . setId ( messageDO . getId ( ) ) ;
return bean ;
}
@SneakyThrows
private Long getFileDuration ( MultipartFile file ) {
AudioInputStream audioStream = AudioSystem . getAudioInputStream ( file . getInputStream ( ) ) ;
return ( long ) ( audioStream . getFrameLength ( ) / audioStream . getFormat ( ) . getFrameRate ( ) ) ;
}
private AiDialogMessageDO translate ( AiDialogMessageDO messageDO , String file , String fileName ) {
private AiDialogMessageDO translate ( AiDialogMessageDO messageDO , String file , String fileName ) {
//调用大模型接口
String result = " " ;
try {
log . info ( translateServer ) ;
result = HttpRequest . post ( translateServer ) . form ( " audio_data " , HttpUtil . downloadBytes ( file ) , fileName ) . timeout ( 20000 ) //20秒超时时间
. execute ( ) . body ( ) ;
} catch ( Exception e ) {
@@ -124,7 +139,7 @@ public class AiDialogServiceImpl extends ServiceImpl<AiDialogMapper, AiDialogDO>
//数据解析
JSONObject translateResult = JSON . parseObject ( result ) ;
if ( translateResult . isEmpty ( ) | | ! translateResult . containsKey ( " intent_result " ) | | ! translateResult . getBoolean ( " is_species_sound " ) ) {
if ( translateResult . isEmpty ( ) | | ! translateResult . containsKey ( " intent_result " ) | | ! translateResult . getBoolean ( " is_species_sound " ) ) {
messageDO . setTransStatus ( 0 ) ;
messageDO . setTransResult ( " " ) ;
return messageDO ;
@@ -149,12 +164,32 @@ public class AiDialogServiceImpl extends ServiceImpl<AiDialogMapper, AiDialogDO>
}
public static void main ( String [ ] args ) {
byte [ ] bytes = HttpUtil . downloadBytes ( " http://192.168.1.231:48080 /admin-api/infra/file/29/get/c7351abf780f18600c104ec5662d843783ed8c309c01fb427046565217f51102.wav " ) ;
// File file = new File("http://192.168.1.231:48080/admin-api/infra/file/29/get/c7351abf780f18600c104ec5662d843783ed8c309c01fb427046565217f51102.wav") ;
String result = HttpRequest . post ( " http://43.139.42.137: 800 0/analyze/audio " ) . form ( " audio_data " , bytes , " aaa " ) . timeout ( 20000 ) //20秒超时时间
. execute ( ) . body ( ) ;
System . out . println ( result ) ;
public static void main ( String [ ] args ) throws Exception {
InputStream inputStream = getInputStreamFromUrl ( " https ://petshy.tashowz.com /admin-api/infra/file/29/get/857def513547ec33a105f71108c8ece329cb64dacc3a4779c94b0fcc3398cc32.webm " ) ;
AudioInputStream audioStream = AudioSystem . getAudioInputStream ( inputStream ) ;
System . out . println ( ( long ) ( audioStream . getFrameLength ( ) / audioStream . getFormat ( ) . getFrameRate ( ) ) ) ;
// byte[] bytes = HttpUtil.downloadBytes("http://192.168.1.231:4 808 0/admin-api/infra/file/29/get/c7351abf780f18600c104ec5662d843783ed8c309c01fb427046565217f51102.wav");
//
//// File file = new File("http://192.168.1.231:48080/admin-api/infra/file/29/get/c7351abf780f18600c104ec5662d843783ed8c309c01fb427046565217f51102.wav") ;
// String result = HttpRequest.post("http://43.139.42.137:8000/analyze/audio").form("audio_data", bytes, "aaa").timeout(20000) //20秒超时时间
// .execute().body();
// System.out.println(result);
}
public static InputStream getInputStreamFromUrl ( String urlString ) throws Exception {
URL url = new URL ( urlString ) ;
HttpURLConnection connection = ( HttpURLConnection ) url . openConnection ( ) ;
connection . setRequestMethod ( " GET " ) ; // 可以是GET, POST等
connection . setConnectTimeout ( 5000 ) ; // 设置连接超时时间
connection . setReadTimeout ( 5000 ) ; // 设置读取超时时间
int responseCode = connection . getResponseCode ( ) ; // 获取响应码
if ( responseCode = = HttpURLConnection . HTTP_OK ) { // 状态码200表示成功
return connection . getInputStream ( ) ;
} else {
// 处理错误情况, 例如抛出异常或返回null等
throw new RuntimeException ( " Failed : HTTP error code : " + responseCode ) ;
}
}
}