Skip to content

Commit

Permalink
Merge pull request #292 from mrhan1993/support_imagemeta
Browse files Browse the repository at this point in the history
Support image meta
  • Loading branch information
mrhan1993 authored Apr 15, 2024
2 parents e7e3951 + 72df59a commit 9be3cdb
Show file tree
Hide file tree
Showing 13 changed files with 4,222 additions and 58 deletions.
57 changes: 19 additions & 38 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#ide config
.idea
.vscode

#runtime
__pycache__
.DS_Store

# models
*.ckpt
*.safetensors
Expand All @@ -22,46 +28,21 @@ conda
logs
log

sorted_styles.json
/language/default.json
lena.png
lena_result.png
lena_test.py
# config files
.cog
config.txt
config_modification_tutorial.txt
user_path_config.txt
user_path_config-deprecated.txt
build_chb.py
experiment.py
/modules/*.png
/tmp
/ui-config.json
/outputs
/config.json
/webui.settings.bat
/embeddings
/styles.csv
/params.txt
/styles.csv.bak
/webui-user.bat
/webui-user.sh
/interrogate
/user.css
/.idea
/notification.ogg
/notification.mp3
/SwinIR
/textual_inversion
.vscode
/extensions
/test/stdout.txt
/test/stderr.txt
/cache.json*
/config_states/
/node_modules
/package-lock.json
/.coverage*
/auth.json
.cog/

sorted_styles.json
/presets
*.db

# db
*.db

# cache
outputs

#other
*.http
2 changes: 1 addition & 1 deletion cog.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ build:
- "colorlog==6.8.2"
- "rich==13.7.1"

# commands run after the environment is setup
# commands run after the environment is set up
# run:
# - "echo env is ready!"
# - "echo another command if needed"
Expand Down
4,019 changes: 4,018 additions & 1 deletion docs/openapi.json

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions examples/examples_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"""
import json
import os
import base64
import requests


Expand All @@ -12,7 +11,6 @@ class Config:
Config
Attributes:
fooocus_host (str): Fooocus API host
text2img_ip (str): Text to Image with IP
img_upscale (str): Upscale or Vary
inpaint_outpaint (str): Inpaint or Outpaint
img_prompt (str): Image Prompt
Expand Down
2 changes: 1 addition & 1 deletion fooocus_api_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = '0.4.0.3'
version = '0.4.0.4'
25 changes: 21 additions & 4 deletions fooocusapi/models/common/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ class Lora(BaseModel):
for lora in default_loras:
if lora[0] != 'None':
default_loras_model.append(
Lora(enabled=lora[0], model_name=lora[1], weight=lora[2])
Lora(
enabled=lora[0],
model_name=lora[1],
weight=lora[2])
)
default_loras_json = LoraList.dump_json(default_loras_model)

Expand Down Expand Up @@ -84,6 +87,16 @@ class DescribeImageType(str, Enum):
anime = 'Anime'


class ImageMetaScheme(str, Enum):
"""Scheme for save image meta
Attributes:
Fooocus: json format
A111: string
"""
Fooocus = 'fooocus'
A111 = 'a111'


def style_selection_parser(style_selections: str) -> List[str]:
"""
Parse style selections, Convert to list
Expand Down Expand Up @@ -143,7 +156,8 @@ def outpaint_selections_parser(outpaint_selections: str) -> List[OutpaintExpansi
outpaint_selections_arr.append(expansion)
except ValueError:
errs = InitErrorDetails(
type='enum', loc=['outpaint_selections'],
type='enum',
loc=tuple('outpaint_selections'),
input=outpaint_selections,
ctx={
'expected': "str, comma separated Left, Right, Top, Bottom"
Expand All @@ -166,6 +180,9 @@ def image_prompt_parser(image_prompts_config: List[Tuple]) -> List[ImagePrompt]:
return []
for config in image_prompts_config:
cn_img, cn_stop, cn_weight, cn_type = config
image_prompts.append(ImagePrompt(cn_img=cn_img, cn_stop=cn_stop,
cn_weight=cn_weight, cn_type=cn_type))
image_prompts.append(ImagePrompt(
cn_img=cn_img,
cn_stop=cn_stop,
cn_weight=cn_weight,
cn_type=cn_type))
return image_prompts
116 changes: 116 additions & 0 deletions fooocusapi/models/common/image_meta.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
"""
Image meta schema
"""
from typing import List

from fooocus_version import version
from pydantic import BaseModel


class ImageMeta(BaseModel):
"""
Image meta data model
"""

metadata_scheme: str = "fooocus"

base_model: str
base_model_hash: str

prompt: str
full_prompt: List[str]
prompt_expansion: str

negative_prompt: str
full_negative_prompt: List[str]

performance: str

style: str

refiner_model: str = "None"
refiner_switch: float = 0.5

loras: List[list]

resolution: str

sampler: str = "dpmpp_2m_sde_gpu"
scheduler: str = "karras"
seed: str
adm_guidance: str
guidance_scale: int
sharpness: int
steps: int

version: str = version

def __repr__(self):
return ""


def loras_parser(loras: list) -> list:
"""
Parse lora list
"""
return [
[
lora[0].rsplit('.', maxsplit=1)[:1][0],
lora[1],
"hash_not_calculated",
] for lora in loras if lora[0] != 'None' and lora[0] is not None]


def image_parse(
async_tak: object,
task: dict
) -> dict | str:
"""
Parse image meta data
Generate meta data for image from task and async task object
Args:
async_tak: async task obj
task: task obj
Returns:
dict: image meta data
"""
req_param = async_tak.req_param
meta = ImageMeta(
metadata_scheme=req_param.meta_scheme,
base_model=req_param.base_model_name.rsplit('.', maxsplit=1)[:1][0],
base_model_hash='',
prompt=req_param.prompt,
full_prompt=task['positive'],
prompt_expansion=task['expansion'],
negative_prompt=req_param.negative_prompt,
full_negative_prompt=task['negative'],
performance=req_param.performance_selection,
style=str(req_param.style_selections),
refiner_model=req_param.refiner_model_name,
refiner_switch=req_param.refiner_switch,
loras=loras_parser(req_param.loras),
resolution=str(tuple([int(n) for n in req_param.aspect_ratios_selection.split('*')])),
sampler=req_param.advanced_params.sampler_name,
scheduler=req_param.advanced_params.scheduler_name,
seed=str(task['task_seed']),
adm_guidance=str((
req_param.advanced_params.adm_scaler_positive,
req_param.advanced_params.adm_scaler_negative,
req_param.advanced_params.adm_scaler_end)),
guidance_scale=req_param.guidance_scale,
sharpness=req_param.sharpness,
steps=-1,
version=version
)
if meta.metadata_scheme not in ["fooocus", "a111"]:
meta.metadata_scheme = "fooocus"
if meta.metadata_scheme == "fooocus":
meta_dict = meta.model_dump()
for i, lora in enumerate(meta.loras):
attr_name = f"lora_combined_{i+1}"
lr = [str(x) for x in lora]
meta_dict[attr_name] = f"{lr[0]} : {lr[1]}"
else:
meta_dict = meta.model_dump()
return meta_dict
4 changes: 4 additions & 0 deletions fooocusapi/models/common/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

default_aspect_ratio = default_aspect_ratio.split(" ")[0].replace("×", "*")


class QueryJobRequest(BaseModel):
"""Query job request"""
job_id: str = Field(description="Job ID to query")
Expand Down Expand Up @@ -90,7 +91,10 @@ class CommonRequest(BaseModel):
refiner_switch: float = Field(default=default_refiner_switch, description="Refiner Switch At", ge=0.1, le=1.0)
loras: List[Lora] = Field(default=default_loras_model)
advanced_params: AdvancedParams = AdvancedParams()
save_meta: bool = Field(default=True, description="Save meta data")
meta_scheme: str = Field(defaut='fooocus', description="Meta data scheme, one of [fooocus, a111]")
save_extension: str = Field(default='png', description="Save extension, one of [png, jpg, webp]")
save_name: str = Field(default='', description="Image name for output image, default is a uuid")
read_wildcards_in_order: bool = Field(default=False, description="Read wildcards in order")
require_base64: bool = Field(default=False, description="Return base64 data of generated image")
async_process: bool = Field(default=False, description="Set to true will run async and return job info for retrieve generation result later")
Expand Down
6 changes: 6 additions & 0 deletions fooocusapi/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ def __init__(
image_prompts: List[Tuple[np.ndarray, float, float, str]],
advanced_params: List[any] | None,
save_extension: str,
save_meta: bool,
meta_scheme: str,
save_name: str,
require_base64: bool,
):
self.prompt = prompt
Expand Down Expand Up @@ -192,6 +195,9 @@ def __init__(
self.inpaint_additional_prompt = inpaint_additional_prompt
self.image_prompts = image_prompts
self.save_extension = save_extension
self.save_meta = save_meta
self.meta_scheme = meta_scheme
self.save_name = save_name
self.require_base64 = require_base64
self.advanced_params = advanced_params

Expand Down
3 changes: 3 additions & 0 deletions fooocusapi/utils/api_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ def req_to_params(req: Text2ImgRequest) -> ImageGenerationParams:
inpaint_additional_prompt=inpaint_additional_prompt,
image_prompts=image_prompts,
advanced_params=advanced_params,
save_meta=req.save_meta,
meta_scheme=req.meta_scheme,
save_name=req.save_name,
save_extension=req.save_extension,
require_base64=req.require_base64,
)
Expand Down
12 changes: 7 additions & 5 deletions fooocusapi/utils/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@
STATIC_SERVER_BASE = 'http://127.0.0.1:8888/files/'


def save_output_file(img: np.ndarray, image_meta: dict = None,
image_name: str = '', extension: str = 'png') -> str:
def save_output_file(
img: np.ndarray,
image_meta: dict = None,
image_name: str = '',
extension: str = 'png') -> str:
"""
Save np image to file
Args:
Expand All @@ -44,8 +47,6 @@ def save_output_file(img: np.ndarray, image_meta: dict = None,
current_time = datetime.datetime.now()
date_string = current_time.strftime("%Y-%m-%d")

image_name = str(uuid.uuid4()) if image_name == '' else image_name

filename = os.path.join(date_string, image_name + '.' + extension)
file_path = os.path.join(output_dir, filename)

Expand All @@ -58,7 +59,8 @@ def save_output_file(img: np.ndarray, image_meta: dict = None,
meta = None
if extension == 'png':
meta = PngInfo()
meta.add_text("params", json.dumps(image_meta))
meta.add_text("parameters", json.dumps(image_meta))
meta.add_text("fooocus_scheme", image_meta['metadata_scheme'])

os.makedirs(os.path.dirname(file_path), exist_ok=True)
Image.fromarray(img).save(file_path, format=extension,
Expand Down
Loading

0 comments on commit 9be3cdb

Please sign in to comment.