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 9e307ed4
authored
Mar 15, 2024
by
杨林龙
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
优化日志打印
1 parent
d8eb77d8
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
54 additions
and
16 deletions
funasr_service.py
logger.py
main.py
funasr_service.py
View file @
9e307ed
import
logging
import
time
import
time
from
funasr
import
AutoModel
from
funasr
import
AutoModel
from
logger
import
log
# 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
logger
=
logging
.
getLogger
()
model
=
AutoModel
(
model
=
AutoModel
(
model
=
"paraformer-zh"
,
model
=
"paraformer-zh"
,
...
@@ -22,7 +21,7 @@ def timeit(func):
...
@@ -22,7 +21,7 @@ def timeit(func):
end_time
=
time
.
perf_counter
()
end_time
=
time
.
perf_counter
()
total_time
=
end_time
-
start_time
total_time
=
end_time
-
start_time
print
(
f
"Function {func.__name__} executed in {total_time} seconds."
)
print
(
f
"Function {func.__name__} executed in {total_time} seconds."
)
log
ger
.
info
(
f
"Function {func.__name__} executed in {total_time} seconds."
)
log
.
info
(
f
"Function {func.__name__} executed in {total_time} seconds."
)
return
result
return
result
return
wrapper
return
wrapper
...
...
logger.py
0 → 100644
View file @
9e307ed
import
logging
import
os
import
sys
from
logging.handlers
import
TimedRotatingFileHandler
def
setup_logging
(
log_file
):
"""
设置日志记录器,将日志输出到文件并按天进行保存
:param log_file: 日志文件路径
"""
# 创建一个日志记录器
logger
=
logging
.
getLogger
(
log_file
)
if
logger
.
hasHandlers
():
logger
.
handlers
.
clear
()
logger
.
setLevel
(
logging
.
DEBUG
)
# 创建一个 TimedRotatingFileHandler 实例,将日志输出到文件,并按天进行保存
file_handler
=
TimedRotatingFileHandler
(
filename
=
log_file
,
when
=
"midnight"
,
interval
=
1
,
backupCount
=
30
,
encoding
=
'UTF-8'
)
file_handler
.
setLevel
(
logging
.
DEBUG
)
# 设置日志格式
formatter
=
logging
.
Formatter
(
'
%(asctime)
s
%(thread)
d
%(filename)
s[line:
%(lineno)
d] -
%(levelname)
s:
%(message)
s'
)
file_handler
.
setFormatter
(
formatter
)
console_handler
=
logging
.
StreamHandler
(
sys
.
stdout
)
console_handler
.
setFormatter
(
formatter
)
# 将处理器添加到日志记录器中
logger
.
addHandler
(
file_handler
)
logger
.
addHandler
(
console_handler
)
return
logger
log
=
setup_logging
(
'funasr.log'
)
\ No newline at end of file
\ No newline at end of file
main.py
View file @
9e307ed
import
argparse
import
argparse
import
traceback
import
traceback
from
datetime
import
timedelta
from
datetime
import
timedelta
from
logger
import
log
import
json
import
json
import
logging
import
os
import
os
import
random
import
random
import
shutil
import
shutil
...
@@ -21,8 +21,9 @@ from pydantic import BaseModel
...
@@ -21,8 +21,9 @@ from pydantic import BaseModel
from
funasr.download.file
import
download_from_url
from
funasr.download.file
import
download_from_url
import
multiprocessing
import
multiprocessing
logger
=
logging
.
getLogger
()
app
=
FastAPI
()
app
=
FastAPI
()
#全局进程池
pool
=
multiprocessing
.
Pool
(
processes
=
4
)
db
=
MysqlHelper
.
MysqlHelper
(
db
=
MysqlHelper
.
MysqlHelper
(
DbConect
.
ali_asr_model
DbConect
.
ali_asr_model
...
@@ -84,7 +85,7 @@ async def transform_file():
...
@@ -84,7 +85,7 @@ async def transform_file():
)
)
consuming_end_time
=
time
.
perf_counter
()
consuming_end_time
=
time
.
perf_counter
()
print
(
f
"Function transform_file executed in {(consuming_end_time - consuming_start_time)} 秒"
)
print
(
f
"Function transform_file executed in {(consuming_end_time - consuming_start_time)} 秒"
)
log
ger
.
info
(
f
"Function transform_file executed in {(consuming_end_time - consuming_start_time)} 秒"
)
log
.
info
(
f
"Function transform_file executed in {(consuming_end_time - consuming_start_time)} 秒"
)
return
BaseResponse
(
return
BaseResponse
(
code
=
200
,
code
=
200
,
msg
=
res
[
0
][
"text"
],
msg
=
res
[
0
][
"text"
],
...
@@ -126,7 +127,7 @@ async def create_file(file: bytes = File()):
...
@@ -126,7 +127,7 @@ async def create_file(file: bytes = File()):
)
)
consuming_end_time
=
time
.
perf_counter
()
consuming_end_time
=
time
.
perf_counter
()
print
(
f
"Function create_file executed in {(consuming_end_time - consuming_start_time)} 秒"
)
print
(
f
"Function create_file executed in {(consuming_end_time - consuming_start_time)} 秒"
)
log
ger
.
info
(
f
"Function create_file executed in {(consuming_end_time - consuming_start_time)} 秒"
)
log
.
info
(
f
"Function create_file executed in {(consuming_end_time - consuming_start_time)} 秒"
)
os
.
remove
(
save_file
)
os
.
remove
(
save_file
)
response
=
BaseResponse
(
response
=
BaseResponse
(
code
=
200
,
code
=
200
,
...
@@ -207,11 +208,11 @@ def deal_worker(url: str, task_id: str):
...
@@ -207,11 +208,11 @@ def deal_worker(url: str, task_id: str):
要执行的函数,在子进程中运行
要执行的函数,在子进程中运行
"""
"""
try
:
try
:
log
ger
.
info
(
f
"Worker {task_id} is running..."
)
log
.
info
(
f
"Worker {task_id} is running..."
)
consuming_start_time
=
time
.
perf_counter
()
consuming_start_time
=
time
.
perf_counter
()
# 解析音频
# 解析音频
res
=
FunasrService
(
url
)
.
transform
()
res
=
FunasrService
(
url
)
.
transform
()
log
ger
.
info
(
"res:
%
s"
%
res
)
log
.
info
(
"res:
%
s"
%
res
)
if
len
(
res
)
<
1
:
if
len
(
res
)
<
1
:
updateSql
=
(
updateSql
=
(
f
"update ali_asr_model_res t set t.output_data = '{res}',t.task_status = 1 where t.task_id = '{task_id}';"
)
f
"update ali_asr_model_res t set t.output_data = '{res}',t.task_status = 1 where t.task_id = '{task_id}';"
)
...
@@ -236,18 +237,18 @@ def deal_worker(url: str, task_id: str):
...
@@ -236,18 +237,18 @@ def deal_worker(url: str, task_id: str):
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
)
log
ger
.
info
(
f
"output:{json_output}"
)
log
.
info
(
f
"output:{json_output}"
)
updateSql
=
(
updateSql
=
(
f
"update ali_asr_model_res t set t.output_data = '{json_output}',t.task_status = 1 where t.task_id = '{task_id}';"
)
f
"update ali_asr_model_res t set t.output_data = '{json_output}',t.task_status = 1 where t.task_id = '{task_id}';"
)
db
.
execute_modify
(
updateSql
)
db
.
execute_modify
(
updateSql
)
log
ger
.
info
(
log
.
info
(
f
"Function create_upload_file executed in {(time.perf_counter() - consuming_start_time)} s"
f
"Function create_upload_file executed in {(time.perf_counter() - consuming_start_time)} s"
)
)
log
ger
.
info
(
f
"Worker {task_id} finished."
)
log
.
info
(
f
"Worker {task_id} finished."
)
except
Exception
as
e
:
except
Exception
as
e
:
updateSql
=
(
f
"update ali_asr_model_res t set t.task_status = 2 where t.task_id = '{task_id}';"
)
updateSql
=
(
f
"update ali_asr_model_res t set t.task_status = 2 where t.task_id = '{task_id}';"
)
db
.
execute_modify
(
updateSql
)
db
.
execute_modify
(
updateSql
)
log
ger
.
error
(
f
"Worker error:{e}"
)
log
.
error
(
f
"Worker error:{e}"
)
traceback
.
print_exc
()
traceback
.
print_exc
()
return
{
"Worker error"
:
str
(
e
)}
return
{
"Worker error"
:
str
(
e
)}
...
@@ -265,17 +266,19 @@ async def create_url(param: UrlParam):
...
@@ -265,17 +266,19 @@ async def create_url(param: UrlParam):
f
"INSERT INTO dj_smartcarlife.ali_asr_model_res (task_id,file_url,task_status) VALUES ('{task_id}','{url}',0);"
)
f
"INSERT INTO dj_smartcarlife.ali_asr_model_res (task_id,file_url,task_status) VALUES ('{task_id}','{url}',0);"
)
res
=
db
.
execute_modify
(
insertSql
)
res
=
db
.
execute_modify
(
insertSql
)
process
=
multiprocessing
.
Process
(
target
=
deal_worker
,
args
=
(
url
,
task_id
,))
process
=
multiprocessing
.
Process
(
target
=
deal_worker
,
args
=
(
url
,
task_id
,))
log
ger
.
info
(
f
"process:{process}"
)
log
.
info
(
f
"process:{process}"
)
process
.
start
()
process
.
start
()
# pool.map_async()
response
=
BaseResponse
(
response
=
BaseResponse
(
code
=
200
,
code
=
200
,
msg
=
"success"
,
msg
=
"success"
,
data
=
{
"task_id"
:
str
(
task_id
)}
data
=
{
"task_id"
:
str
(
task_id
)}
)
)
log
ger
.
info
(
f
"response:{response}"
)
log
.
info
(
f
"response:{response}"
)
return
response
return
response
except
Exception
as
e
:
except
Exception
as
e
:
log
ger
.
error
(
f
"error:{e}"
)
log
.
error
(
f
"error:{e}"
)
traceback
.
print_exc
()
traceback
.
print_exc
()
return
{
"error"
:
str
(
e
)}
return
{
"error"
:
str
(
e
)}
...
...
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