Skip to content

Commit

Permalink
(WIP): Studio2 app infra and SD API
Browse files Browse the repository at this point in the history
UI/app structure and utility implementation.

- Initializers for webui/API launch
- Schedulers file for SD scheduling utilities
- Additions to API-level utilities
- Added embeddings module for LoRA, Lycoris, yada yada
- Added image_processing module for resamplers, resize tools,
  transforms, and any image annotation (PNG metadata)
- shared_cmd_opts module -- sorry, this is stable_args.py. It lives on.
  We still want to have some global control over the app exclusively
  from the command-line. At least we will be free from shark_args.
- Moving around some utility pieces.
- Try to make api+webui concurrency possible in index.py
- SD UI -- this is just img2imgUI but hopefully a little better.
- UI utilities for your nod logos and your gradio temps.

Enable UI / bugfixes / tweaks
  • Loading branch information
monorimet committed Jan 17, 2024
1 parent fa95ed3 commit dbacc36
Show file tree
Hide file tree
Showing 33 changed files with 5,003 additions and 331 deletions.
134 changes: 134 additions & 0 deletions apps/shark_studio/api/controlnet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
# from turbine_models.custom_models.controlnet import control_adapter, preprocessors


class control_adapter:
def __init__(
self,
model: str,
):
self.model = None

def export_control_adapter_model(model_keyword):
return None

def export_xl_control_adapter_model(model_keyword):
return None


class preprocessors:
def __init__(
self,
model: str,
):
self.model = None

def export_controlnet_model(model_keyword):
return None


control_adapter_map = {
"sd15": {
"canny": {"initializer": control_adapter.export_control_adapter_model},
"openpose": {
"initializer": control_adapter.export_control_adapter_model
},
"scribble": {
"initializer": control_adapter.export_control_adapter_model
},
"zoedepth": {
"initializer": control_adapter.export_control_adapter_model
},
},
"sdxl": {
"canny": {
"initializer": control_adapter.export_xl_control_adapter_model
},
},
}
preprocessor_model_map = {
"canny": {"initializer": preprocessors.export_controlnet_model},
"openpose": {"initializer": preprocessors.export_controlnet_model},
"scribble": {"initializer": preprocessors.export_controlnet_model},
"zoedepth": {"initializer": preprocessors.export_controlnet_model},
}


class PreprocessorModel:
def __init__(
self,
hf_model_id,
device,
):
self.model = None

def compile(self, device):
print("compile not implemented for preprocessor.")
return

def run(self, inputs):
print("run not implemented for preprocessor.")
return


def cnet_preview(model, input_img, stencils, images, preprocessed_hints):
if isinstance(input_image, PIL.Image.Image):
img_dict = {
"background": None,
"layers": [None],
"composite": input_image,
}
input_image = EditorValue(img_dict)
images[index] = input_image
if model:
stencils[index] = model
match model:
case "canny":
canny = CannyDetector()
result = canny(
np.array(input_image["composite"]),
100,
200,
)
preprocessed_hints[index] = Image.fromarray(result)
return (
Image.fromarray(result),
stencils,
images,
preprocessed_hints,
)
case "openpose":
openpose = OpenposeDetector()
result = openpose(np.array(input_image["composite"]))
preprocessed_hints[index] = Image.fromarray(result[0])
return (
Image.fromarray(result[0]),
stencils,
images,
preprocessed_hints,
)
case "zoedepth":
zoedepth = ZoeDetector()
result = zoedepth(np.array(input_image["composite"]))
preprocessed_hints[index] = Image.fromarray(result)
return (
Image.fromarray(result),
stencils,
images,
preprocessed_hints,
)
case "scribble":
preprocessed_hints[index] = input_image["composite"]
return (
input_image["composite"],
stencils,
images,
preprocessed_hints,
)
case _:
preprocessed_hints[index] = None
return (
None,
stencils,
images,
preprocessed_hints,
)
87 changes: 87 additions & 0 deletions apps/shark_studio/api/initializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import importlib
import logging
import os
import signal
import sys
import re
import warnings
import json
from threading import Thread

from apps.shark_studio.modules.timer import startup_timer


def imports():
import torch # noqa: F401

startup_timer.record("import torch")
warnings.filterwarnings(
action="ignore", category=DeprecationWarning, module="torch"
)
warnings.filterwarnings(
action="ignore", category=UserWarning, module="torchvision"
)

import gradio # noqa: F401

startup_timer.record("import gradio")

import apps.shark_studio.web.utils.globals as global_obj

global_obj._init()
startup_timer.record("initialize globals")

from apps.shark_studio.modules import (
img_processing,
) # noqa: F401
from apps.shark_studio.modules.schedulers import scheduler_model_map

startup_timer.record("other imports")


def initialize():
configure_sigint_handler()

# from apps.shark_studio.modules import modelloader
# modelloader.cleanup_models()

# from apps.shark_studio.modules import sd_models
# sd_models.setup_model()
# startup_timer.record("setup SD model")

# initialize_rest(reload_script_modules=False)


def initialize_rest(*, reload_script_modules=False):
"""
Called both from initialize() and when reloading the webui.
"""
# Keep this for adding reload options to the webUI.


def dumpstacks():
import threading
import traceback

id2name = {th.ident: th.name for th in threading.enumerate()}
code = []
for threadId, stack in sys._current_frames().items():
code.append(f"\n# Thread: {id2name.get(threadId, '')}({threadId})")
for filename, lineno, name, line in traceback.extract_stack(stack):
code.append(f"""File: "{filename}", line {lineno}, in {name}""")
if line:
code.append(" " + line.strip())

print("\n".join(code))


def configure_sigint_handler():
# make the program just exit at ctrl+c without waiting for anything
def sigint_handler(sig, frame):
print(f"Interrupted with signal {sig} in {frame}")

dumpstacks()

os._exit(0)

signal.signal(signal.SIGINT, sigint_handler)
Loading

0 comments on commit dbacc36

Please sign in to comment.