-
Notifications
You must be signed in to change notification settings - Fork 8.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:Adapt TongYi LLM speech transcription by Spring AI API (#3733)
Signed-off-by: yuluo-yx <yuluo08290126@gmail.com> Co-authored-by: yuluo-yx <yuluo08290126@gmail.com>
- Loading branch information
Showing
36 changed files
with
1,175 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
这是由阿里巴巴达摩院语音实验室提供的实时语音识别技术。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
.../com/alibaba/cloud/ai/example/tongyi/service/impl/audio/transcription/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Spring Cloud Alibaba AI Audio Transcription | ||
|
||
`TongYiController` 接受一个 HTTP GET 请求 `http://localhost:8080/ai/audio/transcription` | ||
`controller` 将会调用 `TongYiService` 中的 `audioTranscription` 方法,完成服务请求得到响应。 | ||
|
||
可设置`file_urls`参数,提供一个或多个需要进行语音识别的音视频文件。 | ||
|
||
## 构建和运行 | ||
|
||
1. 修改配置文件 `application.yml` 中的 apikey 为有效的 apikey; | ||
2. 通过 IDE 或者 `./mvnw spring-boot:run` 运行应用程序。 | ||
|
||
## 访问接口 | ||
|
||
使用 curl 工具对接口发起请求: | ||
|
||
```shell | ||
$ curl -X GET "http://localhost:8080/ai/audio/transcription?audioUrls=url1&audioUrls=url2" | ||
|
||
# Response: | ||
D:\Code\spring-cloud-alibaba\05-13-20-47-08.txt | ||
D:\Code\spring-cloud-alibaba\05-13-20-47-09.txt | ||
``` | ||
|
||
返回参数为保存到当前根路径下的音频转录文本文件的路径。 |
123 changes: 123 additions & 0 deletions
123
.../example/tongyi/service/impl/audio/transcription/TongYiAudioTranscriptionServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* Copyright 2023-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.alibaba.cloud.ai.example.tongyi.service.impl.audio.transcription; | ||
|
||
import java.io.BufferedInputStream; | ||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.List; | ||
|
||
import com.alibaba.cloud.ai.example.tongyi.service.AbstractTongYiServiceImpl; | ||
import com.alibaba.cloud.ai.example.tongyi.service.TongYiService; | ||
import com.alibaba.cloud.ai.tongyi.audio.transcription.TongYiAudioTranscriptionClient; | ||
import com.alibaba.cloud.ai.tongyi.audio.transcription.api.AudioTranscriptionPrompt; | ||
import com.alibaba.cloud.ai.tongyi.audio.transcription.api.AudioTranscriptionResult; | ||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParser; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.core.io.UrlResource; | ||
import org.springframework.stereotype.Service; | ||
|
||
/** | ||
* @author xYLiu | ||
* @since 2024/5/15 14:55 | ||
*/ | ||
@Slf4j | ||
@Service | ||
public class TongYiAudioTranscriptionServiceImpl extends AbstractTongYiServiceImpl { | ||
private static final Logger logger = LoggerFactory.getLogger(TongYiService.class); | ||
private final TongYiAudioTranscriptionClient audioTranscriptionClient; | ||
|
||
@Autowired | ||
public TongYiAudioTranscriptionServiceImpl(final TongYiAudioTranscriptionClient audioTranscriptionClient) { | ||
this.audioTranscriptionClient = audioTranscriptionClient; | ||
} | ||
|
||
@Override | ||
public String audioTranscription(String audioUrls) { | ||
|
||
Resource resource; | ||
|
||
try { | ||
resource = new UrlResource(audioUrls); | ||
} | ||
catch (IOException e) { | ||
logger.error("Failed to create resource."); | ||
throw new RuntimeException(e); | ||
} | ||
AudioTranscriptionPrompt audioTranscriptionPrompt = new AudioTranscriptionPrompt(resource); | ||
|
||
return save(audioTranscriptionClient.call(audioTranscriptionPrompt).getResults()); | ||
} | ||
|
||
private String save(List<AudioTranscriptionResult> resultList) { | ||
String currentPath = System.getProperty("user.dir"); | ||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM-dd-HH-mm-ss"); | ||
StringBuilder retPaths = new StringBuilder(); | ||
for (AudioTranscriptionResult audioTranscriptionResult : resultList) { | ||
String tUrl = audioTranscriptionResult.getOutput(); | ||
LocalDateTime now = LocalDateTime.now(); | ||
String fileName = currentPath + File.separator + now.format(formatter) + ".txt"; | ||
retPaths.append(fileName).append("\n"); | ||
try { | ||
URL url = new URL(tUrl); | ||
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | ||
connection.setRequestMethod("GET"); | ||
StringBuilder sb = new StringBuilder(); | ||
int responseCode = connection.getResponseCode(); | ||
if (responseCode == HttpURLConnection.HTTP_OK) { | ||
try (BufferedInputStream in = new BufferedInputStream(connection.getInputStream()); FileOutputStream fileOutputStream = new FileOutputStream(fileName)) { | ||
byte[] dataBuffer = new byte[1024]; | ||
int bytesRead; | ||
while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) { | ||
sb.append(new String(dataBuffer, 0, bytesRead)); | ||
} | ||
JsonObject rootObj = JsonParser.parseString(sb.toString()).getAsJsonObject(); | ||
JsonArray transcriptsArray = rootObj.getAsJsonArray("transcripts"); | ||
|
||
for (var transcriptElement : transcriptsArray) { | ||
JsonObject transcriptObj = transcriptElement.getAsJsonObject(); | ||
String text = transcriptObj.get("text").getAsString(); | ||
fileOutputStream.write(text.getBytes()); | ||
} | ||
logger.info("File downloaded successfully:{}\n", fileName); | ||
} | ||
} | ||
else { | ||
logger.error("The download failed, and the response code:{}", | ||
responseCode); | ||
} | ||
connection.disconnect(); | ||
} | ||
catch (IOException e) { | ||
logger.error("An error occurred during the file download process."); | ||
} | ||
} | ||
return retPaths.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 0 additions & 22 deletions
22
...n/java/com/alibaba/cloud/ai/example/tongyi/service/impl/textembedding/README.md
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.