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 bd2403c6
authored
Mar 14, 2024
by
‘chizw’
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
音频转移接口
1 parent
2792e8b0
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
213 additions
and
26 deletions
DbConect.py
MysqlHelper.py
main.py
DbConect.py
0 → 100644
View file @
bd2403c
# 广联DJ
ali_asr_model
=
{
"host"
:
'192.168.3.227'
,
"port"
:
3306
,
"user"
:
'os_user'
,
"password"
:
'os#123'
,
"db"
:
'dj_smartcarlife'
,
"charset"
:
'utf8'
}
\ No newline at end of file
MysqlHelper.py
0 → 100644
View file @
bd2403c
#import os,sys
import
logging
import
pymysql
import
DbConect
#print(sys.path)
logger
=
logging
.
getLogger
()
class
MysqlHelper
:
_instance
=
None
# 本类的实例
con
=
None
# 数据库conn
cursor
=
None
# 游标
def
__init__
(
self
,
config
)
->
None
:
self
.
host
=
config
[
'host'
]
self
.
port
=
config
[
"port"
]
self
.
user
=
config
[
"user"
]
self
.
password
=
config
[
"password"
]
self
.
db
=
config
[
"db"
]
self
.
charset
=
config
[
"charset"
]
self
.
con
=
None
self
.
cursor
=
None
try
:
self
.
con
=
pymysql
.
connect
(
host
=
self
.
host
,
port
=
self
.
port
,
user
=
self
.
user
,
password
=
self
.
password
,
database
=
self
.
db
,
charset
=
'utf8'
)
self
.
cursor
=
self
.
con
.
cursor
()
self
.
_instance
=
MysqlHelper
except
Exception
as
e
:
print
(
e
)
return
False
# 创建连接
def
create_con
(
self
):
# try:
# self.con = pymysql.connect(
# host=self.host,
# port=self.port,
# user=self.user,
# password=self.password,
# database=self.db, charset='utf8')
# self.cursor = self.con.cursor()
# return True
# except Exception as e:
# print(e)
# return False
pass
# 关闭链接
def
close_con
(
self
):
if
self
.
cursor
:
self
.
cursor
.
close
()
if
self
.
con
:
self
.
con
.
close
()
# sql执行
def
execute_modify
(
self
,
sql
):
"""
执行插入/更新/删除语句
"""
try
:
logger
.
info
(
"execute_modify sql:
%
s"
%
sql
)
res
=
self
.
cursor
.
execute
(
sql
)
self
.
con
.
commit
()
logger
.
info
(
"execute_modify result:
%
s"
%
res
)
result
=
True
except
Exception
as
e
:
logging
.
error
(
e
)
self
.
con
.
rollback
result
=
False
return
result
# sql执行 - return all
def
execute_select
(
self
,
sql
):
"""
执行查询语句
"""
try
:
logger
.
info
(
"execute_select sql:
%
s"
%
sql
)
self
.
cursor
.
execute
(
sql
)
res
=
self
.
cursor
.
fetchall
()
logger
.
info
(
"execute_select result:
%
s"
%
res
)
return
res
except
Exception
as
e
:
logging
.
error
(
e
)
return
False
finally
:
pass
# sql执行 - return one
def
execute_selectOne
(
self
,
sql
):
"""
执行查询语句
"""
try
:
logging
.
info
(
sql
)
self
.
cursor
.
execute
(
sql
)
res
=
self
.
cursor
.
fetchone
return
res
except
Exception
as
e
:
logging
.
error
(
e
)
return
False
finally
:
pass
if
__name__
==
'__main__'
:
db
=
MysqlHelper
(
DbConect
.
ali_asr_model
)
#添加
# db.execute_sql("insert into user (username, password) values ('username4','password4')")
#修改
# db.execute_sql("update channellist set is_active=1 where id=2")
#查询
selectSql
=
(
"select * from ali_asr_model_res;"
)
print
(
"execute sql:
%
s"
%
selectSql
)
res
=
db
.
execute_select
(
selectSql
)
print
(
res
)
print
(
len
(
res
))
main.py
View file @
bd2403c
...
...
@@ -8,8 +8,10 @@ import shutil
import
threading
import
time
import
requests
import
asyncio
import
uuid
import
MysqlHelper
import
DbConect
from
funasr
import
AutoModel
import
pydantic
import
uvicorn
from
fastapi
import
Body
,
FastAPI
,
File
,
UploadFile
...
...
@@ -21,6 +23,19 @@ import multiprocessing
logger
=
logging
.
getLogger
()
app
=
FastAPI
()
db
=
MysqlHelper
.
MysqlHelper
(
DbConect
.
ali_asr_model
)
model
=
AutoModel
(
model
=
"paraformer-zh"
,
vad_model
=
"fsmn-vad"
,
punc_model
=
"ct-punc-c"
,
spk_model
=
"cam++"
,
# openai_model="Whisper-large-v3",
ncpu
=
2
,
)
class
BaseResponse
(
BaseModel
):
code
:
int
=
pydantic
.
Field
(
200
,
description
=
"HTTP status code"
)
...
...
@@ -170,54 +185,99 @@ async def create_upload_file(file: UploadFile):
return
{
"error"
:
str
(
e
)}
def
deal_worker
(
url
):
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
):
"""
要执行的函数,在子进程中运行
"""
consuming_start_time
=
time
.
perf_counter
()
print
(
f
"Worker {url} is running..."
)
# 解析音频
res
=
FunasrService
(
url
)
.
transform
()
answer
=
[]
print
(
str
(
res
[
0
][
"sentence_info"
]))
for
one
in
res
[
0
][
"sentence_info"
]:
start_time
=
one
[
'start'
]
end_time
=
one
[
'end'
]
answer
.
append
(
f
'{"角色1" if one["spk"] == 0 else "角色2"} : {one["text"]} -- 时长:{end_time - start_time}ms'
try
:
logger
.
info
(
f
"Worker {task_id} is running..."
)
insertSql
=
(
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
)
consuming_start_time
=
time
.
perf_counter
()
# 解析音频
res
=
FunasrService
(
url
)
.
transform
()
logger
.
info
(
"res:
%
s"
%
str
(
res
[
0
][
"sentence_info"
]))
output
=
[]
content
=
""
spk
=
0
duration
=
0
offset
=
0
for
one
in
res
[
0
][
"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
)
logger
.
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
)
logger
.
info
(
f
"Function create_upload_file executed in {(time.perf_counter() - consuming_start_time)} s"
)
consuming_end_time
=
time
.
perf_counter
(
)
print
(
f
"Function create_upload_file executed in {(consuming_end_time - consuming_start_time)} 秒"
)
logger
.
info
(
f
"Function create_upload_file executed in {(consuming_end_time - consuming_start_time)} 秒"
)
print
(
f
"Worker {url} finished."
)
logger
.
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
)
logger
.
error
(
f
"Worker error:{e}"
)
return
{
"Worker error"
:
str
(
e
)}
class
UrlParam
(
BaseModel
):
url
:
str
@app.post
(
"/funasr/url/"
)
async
def
create_url
(
param
:
UrlParam
):
try
:
process
=
multiprocessing
.
Process
(
target
=
deal_worker
,
args
=
(
param
.
url
,))
task_id
=
uuid
.
uuid1
()
process
=
multiprocessing
.
Process
(
target
=
deal_worker
,
args
=
(
param
.
url
,
task_id
,))
logger
.
info
(
f
"process:{process}"
)
process
.
start
()
re
turn
BaseResponse
(
re
sponse
=
BaseResponse
(
code
=
200
,
msg
=
"OK"
,
data
=
"你好"
,
time_consuming
=
0
,
msg
=
"success"
,
data
=
{
"task_id"
:
str
(
task_id
)}
)
logger
.
info
(
f
"response:{response}"
)
return
response
except
Exception
as
e
:
logger
.
error
(
f
"error:{e}"
)
return
{
"error"
:
str
(
e
)}
if
__name__
==
"__main__"
:
save_path
=
"./audio/"
if
os
.
path
.
exists
(
save_path
):
shutil
.
rmtree
(
save_path
)
parser
=
argparse
.
ArgumentParser
()
parser
.
add_argument
(
"--host"
,
type
=
str
,
default
=
"0.0.0.0"
)
parser
.
add_argument
(
"--port"
,
type
=
int
,
default
=
78
65
)
parser
.
add_argument
(
"--port"
,
type
=
int
,
default
=
78
88
)
args
=
parser
.
parse_args
()
uvicorn
.
run
(
app
=
"main:app"
,
...
...
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