Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
konieshadow committed Dec 29, 2023
2 parents 2ea7b3e + 8d3a283 commit 9df8fac
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 217 deletions.
2 changes: 1 addition & 1 deletion docs/openapi.json

Large diffs are not rendered by default.

202 changes: 18 additions & 184 deletions examples/examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,194 +24,18 @@ class Config():
cfg = Config()

upscale_params = {
"prompt": "",
"negative_prompt": "",
"style_selections": [
"Fooocus V2",
"Fooocus Enhance",
"Fooocus Sharp"
],
"performance_selection": "Speed",
"aspect_ratios_selection": "1152*896",
"image_number": 1,
"image_seed": -1,
"sharpness": 2,
"guidance_scale": 4,
"base_model_name": "juggernautXL_version6Rundiffusion.safetensors",
"refiner_model_name": "None",
"refiner_switch": 0.5,
"loras": [
{
"model_name": "sd_xl_offset_example-lora_1.0.safetensors",
"weight": 0.1
}
],
"advanced_params": {
"disable_preview": False,
"adm_scaler_positive": 1.5,
"adm_scaler_negative": 0.8,
"adm_scaler_end": 0.3,
"refiner_swap_method": "joint",
"adaptive_cfg": 7,
"sampler_name": "dpmpp_2m_sde_gpu",
"scheduler_name": "karras",
"overwrite_step": -1,
"overwrite_switch": -1,
"overwrite_width": -1,
"overwrite_height": -1,
"overwrite_vary_strength": -1,
"overwrite_upscale_strength": -1,
"mixing_image_prompt_and_vary_upscale": False,
"mixing_image_prompt_and_inpaint": False,
"debugging_cn_preprocessor": False,
"skipping_cn_preprocessor": False,
"controlnet_softness": 0.25,
"canny_low_threshold": 64,
"canny_high_threshold": 128,
"freeu_enabled": False,
"freeu_b1": 1.01,
"freeu_b2": 1.02,
"freeu_s1": 0.99,
"freeu_s2": 0.95,
"debugging_inpaint_preprocessor": False,
"inpaint_disable_initial_latent": False,
"inpaint_engine": inpaint_engine,
"inpaint_strength": 1,
"inpaint_respective_field": 1
},
"require_base64": False,
"async_process": False,
"uov_method": "Upscale (Custom)",
"upscale_value": 3,
"input_image": ""
}

inpaint_params = {
"prompt": "",
"negative_prompt": "",
"style_selections": [
"Fooocus V2",
"Fooocus Enhance",
"Fooocus Sharp"
],
"performance_selection": "Speed",
"aspect_ratios_selection": "1152*896",
"image_number": 1,
"image_seed": -1,
"sharpness": 2,
"guidance_scale": 4,
"base_model_name": "juggernautXL_version6Rundiffusion.safetensors",
"refiner_model_name": "None",
"refiner_switch": 0.5,
"loras": [
{
"model_name": "sd_xl_offset_example-lora_1.0.safetensors",
"weight": 0.1
}
],
"advanced_params": {
"disable_preview": False,
"adm_scaler_positive": 1.5,
"adm_scaler_negative": 0.8,
"adm_scaler_end": 0.3,
"refiner_swap_method": "joint",
"adaptive_cfg": 7,
"sampler_name": "dpmpp_2m_sde_gpu",
"scheduler_name": "karras",
"overwrite_step": -1,
"overwrite_switch": -1,
"overwrite_width": -1,
"overwrite_height": -1,
"overwrite_vary_strength": -1,
"overwrite_upscale_strength": -1,
"mixing_image_prompt_and_vary_upscale": False,
"mixing_image_prompt_and_inpaint": False,
"debugging_cn_preprocessor": False,
"skipping_cn_preprocessor": False,
"controlnet_softness": 0.25,
"canny_low_threshold": 64,
"canny_high_threshold": 128,
"freeu_enabled": False,
"freeu_b1": 1.01,
"freeu_b2": 1.02,
"freeu_s1": 0.99,
"freeu_s2": 0.95,
"debugging_inpaint_preprocessor": False,
"inpaint_disable_initial_latent": False,
"inpaint_engine": inpaint_engine,
"inpaint_strength": 1,
"inpaint_respective_field": 1
},
"require_base64": False,
"async_process": False,
"input_image": "",
"input_mask": None,
"inpaint_additional_prompt": None,
"outpaint_selections": [],
"outpaint_distance_left": 0,
"outpaint_distance_right": 0,
"outpaint_distance_top": 0,
"outpaint_distance_bottom": 0,
}

