Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -90,6 +91,8 @@ public class AudioController {
private JobQueueProperties jobQueueProperties;
@Autowired
private EndpointDataService endpointDataService;
@Autowired
private AudioFileService audioFileService;

/**
* 实时语音识别WebSocket接口
Expand Down Expand Up @@ -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<String, String> uploadAudioFile(@RequestPart("file") MultipartFile file) throws IOException {
String filename = file.getOriginalFilename() != null ? file.getOriginalFilename() : "audio.wav";
String url = audioFileService.uploadAndGetUrl(file.getBytes(), filename);
Map<String, String> result = new HashMap<>();
result.put("url", url);
return result;
}

@PostMapping("/transcriptions/file")
public AudioTranscriptionResp transcribeAudio(@RequestBody AudioTranscriptionReq audioTranscriptionReq) {
validateRequestParams(audioTranscriptionReq);
Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<QwenProperty> {

@Autowired
private OpenAiServiceFactory openAiServiceFactory;
private AudioFileService audioFileService;

@Override
public FlashAsrResponse asr(AsrRequest request, String url, QwenProperty property, EndpointProcessData processData) {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Loading