Skip to content

Commit

Permalink
Merge pull request #135 from bsormagec/main
Browse files Browse the repository at this point in the history
Added image_prompt feature to text2img
  • Loading branch information
mrhan1993 committed Jan 3, 2024
2 parents 993b920 + b1bd311 commit 9cdbbeb
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
31 changes: 31 additions & 0 deletions fooocusapi/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,37 @@ def text2img_generation(req: Text2ImgRequest, accept: str = Header(None),
results = call_worker(req, accept)
return generation_output(results, streaming_output, req.require_base64)

@app.post("/v2/generation/text-to-image-with-ip", response_model=List[GeneratedImageResult] | AsyncJobResponse, responses=img_generate_responses)
def text_to_img_with_ip(req: Text2ImgRequestWithPrompt,
accept: str = Header(None),
accept_query: str | None = Query(None, alias='accept', description="Parameter to overvide 'Accept' header, 'image/png' for output bytes")):
if accept_query is not None and len(accept_query) > 0:
accept = accept_query

if accept == 'image/png':
streaming_output = True
# image_number auto set to 1 in streaming mode
req.image_number = 1
else:
streaming_output = False

default_image_promt = ImagePrompt(cn_img=None)
image_prompts_files: List[ImagePrompt] = []
for img_prompt in req.image_prompts:
img_prompt.cn_img = base64_to_stream(img_prompt.cn_img)
image = ImagePrompt(cn_img=img_prompt.cn_img,
cn_stop=img_prompt.cn_stop,
cn_weight=img_prompt.cn_weight,
cn_type=img_prompt.cn_type)
image_prompts_files.append(image)

while len(image_prompts_files) <= 4:
image_prompts_files.append(default_image_promt)

req.image_prompts = image_prompts_files

results = call_worker(req, accept)
return generation_output(results, streaming_output, req.require_base64)

@app.post("/v1/generation/image-upscale-vary", response_model=List[GeneratedImageResult] | AsyncJobResponse, responses=img_generate_responses)
def img_upscale_or_vary(input_image: UploadFile, req: ImgUpscaleOrVaryRequest = Depends(ImgUpscaleOrVaryRequest.as_form),
Expand Down
10 changes: 6 additions & 4 deletions fooocusapi/api_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ def req_to_params(req: Text2ImgRequest) -> ImageGenerationParams:
refiner_model_name = req.refiner_model_name
refiner_switch = req.refiner_switch
loras = [(lora.model_name, lora.weight) for lora in req.loras]
uov_input_image = None if not (isinstance(
req, ImgUpscaleOrVaryRequest) or isinstance(req, ImgUpscaleOrVaryRequestJson)) else read_input_image(req.input_image)
uov_input_image = None
if not isinstance(req, Text2ImgRequestWithPrompt):
if isinstance(req, ImgUpscaleOrVaryRequest) or isinstance(req, ImgUpscaleOrVaryRequestJson):
uov_input_image = read_input_image(req.input_image)
uov_method = flags.disabled if not (isinstance(
req, ImgUpscaleOrVaryRequest) or isinstance(req, ImgUpscaleOrVaryRequestJson)) else req.uov_method.value
upscale_value = None if not (isinstance(
Expand Down Expand Up @@ -78,9 +80,9 @@ def req_to_params(req: Text2ImgRequest) -> ImageGenerationParams:
}

image_prompts = []
if isinstance(req, ImgPromptRequest) or isinstance(req, ImgPromptRequestJson):
if isinstance(req, ImgPromptRequest) or isinstance(req, ImgPromptRequestJson) or isinstance(req, Text2ImgRequestWithPrompt):
# 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:
if len(req.image_prompts) > 0 and not isinstance(req, Text2ImgRequestWithPrompt) 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:
Expand Down
3 changes: 3 additions & 0 deletions fooocusapi/models_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ class ImagePromptJson(BaseModel):
class ImgPromptRequestJson(ImgInpaintOrOutpaintRequestJson):
input_image: str | None = Field(None, description="Init image for inpaint or outpaint as base64")
image_prompts: List[ImagePromptJson | ImagePrompt]

class Text2ImgRequestWithPrompt(Text2ImgRequest):
image_prompts: List[ImagePromptJson] = []

0 comments on commit 9cdbbeb

Please sign in to comment.