2022-10-19 05:58:55 +02:00
|
|
|
"""server.py: FastAPI SD-UI Web Host.
|
|
|
|
Notes:
|
|
|
|
async endpoints always run on the main thread. Without they run on the thread pool.
|
|
|
|
"""
|
2022-09-02 10:28:36 +02:00
|
|
|
import os
|
2022-12-07 17:45:35 +01:00
|
|
|
import traceback
|
|
|
|
import logging
|
2022-12-11 08:49:22 +01:00
|
|
|
import datetime
|
2022-12-07 17:45:35 +01:00
|
|
|
from typing import List, Union
|
2022-09-02 10:28:36 +02:00
|
|
|
|
|
|
|
from fastapi import FastAPI, HTTPException
|
2022-09-22 17:01:30 +02:00
|
|
|
from fastapi.staticfiles import StaticFiles
|
2022-10-14 09:47:25 +02:00
|
|
|
from starlette.responses import FileResponse, JSONResponse, StreamingResponse
|
2022-09-02 10:28:36 +02:00
|
|
|
from pydantic import BaseModel
|
|
|
|
|
2022-12-07 17:45:35 +01:00
|
|
|
from sd_internal import app, model_manager, task_manager
|
2022-12-11 13:46:29 +01:00
|
|
|
from sd_internal import TaskData
|
2022-12-12 16:16:11 +01:00
|
|
|
from diffusionkit.types import GenerateImageRequest
|
2022-09-02 10:28:36 +02:00
|
|
|
|
2022-12-09 17:00:18 +01:00
|
|
|
log = logging.getLogger()
|
|
|
|
|
|
|
|
log.info(f'started in {app.SD_DIR}')
|
2022-12-11 08:49:22 +01:00
|
|
|
log.info(f'started at {datetime.datetime.now():%x %X}')
|
2022-09-02 10:28:36 +02:00
|
|
|
|
2022-12-07 17:45:35 +01:00
|
|
|
server_api = FastAPI()
|
2022-10-19 18:04:40 +02:00
|
|
|
|
2022-10-14 09:47:25 +02:00
|
|
|
NOCACHE_HEADERS={"Cache-Control": "no-cache, no-store, must-revalidate", "Pragma": "no-cache", "Expires": "0"}
|
2022-10-19 18:04:40 +02:00
|
|
|
|
2022-11-18 13:01:20 +01:00
|
|
|
class NoCacheStaticFiles(StaticFiles):
|
|
|
|
def is_not_modified(self, response_headers, request_headers) -> bool:
|
|
|
|
if 'content-type' in response_headers and ('javascript' in response_headers['content-type'] or 'css' in response_headers['content-type']):
|
|
|
|
response_headers.update(NOCACHE_HEADERS)
|
|
|
|
return False
|
|
|
|
|
|
|
|
return super().is_not_modified(response_headers, request_headers)
|
|
|
|
|
2022-09-09 17:04:32 +02:00
|
|
|
class SetAppConfigRequest(BaseModel):
|
2022-10-17 03:41:39 +02:00
|
|
|
update_branch: str = None
|
|
|
|
render_devices: Union[List[str], List[int], str, int] = None
|
2022-10-28 16:36:44 +02:00
|
|
|
model_vae: str = None
|
2022-11-16 08:13:46 +01:00
|
|
|
ui_open_browser_on_start: bool = None
|
2022-11-19 17:10:45 +01:00
|
|
|
listen_to_network: bool = None
|
|
|
|
listen_port: int = None
|
2022-11-25 09:27:15 +01:00
|
|
|
test_sd2: bool = None
|
2022-09-09 17:04:32 +02:00
|
|
|
|
2022-12-07 17:45:35 +01:00
|
|
|
server_api.mount('/media', NoCacheStaticFiles(directory=os.path.join(app.SD_UI_DIR, 'media')), name="media")
|
|
|
|
|
|
|
|
for plugins_dir, dir_prefix in app.UI_PLUGINS_SOURCES:
|
2022-12-09 11:41:08 +01:00
|
|
|
server_api.mount(f'/plugins/{dir_prefix}', NoCacheStaticFiles(directory=plugins_dir), name=f"plugins-{dir_prefix}")
|
2022-12-07 17:45:35 +01:00
|
|
|
|
|
|
|
@server_api.post('/app_config')
|
2022-10-17 03:41:39 +02:00
|
|
|
async def setAppConfig(req : SetAppConfigRequest):
|
2022-12-07 17:45:35 +01:00
|
|
|
config = app.getConfig()
|
2022-11-16 08:13:46 +01:00
|
|
|
if req.update_branch is not None:
|
2022-10-17 03:41:39 +02:00
|
|
|
config['update_branch'] = req.update_branch
|
2022-11-16 08:13:46 +01:00
|
|
|
if req.render_devices is not None:
|
2022-11-15 07:52:55 +01:00
|
|
|
update_render_devices_in_config(config, req.render_devices)
|
2022-11-16 08:13:46 +01:00
|
|
|
if req.ui_open_browser_on_start is not None:
|
|
|
|
if 'ui' not in config:
|
|
|
|
config['ui'] = {}
|
|
|
|
config['ui']['open_browser_on_start'] = req.ui_open_browser_on_start
|
2022-11-19 17:10:45 +01:00
|
|
|
if req.listen_to_network is not None:
|
|
|
|
if 'net' not in config:
|
|
|
|
config['net'] = {}
|
|
|
|
config['net']['listen_to_network'] = bool(req.listen_to_network)
|
|
|
|
if req.listen_port is not None:
|
|
|
|
if 'net' not in config:
|
|
|
|
config['net'] = {}
|
|
|
|
config['net']['listen_port'] = int(req.listen_port)
|
2022-11-25 09:27:15 +01:00
|
|
|
if req.test_sd2 is not None:
|
|
|
|
config['test_sd2'] = req.test_sd2
|
2022-10-17 03:41:39 +02:00
|
|
|
try:
|
2022-12-07 17:45:35 +01:00
|
|
|
app.setConfig(config)
|
2022-11-15 07:52:55 +01:00
|
|
|
|
|
|
|
if req.render_devices:
|
2022-12-07 17:45:35 +01:00
|
|
|
app.update_render_threads()
|
2022-11-15 07:52:55 +01:00
|
|
|
|
2022-10-17 03:41:39 +02:00
|
|
|
return JSONResponse({'status': 'OK'}, headers=NOCACHE_HEADERS)
|
|
|
|
except Exception as e:
|
2022-12-09 17:00:18 +01:00
|
|
|
log.error(traceback.format_exc())
|
2022-10-18 08:36:57 +02:00
|
|
|
raise HTTPException(status_code=500, detail=str(e))
|
2022-10-17 03:41:39 +02:00
|
|
|
|
2022-12-07 17:45:35 +01:00
|
|
|
def update_render_devices_in_config(config, render_devices):
|
|
|
|
if render_devices not in ('cpu', 'auto') and not render_devices.startswith('cuda:'):
|
|
|
|
raise HTTPException(status_code=400, detail=f'Invalid render device requested: {render_devices}')
|
2022-10-21 02:08:23 +02:00
|
|
|
|
2022-12-07 17:45:35 +01:00
|
|
|
if render_devices.startswith('cuda:'):
|
|
|
|
render_devices = render_devices.split(',')
|
2022-10-21 02:08:23 +02:00
|
|
|
|
2022-12-07 17:45:35 +01:00
|
|
|
config['render_devices'] = render_devices
|
2022-11-23 11:25:36 +01:00
|
|
|
|
2022-12-07 17:45:35 +01:00
|
|
|
@server_api.get('/get/{key:path}')
|
2022-10-17 03:41:39 +02:00
|
|
|
def read_web_data(key:str=None):
|
|
|
|
if not key: # /get without parameters, stable-diffusion easter egg.
|
2022-10-18 08:36:57 +02:00
|
|
|
raise HTTPException(status_code=418, detail="StableDiffusion is drawing a teapot!") # HTTP418 I'm a teapot
|
2022-10-17 03:41:39 +02:00
|
|
|
elif key == 'app_config':
|
2022-12-07 17:45:35 +01:00
|
|
|
return JSONResponse(app.getConfig(), headers=NOCACHE_HEADERS)
|
2022-11-30 10:04:24 +01:00
|
|
|
elif key == 'system_info':
|
2022-12-07 17:45:35 +01:00
|
|
|
config = app.getConfig()
|
2022-11-30 10:04:24 +01:00
|
|
|
system_info = {
|
|
|
|
'devices': task_manager.get_devices(),
|
2022-12-07 17:45:35 +01:00
|
|
|
'hosts': app.getIPConfig(),
|
|
|
|
'default_output_dir': os.path.join(os.path.expanduser("~"), app.OUTPUT_DIRNAME),
|
2022-11-30 10:04:24 +01:00
|
|
|
}
|
|
|
|
system_info['devices']['config'] = config.get('render_devices', "auto")
|
|
|
|
return JSONResponse(system_info, headers=NOCACHE_HEADERS)
|
2022-10-17 03:41:39 +02:00
|
|
|
elif key == 'models':
|
2022-12-07 17:45:35 +01:00
|
|
|
return JSONResponse(model_manager.getModels(), headers=NOCACHE_HEADERS)
|
|
|
|
elif key == 'modifiers': return FileResponse(os.path.join(app.SD_UI_DIR, 'modifiers.json'), headers=NOCACHE_HEADERS)
|
|
|
|
elif key == 'ui_plugins': return JSONResponse(app.getUIPlugins(), headers=NOCACHE_HEADERS)
|
2022-10-17 03:41:39 +02:00
|
|
|
else:
|
2022-10-18 08:36:57 +02:00
|
|
|
raise HTTPException(status_code=404, detail=f'Request for unknown {key}') # HTTP404 Not Found
|
2022-10-06 10:58:02 +02:00
|
|
|
|
2022-12-07 17:45:35 +01:00
|
|
|
@server_api.get('/ping') # Get server and optionally session status.
|
2022-10-14 09:47:25 +02:00
|
|
|
def ping(session_id:str=None):
|
2022-10-17 03:41:39 +02:00
|
|
|
if task_manager.is_alive() <= 0: # Check that render threads are alive.
|
2022-10-18 08:36:57 +02:00
|
|
|
if task_manager.current_state_error: raise HTTPException(status_code=500, detail=str(task_manager.current_state_error))
|
2022-10-18 08:30:30 +02:00
|
|
|
raise HTTPException(status_code=500, detail='Render thread is dead.')
|
2022-10-18 08:36:57 +02:00
|
|
|
if task_manager.current_state_error and not isinstance(task_manager.current_state_error, StopAsyncIteration): raise HTTPException(status_code=500, detail=str(task_manager.current_state_error))
|
2022-10-14 09:47:25 +02:00
|
|
|
# Alive
|
2022-10-15 09:28:20 +02:00
|
|
|
response = {'status': str(task_manager.current_state)}
|
2022-10-14 09:47:25 +02:00
|
|
|
if session_id:
|
2022-12-08 06:42:46 +01:00
|
|
|
session = task_manager.get_cached_session(session_id, update_ttl=True)
|
|
|
|
response['tasks'] = {id(t): t.status for t in session.tasks}
|
2022-11-14 15:23:40 +01:00
|
|
|
response['devices'] = task_manager.get_devices()
|
2022-10-14 09:47:25 +02:00
|
|
|
return JSONResponse(response, headers=NOCACHE_HEADERS)
|
|
|
|
|
2022-12-07 17:45:35 +01:00
|
|
|
@server_api.post('/render')
|
2022-12-11 13:46:29 +01:00
|
|
|
def render(req: dict):
|
2022-10-15 09:28:20 +02:00
|
|
|
try:
|
2022-12-11 13:46:29 +01:00
|
|
|
# separate out the request data into rendering and task-specific data
|
|
|
|
render_req: GenerateImageRequest = GenerateImageRequest.parse_obj(req)
|
|
|
|
task_data: TaskData = TaskData.parse_obj(req)
|
2022-12-11 09:44:59 +01:00
|
|
|
|
2022-12-11 13:46:29 +01:00
|
|
|
render_req.init_image_mask = req.get('mask') # hack: will rename this in the HTTP API in a future revision
|
2022-12-07 17:45:35 +01:00
|
|
|
|
2022-12-11 13:46:29 +01:00
|
|
|
app.save_model_to_config(task_data.use_stable_diffusion_model, task_data.use_vae_model, task_data.use_hypernetwork_model)
|
2022-12-11 09:44:59 +01:00
|
|
|
|
|
|
|
# enqueue the task
|
2022-12-11 13:46:29 +01:00
|
|
|
new_task = task_manager.render(render_req, task_data)
|
2022-10-15 09:28:20 +02:00
|
|
|
response = {
|
|
|
|
'status': str(task_manager.current_state),
|
2022-10-17 03:41:39 +02:00
|
|
|
'queue': len(task_manager.tasks_queue),
|
2022-12-08 06:42:46 +01:00
|
|
|
'stream': f'/image/stream/{id(new_task)}',
|
2022-10-15 09:28:20 +02:00
|
|
|
'task': id(new_task)
|
|
|
|
}
|
|
|
|
return JSONResponse(response, headers=NOCACHE_HEADERS)
|
|
|
|
except ChildProcessError as e: # Render thread is dead
|
2022-10-18 08:30:30 +02:00
|
|
|
raise HTTPException(status_code=500, detail=f'Rendering thread has died.') # HTTP500 Internal Server Error
|
2022-12-08 06:42:46 +01:00
|
|
|
except ConnectionRefusedError as e: # Unstarted task pending limit reached, deny queueing too many.
|
|
|
|
raise HTTPException(status_code=503, detail=str(e)) # HTTP503 Service Unavailable
|
2022-10-15 09:28:20 +02:00
|
|
|
except Exception as e:
|
2022-12-09 17:00:18 +01:00
|
|
|
log.error(traceback.format_exc())
|
2022-10-18 08:30:30 +02:00
|
|
|
raise HTTPException(status_code=500, detail=str(e))
|
2022-10-14 09:47:25 +02:00
|
|
|
|
2022-12-08 07:28:09 +01:00
|
|
|
@server_api.get('/image/stream/{task_id:int}')
|
2022-12-08 06:42:46 +01:00
|
|
|
def stream(task_id:int):
|
2022-10-14 09:47:25 +02:00
|
|
|
#TODO Move to WebSockets ??
|
2022-12-08 06:42:46 +01:00
|
|
|
task = task_manager.get_cached_task(task_id, update_ttl=True)
|
|
|
|
if not task: raise HTTPException(status_code=404, detail=f'Request {task_id} not found.') # HTTP404 NotFound
|
|
|
|
#if (id(task) != task_id): raise HTTPException(status_code=409, detail=f'Wrong task id received. Expected:{id(task)}, Received:{task_id}') # HTTP409 Conflict
|
2022-10-14 09:47:25 +02:00
|
|
|
if task.buffer_queue.empty() and not task.lock.locked():
|
|
|
|
if task.response:
|
2022-12-09 17:00:18 +01:00
|
|
|
#log.info(f'Session {session_id} sending cached response')
|
2022-10-14 09:47:25 +02:00
|
|
|
return JSONResponse(task.response, headers=NOCACHE_HEADERS)
|
2022-10-18 08:30:30 +02:00
|
|
|
raise HTTPException(status_code=425, detail='Too Early, task not started yet.') # HTTP425 Too Early
|
2022-12-09 17:00:18 +01:00
|
|
|
#log.info(f'Session {session_id} opened live render stream {id(task.buffer_queue)}')
|
2022-10-15 09:28:20 +02:00
|
|
|
return StreamingResponse(task.read_buffer_generator(), media_type='application/json')
|
2022-09-13 16:29:41 +02:00
|
|
|
|
2022-12-07 17:45:35 +01:00
|
|
|
@server_api.get('/image/stop')
|
2022-12-08 06:42:46 +01:00
|
|
|
def stop(task: int):
|
|
|
|
if not task:
|
2022-10-15 09:32:00 +02:00
|
|
|
if task_manager.current_state == task_manager.ServerStates.Online or task_manager.current_state == task_manager.ServerStates.Unavailable:
|
2022-10-18 08:30:30 +02:00
|
|
|
raise HTTPException(status_code=409, detail='Not currently running any tasks.') # HTTP409 Conflict
|
2022-10-15 09:28:20 +02:00
|
|
|
task_manager.current_state_error = StopAsyncIteration('')
|
2022-09-13 16:29:41 +02:00
|
|
|
return {'OK'}
|
2022-12-08 06:42:46 +01:00
|
|
|
task_id = task
|
|
|
|
task = task_manager.get_cached_task(task_id, update_ttl=False)
|
|
|
|
if not task: raise HTTPException(status_code=404, detail=f'Task {task_id} was not found.') # HTTP404 Not Found
|
|
|
|
if isinstance(task.error, StopAsyncIteration): raise HTTPException(status_code=409, detail=f'Task {task_id} is already stopped.') # HTTP409 Conflict
|
|
|
|
task.error = StopAsyncIteration(f'Task {task_id} stop requested.')
|
2022-10-14 09:47:25 +02:00
|
|
|
return {'OK'}
|
2022-09-13 16:29:41 +02:00
|
|
|
|
2022-12-08 07:28:09 +01:00
|
|
|
@server_api.get('/image/tmp/{task_id:int}/{img_id:int}')
|
2022-12-08 06:42:46 +01:00
|
|
|
def get_image(task_id: int, img_id: int):
|
|
|
|
task = task_manager.get_cached_task(task_id, update_ttl=True)
|
|
|
|
if not task: raise HTTPException(status_code=410, detail=f'Task {task_id} could not be found.') # HTTP404 NotFound
|
2022-10-18 08:30:30 +02:00
|
|
|
if not task.temp_images[img_id]: raise HTTPException(status_code=425, detail='Too Early, task data is not available yet.') # HTTP425 Too Early
|
2022-10-14 09:47:25 +02:00
|
|
|
try:
|
|
|
|
img_data = task.temp_images[img_id]
|
|
|
|
img_data.seek(0)
|
|
|
|
return StreamingResponse(img_data, media_type='image/jpeg')
|
|
|
|
except KeyError as e:
|
2022-10-18 08:30:30 +02:00
|
|
|
raise HTTPException(status_code=500, detail=str(e))
|
2022-09-14 18:59:42 +02:00
|
|
|
|
2022-12-07 17:45:35 +01:00
|
|
|
@server_api.get('/')
|
2022-10-17 03:41:39 +02:00
|
|
|
def read_root():
|
2022-12-07 17:45:35 +01:00
|
|
|
return FileResponse(os.path.join(app.SD_UI_DIR, 'index.html'), headers=NOCACHE_HEADERS)
|
2022-10-06 10:58:02 +02:00
|
|
|
|
2022-12-07 17:45:35 +01:00
|
|
|
@server_api.on_event("shutdown")
|
2022-10-17 03:41:39 +02:00
|
|
|
def shutdown_event(): # Signal render thread to close on shutdown
|
|
|
|
task_manager.current_state_error = SystemExit('Application shutting down.')
|
2022-09-02 10:28:36 +02:00
|
|
|
|
2022-12-07 17:45:35 +01:00
|
|
|
# Init the app
|
|
|
|
model_manager.init()
|
|
|
|
app.init()
|
2022-11-07 13:56:10 +01:00
|
|
|
|
2022-09-02 10:28:36 +02:00
|
|
|
# start the browser ui
|
2022-12-07 17:45:35 +01:00
|
|
|
app.open_browser()
|