img_prompt_params = {
"prompt": "",
"negative_prompt": "",
"style_selections": [
"Fooocus V2",
"Fooocus Enhance",
"Fooocus Sharp"
],
"performance_selection": "Speed",
"aspect_ratios_selection": "1152*896",
"image_number": 1,
"image_seed": -1,
"sharpness": 2,
"guidance_scale": 4,
"base_model_name": "juggernautXL_version6Rundiffusion.safetensors",
"refiner_model_name": "None",
"refiner_switch": 0.5,
"loras": [
{
"model_name": "sd_xl_offset_example-lora_1.0.safetensors",
"weight": 0.1
}
],
"advanced_params": {
"disable_preview": False,
"adm_scaler_positive": 1.5,
"adm_scaler_negative": 0.8,
"adm_scaler_end": 0.3,
"refiner_swap_method": "joint",
"adaptive_cfg": 7,
"sampler_name": "dpmpp_2m_sde_gpu",
"scheduler_name": "karras",
"overwrite_step": -1,
"overwrite_switch": -1,
"overwrite_width": -1,
"overwrite_height": -1,
"overwrite_vary_strength": -1,
"overwrite_upscale_strength": -1,
"mixing_image_prompt_and_vary_upscale": False,
"mixing_image_prompt_and_inpaint": False,
"debugging_cn_preprocessor": False,
"skipping_cn_preprocessor": False,
"controlnet_softness": 0.25,
"canny_low_threshold": 64,
"canny_high_threshold": 128,
"freeu_enabled": False,
"freeu_b1": 1.01,
"freeu_b2": 1.02,
"freeu_s1": 0.99,
"freeu_s2": 0.95,
"debugging_inpaint_preprocessor": False,
"inpaint_disable_initial_latent": False,
"inpaint_engine": inpaint_engine,
"inpaint_strength": 1,
"inpaint_respective_field": 1
},
"require_base64": False,
"async_process": False,
"image_prompts": []
}

Expand All @@ -221,7 +45,7 @@ class Config():

imgs_base_path = os.path.join(os.path.dirname(__file__), 'imgs')

with open(os.path.join(imgs_base_path, "1485005453352708.jpeg"), "rb") as f:
with open(os.path.join(imgs_base_path, "bear.jpg"), "rb") as f:
img1 = f.read()
image_base64 = base64.b64encode(img1).decode('utf-8')
f.close()
Expand Down Expand Up @@ -268,7 +92,6 @@ def image_prompt(img_prompt: list, params: dict) -> dict:
"""
Image Prompt
"""
params["prompt"] = "cat"
params["image_prompts"] = img_prompt
data = json.dumps(params)
response = requests.post(url=f"{cfg.fooocus_host}{cfg.img_prompt}",
Expand All @@ -277,20 +100,31 @@ def image_prompt(img_prompt: list, params: dict) -> dict:
timeout=300)
return response.json()

def image_prompt_with_inpaint(img_prompt: list, input_image: str, input_mask: str, params: dict) -> dict:
"""
Image Prompt
"""
params["image_prompts"] = img_prompt
params["input_image"] = input_image
params["input_mask"] = input_mask
params["outpaint_selections"] = ["Left", "Right"]
data = json.dumps(params)
response = requests.post(url=f"{cfg.fooocus_host}{cfg.img_prompt}",
data=data,
headers=headers,
timeout=300)
return response.json()


img_prompt = [
{
"cn_img": image_base64,
"cn_stop": 0.6,
"cn_weight": 0.6,
"cn_type": "ImagePrompt"
},{
"cn_img": s_base64,
"cn_stop": 0.6,
"cn_weight": 0.6,
"cn_type": "ImagePrompt"
}
]
print(upscale_vary(image=image_base64))
# print(upscale_vary(image=image_base64))
# print(inpaint_outpaint(input_image=s_base64, input_mask=m_base64))
# print(image_prompt(img_prompt=img_prompt, params=img_prompt_params))
print(image_prompt_with_inpaint(img_prompt=img_prompt, input_image=s_base64, input_mask=m_base64, params=img_prompt_params))
7 changes: 4 additions & 3 deletions examples/examples_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import requests
import os
import base64
from examples.models import ControlNetEnum, ImagePromptParams, ImagePromptParamsJson, ImgInpaintOrOutpaintParams, ImgInpaintOrOutpaintParamsJson, ImgUpscaleOrVaryParams, ImgUpscaleOrVaryParamsJson, Text2ImgParams, UpscaleOrVaryMethod

