# fasteasySD

<b>fasteasySD</b> is Implementation library of txt2img, img2img function via Latent Consistency Model (LCM) model<br>
This project is designed to enable simple implementation of SD capabilities within other projects

Additionally, This project is designed to work with 'cpu' standalone settings in addition to 'cuda' devices.

by creating a comfyUI-lcm extension in a standalone library format.

Original version of comfyUI-lcm : <br>
https://github.com/0xbitches/ComfyUI-LCM

[![PyPI Downloads](https://img.shields.io/pypi/dm/fasteasySD.svg?label=PyPI%20downloads)](
https://pypi.org/project/fasteasySD/)

# Example Result (CPU Only):

## img2img result

<img src="./readme_img/img2img_example1.png" width="300px"> <img src="./readme_img/img2img_example2.png" width="600px">

### *Warning* Even if the original is a male image, sometimes it can be converted to a female image. so please Set prompt more accurately.

## txt2img result
<img src="./readme_img/txt2img_example1.png" width="500px"> <img src="./readme_img/txt2img_example2.png" width="500px">

# Usage

<b>This library supports two functions: txt2img and img2img.</b>

### simplest Usage

```Python
#import fasteasySD
from fasteasySD import FastEasySD as fesd

#make LCM_base class
test = fesd.FastEasySD(device='cpu',use_fp16=False)

#make img with img2img mode
test.make_image(mode="img2img",prompt="masterpeice, best quality, anime style",seed=0,steps=4,cfg=8,height=1063,width=827,num_images=1,prompt_strength=0.5,input_image_dir="input.jpg")

#make img with txt2img mode
test.make_image(mode="txt2img",prompt="masterpeice, best quality, anime style",seed=0,steps=4,cfg=8,height=768,width=512,num_images=2)
```

# installation

<b>Make Sure Install the Pytorch version for your system.</b>

find install Pytorch : https://pytorch.org/get-started/locally/

```bash
pip install fasteasySD 
```

# Documentation

<b>Provides simple documentation for this library.</b>

## FastEasySD

<b>Main class for LCM model control.</b>

### functions

FastEasySD class function list<br>
```Python
class FastEasySD:
    """ LCM model pipeline control class.

    Create and manage pipeline objects for LCM models,
    It has functions that process the pipeline input and output values as its main methods.

    """

    def __init__(self, device:str, use_fp16:bool):

        """ Class constructors.

        device : device to use (ex: 'cpu' or 'cuda').

        use_fp16 : Enable fp16 mode. (bool) (Not supported by most cpu devices.)

        """

    def __makeSampler(self):
        """ Create a txt2img, img2img sampler object. (automatic load with init)

        Create sampler objects for LCM model use.

        """
    
    def make_seed(self,seed: int, random_seed:bool) -> int:

        """ Automatically generate seed value (random number)

        Automatically generate seed value (random number)

        seed : user input seed value (int)

        random_seed : True, False for use random_seed
        
        """
    
    def __load_img(self,img_dir:str):

        """ Load image file for img2img input

        Load the file specified in img_dir and return it to the form available in img2img sampler.

        img_dir : path for input img.

        """
    
    def save_PIL(self,pils,save_name):

        """ PIL image list storage function.

        Store a list of PIL images generated by the LCM model.

        pils : list of PIL images

        save_name : Set image save filename. (ex: {save_name}_1.png)

        """
    
    def return_PIL(self,images):

        """ Converts LCM Model Tensor results to a PIL list.

        Converts the Tensor list generated through the LCM model to a PIL list.

        images : LCM pipeline output tensor

        """
    
    def i2i_batch_save(self,images_list,base_name):

        """ Save img for img2img result values.

        First clean up the Tensor list generated by img2img function.

        and save img2img result.

        images_list : LCM img2img pipeline output tensor list.

        base_name : base name for saving img. (ex : {base_name}_{save_name}_1.png)

        """

    def make(self,mode:str,prompt:str,seed:int,steps:int,cfg:int,
                   height:int,width:int,num_images:int,prompt_strength:float=0,input_image_dir:str="./input.jpg"):
        
        """ Process user input and forward it to the LCM pipeline.

        Forward variable values for image creation to the LCM pipeline and return the corresponding result values

        mode : string for LCM mode (txt2img or img2img)

        prompt : LCM model input prompt (ex : "masterpeice, best quality, anime style" )

        seed : seed for LCM model (input 0 will make random seed)

        steps : steps for LCM model (recommend 2~4)

        cfg : cfg for LCM model (recommend 6~8)

        height , width : setting height and width for img (** if you are using img2img w and h should be the same as the input image. **)

        num_images : How many images will you create for this input

        prompt_strength : (only for img2img) How Strong will the prompts be applied in the img2img feature

        input_image_dir : (only for img2img) input image dir

        """
        
    def make_image(self,mode:str,prompt:str,seed:int,steps:int,cfg:int,
                   height:int,width:int,num_images:int,prompt_strength:float=0,input_image_dir:str="./input.jpg",output_image_dir:str="."):
        
        """ Most Simplified Image Generation Function

        Save the image generated by the txt2img, img2img function as a separate file based on user input.

        the output img will be save like output_image_dir/fesd_0.png(txt2img) or output_image_dir/fesd_i2i_0_0.png(img2img)

        mode : string for LCM mode (txt2img or img2img)

        prompt : LCM model input prompt (ex : "masterpeice, best quality, anime style" )

        seed : seed for LCM model (input 0 will make random seed)

        steps : steps for LCM model (recommend 2~4)

        cfg : cfg for LCM model (recommend 6~8)

        height , width : setting height and width for img (** if you are using img2img w and h should be the same as the input image. **)

        num_images : How many images will you create for this input

        prompt_strength : (only for img2img) How Strong will the prompts be applied in the img2img feature

        input_image_dir : (only for img2img) input image dir

        output_image_dir : output image dir (it will not make dir)

        """

```
### Usage example : 

```Python
from fasteasySD import FastEasySD as fesd

test = fesd.FastEasySD(device='cpu',use_fp16=False)

mode = "img2img"

images = test.make(mode=mode,seed=0,steps=4,prompt_strength=0.5,cfg=8,prompt="masterpeice, best quality, anime style",height=1063,width=827,num_images=2,input_image_dir="input.jpg")

if mode == "txt2img":
            
    pil_images = test.return_PIL(images)

    test.save_PIL(pils=pil_images,save_name="./fesd")

elif mode == "img2img":
            
    test.i2i_batch_save(images_list=images,base_name="./fesd_i2i")

```

# Additional Plan Scheduled

<b>add batch mode for img2img input.</b>

<b>add controlnet support</b>

# Tech Stack

Python<br>