Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
E
exam_master
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ai-tech
exam_master
Commits
5e0558a5
Commit
5e0558a5
authored
Jun 21, 2024
by
陈立彬
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1.平均分接口优化 2.语音文本提取
parent
8457f3ae
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
267 additions
and
4 deletions
+267
-4
pom.xml
pom.xml
+7
-0
ExamController.java
src/main/java/cn/aibreeze/exam/api/ExamController.java
+7
-2
P2PResponse.java
src/main/java/cn/aibreeze/exam/dto/P2PResponse.java
+82
-0
SessionExamResult.java
src/main/java/cn/aibreeze/exam/dto/SessionExamResult.java
+5
-2
AliyunNLSFacade.java
src/main/java/cn/aibreeze/exam/util/AliyunNLSFacade.java
+150
-0
PeerException.java
src/main/java/cn/aibreeze/exam/util/PeerException.java
+16
-0
No files found.
pom.xml
View file @
5e0558a5
...
@@ -64,6 +64,13 @@
...
@@ -64,6 +64,13 @@
<artifactId>
spring-boot-starter-data-redis
</artifactId>
<artifactId>
spring-boot-starter-data-redis
</artifactId>
</dependency>
</dependency>
<dependency>
<groupId>
com.aliyun
</groupId>
<artifactId>
aliyun-java-sdk-core
</artifactId>
<version>
4.6.4
</version>
</dependency>
</dependencies>
</dependencies>
<build>
<build>
...
...
src/main/java/cn/aibreeze/exam/api/ExamController.java
View file @
5e0558a5
...
@@ -136,6 +136,8 @@ public class ExamController {
...
@@ -136,6 +136,8 @@ public class ExamController {
@RequestParam
(
"user_id"
)
String
userId
)
{
@RequestParam
(
"user_id"
)
String
userId
)
{
Double
avgScore
=
0
D
;
Double
avgScore
=
0
D
;
Double
totalScore
=
0
D
;
int
examNum
=
0
;
LambdaQueryWrapper
<
ElehSessionQaRecordDo
>
queryWrapper
=
new
LambdaQueryWrapper
<>();
LambdaQueryWrapper
<
ElehSessionQaRecordDo
>
queryWrapper
=
new
LambdaQueryWrapper
<>();
queryWrapper
.
eq
(
ElehSessionQaRecordDo:
:
getTenant
,
tenant
);
queryWrapper
.
eq
(
ElehSessionQaRecordDo:
:
getTenant
,
tenant
);
...
@@ -143,11 +145,14 @@ public class ExamController {
...
@@ -143,11 +145,14 @@ public class ExamController {
queryWrapper
.
eq
(
ElehSessionQaRecordDo:
:
getUserId
,
userId
);
queryWrapper
.
eq
(
ElehSessionQaRecordDo:
:
getUserId
,
userId
);
List
<
ElehSessionQaRecordDo
>
qaRecordDos
=
qaRecordMapper
.
selectList
(
queryWrapper
);
List
<
ElehSessionQaRecordDo
>
qaRecordDos
=
qaRecordMapper
.
selectList
(
queryWrapper
);
if
(!
CollectionUtils
.
isEmpty
(
qaRecordDos
))
{
if
(!
CollectionUtils
.
isEmpty
(
qaRecordDos
))
{
double
sum
=
qaRecordDos
.
stream
().
mapToDouble
(
ElehSessionQaRecordDo:
:
getScore
).
sum
();
totalScore
=
qaRecordDos
.
stream
().
mapToDouble
(
ElehSessionQaRecordDo:
:
getScore
).
sum
();
avgScore
=
sum
/
qaRecordDos
.
size
();
examNum
=
qaRecordDos
.
size
();
avgScore
=
totalScore
/
examNum
;
}
}
SessionExamResult
result
=
new
SessionExamResult
();
SessionExamResult
result
=
new
SessionExamResult
();
result
.
setAvgScore
(
avgScore
);
result
.
setAvgScore
(
avgScore
);
result
.
setTotalScore
(
totalScore
);
result
.
setExamNum
(
examNum
);
return
ApiResponse
.
ok
(
result
);
return
ApiResponse
.
ok
(
result
);
}
}
...
...
src/main/java/cn/aibreeze/exam/dto/P2PResponse.java
0 → 100644
View file @
5e0558a5
package
cn
.
aibreeze
.
exam
.
dto
;
import
cn.aibreeze.exam.util.PeerException
;
import
java.io.Serializable
;
public
class
P2PResponse
<
T
extends
Serializable
>
implements
Serializable
{
private
boolean
success
;
private
T
body
;
private
String
error
;
/**
* 成功响应
* @param body
* @return
* @param <T>
*/
public
final
static
<
T
extends
Serializable
>
P2PResponse
<
T
>
ok
(
T
body
)
{
P2PResponse
<
T
>
response
=
new
P2PResponse
<>();
response
.
setSuccess
(
true
);
response
.
setError
(
""
);
response
.
setBody
(
body
);
return
response
;
}
/**
* 失败响应
* @param error
* @return
* @param <T>
*/
public
final
static
<
T
extends
Serializable
>
P2PResponse
<
T
>
fail
(
String
error
)
{
P2PResponse
<
T
>
response
=
new
P2PResponse
<>();
response
.
setSuccess
(
false
);
response
.
setError
(
error
);
response
.
setBody
(
null
);
return
response
;
}
/**
* 获取响应结果并自带检查
* @return
* @param <T>
*/
public
final
T
body
()
{
if
(
success
)
{
return
getBody
();
}
else
{
throw
new
PeerException
(
this
.
error
);
}
}
public
boolean
isSuccess
()
{
return
success
;
}
public
void
setSuccess
(
boolean
success
)
{
this
.
success
=
success
;
}
public
String
getError
()
{
return
error
;
}
public
void
setError
(
String
error
)
{
this
.
error
=
error
;
}
public
T
getBody
()
{
return
body
;
}
public
void
setBody
(
T
body
)
{
this
.
body
=
body
;
}
}
src/main/java/cn/aibreeze/exam/dto/SessionExamResult.java
View file @
5e0558a5
...
@@ -8,9 +8,12 @@ import java.io.Serializable;
...
@@ -8,9 +8,12 @@ import java.io.Serializable;
@Data
@Data
public
class
SessionExamResult
implements
Serializable
{
public
class
SessionExamResult
implements
Serializable
{
private
String
result
;
@JsonProperty
(
"total_score"
)
private
double
totalScore
;
@JsonProperty
(
"avg_score"
)
@JsonProperty
(
"avg_score"
)
private
D
ouble
avgScore
;
private
d
ouble
avgScore
;
@JsonProperty
(
"exam_num"
)
private
int
examNum
;
}
}
src/main/java/cn/aibreeze/exam/util/AliyunNLSFacade.java
0 → 100644
View file @
5e0558a5
package
cn
.
aibreeze
.
exam
.
util
;
import
cn.aibreeze.exam.dto.P2PResponse
;
import
com.alibaba.fastjson2.JSONArray
;
import
com.alibaba.fastjson2.JSONObject
;
import
com.aliyuncs.CommonRequest
;
import
com.aliyuncs.CommonResponse
;
import
com.aliyuncs.DefaultAcsClient
;
import
com.aliyuncs.IAcsClient
;
import
com.aliyuncs.exceptions.ClientException
;
import
com.aliyuncs.http.FormatType
;
import
com.aliyuncs.http.MethodType
;
import
com.aliyuncs.profile.DefaultProfile
;
import
jakarta.annotation.PostConstruct
;
import
lombok.SneakyThrows
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Component
;
@Component
@Slf4j
public
class
AliyunNLSFacade
{
@Value
(
"${aliyun.nls.ak:LTAI5tAtpgLTbH3s7YZceoU3}"
)
private
String
ak
;
@Value
(
"${aliyun.nls.sk:gMfI7uPmu6ohPmE3NeD31dWORX6QMb}"
)
private
String
sk
;
@Value
(
"${aliyun.nls.app_key:j31oKhTHSqQp6QJO}"
)
private
String
appKey
;
@Value
(
"${aliyun.nls.callback_url:http://aib.hc.wxpai.cn/apis/openai/v1/plugin/nls_callback"
)
private
String
callbackUrl
;
private
IAcsClient
client
;
final
String
regionId
=
"cn-shanghai"
;
final
String
endpointName
=
"cn-shanghai"
;
final
String
product
=
"nls-filetrans"
;
final
String
domain
=
"filetrans.cn-shanghai.aliyuncs.com"
;
@SneakyThrows
@PostConstruct
public
void
init
()
{
DefaultProfile
.
addEndpoint
(
endpointName
,
regionId
,
product
,
domain
);
DefaultProfile
profile
=
DefaultProfile
.
getProfile
(
regionId
,
ak
,
sk
);
client
=
new
DefaultAcsClient
(
profile
);
}
/**
* 从媒体中获取文本
* @param mediaUrl
* @return
*/
public
P2PResponse
<
String
>
getTextFromMedia
(
String
mediaUrl
)
{
CommonRequest
postRequest
=
new
CommonRequest
();
postRequest
.
setDomain
(
domain
);
// 设置域名,固定值。
postRequest
.
setVersion
(
"2018-08-17"
);
// 设置中国站的版本号。
postRequest
.
setAction
(
"SubmitTask"
);
// 设置action,固定值。
postRequest
.
setProduct
(
"nls-filetrans"
);
// 设置产品名称,固定值。
// 设置录音文件识别请求参数,以JSON字符串的格式设置到请求Body中。
JSONObject
taskObject
=
new
JSONObject
();
taskObject
.
put
(
"appkey"
,
appKey
);
// 项目的Appkey,获取Appkey请前往控制台:https://nls-portal.console.aliyun.com/applist
taskObject
.
put
(
"file_link"
,
mediaUrl
);
// 设置录音文件的链接
taskObject
.
put
(
"version"
,
"4.0"
);
// 新接入请使用4.0版本,已接入(默认2.0)如需维持现状,请注释掉该参数设置。
taskObject
.
put
(
"enable_callback"
,
true
);
taskObject
.
put
(
"callback_url"
,
callbackUrl
);
taskObject
.
put
(
"auto_split"
,
true
);
//是否开启智能分轨
taskObject
.
put
(
"supervise_type"
,
2
);
//说话人分离的确定人数方式
String
task
=
taskObject
.
toJSONString
();
postRequest
.
putBodyParameter
(
"Task"
,
task
);
// 设置以上JSON字符串为Body参数。
postRequest
.
setMethod
(
MethodType
.
POST
);
// 设置为POST方式请求。
postRequest
.
setHttpContentType
(
FormatType
.
JSON
);
//当aliyun-java-sdk-core 版本为4.6.0及以上时,请取消该行注释
String
taskId
=
""
;
// 获取录音文件识别请求任务的ID,以供识别结果查询使用。
try
{
CommonResponse
postResponse
=
client
.
getCommonResponse
(
postRequest
);
if
(
postResponse
.
getHttpStatus
()
==
200
)
{
JSONObject
result
=
JSONObject
.
parseObject
(
postResponse
.
getData
());
String
statusText
=
result
.
getString
(
"StatusText"
);
if
(
"SUCCESS"
.
equals
(
statusText
))
{
System
.
out
.
println
(
"录音文件识别请求成功响应: "
+
result
.
toJSONString
());
taskId
=
result
.
getString
(
"TaskId"
);
return
P2PResponse
.
ok
(
taskId
);
}
else
{
log
.
error
(
"录音文件识别请求失败: "
+
result
.
toJSONString
());
return
P2PResponse
.
fail
(
result
.
toJSONString
());
}
}
else
{
log
.
error
(
"录音文件识别请求失败,Http错误码:"
+
postResponse
.
getHttpStatus
());
log
.
error
(
"录音文件识别请求失败响应:"
+
JSONObject
.
toJSONString
(
postResponse
));
return
P2PResponse
.
fail
(
JSONObject
.
toJSONString
(
postResponse
));
}
}
catch
(
ClientException
e
)
{
e
.
printStackTrace
();
return
P2PResponse
.
fail
(
e
.
getMessage
());
}
}
@SneakyThrows
public
P2PResponse
<
String
>
getAsrResult
(
String
taskId
)
{
CommonRequest
getRequest
=
new
CommonRequest
();
getRequest
.
setSysDomain
(
domain
);
// 设置域名,固定值。
getRequest
.
setSysVersion
(
"2018-08-17"
);
// 设置中国站的版本号。
getRequest
.
setSysAction
(
"GetTaskResult"
);
// 设置action,固定值。
getRequest
.
setSysProduct
(
"nls-filetrans"
);
// 设置产品名称,固定值。
getRequest
.
putQueryParameter
(
"TaskId"
,
taskId
);
// 设置任务ID为查询参数。
getRequest
.
setSysMethod
(
MethodType
.
GET
);
// 设置为GET方式的请求。
CommonResponse
getResponse
=
client
.
getCommonResponse
(
getRequest
);
if
(
getResponse
.
getHttpStatus
()
!=
200
)
{
log
.
error
(
"识别结果查询请求失败,Http错误码: "
+
getResponse
.
getHttpStatus
());
log
.
error
(
"识别结果查询请求失败: "
+
getResponse
.
getData
());
return
P2PResponse
.
fail
(
getResponse
.
getData
());
}
JSONObject
result
=
JSONObject
.
parseObject
(
getResponse
.
getData
());
String
statusText
=
result
.
getString
(
"StatusText"
);
if
(
"SUCCESS"
.
equals
(
statusText
)
&&
21050000
==
result
.
getInteger
(
"StatusCode"
))
{
JSONObject
res
=
result
.
getJSONObject
(
"Result"
);
JSONArray
items
=
res
.
getJSONArray
(
"Sentences"
);
log
.
info
(
"内容:{}"
,
items
.
toJSONString
());
StringBuilder
sb
=
new
StringBuilder
();
for
(
Object
item
:
items
)
{
JSONObject
line
=
(
JSONObject
)
item
;
if
(
StringUtils
.
equals
(
line
.
getString
(
"ChannelId"
),
"0"
))
{
if
(
StringUtils
.
equals
(
"1"
,
line
.
getString
(
"SpeakerId"
)))
{
sb
.
append
(
"说话人1:"
+
line
.
getString
(
"Text"
)
+
"\n"
);
}
else
if
(
StringUtils
.
equals
(
"2"
,
line
.
getString
(
"SpeakerId"
)))
{
sb
.
append
(
"说话人2:"
+
line
.
getString
(
"Text"
)
+
"\n"
);
}
else
if
(
StringUtils
.
equals
(
"3"
,
line
.
getString
(
"SpeakerId"
)))
{
sb
.
append
(
"说话人3:"
+
line
.
getString
(
"Text"
)
+
"\n"
);
}
System
.
out
.
println
(
"美容师:"
+
line
.
getString
(
"Text"
));
}
}
return
P2PResponse
.
ok
(
sb
.
toString
());
}
else
{
log
.
error
(
"查询结果: "
+
result
.
toJSONString
());
return
P2PResponse
.
fail
(
result
.
toJSONString
());
}
}
}
src/main/java/cn/aibreeze/exam/util/PeerException.java
0 → 100644
View file @
5e0558a5
package
cn
.
aibreeze
.
exam
.
util
;
import
lombok.EqualsAndHashCode
;
import
lombok.Getter
;
import
lombok.RequiredArgsConstructor
;
import
java.io.Serializable
;
@RequiredArgsConstructor
@EqualsAndHashCode
(
callSuper
=
false
)
@Getter
public
class
PeerException
extends
RuntimeException
implements
Serializable
{
private
final
String
error
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment