2022-09-09 17:04:32 +02:00
|
|
|
import json
|
2022-09-02 10:28:36 +02:00
|
|
|
import traceback
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
|
2022-10-06 10:58:02 +02:00
|
|
|
SD_DIR = os.getcwd()
|
|
|
|
print('started in ', SD_DIR)
|
2022-09-02 10:28:36 +02:00
|
|
|
|
|
|
|
SD_UI_DIR = os.getenv('SD_UI_PATH', None)
|
|
|
|
sys.path.append(os.path.dirname(SD_UI_DIR))
|
|
|
|
|
2022-10-06 10:58:02 +02:00
|
|
|
CONFIG_DIR = os.path.abspath(os.path.join(SD_UI_DIR, '..', 'scripts'))
|
|
|
|
MODELS_DIR = os.path.abspath(os.path.join(SD_DIR, '..', 'models'))
|
2022-10-19 18:04:40 +02:00
|
|
|
UI_PLUGINS_DIR = os.path.abspath(os.path.join(SD_DIR, '..', 'plugins', 'ui'))
|
2022-09-09 17:04:32 +02:00
|
|
|
|
2022-09-02 10:28:36 +02:00
|
|
|
OUTPUT_DIRNAME = "Stable Diffusion UI" # in the user's home folder
|
2022-10-15 06:51:06 +02:00
|
|
|
TASK_TTL = 15 * 60 # Discard last session's task timeout
|
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-09-09 17:35:24 +02:00
|
|
|
import logging
|
2022-10-14 09:47:25 +02:00
|
|
|
import queue, threading, time
|
|
|
|
from typing import Any, Generator, Hashable, Optional, Union
|
2022-09-02 10:28:36 +02:00
|
|
|
|
2022-10-15 09:28:20 +02:00
|
|
|
from sd_internal import Request, Response, task_manager
|
2022-09-02 10:28:36 +02:00
|
|
|
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
|
|
modifiers_cache = None
|
|
|
|
outpath = os.path.join(os.path.expanduser("~"), OUTPUT_DIRNAME)
|
|
|
|
|
2022-10-19 18:04:40 +02:00
|
|
|
os.makedirs(UI_PLUGINS_DIR, exist_ok=True)
|
|
|
|
|
2022-09-24 12:25:45 +02:00
|
|
|
# don't show access log entries for URLs that start with the given prefix
|
2022-10-14 09:47:25 +02:00
|
|
|
ACCESS_LOG_SUPPRESS_PATH_PREFIXES = ['/ping', '/image', '/modifier-thumbnails']
|
2022-09-24 12:25:45 +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
|
|
|
|
|
|
|
app.mount('/media', StaticFiles(directory=os.path.join(SD_UI_DIR, 'media')), name="media")
|
|
|
|
app.mount('/plugins', StaticFiles(directory=UI_PLUGINS_DIR), name="plugins")
|
2022-09-24 12:25:45 +02:00
|
|
|
|
2022-09-09 17:04:32 +02:00
|
|
|
class SetAppConfigRequest(BaseModel):
|
|
|
|
update_branch: str = "main"
|
|
|
|
|
2022-10-06 10:58:02 +02:00
|
|
|
# needs to support the legacy installations
|
|
|
|
def get_initial_model_to_load():
|
|
|
|
custom_weight_path = os.path.join(SD_DIR, 'custom-model.ckpt')
|
|
|
|
ckpt_to_use = "sd-v1-4" if not os.path.exists(custom_weight_path) else "custom-model"
|
|
|
|
|
|
|
|
ckpt_to_use = os.path.join(SD_DIR, ckpt_to_use)
|
|
|
|
|
|
|
|
config = getConfig()
|
|
|
|
if 'model' in config and 'stable-diffusion' in config['model']:
|
|
|
|
model_name = config['model']['stable-diffusion']
|
|
|
|
model_path = resolve_model_to_use(model_name)
|
|
|
|
|
|
|
|
if os.path.exists(model_path + '.ckpt'):
|
|
|
|
ckpt_to_use = model_path
|
|
|
|
else:
|
|
|
|
print('Could not find the configured custom model at:', model_path + '.ckpt', '. Using the default one:', ckpt_to_use + '.ckpt')
|
|
|
|
return ckpt_to_use
|
|
|
|
|
|
|
|
def resolve_model_to_use(model_name):
|
|
|
|
if model_name in ('sd-v1-4', 'custom-model'):
|
|
|
|
model_path = os.path.join(MODELS_DIR, 'stable-diffusion', model_name)
|
|
|
|
|
|
|
|
legacy_model_path = os.path.join(SD_DIR, model_name)
|
|
|
|
if not os.path.exists(model_path + '.ckpt') and os.path.exists(legacy_model_path + '.ckpt'):
|
|
|
|
model_path = legacy_model_path
|
|
|
|
else:
|
|
|
|
model_path = os.path.join(MODELS_DIR, 'stable-diffusion', model_name)
|
|
|
|
return model_path
|
|
|
|
|
2022-10-15 09:28:20 +02:00
|
|
|
@app.on_event("shutdown")
|
|
|
|
def shutdown_event(): # Signal render thread to close on shutdown
|
|
|
|
task_manager.current_state_error = SystemExit('Application shutting down.')
|
2022-10-06 10:58:02 +02:00
|
|
|
|
2022-10-15 09:28:20 +02:00
|
|
|
@app.get('/')
|
|
|
|
def read_root():
|
|
|
|
return FileResponse(os.path.join(SD_UI_DIR, 'index.html'), headers=NOCACHE_HEADERS)
|
2022-10-06 10:58:02 +02:00
|
|
|
|
2022-10-14 09:47:25 +02:00
|
|
|
@app.get('/ping') # Get server and optionally session status.
|
|
|
|
def ping(session_id:str=None):
|
2022-10-15 09:28:20 +02:00
|
|
|
if not task_manager.render_thread.is_alive(): # Render thread is dead.
|
2022-10-23 21:00:36 +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-23 21:00:36 +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-10-15 09:28:20 +02:00
|
|
|
task = task_manager.task_cache.tryGet(session_id)
|
2022-10-14 09:47:25 +02:00
|
|
|
if task:
|
|
|
|
response['task'] = id(task)
|
|
|
|
if task.lock.locked():
|
|
|
|
response['session'] = 'running'
|
|
|
|
elif isinstance(task.error, StopAsyncIteration):
|
|
|
|
response['session'] = 'stopped'
|
|
|
|
elif task.error:
|
|
|
|
response['session'] = 'error'
|
|
|
|
elif not task.buffer_queue.empty():
|
|
|
|
response['session'] = 'buffer'
|
|
|
|
elif task.response:
|
|
|
|
response['session'] = 'completed'
|
|
|
|
else:
|
|
|
|
response['session'] = 'pending'
|
|
|
|
return JSONResponse(response, headers=NOCACHE_HEADERS)
|
|
|
|
|
2022-10-15 09:28:20 +02:00
|
|
|
def save_model_to_config(model_name):
|
|
|
|
config = getConfig()
|
|
|
|
if 'model' not in config:
|
|
|
|
config['model'] = {}
|
|
|
|
|
|
|
|
config['model']['stable-diffusion'] = model_name
|
|
|
|
setConfig(config)
|
|
|
|
|
2022-10-14 09:47:25 +02:00
|
|
|
@app.post('/render')
|
2022-10-15 09:28:20 +02:00
|
|
|
def render(req : task_manager.ImageRequest):
|
|
|
|
try:
|
|
|
|
save_model_to_config(req.use_stable_diffusion_model)
|
|
|
|
req.use_stable_diffusion_model = resolve_model_to_use(req.use_stable_diffusion_model)
|
|
|
|
new_task = task_manager.render(req)
|
|
|
|
response = {
|
|
|
|
'status': str(task_manager.current_state),
|
|
|
|
'queue': task_manager.tasks_queue.qsize(),
|
|
|
|
'stream': f'/image/stream/{req.session_id}/{id(new_task)}',
|
|
|
|
'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-10-15 09:28:20 +02:00
|
|
|
except ConnectionRefusedError as e: # Unstarted task pending, deny queueing more than one.
|
2022-10-18 08:30:30 +02:00
|
|
|
raise HTTPException(status_code=503, detail=f'Session {req.session_id} has an already pending task.') # HTTP503 Service Unavailable
|
2022-10-15 09:28:20 +02:00
|
|
|
except Exception as e:
|
2022-10-18 08:30:30 +02:00
|
|
|
raise HTTPException(status_code=500, detail=str(e))
|
2022-10-14 09:47:25 +02:00
|
|
|
|
|
|
|
@app.get('/image/stream/{session_id:str}/{task_id:int}')
|
|
|
|
def stream(session_id:str, task_id:int):
|
|
|
|
#TODO Move to WebSockets ??
|
2022-10-15 09:28:20 +02:00
|
|
|
task = task_manager.task_cache.tryGet(session_id)
|
2022-10-18 08:30:30 +02:00
|
|
|
if not task: raise HTTPException(status_code=410, detail='No request received.') # HTTP410 Gone
|
|
|
|
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:
|
|
|
|
#print(f'Session {session_id} sending cached response')
|
|
|
|
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-10-14 09:47:25 +02:00
|
|
|
#print(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-10-14 09:47:25 +02:00
|
|
|
@app.get('/image/stop')
|
|
|
|
def stop(session_id:str=None):
|
|
|
|
if not session_id:
|
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-10-15 09:28:20 +02:00
|
|
|
task = task_manager.task_cache.tryGet(session_id)
|
2022-10-18 08:30:30 +02:00
|
|
|
if not task: raise HTTPException(status_code=404, detail=f'Session {session_id} has no active task.') # HTTP404 Not Found
|
|
|
|
if isinstance(task.error, StopAsyncIteration): raise HTTPException(status_code=409, detail=f'Session {session_id} task is already stopped.') # HTTP409 Conflict
|
2022-10-14 09:47:25 +02:00
|
|
|
task.error = StopAsyncIteration('')
|
|
|
|
return {'OK'}
|
2022-09-13 16:29:41 +02:00
|
|
|
|
2022-10-14 09:47:25 +02:00
|
|
|
@app.get('/image/tmp/{session_id}/{img_id:int}')
|
2022-09-14 18:59:42 +02:00
|
|
|
def get_image(session_id, img_id):
|
2022-10-15 09:28:20 +02:00
|
|
|
task = task_manager.task_cache.tryGet(session_id)
|
2022-10-18 08:30:30 +02:00
|
|
|
if not task: raise HTTPException(status_code=410, detail=f'Session {session_id} has not submitted a task.') # HTTP410 Gone
|
|
|
|
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]
|
|
|
|
if isinstance(img_data, str):
|
|
|
|
return img_data
|
|
|
|
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-09-09 17:04:32 +02:00
|
|
|
@app.post('/app_config')
|
|
|
|
async def setAppConfig(req : SetAppConfigRequest):
|
|
|
|
try:
|
|
|
|
config = {
|
|
|
|
'update_branch': req.update_branch
|
|
|
|
}
|
|
|
|
|
|
|
|
config_json_str = json.dumps(config)
|
|
|
|
config_bat_str = f'@set update_branch={req.update_branch}'
|
|
|
|
config_sh_str = f'export update_branch={req.update_branch}'
|
|
|
|
|
|
|
|
config_json_path = os.path.join(CONFIG_DIR, 'config.json')
|
|
|
|
config_bat_path = os.path.join(CONFIG_DIR, 'config.bat')
|
|
|
|
config_sh_path = os.path.join(CONFIG_DIR, 'config.sh')
|
|
|
|
|
2022-10-18 04:35:51 +02:00
|
|
|
with open(config_json_path, 'w', encoding='utf-8') as f:
|
2022-09-09 17:04:32 +02:00
|
|
|
f.write(config_json_str)
|
|
|
|
|
2022-10-18 04:35:51 +02:00
|
|
|
with open(config_bat_path, 'w', encoding='utf-8') as f:
|
2022-09-09 17:04:32 +02:00
|
|
|
f.write(config_bat_str)
|
|
|
|
|
2022-10-18 04:35:51 +02:00
|
|
|
with open(config_sh_path, 'w', encoding='utf-8') as f:
|
2022-09-09 17:04:32 +02:00
|
|
|
f.write(config_sh_str)
|
|
|
|
|
|
|
|
return {'OK'}
|
|
|
|
except Exception as e:
|
|
|
|
print(traceback.format_exc())
|
2022-10-18 08:30:30 +02:00
|
|
|
raise HTTPException(status_code=500, detail=str(e))
|
2022-09-09 17:04:32 +02:00
|
|
|
|
2022-10-14 09:42:43 +02:00
|
|
|
def getConfig(default_val={}):
|
2022-09-09 17:04:32 +02:00
|
|
|
try:
|
|
|
|
config_json_path = os.path.join(CONFIG_DIR, 'config.json')
|
2022-09-09 20:59:46 +02:00
|
|
|
if not os.path.exists(config_json_path):
|
2022-10-14 09:42:43 +02:00
|
|
|
return default_val
|
2022-10-18 04:35:51 +02:00
|
|
|
with open(config_json_path, 'r', encoding='utf-8') as f:
|
2022-10-06 10:58:02 +02:00
|
|
|
return json.load(f)
|
2022-09-09 17:04:32 +02:00
|
|
|
except Exception as e:
|
2022-10-14 09:42:43 +02:00
|
|
|
print(str(e))
|
2022-09-09 17:04:32 +02:00
|
|
|
print(traceback.format_exc())
|
2022-10-14 09:42:43 +02:00
|
|
|
return default_val
|
2022-10-06 10:58:02 +02:00
|
|
|
|
|
|
|
def setConfig(config):
|
|
|
|
try:
|
|
|
|
config_json_path = os.path.join(CONFIG_DIR, 'config.json')
|
2022-10-18 04:35:51 +02:00
|
|
|
with open(config_json_path, 'w', encoding='utf-8') as f:
|
2022-10-06 10:58:02 +02:00
|
|
|
return json.dump(config, f)
|
2022-10-23 21:01:11 +02:00
|
|
|
except Exception as e:
|
2022-10-14 09:42:43 +02:00
|
|
|
print(str(e))
|
2022-10-06 10:58:02 +02:00
|
|
|
print(traceback.format_exc())
|
|
|
|
|
|
|
|
def getModels():
|
|
|
|
models = {
|
|
|
|
'active': {
|
|
|
|
'stable-diffusion': 'sd-v1-4',
|
|
|
|
},
|
|
|
|
'options': {
|
|
|
|
'stable-diffusion': ['sd-v1-4'],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
# custom models
|
|
|
|
sd_models_dir = os.path.join(MODELS_DIR, 'stable-diffusion')
|
|
|
|
for file in os.listdir(sd_models_dir):
|
|
|
|
if file.endswith('.ckpt'):
|
|
|
|
model_name = os.path.splitext(file)[0]
|
|
|
|
models['options']['stable-diffusion'].append(model_name)
|
|
|
|
|
|
|
|
# legacy
|
|
|
|
custom_weight_path = os.path.join(SD_DIR, 'custom-model.ckpt')
|
|
|
|
if os.path.exists(custom_weight_path):
|
|
|
|
models['active']['stable-diffusion'] = 'custom-model'
|
|
|
|
models['options']['stable-diffusion'].append('custom-model')
|
|
|
|
|
|
|
|
config = getConfig()
|
|
|
|
if 'model' in config and 'stable-diffusion' in config['model']:
|
|
|
|
models['active']['stable-diffusion'] = config['model']['stable-diffusion']
|
|
|
|
|
|
|
|
return models
|
|
|
|
|
2022-10-19 18:04:40 +02:00
|
|
|
def getUIPlugins():
|
|
|
|
plugins = []
|
|
|
|
|
|
|
|
for file in os.listdir(UI_PLUGINS_DIR):
|
|
|
|
if file.endswith('.plugin.js'):
|
|
|
|
plugins.append(f'/plugins/{file}')
|
|
|
|
|
|
|
|
return plugins
|
|
|
|
|
2022-10-15 07:32:53 +02:00
|
|
|
@app.get('/get/{key:path}')
|
2022-10-14 09:42:43 +02:00
|
|
|
def read_web_data(key:str=None):
|
2022-10-15 07:32:53 +02:00
|
|
|
if not key: # /get without parameters, stable-diffusion easter egg.
|
2022-10-18 08:30:30 +02:00
|
|
|
raise HTTPException(status_code=418, detail="StableDiffusion is drawing a teapot!") # HTTP418 I'm a teapot
|
2022-10-14 09:42:43 +02:00
|
|
|
elif key == 'app_config':
|
|
|
|
config = getConfig(default_val=None)
|
|
|
|
if config is None:
|
2022-10-18 08:30:30 +02:00
|
|
|
raise HTTPException(status_code=500, detail="Config file is missing or unreadable")
|
2022-10-15 07:32:53 +02:00
|
|
|
return JSONResponse(config, headers=NOCACHE_HEADERS)
|
2022-10-14 09:42:43 +02:00
|
|
|
elif key == 'models':
|
2022-10-15 07:32:53 +02:00
|
|
|
return JSONResponse(getModels(), headers=NOCACHE_HEADERS)
|
2022-10-14 09:42:43 +02:00
|
|
|
elif key == 'modifiers': return FileResponse(os.path.join(SD_UI_DIR, 'modifiers.json'), headers=NOCACHE_HEADERS)
|
2022-10-15 10:47:12 +02:00
|
|
|
elif key == 'output_dir': return JSONResponse({ 'output_dir': outpath }, headers=NOCACHE_HEADERS)
|
2022-10-19 18:04:40 +02:00
|
|
|
elif key == 'ui_plugins': return JSONResponse(getUIPlugins(), headers=NOCACHE_HEADERS)
|
2022-10-14 09:42:43 +02:00
|
|
|
else:
|
2022-10-18 08:30:30 +02:00
|
|
|
raise HTTPException(status_code=404, detail=f'Request for unknown {key}') # HTTP404 Not Found
|
2022-09-02 10:28:36 +02:00
|
|
|
|
2022-09-24 12:25:45 +02:00
|
|
|
# don't log certain requests
|
|
|
|
class LogSuppressFilter(logging.Filter):
|
2022-09-09 17:35:24 +02:00
|
|
|
def filter(self, record: logging.LogRecord) -> bool:
|
2022-09-24 12:25:45 +02:00
|
|
|
path = record.getMessage()
|
|
|
|
for prefix in ACCESS_LOG_SUPPRESS_PATH_PREFIXES:
|
|
|
|
if path.find(prefix) != -1:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
logging.getLogger('uvicorn.access').addFilter(LogSuppressFilter())
|
2022-09-09 17:35:24 +02:00
|
|
|
|
2022-10-15 09:28:20 +02:00
|
|
|
task_manager.default_model_to_load = get_initial_model_to_load()
|
|
|
|
task_manager.start_render_thread()
|
|
|
|
|
2022-09-02 10:28:36 +02:00
|
|
|
# start the browser ui
|
2022-09-22 17:01:30 +02:00
|
|
|
import webbrowser; webbrowser.open('http://localhost:9000')
|