from models import *

Expand All @@ -24,9 +25,9 @@ def txt2img(params: Text2ImgParams) -> dict:
"""
text to image
"""
date = json.dumps(params.model_dump())
data = json.dumps(params.model_dump())
response = requests.post(url=f"{cfg.fooocus_host}{cfg.text2img}",
data=date,
data=data,
timeout=30)
return response.json()

Expand Down Expand Up @@ -111,7 +112,7 @@ def image_prompt_v2(params: ImagePromptParamsJson = ImagePromptParamsJson()) ->
if __name__ == '__main__':
imgs_base_path = os.path.join(os.path.dirname(__file__), 'imgs')

input_image = open(os.path.join(imgs_base_path,'1485005453352708.jpeg'), 'rb').read()
input_image = open(os.path.join(imgs_base_path,'bear.jpg'), 'rb').read()
input_source = open(os.path.join(imgs_base_path,'s.jpg'), 'rb').read()
input_mask = open(os.path.join(imgs_base_path,'m.png'), 'rb').read()

Expand Down
Binary file removed examples/imgs/1485005453352708.jpeg
Binary file not shown.
Binary file added examples/imgs/bear.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 7 additions & 2 deletions fooocusapi/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ def call_worker(req: Text2ImgRequest, accept: str):
task_type = TaskType.text_2_img
if isinstance(req, ImgUpscaleOrVaryRequest) or isinstance(req, ImgUpscaleOrVaryRequestJson):
task_type = TaskType.img_uov
elif isinstance(req, ImgInpaintOrOutpaintRequest) or isinstance(req, ImgInpaintOrOutpaintRequestJson):
task_type = TaskType.img_inpaint_outpaint
elif isinstance(req, ImgPromptRequest) or isinstance(req, ImgPromptRequestJson):
task_type = TaskType.img_prompt
elif isinstance(req, ImgInpaintOrOutpaintRequest) or isinstance(req, ImgInpaintOrOutpaintRequestJson):
task_type = TaskType.img_inpaint_outpaint

params = req_to_params(req)
queue_task = task_queue.add_task(
Expand Down Expand Up @@ -217,6 +217,11 @@ def img_prompt(req: ImgPromptRequestJson,
else:
streaming_output = False

if req.input_image is not None:
req.input_image = base64_to_stream(req.input_image)
if req.input_mask is not None:
req.input_mask = base64_to_stream(req.input_mask)

default_image_promt = ImagePrompt(cn_img=None)
image_prompts_files: List[ImagePrompt] = []
for img_prompt in req.image_prompts:
Expand Down
8 changes: 6 additions & 2 deletions fooocusapi/api_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,16 @@ def req_to_params(req: Text2ImgRequest) -> ImageGenerationParams:

image_prompts = []
if isinstance(req, ImgPromptRequest) or isinstance(req, ImgPromptRequestJson):
# Auto set mixing_image_prompt_and_inpaint to True
if len(req.image_prompts) > 0 and req.input_image is not None and req.advanced_params is not None:
req.advanced_params.mixing_image_prompt_and_inpaint = True

for img_prompt in req.image_prompts:
if img_prompt.cn_img is not None:
cn_img = read_input_image(img_prompt.cn_img)
if img_prompt.cn_stop is None:
if img_prompt.cn_stop is None or img_prompt.cn_stop == 0:
img_prompt.cn_stop = flags.default_parameters[img_prompt.cn_type.value][0]
if img_prompt.cn_weight is None:
if img_prompt.cn_weight is None or img_prompt.cn_weight == 0:
img_prompt.cn_weight = flags.default_parameters[img_prompt.cn_type.value][1]
image_prompts.append(
(cn_img, img_prompt.cn_stop, img_prompt.cn_weight, img_prompt.cn_type.value))
Expand Down
4 changes: 3 additions & 1 deletion fooocusapi/img_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ def read_input_image(input_image: UploadFile) -> np.ndarray:
image = np.array(pil_image)
return image

def base64_to_stream(image: str) -> UploadFile:
def base64_to_stream(image: str) -> UploadFile | None:
if image == '':
return None
if image.startswith('data:image'):
image = image.split(sep=',', maxsplit=1)[1]
image_bytes = base64.b64decode(image)
Expand Down
Loading

0 comments on commit 9df8fac

Please sign in to comment.