File size: 9,368 Bytes
488b360
 
 
 
 
 
5c81b63
 
488b360
 
87914c1
488b360
100e61a
488b360
2ab3299
100e61a
488b360
 
 
 
 
100e61a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
488b360
 
 
 
 
 
100e61a
488b360
100e61a
488b360
 
100e61a
5c81b63
 
488b360
 
100e61a
488b360
 
 
 
 
 
 
 
 
100e61a
488b360
 
 
 
 
 
 
 
 
 
 
100e61a
488b360
100e61a
488b360
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100e61a
 
 
87914c1
100e61a
 
 
 
 
 
 
87914c1
 
100e61a
87914c1
 
 
 
100e61a
 
 
 
 
87914c1
 
 
488b360
 
 
 
 
 
100e61a
488b360
 
 
 
100e61a
488b360
100e61a
 
488b360
 
87914c1
 
 
488b360
100e61a
 
 
 
87914c1
488b360
100e61a
 
 
5c81b63
100e61a
5c81b63
 
 
 
488b360
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100e61a
488b360
 
 
100e61a
 
 
 
 
488b360
 
100e61a
488b360
 
 
 
 
100e61a
488b360
 
 
 
 
 
76336af
 
 
488b360
 
100e61a
 
 
 
 
 
 
 
 
 
 
 
 
 
ddaada9
 
100e61a
488b360
100e61a
 
 
488b360
 
 
 
100e61a
 
488b360
100e61a
488b360
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
from fastapi import FastAPI, WebSocket, HTTPException, WebSocketDisconnect
from fastapi.responses import StreamingResponse, JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi import Request
import markdown2
from pipelines.utils.safety_checker import SafetyChecker
from PIL import Image
import logging
from config import config, Args
from connection_manager import ConnectionManager, ServerFullException
import uuid
from uuid import UUID
import time
from typing import Any, Protocol, runtime_checkable
from util import pil_to_frame, bytes_to_pil, is_firefox, get_pipeline_class, ParamsModel
from device import device, torch_dtype
import asyncio
import os
import time
import torch
from pydantic import BaseModel, create_model

@runtime_checkable
class BasePipeline(Protocol):
    class Info:
        @classmethod
        def schema(cls) -> dict[str, Any]:
            ...
        page_content: str | None
        input_mode: str
    
    class InputParams(ParamsModel):
        @classmethod
        def schema(cls) -> dict[str, Any]:
            ...
        
        def dict(self) -> dict[str, Any]:
            ...
            
    def predict(self, params: ParamsModel) -> Image.Image | None:
        ...


THROTTLE = 1.0 / 120


