lllyasviel / Fooocus

Focus on prompting and generating
GNU General Public License v3.0
41.03k stars 5.76k forks source link

How to Bulk Image Generation #246

Closed pendekarcode closed 9 months ago

pendekarcode commented 1 year ago

How might one create a bulk image generator without utilizing Gradio UI? Ideally, I'm picturing a setup like this:

prompt: Realistic cosmic red owl looking up illustration, vibrant color, black background neg_prompt: blurry, out of focus, monochrome size: 1024x1024 seed: 765059328 model: Choose a model timg: 15 # total images

To run function like this: bulk_gen_img( prompt, neg_prompt, size, seed, model, timg )

Your expertise in this field is evident, and I believe your insights could illuminate the path to creating an efficient and powerful bulk image generation process.

If you could spare a moment to share your thoughts on this approach, I would be incredibly grateful. Your input could help not only me but also others in the community looking to explore this fascinating territory.

Thank you once again for your dedication to fostering knowledge-sharing and innovation. Looking forward to your response.

qdu123 commented 1 year ago

You can use the gradio_client Python library or the @gradio/client Javascript package to query the demo via API.

from gradio_client import Client
# serialize=False  is needed
client = Client("http://127.0.0.1:7861/", serialize=False)

result = client.predict(
                "Howdy!",   # str in 'parameter_8' Textbox component
                "Howdy!",   # str in 'Negative Prompt' Textbox component
                "None", # str in 'parameter_23' Radio component
                "Speed",    # str in 'Performance' Radio component
                "704×1408", # str in 'Aspect Ratios (width × height)' Radio component
                1,  # int | float (numeric value between 1 and 32) in 'Image Number' Slider component
                5,  # int | float in 'Seed' Number component
                0,  # int | float (numeric value between 0.0 and 40.0) in 'Sampling Sharpness' Slider component
                "sd_xl_base_1.0_0.9vae.safetensors",    # str (Option from: ['sd_xl_base_1.0_0.9vae.safetensors', 'sd_xl_refiner_1.0_0.9vae.safetensors']) in 'SDXL Base Model' Dropdown component
                "None", # str (Option from: ['None', 'sd_xl_base_1.0_0.9vae.safetensors', 'sd_xl_refiner_1.0_0.9vae.safetensors']) in 'SDXL Refiner' Dropdown component
                "None", # str (Option from: ['None', 'sd_xl_offset_example-lora_1.0.safetensors']) in 'SDXL LoRA 1' Dropdown component
                -2, # int | float (numeric value between -2 and 2) in 'Weight' Slider component
                "None", # str (Option from: ['None', 'sd_xl_offset_example-lora_1.0.safetensors']) in 'SDXL LoRA 2' Dropdown component
                -2, # int | float (numeric value between -2 and 2) in 'Weight' Slider component
                "None", # str (Option from: ['None', 'sd_xl_offset_example-lora_1.0.safetensors']) in 'SDXL LoRA 3' Dropdown component
                -2, # int | float (numeric value between -2 and 2) in 'Weight' Slider component
                "None", # str (Option from: ['None', 'sd_xl_offset_example-lora_1.0.safetensors']) in 'SDXL LoRA 4' Dropdown component
                -2, # int | float (numeric value between -2 and 2) in 'Weight' Slider component
                "None", # str (Option from: ['None', 'sd_xl_offset_example-lora_1.0.safetensors']) in 'SDXL LoRA 5' Dropdown component
                -2, # int | float (numeric value between -2 and 2) in 'Weight' Slider component
                fn_index=4
)
print(result)
TimothyAlexisVass commented 1 year ago

Here's how you can do it properly with the above method:

First you need to know which fn_index the relevant predict function has:

import os
from gradio_client import Client

client = Client("https://f676c563029db39e44.gradio.live", serialize=False)

print(client._render_endpoints_info)

