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 91b5ff34
authored
Mar 13, 2024
by
‘chizw’
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
url接口
1 parent
33b08429
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
86 additions
and
32 deletions
main.py
requirements.txt
test01.py
main.py
View file @
91b5ff3
...
@@ -7,12 +7,15 @@ import random
...
@@ -7,12 +7,15 @@ import random
import
shutil
import
shutil
import
threading
import
threading
import
time
import
time
import
requests
import
asyncio
import
pydantic
import
pydantic
import
uvicorn
import
uvicorn
from
fastapi
import
Body
,
FastAPI
,
File
,
UploadFile
from
fastapi
import
Body
,
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
logger
=
logging
.
getLogger
()
logger
=
logging
.
getLogger
()
app
=
FastAPI
()
app
=
FastAPI
()
...
@@ -41,7 +44,7 @@ def generate_random_filename():
...
@@ -41,7 +44,7 @@ def generate_random_filename():
timestamp
=
int
(
time
.
time
())
# 获取当前时间戳
timestamp
=
int
(
time
.
time
())
# 获取当前时间戳
random_number
=
random
.
randint
(
1000
,
9999
)
# 生成一个随机数
random_number
=
random
.
randint
(
1000
,
9999
)
# 生成一个随机数
filename
=
f
"file_{timestamp}_{random_number}"
# 创建文件名
filename
=
f
"file_{timestamp}_{random_number}"
# 创建文件名
return
filename
+
".wav"
return
filename
+
".wav"
@app.get
(
"/"
)
@app.get
(
"/"
)
...
@@ -49,7 +52,7 @@ def hello_world():
...
@@ -49,7 +52,7 @@ def hello_world():
return
"Hello World!"
return
"Hello World!"
@app.get
(
"/fun
sa
r/"
)
@app.get
(
"/fun
as
r/"
)
async
def
transform_file
():
async
def
transform_file
():
consuming_start_time
=
time
.
perf_counter
()
consuming_start_time
=
time
.
perf_counter
()
res
=
FunasrService
(
"asr_example.wav"
)
.
transform
()
res
=
FunasrService
(
"asr_example.wav"
)
.
transform
()
...
@@ -57,14 +60,14 @@ async def transform_file():
...
@@ -57,14 +60,14 @@ async def transform_file():
answer
=
[]
answer
=
[]
print
(
str
(
res
[
0
][
"sentence_info"
]))
print
(
str
(
res
[
0
][
"sentence_info"
]))
for
one
in
res
[
0
][
"sentence_info"
]:
for
one
in
res
[
0
][
"sentence_info"
]:
start_time
=
one
[
'start'
]
start_time
=
one
[
'start'
]
end_time
=
one
[
'end'
]
end_time
=
one
[
'end'
]
answer
.
append
(
answer
.
append
(
f
'{"角色1" if one["spk"] == 0 else "角色2"} : {one["text"]} -- 时长:{end_time
-
start_time}ms'
f
'{"角色1" if one["spk"] == 0 else "角色2"} : {one["text"]} -- 时长:{end_time
-
start_time}ms'
)
)
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)} 秒"
)
logger
.
info
(
f
"Function transform_file executed in {
(consuming_end_time - consuming_start_time)} 秒"
)
logger
.
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"
],
...
@@ -73,7 +76,7 @@ async def transform_file():
...
@@ -73,7 +76,7 @@ async def transform_file():
)
)
@app.post
(
"/fun
sa
r/file/"
)
@app.post
(
"/fun
as
r/file/"
)
async
def
create_file
(
file
:
bytes
=
File
()):
async
def
create_file
(
file
:
bytes
=
File
()):
# 获取文件内容
# 获取文件内容
# 打开文件以写入模式
# 打开文件以写入模式
...
@@ -99,27 +102,29 @@ async def create_file(file: bytes = File()):
...
@@ -99,27 +102,29 @@ async def create_file(file: bytes = File()):
answer
=
[]
answer
=
[]
print
(
str
(
res
[
0
][
"sentence_info"
]))
print
(
str
(
res
[
0
][
"sentence_info"
]))
for
one
in
res
[
0
][
"sentence_info"
]:
for
one
in
res
[
0
][
"sentence_info"
]:
start_time
=
one
[
'start'
]
start_time
=
one
[
'start'
]
end_time
=
one
[
'end'
]
end_time
=
one
[
'end'
]
answer
.
append
(
answer
.
append
(
f
'{"角色1" if one["spk"] == 0 else "角色2"} : {one["text"]} -- 时长:{end_time-
start_time}ms'
f
'{"角色1" if one["spk"] == 0 else "角色2"} : {one["text"]} -- 时长:{end_time -
start_time}ms'
)
)
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)} 秒"
)
logger
.
info
(
f
"Function create_file executed in {
(consuming_end_time - consuming_start_time)} 秒"
)
logger
.
info
(
f
"Function create_file executed in {(consuming_end_time - consuming_start_time)} 秒"
)
os
.
remove
(
save_file
)
os
.
remove
(
save_file
)
re
turn
BaseResponse
(
re
sponse
=
BaseResponse
(
code
=
200
,
code
=
200
,
msg
=
res
[
0
][
"text"
],
msg
=
res
[
0
][
"text"
],
data
=
answer
,
data
=
answer
,
time_consuming
=
int
(
consuming_end_time
-
consuming_start_time
),
time_consuming
=
int
(
consuming_end_time
-
consuming_start_time
),
)
)
print
(
f
"response:{response}"
)
return
response
except
Exception
as
e
:
except
Exception
as
e
:
os
.
remove
(
save_file
)
os
.
remove
(
save_file
)
return
{
"error"
:
str
(
e
)}
return
{
"error"
:
str
(
e
)}
@app.post
(
"/fun
sa
r/uploadfile/"
)
@app.post
(
"/fun
as
r/uploadfile/"
)
async
def
create_upload_file
(
file
:
UploadFile
):
async
def
create_upload_file
(
file
:
UploadFile
):
try
:
try
:
consuming_start_time
=
time
.
perf_counter
()
consuming_start_time
=
time
.
perf_counter
()
...
@@ -142,15 +147,15 @@ async def create_upload_file(file: UploadFile):
...
@@ -142,15 +147,15 @@ async def create_upload_file(file: UploadFile):
answer
=
[]
answer
=
[]
print
(
str
(
res
[
0
][
"sentence_info"
]))
print
(
str
(
res
[
0
][
"sentence_info"
]))
for
one
in
res
[
0
][
"sentence_info"
]:
for
one
in
res
[
0
][
"sentence_info"
]:
start_time
=
one
[
'start'
]
start_time
=
one
[
'start'
]
end_time
=
one
[
'end'
]
end_time
=
one
[
'end'
]
answer
.
append
(
answer
.
append
(
f
'{"角色1" if one["spk"] == 0 else "角色2"} : {one["text"]} -- 时长:{end_time
-
start_time}ms'
f
'{"角色1" if one["spk"] == 0 else "角色2"} : {one["text"]} -- 时长:{end_time
-
start_time}ms'
)
)
consuming_end_time
=
time
.
perf_counter
()
consuming_end_time
=
time
.
perf_counter
()
print
(
f
"Function create_upload_file executed in {
(consuming_end_time - consuming_start_time)} 秒"
)
print
(
f
"Function create_upload_file executed in {(consuming_end_time - consuming_start_time)} 秒"
)
logger
.
info
(
logger
.
info
(
f
"Function create_upload_file executed in {
(consuming_end_time - consuming_start_time)} 秒"
f
"Function create_upload_file executed in {(consuming_end_time - consuming_start_time)} 秒"
)
)
os
.
remove
(
save_file
)
os
.
remove
(
save_file
)
return
BaseResponse
(
return
BaseResponse
(
...
@@ -164,13 +169,45 @@ async def create_upload_file(file: UploadFile):
...
@@ -164,13 +169,45 @@ async def create_upload_file(file: UploadFile):
return
{
"error"
:
str
(
e
)}
return
{
"error"
:
str
(
e
)}
def
url_to_byte_stream
(
url
):
# 发送HTTP请求获取文件内容
response
=
requests
.
get
(
url
)
# 检查请求是否成功
if
response
.
status_code
==
200
:
# 返回HTTP响应的内容
return
response
.
content
else
:
print
(
f
"Failed to retrieve the file. Status code: {response.status_code}"
)
return
None
class
UrlParam
(
BaseModel
):
url
:
str
@app.post
(
"/funasr/url"
)
async
def
create_url
(
param
:
UrlParam
):
try
:
url
=
param
.
url
print
(
f
"url=={url}"
)
# file = download_from_url(url)
# print({f"file=={file}"})
asyncio
.
run
(
async_task
(
url
))
return
{
"msg"
:
"hello_world"
}
except
Exception
as
e
:
return
{
"error"
:
str
(
e
)}
async
def
async_task
(
url
:
str
):
file_stream
=
url_to_byte_stream
(
url
)
print
(
"its ok"
)
asyncio
.
create_task
(
create_file
(
file_stream
))
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
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
)
parser
=
argparse
.
ArgumentParser
()
parser
=
argparse
.
ArgumentParser
()
parser
.
add_argument
(
"--host"
,
type
=
str
,
default
=
"0.0.0.0"
)
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
()
args
=
parser
.
parse_args
()
uvicorn
.
run
(
uvicorn
.
run
(
app
=
"main:app"
,
app
=
"main:app"
,
...
...
requirements.txt
View file @
91b5ff3
No preview for this file type
test01.py
View file @
91b5ff3
import
time
import
time
from
funasr
import
AutoModel
from
funasr
import
AutoModel
# 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
(
...
@@ -7,6 +8,7 @@ model = AutoModel(
...
@@ -7,6 +8,7 @@ model = AutoModel(
vad_model
=
"fsmn-vad"
,
vad_model
=
"fsmn-vad"
,
punc_model
=
"ct-punc-c"
,
punc_model
=
"ct-punc-c"
,
spk_model
=
"cam++"
,
spk_model
=
"cam++"
,
# openai_model="Whisper-large-v3",
ncpu
=
8
,
ncpu
=
8
,
)
)
...
@@ -21,26 +23,41 @@ consuming_start_time = time.perf_counter()
...
@@ -21,26 +23,41 @@ consuming_start_time = time.perf_counter()
# res = model.generate(input="asr_example.wav", batch_size_s=300, hotword="魔搭")
# res = model.generate(input="asr_example.wav", batch_size_s=300, hotword="魔搭")
# res = model.generate(input="123456.wav", batch_size_s=300, hotword="魔搭")
# res = model.generate(input="123456.wav", batch_size_s=300, hotword="魔搭")
res
=
model
.
generate
(
res
=
model
.
generate
(
input
=
"
陕西西安店-陕西店1-2024-02-29_14.39.15-43.0
.MP3"
,
input
=
"
北京店1-2024-02-29_14.58.11
.MP3"
,
batch_size_s
=
300
,
batch_size_s
=
300
,
hotword
=
"
华为问界 80
"
,
hotword
=
"
问界 80
\n
电瓶 100
\n
保修 100
\n
问界店 100
\n
M7
\n
M5
\n
M9
"
,
)
)
print
(
res
)
print
(
f
"res:{res}"
)
print
(
res
[
0
][
"sentence_info"
])
sentence_info
=
res
[
0
][
"sentence_info"
]
print
(
res
[
0
][
"sentence_info"
])
print
(
f
"res_sentence_info:{sentence_info}"
)
for
one
in
res
[
0
][
"sentence_info"
]:
# print(res[0]["sentence_info"])
spk
=
0
total_time
=
0
content
=
""
for
one
in
sentence_info
:
# print(one)
# print(one)
# print(one["end"] - one["start"])
# print(one["end"] - one["start"])
# print(one["text"])
# print(one["text"])
# print(one["spk"])
# print(one["spk"])
# print(f'{one["end"] - one["start"]}; {one["text"]}; {one["spk"]}')
# print(f'{one["end"] - one["start"]}; {one["text"]}; {one["spk"]}')
start_time
=
one
[
'start'
]
total_time
=
total_time
+
(
one
[
"end"
]
-
one
[
"start"
])
end_time
=
one
[
"end"
]
if
spk
==
one
[
"spk"
]:
text
=
f
'{"角色1" if one["spk"] == 0 else "角色2"} : {one["text"]} -- 时长:{end_time-start_time}ms'
content
=
content
+
one
[
"text"
]
else
:
text
=
f
'角色{spk + 1} : {content} -- 时长:{total_time}ms'
print
(
text
)
write_text
(
text
)
total_time
=
0
content
=
one
[
"text"
]
spk
=
one
[
"spk"
]
if
content
is
not
None
:
text
=
f
'角色{spk + 1} : {content} -- 时长:{total_time}ms'
print
(
text
)
print
(
text
)
write_text
(
text
)
write_text
(
text
)
consuming_end_time
=
time
.
perf_counter
()
consuming_end_time
=
time
.
perf_counter
()
print
(
print
(
f
"Function transform_file executed in {
(consuming_end_time - consuming_start_time)} 秒"
f
"Function transform_file executed in {(consuming_end_time - consuming_start_time)} 秒"
)
)
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