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 6c1e1115
authored
Mar 13, 2024
by
杨林龙
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
优化异步识别
1 parent
91b5ff34
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
12 additions
and
21 deletions
main.py
main.py
View file @
6c1e111
...
...
@@ -16,6 +16,7 @@ from fastapi import Body, FastAPI, File, UploadFile
from
funasr_service
import
FunasrService
from
pydantic
import
BaseModel
from
funasr.download.file
import
download_from_url
import
multiprocessing
logger
=
logging
.
getLogger
()
app
=
FastAPI
()
...
...
@@ -169,17 +170,16 @@ async def create_upload_file(file: UploadFile):
return
{
"error"
:
str
(
e
)}
def
url_to_byte_stream
(
url
):
# 发送HTTP请求获取文件内容
response
=
requests
.
get
(
url
)
def
deal_worker
(
url
):
"""
要执行的函数,在子进程中运行
"""
print
(
f
"Worker {url} is running..."
)
# 解析音频
res
=
FunasrService
(
url
)
.
transform
()
time
.
sleep
(
1
)
print
(
f
"Worker {url} finished."
)
# 检查请求是否成功
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
...
...
@@ -187,20 +187,11 @@ class UrlParam(BaseModel):
@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"
}
process
=
multiprocessing
.
Process
(
target
=
deal_worker
,
args
=
(
param
.
url
))
process
.
start
()
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__"
:
save_path
=
"./audio/"
if
os
.
path
.
exists
(
save_path
):
...
...
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