调用dify,解决时间问题,源语料为空的问题

This commit is contained in:
spllzh
2025-09-07 23:58:50 +08:00
parent b475a864c6
commit d84aa8ae78
7 changed files with 290 additions and 49 deletions

View File

View File

@@ -1,27 +0,0 @@
-- 测试用户数据
-- 密码都是 123456 的MD5加密值
-- 插入测试用户
INSERT INTO `user` (`user_name`, `email`, `password`, `avatar`) VALUES
('admin', 'admin@example.com', 'e10adc3949ba59abbe56e057f20f883e', '/avatar/admin.jpg'),
('user1', 'user1@example.com', 'e10adc3949ba59abbe56e057f20f883e', '/avatar/user1.jpg'),
('user2', 'user2@example.com', 'e10adc3949ba59abbe56e057f20f883e', '/avatar/user2.jpg'),
('test', 'test@example.com', 'e10adc3949ba59abbe56e057f20f883e', '/avatar/test.jpg');
-- 插入测试角色
INSERT INTO `role` (`role_name`) VALUES
('管理员'),
('普通用户'),
('访客');
-- 插入用户角色关联
INSERT INTO `user_role` (`user_id`, `role_id`) VALUES
(1, 1), -- admin -> 管理员
(2, 2), -- user1 -> 普通用户
(3, 2), -- user2 -> 普通用户
(4, 3); -- test -> 访客

View File

@@ -1,17 +0,0 @@
-- 更新用户表结构,添加原始密码字段
-- 执行时间2025-08-07
-- 为user表添加original_password字段
ALTER TABLE `user`
ADD COLUMN `original_password` varchar(255) DEFAULT NULL COMMENT '原始密码'
AFTER `password`;
-- 更新现有用户的原始密码字段如果密码是123456的MD5值则设置原始密码为123456
UPDATE `user`
SET `original_password` = '123456'
WHERE `password` = 'e10adc3949ba59abbe56e057f20f883e';
-- 为original_password字段添加索引可选
-- ALTER TABLE `user` ADD INDEX `idx_original_password` (`original_password`);

79
src/main/sql/语音识别 Normal file
View File

@@ -0,0 +1,79 @@
paraformer-v2中文包含中文普通话和各种方言上海话、吴语、闽南语、东北话、甘肃话、贵州话、河南话、湖北话、湖南话、江西话、宁夏话、山西话、陕西话、山东话、四川话、天津话、云南话、粤语
英文 日语 韩语
paraformer-mtl-v1中文普通话、中文方言粤语、吴语、闽南语、东北话、甘肃话、贵州话、河南话、湖北话、湖南话、宁夏话、山西话、陕西话、山东话、四川话、天津话、英语、日语、韩语、西班牙语、印尼语、法语、德语、意大利语、马来语
"task_id": "a0c8f837-e31c-428c-9746-f782730194b3",
"task_status": "SUCCEEDED",
"submit_time": "2025-08-22 20:53:53.242",
"scheduled_time": "2025-08-22 20:53:53.278",
"end_time": "2025-08-22 20:54:03.556",
"results": [
{
"file_url": "https://api.huayang-star.com/api/audio/1957424645898579969_a2db5d76-b52c-4289-9c7e-ed55d7f15e42.mp3",
"transcription_url": "https://dashscope-result-bj.oss-cn-beijing.aliyuncs.com/prod/paraformer-v2/20250822/20%3A54/0fa28199-7c18-4a6a-b4da-796dca0f16f3-1.json?Expires\u003d1755953643\u0026OSSAccessKeyId\u003dLTAI5tQZd8AEcZX6KZV4G8qL\u0026Signature\u003dnrKHcajG6o0VnMMs%2BP001vZ5yyk%3D",
"subtask_status": "SUCCEEDED"
},
{
"file_url": "https://api.huayang-star.com/api/audio/1957424645898579969_a2db5d76-b52c-4289-9c7e-ed55d7f15e42.mp3",
"transcription_url": "https://dashscope-result-bj.oss-cn-beijing.aliyuncs.com/prod/paraformer-v2/20250822/20%3A54/1f25c924-40bf-485b-a5c9-132952b71527-1.json?Expires\u003d1755953643\u0026OSSAccessKeyId\u003dLTAI5tQZd8AEcZX6KZV4G8qL\u0026Signature\u003dbGDt9EVPeOdIJUVN0jdD9MXe80M%3D",
"subtask_status": "SUCCEEDED"
},
{
"file_url": "https://api.huayang-star.com/api/audio/1957424665985101825_7b9b03f4-a458-4eb6-ac7b-b5cf827a2315.mp3",
"transcription_url": "https://dashscope-result-bj.oss-cn-beijing.aliyuncs.com/prod/paraformer-v2/20250822/20%3A54/153af9fe-cc12-4ffe-bdb0-a7d2c884e2cc-1.json?Expires\u003d1755953643\u0026OSSAccessKeyId\u003dLTAI5tQZd8AEcZX6KZV4G8qL\u0026Signature\u003dI0Ymz%2FTFBL2yiPcr%2FdA%2FbxGSRk8%3D",
"subtask_status": "SUCCEEDED"
}
],
"task_metrics": {
"TOTAL": 3,
"SUCCEEDED": 3,
"FAILED": 0
}
}

