api_server.py 2.48 KB
Newer Older
1
2
3
import signal
import sys
import psutil
helloyongyang's avatar
helloyongyang committed
4
import argparse
5
from fastapi import FastAPI, Request
helloyongyang's avatar
helloyongyang committed
6
7
8
from pydantic import BaseModel
import uvicorn
import json
9
import asyncio
helloyongyang's avatar
helloyongyang committed
10
11
12
13
14
15

from lightx2v.utils.profiler import ProfilingContext
from lightx2v.utils.set_config import set_config
from lightx2v.infer import init_runner


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# =========================
# 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("\nReceived Ctrl+C, shutting down all related processes...")
    kill_all_related_processes()
    sys.exit(0)


# =========================
# FastAPI Related Code
# =========================

runner = None

app = FastAPI()


helloyongyang's avatar
helloyongyang committed
51
52
53
54
55
56
57
58
59
60
class Message(BaseModel):
    prompt: str
    negative_prompt: str = ""
    image_path: str = ""
    save_video_path: str

    def get(self, key, default=None):
        return getattr(self, key, default)


61
@app.post("/v1/local/video/generate")
helloyongyang's avatar
helloyongyang committed
62
async def v1_local_video_generate(message: Message):
63
    global runner
helloyongyang's avatar
helloyongyang committed
64
    runner.set_inputs(message)
65
66
    await asyncio.to_thread(runner.run_pipeline)
    return {"response": "finished", "save_video_path": message.save_video_path}
helloyongyang's avatar
helloyongyang committed
67
68


69
70
71
72
# =========================
# Main Entry
# =========================

helloyongyang's avatar
helloyongyang committed
73
if __name__ == "__main__":
74
    signal.signal(signal.SIGINT, signal_handler)
helloyongyang's avatar
helloyongyang committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
    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")
    parser.add_argument("--model_path", type=str, required=True)
    parser.add_argument("--config_json", type=str, required=True)
    parser.add_argument("--port", type=int, default=8000)
    args = parser.parse_args()
    print(f"args: {args}")

    with ProfilingContext("Init Server Cost"):
        config = set_config(args)
        print(f"config:\n{json.dumps(config, ensure_ascii=False, indent=4)}")
        runner = init_runner(config)

89
    uvicorn.run(app, host="0.0.0.0", port=config.port, reload=False, workers=1)