diff --git a/api/server/src/main/java/com/ke/bella/openapi/endpoints/AudioController.java b/api/server/src/main/java/com/ke/bella/openapi/endpoints/AudioController.java index 715ba411f..d0554c7fc 100644 --- a/api/server/src/main/java/com/ke/bella/openapi/endpoints/AudioController.java +++ b/api/server/src/main/java/com/ke/bella/openapi/endpoints/AudioController.java @@ -34,6 +34,7 @@ import com.ke.bella.openapi.protocol.tts.TtsAdaptor; import com.ke.bella.openapi.protocol.tts.TtsProperty; import com.ke.bella.openapi.protocol.tts.TtsRequest; +import com.ke.bella.openapi.service.AudioFileService; import com.ke.bella.openapi.service.EndpointDataService; import com.ke.bella.openapi.tables.pojos.ChannelDB; import com.ke.bella.openapi.utils.JacksonUtils; @@ -90,6 +91,8 @@ public class AudioController { private JobQueueProperties jobQueueProperties; @Autowired private EndpointDataService endpointDataService; + @Autowired + private AudioFileService audioFileService; /** * 实时语音识别WebSocket接口 @@ -212,6 +215,15 @@ public TranscriptionsResponse transcriptions(TranscriptionsRequest request) thro return TranscriptionsConverter.convertFlashAsrToOpenAI(flashResponse, request.getResponseFormat()); } + @PostMapping(value = "/transcriptions/file/upload", consumes = "multipart/form-data") + public Map uploadAudioFile(@RequestPart("file") MultipartFile file) throws IOException { + String filename = file.getOriginalFilename() != null ? file.getOriginalFilename() : "audio.wav"; + String url = audioFileService.uploadAndGetUrl(file.getBytes(), filename); + Map result = new HashMap<>(); + result.put("url", url); + return result; + } + @PostMapping("/transcriptions/file") public AudioTranscriptionResp transcribeAudio(@RequestBody AudioTranscriptionReq audioTranscriptionReq) { validateRequestParams(audioTranscriptionReq); @@ -272,8 +284,8 @@ private void validateRequestParams(AudioTranscriptionReq audioTranscriptionReq) if(audioTranscriptionReq.getModel() == null || audioTranscriptionReq.getModel().isEmpty()) { throw new IllegalArgumentException("Model is required"); } - if(audioTranscriptionReq.getCallbackUrl() == null || audioTranscriptionReq.getCallbackUrl().isEmpty()) { - throw new IllegalArgumentException("Callback url is required"); + if(audioTranscriptionReq.getCallbackUrl() == null) { + audioTranscriptionReq.setCallbackUrl(""); } if(audioTranscriptionReq.getUrl() == null || audioTranscriptionReq.getUrl().isEmpty()) { throw new IllegalArgumentException("Url is required"); diff --git a/api/server/src/main/java/com/ke/bella/openapi/protocol/asr/flash/QwenAdaptor.java b/api/server/src/main/java/com/ke/bella/openapi/protocol/asr/flash/QwenAdaptor.java index b0377bff7..f96f5b58f 100644 --- a/api/server/src/main/java/com/ke/bella/openapi/protocol/asr/flash/QwenAdaptor.java +++ b/api/server/src/main/java/com/ke/bella/openapi/protocol/asr/flash/QwenAdaptor.java @@ -1,13 +1,10 @@ package com.ke.bella.openapi.protocol.asr.flash; import com.ke.bella.openapi.EndpointProcessData; -import com.ke.bella.openapi.server.OpenAiServiceFactory; -import com.theokanning.openai.file.FileUrl; -import com.theokanning.openai.service.OpenAiService; +import com.ke.bella.openapi.service.AudioFileService; import com.ke.bella.openapi.common.exception.BellaException; import com.ke.bella.openapi.protocol.asr.QwenProperty; import com.ke.bella.openapi.protocol.asr.AsrRequest; -import com.theokanning.openai.file.File; import com.ke.bella.openapi.utils.HttpUtils; import com.ke.bella.openapi.utils.JacksonUtils; import okhttp3.MediaType; @@ -21,13 +18,12 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.UUID; @Component("QwenFlashAsr") public class QwenAdaptor implements FlashAsrAdaptor { @Autowired - private OpenAiServiceFactory openAiServiceFactory; + private AudioFileService audioFileService; @Override public FlashAsrResponse asr(AsrRequest request, String url, QwenProperty property, EndpointProcessData processData) { @@ -51,17 +47,8 @@ public FlashAsrResponse asr(AsrRequest request, String url, QwenProperty propert } private String uploadAudioAndGetUrl(AsrRequest request) { - OpenAiService openAiService = openAiServiceFactory.create(); - - // Upload file with proper filename based on format - String filename = UUID.randomUUID().toString() + "_audio." + (request.getFormat() != null ? request.getFormat() : "wav"); - - // Create file upload request - File openAiFile = openAiService.uploadFile("temp", request.getContent(), filename); - - FileUrl fileUrlResponse = openAiService.retrieveFileUrl(openAiFile.getId()); - - return fileUrlResponse.getUrl(); + String filename = "audio." + (request.getFormat() != null ? request.getFormat() : "wav"); + return audioFileService.uploadAndGetUrl(request.getContent(), filename); } private QwenFlashAsrRequest buildAliRequest(AsrRequest request, String audioUrl, QwenProperty property) { diff --git a/api/server/src/main/java/com/ke/bella/openapi/service/AudioFileService.java b/api/server/src/main/java/com/ke/bella/openapi/service/AudioFileService.java new file mode 100644 index 000000000..f61c7b350 --- /dev/null +++ b/api/server/src/main/java/com/ke/bella/openapi/service/AudioFileService.java @@ -0,0 +1,22 @@ +package com.ke.bella.openapi.service; + +import com.ke.bella.openapi.server.OpenAiServiceFactory; +import com.theokanning.openai.file.File; +import com.theokanning.openai.service.OpenAiService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.UUID; + +@Service +public class AudioFileService { + @Autowired + private OpenAiServiceFactory openAiServiceFactory; + + public String uploadAndGetUrl(byte[] content, String filename) { + String uniqueName = UUID.randomUUID() + "_" + filename; + OpenAiService service = openAiServiceFactory.create(); + File file = service.uploadFile("temp", content, uniqueName); + return service.retrieveFileUrl(file.getId()).getUrl(); + } +}