For today, we get this result:

 - predict(parameter_8, negative_prompt, image_style, performance, aspect_ratios, image_number, seed, sampling_sharpness, sdxl_base_model, sdxl_refiner, sdxl_lora_1, weight, sdxl_lora_2, weight, sdxl_lora_3, weight, sdxl_lora_4, weight, sdxl_lora_5, weight, input_image, method, drag_above_image_to_here, fn_index=7) -> (value_4, preview, gallery)
    Parameters:
     - [Textbox] parameter_8: str 
     - [Textbox] negative_prompt: str 
     - [Checkboxgroup] image_style: List[str] 
     - [Radio] performance: str 
     - [Radio] aspect_ratios: str 
     - [Slider] image_number: int | float (numeric value between 1 and 32) 
     - [Number] seed: int | float 
     - [Slider] sampling_sharpness: int | float (numeric value between 0.0 and 30.0) 
     - [Dropdown] sdxl_base_model: str (Option from: ['sd_xl_base_1.0_0.9vae.safetensors', 'sd_xl_refiner_1.0_0.9vae.safetensors']) 
     - [Dropdown] sdxl_refiner: str (Option from: ['None', 'sd_xl_base_1.0_0.9vae.safetensors', 'sd_xl_refiner_1.0_0.9vae.safetensors']) 
     - [Dropdown] sdxl_lora_1: str (Option from: ['None', 'sd_xl_offset_example-lora_1.0.safetensors']) 
     - [Slider] weight: int | float (numeric value between -2 and 2) 
     - [Dropdown] sdxl_lora_2: str (Option from: ['None', 'sd_xl_offset_example-lora_1.0.safetensors']) 
     - [Slider] weight: int | float (numeric value between -2 and 2) 
     - [Dropdown] sdxl_lora_3: str (Option from: ['None', 'sd_xl_offset_example-lora_1.0.safetensors']) 
     - [Slider] weight: int | float (numeric value between -2 and 2) 
     - [Dropdown] sdxl_lora_4: str (Option from: ['None', 'sd_xl_offset_example-lora_1.0.safetensors']) 
     - [Slider] weight: int | float (numeric value between -2 and 2) 
     - [Dropdown] sdxl_lora_5: str (Option from: ['None', 'sd_xl_offset_example-lora_1.0.safetensors']) 
     - [Slider] weight: int | float (numeric value between -2 and 2) 
     - [Checkbox] input_image: bool 
     - [Radio] method: str 
     - [Image] drag_above_image_to_here: str (base64 representation of an image) 
    Returns:
     - [Html] value_4: str 
     - [Image] preview: str (base64 representation of an image) 
     - [Gallery] gallery: Tuple[Dict(name: str (name of file), data: str (base64 representation of file), size: int (size of image in bytes), is_file: bool (true if the file has been uploaded to the server), orig_name: str (original name of the file)), str | None] 

There you can see which parameters are required and also the fn_index=7.

Now you can use that information and do something like this:

import os
from gradio_client import Client

client = Client("https://117e50486cb756c772.gradio.live", serialize=False)

my_prompts=[
  {
    "positive": "modern sports car",
    "negative": "rusty"
  },
  {
    "positive": "retro sports car",
    "negative": "broken"
  }
]

universal_negative = "blurry, drawn, painted, unrealistic"

