Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
eshiled-new
/
funasr_web
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit b8c6daa4
authored
Oct 08, 2024
by
‘chizw’
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
[+]脚本更新
1 parent
6beeae95
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
275 additions
and
37 deletions
funasr_service.py
mysql_service/funasr_db.py
utils/ali/alinlp_sdk.py
utils/ali/dashscope_sdk.py
utils/finetune.py
funasr_service.py
View file @
b8c6daa
...
@@ -246,8 +246,6 @@ def test_comparison_transform_output(sentence_info):
...
@@ -246,8 +246,6 @@ def test_comparison_transform_output(sentence_info):
print
(
f
"[{time}] {spk}:{text}"
)
print
(
f
"[{time}] {spk}:{text}"
)
return
output
return
output
def
task_compensate_send
():
def
task_compensate_send
():
try
:
try
:
log
.
info
(
"task_compensate_send start"
)
log
.
info
(
"task_compensate_send start"
)
...
@@ -259,3 +257,32 @@ def task_compensate_send():
...
@@ -259,3 +257,32 @@ def task_compensate_send():
except
Exception
as
e
:
except
Exception
as
e
:
log
.
error
(
"task_compensate_send error"
,
e
)
log
.
error
(
"task_compensate_send error"
,
e
)
def
adjust_time_with_milliseconds
(
offset_milliseconds
,
duration_milliseconds
):
# 创建一个基于当天00:00:00的时间点
base_time
=
datetime
.
datetime
.
now
()
.
replace
(
hour
=
0
,
minute
=
0
,
second
=
0
,
microsecond
=
0
)
# 将毫秒转换为秒
offset_seconds
=
int
(
offset_milliseconds
/
1000.0
)
duration_seconds
=
int
(
duration_milliseconds
/
1000.0
)
# 计算偏移时间和持续时间
offset_time
=
datetime
.
timedelta
(
seconds
=
offset_seconds
)
duration_time
=
datetime
.
timedelta
(
seconds
=
duration_seconds
)
# 应用偏移时间
adjusted_time
=
base_time
+
offset_time
# 应用持续时间
end_time
=
adjusted_time
+
duration_time
formatted_start_time
=
adjusted_time
.
strftime
(
'
%
M:
%
S'
)
formatted_end_time
=
end_time
.
strftime
(
'
%
H:
%
M:
%
S'
)
# 检查是否跨越了午夜
if
end_time
.
day
>
base_time
.
day
:
end_time
=
datetime
.
datetime
.
combine
(
base_time
.
date
()
+
datetime
.
timedelta
(
days
=
1
),
end_time
.
time
())
formatted_end_time
=
end_time
.
strftime
(
'
%
M:
%
S'
)
return
formatted_start_time
,
formatted_end_time
def
careful_transform_output
(
output_data
):
json_output
=
json
.
loads
(
output_data
)
for
item
in
json_output
:
start_time
,
end_time
=
adjust_time_with_milliseconds
(
item
.
get
(
"offset"
),
item
.
get
(
"duration"
))
role
=
item
.
get
(
"role"
)
content
=
item
.
get
(
"content"
)
return
f
"{start_time}-{end_time} 角色{role}:{content}
\n
"
mysql_service/funasr_db.py
View file @
b8c6daa
...
@@ -144,7 +144,16 @@ and ibr.created_date >= %s
...
@@ -144,7 +144,16 @@ and ibr.created_date >= %s
and ibr.recording_duration/60>20 and ibr.recording_duration/60<31
and ibr.recording_duration/60>20 and ibr.recording_duration/60<31
and ieb.`language` !=1
and ieb.`language` !=1
and aamr.output_data is not null
and aamr.output_data is not null
and aamr.task_id = '1acddf88-69c8-11ef-b08a-00163e14f7ff'
#
and aamr.task_id = '1acddf88-69c8-11ef-b08a-00163e14f7ff'
group by ibr.id order by ibr.created_date desc"""
group by ibr.id order by ibr.created_date desc"""
)
)
return
pool
.
select_all
(
sql
,
(
created_date
,
'
%
"role": 2
%
'
))
return
pool
.
select_all
(
sql
,
(
created_date
,
'
%
"role": 2
%
'
))
def
select_reception_material_data
(
table_name
:
str
,
config_id
):
sql
=
(
f
"""select * from {table_name} t
where
t.deleted_flag = 'N' and t.merchant_config_id =
%
s"""
)
return
pool
.
select_all
(
sql
,
(
config_id
))
utils/ali/alinlp_sdk.py
0 → 100644
View file @
b8c6daa
# -*- coding: UTF-8 -*-
import
json
import
os
# 这里以分词为例,其它算法的API名称和参数请参考文档
from
aliyunsdkalinlp.request.v20200629
import
GetWsCustomizedChGeneralRequest
from
aliyunsdkcore.client
import
AcsClient
from
aliyunsdkcore.acs_exception.exceptions
import
ClientException
from
aliyunsdkcore.acs_exception.exceptions
import
ServerException
from
aliyunsdkcore.request
import
CommonRequest
# /**
# * 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
# * 此处以把AccessKey和AccessKeySecret保存在环境变量为例说明。您也可以根据业务需要,保存到配置文件里。
# * 强烈建议不要把AccessKey和AccessKeySecret保存到代码里,会存在密钥泄漏风险
# */
# access_key_id = os.environ['NLP_AK_ENV']
# access_key_secret = os.environ['NLP_SK_ENV']
# 创建AcsClient实例
client
=
AcsClient
(
"LTAI5tNChXFxBNkSuoUrZe9M"
,
"1mhxqTSu484avmUIeucmXrRJ0Vq5ag"
,
"cn-hangzhou"
)
request
=
CommonRequest
()
# domain和version是固定值
request
.
set_domain
(
'alinlp.cn-hangzhou.aliyuncs.com'
)
request
.
set_version
(
'2020-06-29'
)
# action name可以在API文档里查到
request
.
set_action_name
(
'GetSaChGeneral'
)
# 需要add哪些param可以在API文档里查到
request
.
add_query_param
(
'ServiceCode'
,
'alinlp'
)
request
.
add_query_param
(
'Text'
,
"""
"""
)
request
.
add_query_param
(
'TokenizerId'
,
'GENERAL_CHN'
)
response
=
client
.
do_action_with_exception
(
request
)
resp_obj
=
json
.
loads
(
response
)
print
(
resp_obj
)
# request = GetWsCustomizedChGeneralRequest.GetWsCustomizedChGeneralRequest()
# request = GetSaChGeneralizedChGeneralRequest.GetWsCustomizedChGeneralRequest()
# request.set_Text("云麦好轻智能体脂秤精准蓝牙秤体重测人体脂肪秤家用称健康电子秤")
# request.set_OutType("1")
# request.set_ServiceCode("alinlp")
# request.set_TokenizerId("GENERAL_CHN")
# response = client.do_action_with_exception(request)
# resp_obj = json.loads(response)
# print(resp_obj)
\ No newline at end of file
\ No newline at end of file
utils/ali/dashscope_sdk.py
0 → 100644
View file @
b8c6daa
from
dashscope
import
MultiModalConversation
import
dashscope
import
json
from
urllib
import
request
from
http
import
HTTPStatus
base_path
=
"D:/software/python/project/funasr_web/audios/"
api_key
=
"sk-33d44eb3658a4e9bad10c81b7488d6b3"
def
call_with_local_file
():
"""Sample of use local file.
linux&mac file schema: file:///home/audios/test.wav
windows file schema: file://D:/audios/test.wav
"""
local_file_path1
=
f
'{base_path}audioscut_.merged_1722503770172172249931114211_temp.mp3'
local_file_path2
=
f
'{base_path}00e8a520-61b8-11ef-aa99-00163e14f7ff_cut.mp3'
messages
=
[{
'role'
:
'system'
,
'content'
:
[{
'text'
:
"""
你是一名资深的销售顾问,擅长通过对话音频内容分析销售人员在介绍产品过程存在哪些问题,提出改进建议,帮助销售人员提高产品成交成功率。
"""
# 例如:介绍注意事项时,语速快而平淡,没有感情,容易让客户反感;
# 面对客户不想听的情况时,可能出于对质检考核的理解,一定要照本宣科的把所有延保内容念完,这种僵硬的执行可能造成反感。
}]
},
{
'role'
:
'user'
,
'content'
:
[
# {
# 'audio': "https://glsk-oss.oss-cn-shenzhen.aliyuncs.com/quality/cut_merged_172560773968317256039029708_23163101.mp3"
# },
{
'audio'
:
"https://glsk-oss.oss-cn-shenzhen.aliyuncs.com/quality/cut_merged_172464880779217246448610883_10137502.mp3"
},
{
'audio'
:
"https://glsk-oss.oss-cn-shenzhen.aliyuncs.com/quality/cut_merged_172464880779217246448610883_10437502.mp3"
},
{
'audio'
:
"https://glsk-oss.oss-cn-shenzhen.aliyuncs.com/quality/cut_merged_172464880779217246448610883_10737502.mp3"
},
{
'text'
:
"""
请根据提供的音频,分析说话人的情绪、性别、所处环境及情感,并针对销售人员的语速、语调、感情、话术技巧、礼貌程度、口齿清晰度、
逻辑条理性、考虑顾客感受、语言表达及说话方式等方面,评估其表现并提出改进建议。
【输出格式】
请严格按照标准JSON格式输出结果:
{
"mood": "销售人员的情绪(如:高兴、兴奋、平淡、愤怒、焦虑等)",
"sex": "销售人员的性别(如:男、女)",
"environment": "对话现场所处的环境",
"emotion": "销售人员的情感(如:乐观、冷漠、厌恶、怀疑等)",
"evaluate": "详细评价销售人员在营销时与顾客对话过程中是否存在不足之处",
"suggest": "针对存在的问题,提出具体的改进建议"
}
"""
},
]
}
]
# qwen-audio-turbo 通义千问大规模语音模型增强版。增强了语音识别、语音定位、说话人信息识别和音乐分析鉴赏的能力。
# qwen2-audio-instruct 通义千问Audio更新增强版。拓展音频多模态理解和生成能力,额外提供语音聊天与音频分析能力,能够实现自由灵活的音频交互。
# qwen-audio-chat 通义千问Audio支持灵活的交互方式,包括全音频类型、多轮问答、基于音频能力的推理与创作等能力的模型,同时支持说话人情绪,
# 性别识别,环境识别,音乐的乐器、风格识别等多样的音频理解能力。
response
=
MultiModalConversation
.
call
(
model
=
'qwen2-audio-instruct'
,
messages
=
messages
,
top_p
=
1.0
,
max_tokens
=
2000
,
top_k
=
100
,
api_key
=
api_key
,
)
print
(
response
)
def
get_sensevoice_data
(
urls
):
dashscope
.
api_key
=
api_key
task_response
=
dashscope
.
audio
.
asr
.
Transcription
.
async_call
(
model
=
'sensevoice-v1'
,
file_urls
=
urls
,
language_hints
=
[
'zh'
],
)
transcription_response
=
dashscope
.
audio
.
asr
.
Transcription
.
wait
(
task
=
task_response
.
output
.
task_id
)
if
transcription_response
.
status_code
==
HTTPStatus
.
OK
:
results
=
transcription_response
.
output
[
'results'
]
print
(
results
)
for
transcription
in
transcription_response
.
output
[
'results'
]:
url
=
transcription
[
'transcription_url'
]
result
=
json
.
loads
(
request
.
urlopen
(
url
)
.
read
()
.
decode
(
'utf8'
))
print
(
json
.
dumps
(
result
,
indent
=
4
,
ensure_ascii
=
False
))
print
(
'transcription done!'
)
else
:
print
(
'Error: '
,
transcription_response
.
output
.
message
)
if
__name__
==
'__main__'
:
urls
=
[
"https://glsk-oss.oss-cn-shenzhen.aliyuncs.com/quality/cut_merged_1722503770172172249931114211_1038720.mp3"
]
# call_with_local_file()
get_sensevoice_data
(
urls
)
"""
以上这些音频是车辆延保销售人员对客户介绍产品的语录。
你要根据音频分析出说话人的情绪,性别,环境和情感,不要复述音频的内容,
针对音频中销售人员的语速、语调、感情、话术技巧、礼貌程度、口齿清晰、逻辑条理、考虑顾客感受、语言表达、说话方式等方面,
用你专业的销售经验对音频里的人员的说话方式和语气表达的提出存在问题,
并结合问题或者某一个段话,给出改进建议。
【输出格式】
格式必须严格输出标准JSON格式:
{
"mood":"销售人员的情绪,心情,例如:高兴、兴奋、平淡、愤怒、焦虑等",
"sex":"销售人员的性别,例:男、女",
"environment":"对话现场所处环境",
"emotion":"销售人员的情感,例如:乐观、冷漠、厌恶、怀疑等",
"evaluate":"评价销售人员在营销时跟顾客的对话过程中是否存在问题",
"suggest":"销售人员营销的改进建议"
}
"""
utils/finetune.py
View file @
b8c6daa
import
datetime
import
datetime
import
uuid
from
log.logger
import
log
from
log.logger
import
log
import
os
import
os
...
@@ -129,7 +130,10 @@ def cut_audio(input_file, output_remaining_file, start_time_ms, duration_ms):
...
@@ -129,7 +130,10 @@ def cut_audio(input_file, output_remaining_file, start_time_ms, duration_ms):
# 剪切指定部分
# 剪切指定部分
cut_part
=
audio
[
start_time_ms
:
end_time_ms
]
cut_part
=
audio
[
start_time_ms
:
end_time_ms
]
cut_part
.
export
(
output_remaining_file
+
"_cut"
,
format
=
"mp3"
)
directory
=
os
.
path
.
dirname
(
output_remaining_file
)
basename
=
"cut_"
+
os
.
path
.
basename
(
output_remaining_file
)
cut_part
.
export
(
f
"{directory}/{basename}"
,
format
=
"mp3"
)
upload_file
(
basename
,
f
"{directory}/{basename}"
)
# 提取剩余部分
# 提取剩余部分
if
start_time_ms
==
0
:
if
start_time_ms
==
0
:
...
@@ -235,7 +239,7 @@ def crop_audio_by_rule(output_duration: int):
...
@@ -235,7 +239,7 @@ def crop_audio_by_rule(output_duration: int):
pattern
=
r"(?:[零幺一二三四五六七八九0-9].*){6,}"
pattern
=
r"(?:[零幺一二三四五六七八九0-9].*){6,}"
base_path
=
"D:/software/python/project/funasr_web/audios/train_audio/"
base_path
=
"D:/software/python/project/funasr_web/audios/train_audio/"
output_path
=
f
'{base_path}final_audio/'
output_path
=
f
'{base_path}final_audio/'
down_path
=
f
'{base_path}down_
text
/'
down_path
=
f
'{base_path}down_
audio
/'
total_duration
=
0
total_duration
=
0
from
mysql_service
import
funasr_db
from
mysql_service
import
funasr_db
data
=
funasr_db
.
select_funasr_audio
(
'2024-08-01'
)
data
=
funasr_db
.
select_funasr_audio
(
'2024-08-01'
)
...
@@ -291,38 +295,16 @@ def upload_files(name, file_path):
...
@@ -291,38 +295,16 @@ def upload_files(name, file_path):
upload_file
(
f
"{index}.wav"
,
f
"D:/software/python/project/funasr_web/dataset/temp/0830_{name}.wav"
)
upload_file
(
f
"{index}.wav"
,
f
"D:/software/python/project/funasr_web/dataset/temp/0830_{name}.wav"
)
crop_audio_by_rule
(
500
*
1000
*
3600
)
# crop_audio_by_rule(500 * 1000 * 3600)
def
adjust_time_with_milliseconds
(
offset_milliseconds
,
duration_milliseconds
):
# 创建一个基于当天00:00:00的时间点
base_time
=
datetime
.
datetime
.
now
()
.
replace
(
hour
=
0
,
minute
=
0
,
second
=
0
,
microsecond
=
0
)
# 将毫秒转换为秒
offset_seconds
=
int
(
offset_milliseconds
/
1000.0
)
duration_seconds
=
int
(
duration_milliseconds
/
1000.0
)
# 计算偏移时间和持续时间
offset_time
=
datetime
.
timedelta
(
seconds
=
offset_seconds
)
duration_time
=
datetime
.
timedelta
(
seconds
=
duration_seconds
)
# 应用偏移时间
adjusted_time
=
base_time
+
offset_time
# 应用持续时间
end_time
=
adjusted_time
+
duration_time
formatted_start_time
=
adjusted_time
.
strftime
(
'
%
M:
%
S'
)
formatted_end_time
=
end_time
.
strftime
(
'
%
H:
%
M:
%
S'
)
# 检查是否跨越了午夜
if
end_time
.
day
>
base_time
.
day
:
end_time
=
datetime
.
datetime
.
combine
(
base_time
.
date
()
+
datetime
.
timedelta
(
days
=
1
),
end_time
.
time
())
formatted_end_time
=
end_time
.
strftime
(
'
%
M:
%
S'
)
return
formatted_start_time
,
formatted_end_time
def
format_audio_and_text
():
def
format_audio_and_text
():
append_sys_param
(
"env"
,
"release"
)
import
funasr_service
from
mysql_service
import
funasr_db
audio_path
=
f
'D:/software/python/project/funasr_web/audios/train_audio/down_audio/'
audio_path
=
f
'D:/software/python/project/funasr_web/audios/train_audio/down_audio/'
text_path
=
f
'D:/software/python/project/funasr_web/audios/train_audio/upload_text.txt'
text_path
=
f
'D:/software/python/project/funasr_web/audios/train_audio/upload_text.txt'
output_path
=
f
'D:/software/python/project/funasr_web/audios/train_audio/result.txt'
output_path
=
f
'D:/software/python/project/funasr_web/audios/train_audio/result.txt'
filter_path
=
f
'D:/software/python/project/funasr_web/audios/train_audio/filter.txt'
filter_path
=
f
'D:/software/python/project/funasr_web/audios/train_audio/filter.txt'
append_sys_param
(
"env"
,
"release"
)
from
mysql_service
import
funasr_db
task_set
=
set
()
task_set
=
set
()
index
=
0
index
=
0
with
open
(
filter_path
,
'r'
,
encoding
=
'utf-8'
)
as
file
:
with
open
(
filter_path
,
'r'
,
encoding
=
'utf-8'
)
as
file
:
...
@@ -343,16 +325,61 @@ def format_audio_and_text():
...
@@ -343,16 +325,61 @@ def format_audio_and_text():
file_path
=
f
"{audio_path}{task_id}.mp3"
file_path
=
f
"{audio_path}{task_id}.mp3"
data
=
funasr_db
.
select_ali_asr_model_res
(
task_id
)
data
=
funasr_db
.
select_ali_asr_model_res
(
task_id
)
output_data
=
data
[
0
]
.
get
(
"output_data"
,
""
)
output_data
=
data
[
0
]
.
get
(
"output_data"
,
""
)
json_output
=
json
.
loads
(
output_data
)
with
open
(
text_path
,
'w'
,
encoding
=
'gbk'
)
as
text_file
:
with
open
(
text_path
,
'w'
,
encoding
=
'gbk'
)
as
text_file
:
for
item
in
json_output
:
text_file
.
write
(
funasr_service
.
careful_transform_output
(
output_data
))
start_time
,
end_time
=
adjust_time_with_milliseconds
(
item
.
get
(
"offset"
),
item
.
get
(
"duration"
))
role
=
item
.
get
(
"role"
)
content
=
item
.
get
(
"content"
)
text_file
.
write
(
f
"{start_time}-{end_time} 角色{role}:{content}
\n
"
)
text_file
.
close
()
text_file
.
close
()
audio_url
=
upload_file
(
f
"{task_id}.mp3"
,
file_path
)
audio_url
=
upload_file
(
f
"{task_id}.mp3"
,
file_path
)
text_url
=
upload_file
(
f
"{task_id}.txt"
,
text_path
)
text_url
=
upload_file
(
f
"{task_id}.txt"
,
text_path
)
result
=
f
"{task_id}:{audio_url}:{text_url}
\n
"
result
=
f
"{task_id}:{audio_url}:{text_url}
\n
"
file
.
write
(
result
)
file
.
write
(
result
)
# with open("D:/software/python/project/funasr_web/audios/train_audio/upload_text.txt", 'w', encoding='utf-8') as f:
# for root, dirs, files in os.walk("D:/software/python/project/funasr_web/log/111"):
# for file in files:
# file_path = os.path.join(root, file)
# url = upload_file(file,file_path)
# f.write(f"{url}\n")
def
down_to_cut_to_upload
(
url
,
offset
):
basename
,
extension
=
os
.
path
.
splitext
(
os
.
path
.
basename
(
url
))
base_path
=
f
"D:/software/python/project/funasr_web/audios/qwen/"
file_path
=
f
"{base_path}{basename}"
download_audio
(
f
"https://glsk-oss.oss-cn-shenzhen.aliyuncs.com/quality/{basename}.mp3"
,
file_path
)
cut_audio
(
file_path
,
f
"D:/software/python/project/funasr_web/audios/qwen/{basename}_{offset}.mp3"
,
offset
,
30
*
1000
)
def
down_file_upload_release
(
table_name
):
down_path
=
"D:/software/python/project/funasr_web/audios/pic_temp/"
append_sys_param
(
"env"
,
"release"
)
from
mysql_service
import
funasr_db
data
=
funasr_db
.
select_reception_material_data
(
table_name
,
1
)
list
=
[]
for
item
in
data
:
uuid_id
=
uuid
.
uuid4
()
.
hex
id
=
item
[
'id'
]
if
table_name
==
"reception_material_case_info"
:
thumbnail_path
=
f
"{down_path}thumbnail_{uuid_id}"
download_audio
(
item
[
'thumbnail_url'
],
thumbnail_path
)
thumbnail_url
=
upload_file
(
f
"thumbnail_{uuid_id}.png"
,
thumbnail_path
)
material_path
=
f
"{down_path}material_{uuid_id}"
download_audio
(
item
[
'material_url'
],
material_path
)
material_url
=
upload_file
(
f
"material_{uuid_id}.mp4"
,
material_path
)
update_sql
=
f
"""UPDATE {table_name} SET thumbnail_url = '{thumbnail_url}', material_url = '{material_url}' WHERE id = {id};"""
else
:
model_path
=
f
"{down_path}model_{uuid_id}"
download_audio
(
item
[
'model_display_picurl'
],
model_path
)
model_url
=
upload_file
(
f
"model_{uuid_id}.png"
,
model_path
)
detail_path
=
f
"{down_path}detail_{uuid_id}"
download_audio
(
item
[
'detail_picurl'
],
detail_path
)
detail_url
=
upload_file
(
f
"detail_{uuid_id}.png"
,
detail_path
)
update_sql
=
f
"""UPDATE {table_name} SET model_display_picurl='{model_url}', detail_picurl='{detail_url}' WHERE id={id};"""
list
.
append
(
update_sql
)
for
sql
in
list
:
print
(
sql
)
down_file_upload_release
(
'reception_material_case_info'
)
Write
Preview
Markdown
is supported
Attach a file
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 post a comment