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 dbe30551
authored
Mar 15, 2024
by
‘chizw’
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
mysql优化
1 parent
c8f70693
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
100 additions
and
95 deletions
funasr_service.py
main.py
DbConect.py → mysql_service/DbConect.py
MysqlHelper.py → mysql_service/MysqlHelper.py
mysql_service/funasr_db.py
funasr_service.py
View file @
dbe3055
import
time
import
time
from
funasr
import
AutoModel
from
funasr
import
AutoModel
from
logger
import
log
from
logger
import
log
import
json
import
traceback
from
mysql_service
import
funasr_db
# 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
...
@@ -34,3 +38,66 @@ class FunasrService(object):
...
@@ -34,3 +38,66 @@ class FunasrService(object):
res
=
model
.
generate
(
input
=
self
.
path
,
batch_size_s
=
300
,
hotword
=
"问界 80"
)
res
=
model
.
generate
(
input
=
self
.
path
,
batch_size_s
=
300
,
hotword
=
"问界 80"
)
# print(res)
# print(res)
return
res
return
res
class
model_output
():
def
__init__
(
self
,
role
,
offset
,
duration
,
content
):
self
.
role
=
role
self
.
offset
=
offset
self
.
duration
=
duration
self
.
content
=
content
def
to_dict
(
self
):
return
{
'role'
:
self
.
role
,
'offset'
:
self
.
offset
,
'duration'
:
self
.
duration
,
'content'
:
self
.
content
}
def
deal_worker
(
url
:
str
,
task_id
:
str
):
"""
要执行的函数,在子进程中运行
"""
try
:
log
.
info
(
f
"Worker {task_id} is running..."
)
consuming_start_time
=
time
.
perf_counter
()
# 解析音频
res
=
FunasrService
(
url
)
.
transform
()
if
len
(
res
)
<
1
:
log
.
info
(
"res:
%
s"
%
res
)
funasr_db
.
update_ali_asr_model_res
(
task_id
,
res
)
return
sentence_info
=
res
[
0
][
"sentence_info"
]
log
.
info
(
"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
)
log
.
info
(
f
"output:{json_output}"
)
funasr_db
.
update_ali_asr_model_res
(
task_id
,
json_output
)
log
.
info
(
f
"Function create_upload_file executed in {(time.perf_counter() - consuming_start_time)} s"
)
log
.
info
(
f
"Worker {task_id} finished."
)
except
Exception
as
e
:
funasr_db
.
update_ali_asr_model_res_fail
(
task_id
)
log
.
error
(
f
"Worker error{e}"
)
traceback
.
print_exc
()
return
{
"Worker error"
:
str
(
e
)}
\ No newline at end of file
\ No newline at end of file
main.py
View file @
dbe3055
import
argparse
import
argparse
import
traceback
import
traceback
from
datetime
import
timedelta
import
funasr_service
from
logger
import
log
from
logger
import
log
import
json
import
os
import
os
import
random
import
random
import
shutil
import
shutil
import
threading
import
threading
import
time
import
time
import
requests
import
uuid
import
uuid
import
MysqlHelper
import
DbConect
from
funasr
import
AutoModel
import
pydantic
import
pydantic
import
uvicorn
import
uvicorn
from
fastapi
import
Body
,
FastAPI
,
File
,
UploadFile
from
fastapi
import
FastAPI
,
File
,
UploadFile
from
funasr_service
import
FunasrService
from
funasr_service
import
FunasrService
from
pydantic
import
BaseModel
from
pydantic
import
BaseModel
from
funasr.download.file
import
download_from_url
import
multiprocessing
import
multiprocessing
from
mysql_service
import
funasr_db
app
=
FastAPI
()
app
=
FastAPI
()
# 全局进程池
pool
=
multiprocessing
.
Pool
(
processes
=
1
)
db
=
MysqlHelper
.
MysqlHelper
(
DbConect
.
ali_asr_model
)
class
BaseResponse
(
BaseModel
):
class
BaseResponse
(
BaseModel
):
code
:
int
=
pydantic
.
Field
(
200
,
description
=
"HTTP status code"
)
code
:
int
=
pydantic
.
Field
(
200
,
description
=
"HTTP status code"
)
...
@@ -177,75 +169,6 @@ async def create_upload_file(file: UploadFile):
...
@@ -177,75 +169,6 @@ async def create_upload_file(file: UploadFile):
return
{
"error"
:
str
(
e
)}
return
{
"error"
:
str
(
e
)}
class
model_output
():
def
__init__
(
self
,
role
,
offset
,
duration
,
content
):
self
.
role
=
role
self
.
offset
=
offset
self
.
duration
=
duration
self
.
content
=
content
def
to_dict
(
self
):
return
{
'role'
:
self
.
role
,
'offset'
:
self
.
offset
,
'duration'
:
self
.
duration
,
'content'
:
self
.
content
}
def
deal_worker
(
url
:
str
,
task_id
:
str
):
"""
要执行的函数,在子进程中运行
"""
try
:
# 获取当前进程的名字
name
=
multiprocessing
.
current_process
()
.
name
log
.
info
(
f
"Worker {task_id} is running...进程名字 {name}"
)
consuming_start_time
=
time
.
perf_counter
()
# 解析音频
res
=
FunasrService
(
url
)
.
transform
()
if
len
(
res
)
<
1
:
log
.
info
(
"res:
%
s"
%
res
)
updateSql
=
(
f
"update ali_asr_model_res t set t.output_data = '{res}',t.task_status = 1 where t.task_id = '{task_id}';"
)
db
.
execute_modify
(
updateSql
)
return
sentence_info
=
res
[
0
][
"sentence_info"
]
log
.
info
(
"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
)
log
.
info
(
f
"output:{json_output}"
)
updateSql
=
(
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
)
log
.
info
(
f
"Function create_upload_file executed in {(time.perf_counter() - consuming_start_time)} s"
)
log
.
info
(
f
"Worker {task_id} finished."
)
except
Exception
as
e
:
updateSql
=
(
f
"update ali_asr_model_res t set t.task_status = 2 where t.task_id = '{task_id}';"
)
db
.
execute_modify
(
updateSql
)
log
.
error
(
f
"Worker error{e}"
)
traceback
.
print_exc
()
return
{
"Worker error"
:
str
(
e
)}
class
UrlParam
(
BaseModel
):
class
UrlParam
(
BaseModel
):
url
:
str
url
:
str
...
@@ -255,17 +178,10 @@ async def create_url(param: UrlParam):
...
@@ -255,17 +178,10 @@ async def create_url(param: UrlParam):
try
:
try
:
task_id
=
uuid
.
uuid1
()
task_id
=
uuid
.
uuid1
()
url
=
param
.
url
url
=
param
.
url
insertSql
=
(
funasr_db
.
insert_ali_asr_model_res
(
str
(
task_id
),
url
)
f
"INSERT INTO dj_smartcarlife.ali_asr_model_res (task_id,file_url,task_status) VALUES ('{task_id}','{url}',0);"
)
process
=
multiprocessing
.
Process
(
target
=
funasr_service
.
deal_worker
,
args
=
(
url
,
task_id
,))
res
=
db
.
execute_modify
(
insertSql
)
log
.
info
(
f
"process:{process}"
)
# process = multiprocessing.Process(target=deal_worker, args=(url, task_id,))
process
.
start
()
# log.info(f"process:{process}")
# process.start()
# multiprocessing.freeze_support()
pool
.
apply
(
deal_worker
,
(
url
,
task_id
))
# 关闭进程池,不再接受新的任务
pool
.
close
()
response
=
BaseResponse
(
response
=
BaseResponse
(
code
=
200
,
code
=
200
,
msg
=
"success"
,
msg
=
"success"
,
...
@@ -280,7 +196,6 @@ async def create_url(param: UrlParam):
...
@@ -280,7 +196,6 @@ async def create_url(param: UrlParam):
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
multiprocessing
.
freeze_support
()
save_path
=
"./audio/"
save_path
=
"./audio/"
if
os
.
path
.
exists
(
save_path
):
if
os
.
path
.
exists
(
save_path
):
shutil
.
rmtree
(
save_path
)
shutil
.
rmtree
(
save_path
)
...
...
DbConect.py
→
mysql_service/
DbConect.py
View file @
dbe3055
File moved
MysqlHelper.py
→
mysql_service/
MysqlHelper.py
View file @
dbe3055
#import os,sys
#import os,sys
import
logging
import
logging
import
pymysql
import
pymysql
import
DbConect
from
mysql_service
import
DbConect
#print(sys.path)
#print(sys.path)
...
...
mysql_service/funasr_db.py
0 → 100644
View file @
dbe3055
from
mysql_service
import
DbConect
,
MysqlHelper
db
=
MysqlHelper
.
MysqlHelper
(
DbConect
.
ali_asr_model
)
def
insert_ali_asr_model_res
(
task_id
:
str
,
url
:
str
):
insertSql
=
(
f
"INSERT INTO dj_smartcarlife.ali_asr_model_res (task_id,file_url,task_status) VALUES ('{task_id}','{url}',0);"
)
return
db
.
execute_modify
(
insertSql
)
def
update_ali_asr_model_res
(
task_id
:
str
,
json_output
:
str
):
updateSql
=
(
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
)
def
update_ali_asr_model_res_fail
(
task_id
:
str
):
updateSql
=
(
f
"update ali_asr_model_res t set t.task_status = 2 where t.task_id = '{task_id}';"
)
db
.
execute_modify
(
updateSql
)
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