for index, prompt in enumerate(my_prompts):
  seed = 1337
  image = client.predict(
    prompt["positive"],                                                 # parameter_8: str (prompt)
    f"{universal_negative}, {prompt["negative"]}",                      # negative_prompt: str
    ["Fooocus V2"],                                                     # image_style: List[str] (style_selections)
    "Speed",                                                            # performance: str (performance_selction)
    "1024×1024",                                                        # aspect_ratios: str (aspect_ratios_selction)
    1,                                                                  # image_number: int (image_number)
    seed,                                                               # seed: int (image_seed)
    5,                                                                  # sampling_sharpness: int (sharpness)
    "sd_xl_base_1.0_0.9vae.safetensors",                                # sdxl_base_model: str (base_model_name)
    "sd_xl_refiner_1.0_0.9vae.safetensors",                             # sdxl_refiner: str (refiner_model_name)
    "sd_xl_offset_example-lora_1.0.safetensors",                        # sdxl_lora_1: str (l1)
    0.5,                                                                # weight: float (w1)
    "None",                                                             # sdxl_lora_2: str (l2)
    0.5,                                                                # weight: float (w2)
    "None",                                                             # sdxl_lora_3: str (l3)
    0.5,                                                                # weight: float (w3)
    "None",                                                             # sdxl_lora_4: str (l4)
    0.5,                                                                # weight: float (w4)
    "None",                                                             # sdxl_lora_5: str (l5)
    0.5,                                                                # weight: float (w5)
    False,                                                              # input_image: bool (input_image_checkbox)
    "disabled",                                                         # method: str (uov_method)
    "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mJ0GgAA", # drag_above_image_to_here: str (base64 image)
    fn_index=7                                                          # fn_index: int (api endpoint)
  )
  print(f"Image {index} generated with seed {seed}: {prompt}")

You will find the images in the outputs folder.

energy888666 commented 1 year ago

以下是如何使用上述方法正确执行此操作:

首先你需要知道fn_index相关的预测函数有哪些:

import os
from gradio_client import Client

client = Client("https://f676c563029db39e44.gradio.live", serialize=False)

print(client._render_endpoints_info)

今天,我们得到这个结果:

 - predict(parameter_8, negative_prompt, image_style, performance, aspect_ratios, image_number, seed, sampling_sharpness, sdxl_base_model, sdxl_refiner, sdxl_lora_1, weight, sdxl_lora_2, weight, sdxl_lora_3, weight, sdxl_lora_4, weight, sdxl_lora_5, weight, input_image, method, drag_above_image_to_here, fn_index=7) -> (value_4, preview, gallery)
    Parameters:
     - [Textbox] parameter_8: str 
     - [Textbox] negative_prompt: str 
     - [Checkboxgroup] image_style: List[str] 
     - [Radio] performance: str 
     - [Radio] aspect_ratios: str 
     - [Slider] image_number: int | float (numeric value between 1 and 32) 
     - [Number] seed: int | float 
     - [Slider] sampling_sharpness: int | float (numeric value between 0.0 and 30.0) 
     - [Dropdown] sdxl_base_model: str (Option from: ['sd_xl_base_1.0_0.9vae.safetensors', 'sd_xl_refiner_1.0_0.9vae.safetensors']) 
     - [Dropdown] sdxl_refiner: str (Option from: ['None', 'sd_xl_base_1.0_0.9vae.safetensors', 'sd_xl_refiner_1.0_0.9vae.safetensors']) 
     - [Dropdown] sdxl_lora_1: str (Option from: ['None', 'sd_xl_offset_example-lora_1.0.safetensors']) 
     - [Slider] weight: int | float (numeric value between -2 and 2) 
     - [Dropdown] sdxl_lora_2: str (Option from: ['None', 'sd_xl_offset_example-lora_1.0.safetensors']) 
     - [Slider] weight: int | float (numeric value between -2 and 2) 
     - [Dropdown] sdxl_lora_3: str (Option from: ['None', 'sd_xl_offset_example-lora_1.0.safetensors']) 
     - [Slider] weight: int | float (numeric value between -2 and 2) 
     - [Dropdown] sdxl_lora_4: str (Option from: ['None', 'sd_xl_offset_example-lora_1.0.safetensors']) 
     - [Slider] weight: int | float (numeric value between -2 and 2) 
     - [Dropdown] sdxl_lora_5: str (Option from: ['None', 'sd_xl_offset_example-lora_1.0.safetensors']) 
     - [Slider] weight: int | float (numeric value between -2 and 2) 
     - [Checkbox] input_image: bool 
     - [Radio] method: str 
     - [Image] drag_above_image_to_here: str (base64 representation of an image) 
    Returns:
     - [Html] value_4: str 
     - [Image] preview: str (base64 representation of an image) 
     - [Gallery] gallery: Tuple[Dict(name: str (name of file), data: str (base64 representation of file), size: int (size of image in bytes), is_file: bool (true if the file has been uploaded to the server), orig_name: str (original name of the file)), str | None] 

