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
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
134 additions
and
58 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():
}
def
handle_process
(
message
:
str
):
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}"""
)
# 处理逻辑
start_time
=
time
.
time
()
...
...
@@ -101,25 +101,7 @@ def deal_worker(task_id: str):
return
sentence_info
=
output_res
[
0
][
"sentence_info"
]
log
.
info
(
"output_res.sentence_info:
%
s"
%
sentence_info
)
output
=
[]
content
=
""
spk
=
0
duration
=
0
offset
=
0
for
one
in
sentence_info
:
duration
=
duration
+
(
one
[
"end"
]
-
one
[
"start"
])
offset
=
one
[
"start"
]
if
spk
==
one
[
"spk"
]:
content
=
content
+
one
[
"text"
]
else
:
output
.
append
(
model_output
(
spk
,
offset
,
duration
,
content
)
.
to_dict
())
duration
=
0
content
=
one
[
"text"
]
spk
=
one
[
"spk"
]
if
content
!=
""
:
output
.
append
(
model_output
(
spk
,
offset
,
duration
,
content
)
.
to_dict
())
json_output
=
json
.
dumps
(
output
,
ensure_ascii
=
False
)
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
(
...
...
@@ -131,3 +113,32 @@ def deal_worker(task_id: str):
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
=
[]
content
=
""
spk
=
0
duration
=
0
offset
=
0
for
one
in
sentence_info
:
duration
=
duration
+
(
one
[
"end"
]
-
one
[
"start"
])
offset
=
one
[
"start"
]
if
spk
==
one
[
"spk"
]:
content
=
content
+
one
[
"text"
]
else
:
output
.
append
(
model_output
(
spk
,
offset
,
duration
,
content
)
.
to_dict
())
duration
=
0
content
=
one
[
"text"
]
spk
=
one
[
"spk"
]
if
content
!=
""
:
output
.
append
(
model_output
(
spk
,
offset
,
duration
,
content
)
.
to_dict
())
json_output
=
json
.
dumps
(
output
,
ensure_ascii
=
False
)
return
json_output
kafka_service/test01.py
View file @
e1a5f96
...
...
@@ -8,8 +8,17 @@ consumer = KafkaConsumer(
)
# 循环消费消息
for
message
in
consumer
:
# 从消息中获取键和值
key
=
message
.
key
value
=
message
.
value
print
(
f
"Key: {key}, Value: {value}"
)
# for message in consumer:
# # 从消息中获取键和值
# key = message.key
# value = message.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()
producer
.
close
()
res
=
funasr_db
.
select_ali_asr_model_wait
()
json_res
=
json
.
loads
(
res
)
# json_res = json_res.replace("'", "\"")
print
(
f
"json_res{json_res}"
)
for
item
in
json_res
:
wait_res
=
funasr_db
.
select_ali_asr_model_wait
()
print
(
f
"json_res{wait_res}"
)
for
item
in
wait_res
:
print
(
f
"item:{item}"
)
task_id
=
item
[
'task_id'
]
data
=
{
"task_id"
:
task_id
}
message
=
json
.
dumps
(
data
)
.
encode
(
'utf-8'
)
producer
.
send
(
ConfigInfo
.
kafka_consumer_analysis_topic
,
message
)
print
(
f
"success
{task_id
}"
)
#
producer.send(ConfigInfo.kafka_consumer_analysis_topic, message)
print
(
f
"success
:{message
}"
)
# 等待所有消息发送完成
producer
.
flush
()
# 关闭生产者
...
...
kafka_service/test03.py
View file @
e1a5f96
import
grpc
import
json
import
os
from
kafka
import
KafkaProducer
# 创建Kafka生产者
producer
=
KafkaProducer
(
bootstrap_servers
=
"192.168.3.131:9092"
)
# Kafka集群的地址
# 发送消息
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
}
message
=
json
.
dumps
(
data
)
.
encode
(
'utf-8'
)
# producer.send(topic,message)
# 等待所有消息发送完成
producer
.
flush
()
# 关闭生产者
producer
.
close
()
# 获取当前文件所在目录的绝对路径
current_dir
=
os
.
path
.
dirname
(
os
.
path
.
abspath
(
__file__
))
print
(
f
"current_dir:{current_dir}"
)
# 将根目录添加到 Python 解释器的搜索路径中
root_dir
=
os
.
path
.
join
(
current_dir
,
'..'
)
# 假设根目录是当前目录的父目录
print
(
f
"root_dir:{root_dir}"
)
import
time
import
asyncio
import
soundfile
as
sf
import
argparse
from
grpc_client
import
transcribe_audio_bytes
from
paraformer_pb2_grpc
import
ASRStub
# send the audio data once
async
def
grpc_rec
(
wav_scp
,
grpc_uri
,
asr_user
,
language
):
with
grpc
.
insecure_channel
(
grpc_uri
)
as
channel
:
stub
=
ASRStub
(
channel
)
for
line
in
wav_scp
:
wav_file
=
line
.
split
()[
1
]
wav
,
_
=
sf
.
read
(
wav_file
,
dtype
=
'int16'
)
speech_length
=
wav
.
shape
[
0
]
sample_offset
=
0
chunk_size
=
[
8
,
8
,
4
]
# [5, 10, 5] 600ms, [8, 8, 4] 480ms
stride_size
=
chunk_size
[
1
]
*
960
for
sample_offset
in
range
(
0
,
speech_length
,
min
(
stride_size
,
speech_length
-
sample_offset
)):
if
sample_offset
+
stride_size
>=
speech_length
-
1
:
stride_size
=
speech_length
-
sample_offset
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
test01.py
View file @
e1a5f96
import
time
from
funasr
import
AutoModel
import
funasr_service
# paraformer-zh is a multi-functional asr model
# use vad, punc, spk or not as you need
model
=
AutoModel
(
...
...
@@ -56,6 +58,9 @@ if content is not None:
print
(
text
)
write_text
(
text
)
res
=
funasr_service
.
fine_grained_transform_output
(
sentence_info
)
print
(
res
)
consuming_end_time
=
time
.
perf_counter
()
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