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 e1a5f96c
authored
Mar 21, 2024
by
‘chizw’
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
[+]输出转换优化
1 parent
eeaddc61
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
127 additions
and
51 deletions
funasr_service.py
kafka_service/test01.py
kafka_service/test02.py
kafka_service/test03.py
test01.py
funasr_service.py
View file @
e1a5f96
...
@@ -62,10 +62,10 @@ class model_output():
...
@@ -62,10 +62,10 @@ class model_output():
}
}
def
handle_process
(
message
:
str
):
def
handle_process
(
message
:
str
):
try
:
try
:
log
.
info
(
f
"""handle_process process name:{multiprocessing.current_process()},thread name:{threading.current_thread().name},
log
.
info
(
f
"""handle_process process name:{multiprocessing.current_process()},thread name:{threading.current_thread().name},
Received task_id: {message}"""
)
Received task_id: {message}"""
)
# 处理逻辑
# 处理逻辑
start_time
=
time
.
time
()
start_time
=
time
.
time
()
...
@@ -101,6 +101,28 @@ def deal_worker(task_id: str):
...
@@ -101,6 +101,28 @@ def deal_worker(task_id: str):
return
return
sentence_info
=
output_res
[
0
][
"sentence_info"
]
sentence_info
=
output_res
[
0
][
"sentence_info"
]
log
.
info
(
"output_res.sentence_info:
%
s"
%
sentence_info
)
log
.
info
(
"output_res.sentence_info:
%
s"
%
sentence_info
)
json_output
=
fine_grained_transform_output
(
sentence_info
)
execute_time
=
time
.
perf_counter
()
-
consuming_start_time
funasr_db
.
update_ali_asr_model_res
(
task_id
,
json_output
,
int
((
execute_time
*
1000
)))
log
.
info
(
f
"Function create_upload_file executed in {execute_time} s"
)
log
.
info
(
f
"Worker {task_id} finished."
)
except
Exception
as
e
:
funasr_db
.
update_ali_asr_model_res_fail
(
task_id
,
traceback
.
format_exc
())
log
.
error
(
f
"Worker error{e}"
)
traceback
.
print_exc
()
return
{
"Worker error"
:
str
(
e
)}
def
fine_grained_transform_output
(
sentence_info
):
output
=
[]
for
one
in
sentence_info
:
duration
=
one
[
"end"
]
-
one
[
"start"
]
output
.
append
(
model_output
(
one
[
"spk"
],
one
[
"start"
],
duration
,
one
[
"text"
])
.
to_dict
())
json_output
=
json
.
dumps
(
output
,
ensure_ascii
=
False
)
return
json_output
def
merge_spk_transform_output
(
sentence_info
):
output
=
[]
output
=
[]
content
=
""
content
=
""
spk
=
0
spk
=
0
...
@@ -116,18 +138,7 @@ def deal_worker(task_id: str):
...
@@ -116,18 +138,7 @@ def deal_worker(task_id: str):
duration
=
0
duration
=
0
content
=
one
[
"text"
]
content
=
one
[
"text"
]
spk
=
one
[
"spk"
]
spk
=
one
[
"spk"
]
if
content
!=
""
:
if
content
!=
""
:
output
.
append
(
model_output
(
spk
,
offset
,
duration
,
content
)
.
to_dict
())
output
.
append
(
model_output
(
spk
,
offset
,
duration
,
content
)
.
to_dict
())
json_output
=
json
.
dumps
(
output
,
ensure_ascii
=
False
)
json_output
=
json
.
dumps
(
output
,
ensure_ascii
=
False
)
execute_time
=
time
.
perf_counter
()
-
consuming_start_time
return
json_output
funasr_db
.
update_ali_asr_model_res
(
task_id
,
json_output
,
int
((
execute_time
*
1000
)))
log
.
info
(
f
"Function create_upload_file executed in {execute_time} s"
)
log
.
info
(
f
"Worker {task_id} finished."
)
except
Exception
as
e
:
funasr_db
.
update_ali_asr_model_res_fail
(
task_id
,
traceback
.
format_exc
())
log
.
error
(
f
"Worker error{e}"
)
traceback
.
print_exc
()
return
{
"Worker error"
:
str
(
e
)}
kafka_service/test01.py
View file @
e1a5f96
...
@@ -8,8 +8,17 @@ consumer = KafkaConsumer(
...
@@ -8,8 +8,17 @@ consumer = KafkaConsumer(
)
)
# 循环消费消息
# 循环消费消息
for
message
in
consumer
:
# for message in consumer:
# 从消息中获取键和值
# # 从消息中获取键和值
key
=
message
.
key
# key = message.key
value
=
message
.
value
# value = message.value
print
(
f
"Key: {key}, Value: {value}"
)
# print(f"Key: {key}, Value: {value}")
datas
=
[
""
,
" "
,
"1111"
,
"https"
,
"1http"
,]
for
data
in
datas
:
data
.
replace
(
" "
,
""
)
if
data
==
""
or
len
(
data
)
<
1
or
not
data
.
startswith
(
"http"
):
print
(
False
)
else
:
print
(
True
)
kafka_service/test02.py
View file @
e1a5f96
...
@@ -20,17 +20,15 @@ producer.flush()
...
@@ -20,17 +20,15 @@ producer.flush()
producer
.
close
()
producer
.
close
()
res
=
funasr_db
.
select_ali_asr_model_wait
()
wait_res
=
funasr_db
.
select_ali_asr_model_wait
()
json_res
=
json
.
loads
(
res
)
print
(
f
"json_res{wait_res}"
)
# json_res = json_res.replace("'", "\"")
for
item
in
wait_res
:
print
(
f
"json_res{json_res}"
)
for
item
in
json_res
:
print
(
f
"item:{item}"
)
print
(
f
"item:{item}"
)
task_id
=
item
[
'task_id'
]
task_id
=
item
[
'task_id'
]
data
=
{
"task_id"
:
task_id
}
data
=
{
"task_id"
:
task_id
}
message
=
json
.
dumps
(
data
)
.
encode
(
'utf-8'
)
message
=
json
.
dumps
(
data
)
.
encode
(
'utf-8'
)
producer
.
send
(
ConfigInfo
.
kafka_consumer_analysis_topic
,
message
)
#
producer.send(ConfigInfo.kafka_consumer_analysis_topic, message)
print
(
f
"success
{task_id
}"
)
print
(
f
"success
:{message
}"
)
# 等待所有消息发送完成
# 等待所有消息发送完成
producer
.
flush
()
producer
.
flush
()
# 关闭生产者
# 关闭生产者
...
...
kafka_service/test03.py
View file @
e1a5f96
import
grpc
import
json
import
json
import
os
import
time
from
kafka
import
KafkaProducer
import
asyncio
import
soundfile
as
sf
# 创建Kafka生产者
import
argparse
producer
=
KafkaProducer
(
bootstrap_servers
=
"192.168.3.131:9092"
)
# Kafka集群的地址
from
grpc_client
import
transcribe_audio_bytes
# 发送消息
from
paraformer_pb2_grpc
import
ASRStub
topic
=
"ocr_topic"
# Kafka主题名称
key
=
b
"my-key"
# 消息的键(可选)
data
=
{
'orderId'
:
1234
,
'fileId'
:
31
,
'filePath'
:
'https://bhg.didihu.com.cn/group1/M00/22/05/wKgFDWWnocCAZllpAADp9tDJibY696.jpg'
,
'sns'
:
'96312110851'
,
'vin'
:
'VIN654321'
,
'fileType'
:
1
}
# send the audio data once
message
=
json
.
dumps
(
data
)
.
encode
(
'utf-8'
)
async
def
grpc_rec
(
wav_scp
,
grpc_uri
,
asr_user
,
language
):
# producer.send(topic,message)
with
grpc
.
insecure_channel
(
grpc_uri
)
as
channel
:
stub
=
ASRStub
(
channel
)
# 等待所有消息发送完成
for
line
in
wav_scp
:
producer
.
flush
()
wav_file
=
line
.
split
()[
1
]
wav
,
_
=
sf
.
read
(
wav_file
,
dtype
=
'int16'
)
# 关闭生产者
speech_length
=
wav
.
shape
[
0
]
producer
.
close
()
sample_offset
=
0
# 获取当前文件所在目录的绝对路径
chunk_size
=
[
8
,
8
,
4
]
# [5, 10, 5] 600ms, [8, 8, 4] 480ms
current_dir
=
os
.
path
.
dirname
(
os
.
path
.
abspath
(
__file__
))
stride_size
=
chunk_size
[
1
]
*
960
print
(
f
"current_dir:{current_dir}"
)
for
sample_offset
in
range
(
0
,
speech_length
,
min
(
stride_size
,
speech_length
-
sample_offset
)):
# 将根目录添加到 Python 解释器的搜索路径中
if
sample_offset
+
stride_size
>=
speech_length
-
1
:
root_dir
=
os
.
path
.
join
(
current_dir
,
'..'
)
# 假设根目录是当前目录的父目录
stride_size
=
speech_length
-
sample_offset
print
(
f
"root_dir:{root_dir}"
)
b
=
time
.
time
()
response
=
transcribe_audio_bytes
(
stub
,
wav
[
sample_offset
:
sample_offset
+
stride_size
]
.
tobytes
(),
user
=
asr_user
,
language
=
language
,
speaking
=
True
,
isEnd
=
False
)
response
=
transcribe_audio_bytes
(
stub
,
None
,
user
=
asr_user
,
language
=
language
,
speaking
=
False
,
isEnd
=
False
)
resp
=
response
.
next
()
text
=
''
if
'decoding'
==
resp
.
action
:
resp
=
response
.
next
()
if
'finish'
==
resp
.
action
:
text
=
json
.
loads
(
resp
.
sentence
)[
'text'
]
res
=
{
'text'
:
text
,
'time'
:
time
.
time
()
-
b
}
print
(
res
)
time
.
sleep
(
0.48
)
response
=
transcribe_audio_bytes
(
stub
,
None
,
user
=
asr_user
,
language
=
language
,
speaking
=
False
,
isEnd
=
True
)
async
def
test
(
args
):
wav_scp
=
open
(
args
.
wav_scp
,
"r"
)
.
readlines
()
uri
=
'{}:{}'
.
format
(
args
.
host
,
args
.
port
)
res
=
await
grpc_rec
(
wav_scp
,
uri
,
args
.
user_allowed
,
language
=
'zh-CN'
)
if
__name__
==
'__main__'
:
parser
=
argparse
.
ArgumentParser
()
parser
.
add_argument
(
"--host"
,
type
=
str
,
default
=
"127.0.0.1"
,
required
=
False
,
help
=
"grpc server host ip"
)
parser
.
add_argument
(
"--port"
,
type
=
int
,
default
=
10108
,
required
=
False
,
help
=
"grpc server port"
)
parser
.
add_argument
(
"--user_allowed"
,
type
=
str
,
default
=
"project1_user1"
,
help
=
"allowed user for grpc client"
)
parser
.
add_argument
(
"--sample_rate"
,
type
=
int
,
default
=
16000
,
help
=
"audio sample_rate from client"
)
parser
.
add_argument
(
"--wav_scp"
,
type
=
str
,
required
=
True
,
help
=
"audio wav scp"
)
args
=
parser
.
parse_args
()
asyncio
.
run
(
test
(
args
))
\ No newline at end of file
\ No newline at end of file
test01.py
View file @
e1a5f96
import
time
import
time
from
funasr
import
AutoModel
from
funasr
import
AutoModel
import
funasr_service
# paraformer-zh is a multi-functional asr model
# paraformer-zh is a multi-functional asr model
# use vad, punc, spk or not as you need
# use vad, punc, spk or not as you need
model
=
AutoModel
(
model
=
AutoModel
(
...
@@ -56,6 +58,9 @@ if content is not None:
...
@@ -56,6 +58,9 @@ if content is not None:
print
(
text
)
print
(
text
)
write_text
(
text
)
write_text
(
text
)
res
=
funasr_service
.
fine_grained_transform_output
(
sentence_info
)
print
(
res
)
consuming_end_time
=
time
.
perf_counter
()
consuming_end_time
=
time
.
perf_counter
()
print
(
print
(
...
...
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