在那里您可以看到需要哪些参数以及fn_index=7.

现在您可以使用该信息并执行如下操作:

import os
from gradio_client import Client

client = Client("https://117e50486cb756c772.gradio.live", serialize=False)

my_prompts=[
  {
    "positive": "modern sports car",
    "negative": "rusty"
  },
  {
    "positive": "retro sports car",
    "negative": "broken"
  }
]

universal_negative = "blurry, drawn, painted, unrealistic"

for index, prompt in enumerate(my_prompts):
  seed = 1337
  image = client.predict(
    prompt["positive"],                                                 # parameter_8: str (prompt)
    f"{universal_negative}, {prompt["negative"]}",                      # negative_prompt: str
    ["Fooocus V2"],                                                     # image_style: List[str] (style_selections)
    "Speed",                                                            # performance: str (performance_selction)
    "1024×1024",                                                        # aspect_ratios: str (aspect_ratios_selction)
    1,                                                                  # image_number: int (image_number)
    seed,                                                               # seed: int (image_seed)
    5,                                                                  # sampling_sharpness: int (sharpness)
    "sd_xl_base_1.0_0.9vae.safetensors",                                # sdxl_base_model: str (base_model_name)
    "sd_xl_refiner_1.0_0.9vae.safetensors",                             # sdxl_refiner: str (refiner_model_name)
    "sd_xl_offset_example-lora_1.0.safetensors",                        # sdxl_lora_1: str (l1)
    0.5,                                                                # weight: float (w1)
    "None",                                                             # sdxl_lora_2: str (l2)
    0.5,                                                                # weight: float (w2)
    "None",                                                             # sdxl_lora_3: str (l3)
    0.5,                                                                # weight: float (w3)
    "None",                                                             # sdxl_lora_4: str (l4)
    0.5,                                                                # weight: float (w4)
    "None",                                                             # sdxl_lora_5: str (l5)
    0.5,                                                                # weight: float (w5)
    False,                                                              # input_image: bool (input_image_checkbox)
    "disabled",                                                         # method: str (uov_method)
    "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mJ0GgAA", # drag_above_image_to_here: str (base64 image)
    fn_index=7                                                          # fn_index: int (api endpoint)
  )
  print(f"Image {index} generated with seed {seed}: {prompt}")

我还是找不到文件啊 print(image) 结果如下: ({'visible': True, 'type': 'update', 'mode': 'dynamic'}, {'visible': False, 'type': 'update'}, [])

TimothyAlexisVass commented 1 year ago

是的,但你看,你首先需要检查哪个端点“fn_index”是正确的,然后包含所需的所有内容。 另一种方法是检查“API”并查看可视化界面中所需的 API 详细信息。

该项目仍然经常变化,因此功能编号和所需参数也会发生变化。

TimothyAlexisVass commented 1 year ago

The best option for bulk generation is to fix headless interaction: #385

energy888666 commented 1 year ago

是的,但你看,你首先需要检查哪个端点“fn_index”是正确的,然后包含所需的所有内容。 另一种方法是检查“API”并查看可视化界面中所需的 API 详细信息。

该项目仍然经常变化,因此功能编号和所需参数也会发生变化。

是的,但你看,你首先需要检查哪个端点“fn_index”是正确的,然后包含所需的所有内容。 另一种方法是检查“API”并查看可视化界面中所需的 API 详细信息。

该项目仍然经常变化,因此功能编号和所需参数也会发生变化。

多谢, 我已经解决了 目前的这个版本用的是 13,参数的确有变换