Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
xuwx1
LightX2V
Commits
c98d486d
Commit
c98d486d
authored
Apr 28, 2025
by
helloyongyang
Browse files
Support Ctrl-C when server is running.
parent
704bf91e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
53 additions
and
12 deletions
+53
-12
lightx2v/api_server.py
lightx2v/api_server.py
+51
-12
scripts/post.py
scripts/post.py
+2
-0
No files found.
lightx2v/api_server.py
View file @
c98d486d
import
signal
import
sys
import
psutil
import
argparse
from
fastapi
import
FastAPI
from
fastapi
import
FastAPI
,
Request
from
pydantic
import
BaseModel
import
uvicorn
import
json
import
asyncio
from
lightx2v.utils.profiler
import
ProfilingContext
from
lightx2v.utils.set_config
import
set_config
from
lightx2v.infer
import
init_runner
# =========================
# Signal & Process Control
# =========================
def
kill_all_related_processes
():
"""Kill the current process and all its child processes"""
current_process
=
psutil
.
Process
()
children
=
current_process
.
children
(
recursive
=
True
)
for
child
in
children
:
try
:
child
.
kill
()
except
Exception
as
e
:
print
(
f
"Failed to kill child process
{
child
.
pid
}
:
{
e
}
"
)
try
:
current_process
.
kill
()
except
Exception
as
e
:
print
(
f
"Failed to kill main process:
{
e
}
"
)
def
signal_handler
(
sig
,
frame
):
print
(
"
\n
Received Ctrl+C, shutting down all related processes..."
)
kill_all_related_processes
()
sys
.
exit
(
0
)
# =========================
# FastAPI Related Code
# =========================
runner
=
None
app
=
FastAPI
()
class
Message
(
BaseModel
):
prompt
:
str
negative_prompt
:
str
=
""
...
...
@@ -19,13 +58,20 @@ class Message(BaseModel):
return
getattr
(
self
,
key
,
default
)
async
def
main
(
message
):
@
app
.
post
(
"/v1/local/video/generate"
)
async
def
v1_local_video_generate
(
message
:
Message
,
request
:
Request
):
global
runner
runner
.
set_inputs
(
message
)
runner
.
run_pipeline
(
)
return
{
"response"
:
"finished"
}
await
asyncio
.
to_thread
(
runner
.
run_pipeline
)
return
{
"response"
:
"finished"
,
"save_video_path"
:
message
.
save_video_path
}
# =========================
# Main Entry
# =========================
if
__name__
==
"__main__"
:
signal
.
signal
(
signal
.
SIGINT
,
signal_handler
)
parser
=
argparse
.
ArgumentParser
()
parser
.
add_argument
(
"--model_cls"
,
type
=
str
,
required
=
True
,
choices
=
[
"wan2.1"
,
"hunyuan"
,
"wan2.1_causal"
],
default
=
"hunyuan"
)
parser
.
add_argument
(
"--task"
,
type
=
str
,
choices
=
[
"t2v"
,
"i2v"
],
default
=
"t2v"
)
...
...
@@ -40,11 +86,4 @@ if __name__ == "__main__":
print
(
f
"config:
\n
{
json
.
dumps
(
config
,
ensure_ascii
=
False
,
indent
=
4
)
}
"
)
runner
=
init_runner
(
config
)
app
=
FastAPI
()
@
app
.
post
(
"/v1/local/video/generate"
)
async
def
generate_video
(
message
:
Message
):
response
=
await
main
(
message
)
return
response
uvicorn
.
run
(
app
,
host
=
"0.0.0.0"
,
port
=
config
.
port
)
uvicorn
.
run
(
app
,
host
=
"0.0.0.0"
,
port
=
config
.
port
,
reload
=
False
,
workers
=
1
)
scripts/post.py
View file @
c98d486d
...
...
@@ -10,6 +10,8 @@ message = {
"save_video_path"
:
"./output_lightx2v_wan_t2v_ap4.mp4"
,
# It is best to set it to an absolute path.
}
print
(
f
"message:
{
message
}
"
)
response
=
requests
.
post
(
url
,
json
=
message
)
print
(
f
"response:
{
response
.
json
()
}
"
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
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 comment