View File

@@ -5,9 +5,8 @@ package com.rj.service;
* Date: 2025/8/5 19:02
**/
import com.rj.Langchain4jHeima20250803Application;
import com.rj.AISmartCard20250803Application;
import com.rj.entity.AudioManagement;
import com.rj.service.impl.AudioManagementServiceImpl;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@@ -18,7 +17,7 @@ import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest(classes = Langchain4jHeima20250803Application.class)
//@SpringBootTest(classes = AISmartCard20250803Application.class)
class AudioManagementServiceTest {
@Autowired

View File

@@ -17,8 +17,8 @@ import static org.junit.jupiter.api.Assertions.*;
* @author 李中华 ,spllzh
* @since 2025-08-07
*/
@SpringBootTest
@ActiveProfiles("test")
//@SpringBootTest
//@ActiveProfiles("test")
public class AudioManagementStatisticsServiceTest {
@Autowired

View File

@@ -0,0 +1,207 @@
/*
* Copyright (C) Alibaba Group. All Rights Reserved.
* MIT License (https://opensource.org/licenses/MIT)
*/
package com.rj.speech;
import com.alibaba.dashscope.audio.asr.transcription.*;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.ApiKey;
import com.google.gson.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.*;
import java.net.HttpURLConnection;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class SenseVoiceParser {
private static final List<String> EMOTION_LIST =
Arrays.asList("NEUTRAL", "HAPPY", "ANGRY", "SAD");
private static final List<String> EVENT_LIST =
Arrays.asList("Speech", "Applause", "BGM", "Laughter");
private static final List<String> ALL_TAGS =
Arrays.asList(
"Speech",
"Applause",
"BGM",
"Laughter",
"NEUTRAL",
"HAPPY",
"ANGRY",
"SAD",
"SPECIAL_TOKEN_1");
/**
* 本工具用于解析 sensevoice 识别结果
*
* @param data json格式的sensevoice转写结果
* @param keepTrans 是否保留转写文本
* @param keepEmotions 是否保留情感标签
* @param keepEvents 是否保留事件标签
* @return
*/
public static JsonObject parseSenseVoiceResult(
JsonObject data, boolean keepTrans, boolean keepEmotions, boolean keepEvents) {
List<String> tagsToCleanup =
ALL_TAGS.stream()
.flatMap(tag -> Stream.of("<|" + tag + "|> ", "<|/" + tag + "|>", "<|" + tag + "|>"))
.collect(Collectors.toList());
JsonArray transcripts = data.getAsJsonArray("transcripts");
for (JsonElement transcriptElement : transcripts) {
JsonObject transcript = transcriptElement.getAsJsonObject();
JsonArray sentences = transcript.getAsJsonArray("sentences");
for (JsonElement sentenceElement : sentences) {
JsonObject sentence = sentenceElement.getAsJsonObject();
String text = sentence.get("text").getAsString();
if (keepEmotions) {
extractTags(sentence, text, EMOTION_LIST, "emotion");
}
if (keepEvents) {
extractTags(sentence, text, EVENT_LIST, "event");
}
if (keepTrans) {
String cleanText = getCleanText(text, tagsToCleanup);
sentence.addProperty("text", cleanText);
} else {
sentence.remove("text");
}
}
if (keepTrans) {
transcript.addProperty(
"text", getCleanText(transcript.get("text").getAsString(), tagsToCleanup));
} else {
transcript.remove("text");
}
JsonArray filteredSentences = new JsonArray();
for (JsonElement sentenceElement : sentences) {
JsonObject sentence = sentenceElement.getAsJsonObject();
if (sentence.has("text") || sentence.has("emotion") || sentence.has("event")) {
filteredSentences.add(sentence);
}
}
transcript.add("sentences", filteredSentences);
}
return data;
}
private static void extractTags(
JsonObject sentence, String text, List<String> tagList, String key) {
String pattern = "<\\|(" + String.join("|", tagList) + ")\\|>";
Pattern compiledPattern = Pattern.compile(pattern);
Matcher matcher = compiledPattern.matcher(text);
Set<String> tags = new HashSet<>();
while (matcher.find()) {
tags.add(matcher.group(1));
}
if (!tags.isEmpty()) {
JsonArray tagArray = new JsonArray();
tags.forEach(tagArray::add);
sentence.add(key, tagArray);
} else {
sentence.remove(key);
}
}
private static String getCleanText(String text, List<String> tagsToCleanup) {
for (String tag : tagsToCleanup) {
text = text.replace(tag, "");
}
return text.replaceAll("\\s{2,}", " ").trim();
}
}
public class TranscriptFilesByRestfulApi {
public static void main(String[] args) throws NoApiKeyException {
// create transcription params, use getDashScopeApiKey to get api key.
TranscriptionParam param =
TranscriptionParam.builder()
.apiKey(getDashScopeApiKey()) // set your apikey in config.Environments.yourApikey
.model("sensevoice-v1") // 'paraformer-8k-v1', 'paraformer-mtl-v1'
.fileUrls(
Arrays.asList(
// "https://api.huayang-star.com/api/audio/1957424645898579969_a2db5d76-b52c-4289-9c7e-ed55d7f15e42.mp3",
// "https://api.huayang-star.com/api/audio/1957424665985101825_7b9b03f4-a458-4eb6-ac7b-b5cf827a2315.mp3",
"https://api.huayang-star.com/api/audio/1957424645898579969_a2db5d76-b52c-4289-9c7e-ed55d7f15e42.mp3"))
.build();
try {
Transcription transcription = new Transcription();
// post request to transcribe service
TranscriptionResult result = transcription.asyncCall(param);
System.out.println("RequestId: " + result.getRequestId());
// waiting for transcription finish
result =
transcription.wait(
TranscriptionQueryParam.FromTranscriptionParam(param, result.getTaskId()));
// get Transcription result after waiting
List<TranscriptionTaskResult> taskResultList = result.getResults();
if (taskResultList != null && !taskResultList.isEmpty()) {
for (TranscriptionTaskResult taskResult : taskResultList) {
// get Transcription rersult url
String transcriptionUrl = taskResult.getTranscriptionUrl();
// get transcription result by http get
HttpURLConnection connection =
(HttpURLConnection) new URL(transcriptionUrl).openConnection();
connection.setRequestMethod("GET");
connection.connect();
BufferedReader reader =
new BufferedReader(new InputStreamReader(connection.getInputStream()));
// format json print
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonElement jsonResult = gson.fromJson(reader, JsonObject.class);
System.out.println(gson.toJson(jsonResult));
// System.out.println(
// gson.toJson(
// SenseVoiceParser.parseSenseVoiceResult(
// jsonResult.getAsJsonObject(), true, true, true)));
}
}
} catch (Exception e) {
System.out.println("error: " + e);
}
System.exit(0);
}
/**
* Set your DashScope API key. More information: <a
* href="https://help.aliyun.com/document_detail/2712195.html">...</a> In fact, if you have set
* DASHSCOPE_API_KEY in your environment variable, you can ignore this, and the SDK will
* automatically get the api_key from the environment variable
*/
private static String getDashScopeApiKey() throws NoApiKeyException {
String dashScopeApiKey = null;
try {
ApiKey apiKey = new ApiKey();
dashScopeApiKey = apiKey.getApiKey(null); // Retrieve from environment variable.
} catch (NoApiKeyException e) {
System.out.println("No API key found in environment.");
}
if (dashScopeApiKey == null) {
// If you cannot set api_key in your environment variable,
// you can set it here by code
dashScopeApiKey = "your-dashscope-api-key";
}
return dashScopeApiKey;
}
}