class App:
    def __init__(self, config: Args, pipeline_instance: BasePipeline):
        self.args = config
        self.pipeline = pipeline_instance
        self.app = FastAPI()
        self.conn_manager = ConnectionManager()
        self.safety_checker: SafetyChecker | None = None
        if self.args.safety_checker:
            self.safety_checker = SafetyChecker(device=device.type)
        self.init_app()

    def init_app(self) -> None:
        self.app.add_middleware(
            CORSMiddleware,
            allow_origins=["*"],
            allow_credentials=True,
            allow_methods=["*"],
            allow_headers=["*"],
        )

        @self.app.websocket("/api/ws/{user_id}")
        async def websocket_endpoint(user_id: UUID, websocket: WebSocket) -> None:
            try:
                await self.conn_manager.connect(
                    user_id, websocket, self.args.max_queue_size
                )
                await handle_websocket_data(user_id)
            except ServerFullException as e:
                logging.error(f"Server Full: {e}")
            finally:
                await self.conn_manager.disconnect(user_id)
                logging.info(f"User disconnected: {user_id}")

        async def handle_websocket_data(user_id: UUID) -> None:
            if not self.conn_manager.check_user(user_id):
                raise HTTPException(status_code=404, detail="User not found")
            last_time = time.time()
            try:
                while True:
                    if (
                        self.args.timeout > 0
                        and time.time() - last_time > self.args.timeout
                    ):
                        await self.conn_manager.send_json(
                            user_id,
                            {
                                "status": "timeout",
                                "message": "Your session has ended",
                            },
                        )
                        await self.conn_manager.disconnect(user_id)
                        return
                    data = await self.conn_manager.receive_json(user_id)
                    if data is None:
                        continue
                        
                    if data["status"] == "next_frame":
                        info = self.pipeline.Info()
                        params_data = await self.conn_manager.receive_json(user_id)
                        if params_data is None:
                            continue
                            
                        params = self.pipeline.InputParams.model_validate(params_data)
                        
                        if info.input_mode == "image":
                            image_data = await self.conn_manager.receive_bytes(user_id)
                            if image_data is None or len(image_data) == 0:
                                await self.conn_manager.send_json(
                                    user_id, {"status": "send_frame"}
                                )
                                continue
                            
                            # Create a new Pydantic model with the image field
                            params_dict = params.model_dump()
                            params_dict["image"] = bytes_to_pil(image_data)
                            params = self.pipeline.InputParams.model_validate(params_dict)

                        await self.conn_manager.update_data(user_id, params)
                        await self.conn_manager.send_json(user_id, {"status": "wait"})

            except Exception as e:
                logging.error(f"Websocket Error: {e}, {user_id} ")
                await self.conn_manager.disconnect(user_id)

        @self.app.get("/api/queue")
        async def get_queue_size() -> JSONResponse:
            queue_size = self.conn_manager.get_user_count()
            return JSONResponse({"queue_size": queue_size})

        @self.app.get("/api/stream/{user_id}")
        async def stream(user_id: UUID, request: Request) -> StreamingResponse:
            try:
                async def generate() -> bytes:
                    last_params: ParamsModel | None = None
                    while True:
                        last_time = time.time()
                        await self.conn_manager.send_json(
                            user_id, {"status": "send_frame"}
                        )
                        params = await self.conn_manager.get_latest_data(user_id)
                        
                        if (params is None or 
                            (last_params is not None and 
                             params.model_dump() == last_params.model_dump())):
                            await asyncio.sleep(THROTTLE)
                            continue
                            
                        last_params = params
                        image = self.pipeline.predict(params)

                        if self.args.safety_checker and self.safety_checker is not None and image is not None:
                            image, has_nsfw_concept = self.safety_checker(image)
                            if has_nsfw_concept:
                                image = None

                        if image is None:
                            continue
                        frame = pil_to_frame(image)
                        yield frame
                        # https://bugs.chromium.org/p/chromium/issues/detail?id=1250396
                        if not is_firefox(request.headers["user-agent"]):
                            yield frame
                        if self.args.debug:
                            print(f"Time taken: {time.time() - last_time}")

                return StreamingResponse(
                    generate(),
                    media_type="multipart/x-mixed-replace;boundary=frame",
                    headers={"Cache-Control": "no-cache"},
                )
            except Exception as e:
                logging.error(f"Streaming Error: {e}, {user_id} ")
                raise HTTPException(status_code=404, detail="User not found")

        # route to setup frontend
        @self.app.get("/api/settings")
        async def settings() -> JSONResponse:
            info_schema = self.pipeline.Info.schema()
            info = self.pipeline.Info()
            page_content = ""
            if hasattr(info, 'page_content') and info.page_content:
                page_content = markdown2.markdown(info.page_content)

            input_params = self.pipeline.InputParams.schema()
            return JSONResponse(
                {
                    "info": info_schema,
                    "input_params": input_params,
                    "max_queue_size": self.args.max_queue_size,
                    "page_content": page_content,
                }
            )

        if not os.path.exists("public"):
            os.makedirs("public")

        self.app.mount(
            "/", StaticFiles(directory="frontend/public", html=True), name="public"
        )


# def create_app(config):
#     print(f"Device: {device}")
#     print(f"torch_dtype: {torch_dtype}")

#     # Create pipeline once
#     pipeline_class = get_pipeline_class(config.pipeline)
#     pipeline_instance = pipeline_class(config, device, torch_dtype)

#     # Pass the existing pipeline instance to App
#     app = App(config, pipeline_instance).app
#     return app


# Create app instance at module level
print(f"Device: {device}")
print(f"torch_dtype: {torch_dtype}")

pipeline_class = get_pipeline_class(config.pipeline)
pipeline_instance = pipeline_class(config, device, torch_dtype)
app = App(config, pipeline_instance).app  # This creates the FastAPI app instance


if __name__ == "__main__":
    import uvicorn

    # app = create_app(config)  # Create the app once

    uvicorn.run(
        app,
        host=config.host,
        port=config.port,
        reload=config.reload,
        ssl_certfile=config.ssl_certfile,
        ssl_keyfile=config.ssl_keyfile,
    )