a self-hosted webui for several generative ai
# https://github.com/Woolverine94/biniou
# Audiogen.py
import os
import gradio as gr
import torch
import torchaudio
from audiocraft.models import AudioGen
from audiocraft.data.audio import audio_write
import time
import random
from ressources.common import *

device_audiogen = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

model_path_audiogen = "./models/Audiocraft/"
os.makedirs(model_path_audiogen, exist_ok=True)

modellist_audiogen = [
    "facebook/audiogen-medium",  
]

# Bouton Cancel
stop_audiogen = False

def initiate_stop_audiogen() :
    global stop_audiogen
    stop_audiogen = True

def check_audiogen(generated_tokens, total_tokens) : 
    global stop_audiogen
    if stop_audiogen == False :
        return
    elif stop_audiogen == True :
        stop_audiogen = False
        try:
            del ressources.audiogen.pipe_audiogen
        except NameError as e:
            raise Exception("Interrupting ...")
    return


def music_audiogen(
    prompt_audiogen, 
    model_audiogen, 
    duration_audiogen, 
    num_batch_audiogen, 
    temperature_audiogen, 
    top_k_audiogen, 
    top_p_audiogen, 
    use_sampling_audiogen, 
    cfg_coef_audiogen, 
    progress_audiogen=gr.Progress(track_tqdm=True)
    ):

    pipe_audiogen = AudioGen.get_pretrained(model_audiogen, device=device_audiogen)
    pipe_audiogen.set_generation_params(
        duration=duration_audiogen, 
        use_sampling=use_sampling_audiogen, 
        temperature=temperature_audiogen, 
        top_k=top_k_audiogen, 
        top_p=top_p_audiogen, 
        cfg_coef=cfg_coef_audiogen
    )
    
    pipe_audiogen.set_custom_progress_callback(check_audiogen)
    prompt_audiogen_final = [f"{prompt_audiogen}"] 
    
    for i in range (num_batch_audiogen):
        wav = pipe_audiogen.generate(prompt_audiogen_final, progress=True)
        for idx, one_wav in enumerate(wav):
            timestamp = time.time()
            savename = f"outputs/{timestamp}_{idx}"
            savename_final = savename+ ".wav" 
            audio_write(savename, one_wav.cpu(), pipe_audiogen.sample_rate, strategy="loudness", loudness_compressor=True)

    del pipe_audiogen
    clean_ram()
            
    return savename_final

ressources/audiogen.py
Woolverine94/biniou
# https://github.com/Woolverine94/biniou
# bark.py
import gradio as gr
import os
from transformers import AutoProcessor, BarkModel
from scipy.io.wavfile import write as write_wav
import time
import random
from ressources.common import *

model_path_bark = "./models/Bark/"
os.makedirs(model_path_bark, exist_ok=True)

model_list_bark = [
    "suno/bark-small",
    "suno/bark",
]

voice_preset_list_bark = {
    "DE Male": "v2/de_speaker_9",
    "DE Female": "v2/de_speaker_8",
    "EN Male": "v2/en_speaker_6", 
    "EN Female": "v2/en_speaker_9",
    "ES Male": "v2/es_speaker_7",
    "ES Female": "v2/es_speaker_9",
    "FR Male" : "v2/fr_speaker_8",
    "FR Female": "v2/fr_speaker_5",
    "HI Male": "v2/hi_speaker_8",
    "HI Female": "v2/hi_speaker_9",
    "JA Male": "v2/ja_speaker_6",
    "JA Female": "v2/ja_speaker_7",
    "KO Male": "v2/ko_speaker_9",
    "KO Female": "v2/ko_speaker_0",
    "PL Male": "v2/pl_speaker_8",
    "PL Female": "v2/pl_speaker_9",
    "PT Male": "v2/pt_speaker_9",
    "RU Male": "v2/ru_speaker_7",
    "RU Female": "v2/ru_speaker_5",
    "TR Male": "v2/tr_speaker_9",
    "TR Female": "v2/tr_speaker_5",
    "ZH Male": "v2/zh_speaker_8",
    "ZH Female": "v2/zh_speaker_9",
}

# Bouton Cancel
stop_bark = False

def initiate_stop_bark() :
    global stop_bark
    stop_bark = True

def check_bark(step, timestep, latents) : 
    global stop_bark
    if stop_bark == False :
        return
    elif stop_bark== True :
        stop_bark = False
        try:
            del ressources.bark.pipe_bark
        except NameError as e:
            raise Exception("Interrupting ...")
    return

def music_bark(prompt_bark, model_bark, voice_preset_bark, progress_bark=gr.Progress(track_tqdm=True)):
    
    processor = AutoProcessor.from_pretrained(
        model_bark, 
        cache_dir=model_path_bark, 
        resume_download=True,
        local_files_only=True if offline_test() else None
    )
    
    pipe_bark = BarkModel.from_pretrained(
        model_bark, 
        cache_dir=model_path_bark,
        resume_download=True,
        local_files_only=True if offline_test() else None        
        )
        
    pipe_bark = pipe_bark.to_bettertransformer()
    voice_preset = voice_preset_list_bark[voice_preset_bark]
    inputs = processor(prompt_bark, voice_preset=voice_preset)
    audio_array = pipe_bark.generate(**inputs, do_sample=True)
    audio_array = audio_array.cpu().numpy().squeeze()
    sample_rate = pipe_bark.generation_config.sample_rate
    timestamp = time.time()
    savename = f"outputs/{timestamp}.wav"
    write_wav(savename, sample_rate, audio_array)
    
    del processor, pipe_bark, audio_array    
    clean_ram()
    
    return savename 

ressources/bark.py
Woolverine94/biniou
# https://github.com/Woolverine94/biniou
# common.py
from PIL import Image, ExifTags
from io import BytesIO
import gradio as gr
import torch
import base64
import re
import zipfile as zf
import time
import gc
import psutil
import requests as rq
from diffusers.pipelines.stable_diffusion import StableDiffusionSafetyChecker
from transformers import AutoFeatureExtractor

device_torch = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

ACTION_LIST = [
    "Outputs",
    "Inputs",
    "Both",
]

MODULES_DB = {
# Structure "Nom onglet" : {"Code module"},
    "Choose ...": {"codename": "dummy"}, 
    "Stable Beluga": {"codename": "chatbot"}, 
    "Stable Diffusion" : {"codename": "txt2img_sd"},  
    "Kandinsky" : {"codename": "txt2img_kd"},
    "img2img" : {"codename": "img2img"},
    "pix2pix" : {"codename": "pix2pix"},
    "inpaint" : {"codename": "inpaint"},
    "Real ESRGAN" : {"codename": "resrgan"},
    "GFPGAN" : {"codename": "gfpgan"},
}

MODULES_LIST = list(MODULES_DB.keys())

RESRGAN_SCALES = { 
    "x2": 2,
#    "x3": 3,
    "x4": 4,
    "x8": 8,    
}    

# {'codename': 'txt2img_sd', 'tab': 'tab_image_num', 'tab_item': 'tab_txt2img_sd_num', 'inject': None, 'extract': None, 'outputs': ['gs_out_txt2img_sd', 'sel_out_txt2img_sd'], 'inputs': ['prompt_txt2img_sd', 'negative_prompt_txt2img_sd'], 'return_tabitem': 'tabs_image'}

# txt2img_sd_img2img.click(fn=send_to_module, inputs=[gs_out_txt2img_sd, sel_out_txt2img_sd, tab_image_num, tab_img2img_num], outputs=[img_img2img, tabs, tabs_image])

# send_to_module ['gs_out_txt2img_sd', 'sel_out_txt2img_sd', 'tab_image_num', 'tab_img2img_num'] ['prompt_img2img', 'negative_prompt_img2img', 'img_img2img', ', tabs', 'tab_img2img_num']

def send_input():
    return True
    
def zipper(content):
    timestamp = time.time()
    savename = f"./.tmp/{timestamp}.zip"
    with zf.ZipFile(savename, 'w') as myzip:
        for idx, file in enumerate(content):
            file_name=file["name"].replace("\\", "/")
            myzip.write(file["name"], f"{idx}_"+ file_name.split("/")[-1])
    return savename

def correct_size(width, height, max_size) :
    if (width>height) :
        dim_max = width
        dim_min = height
        orientation = "l"
    else :
        dim_max = height
        dim_min = width
        orientation = "p"
    approx_int = round(dim_min/(dim_max/max_size))
    approx = (approx_int + 7) & (-8)
    if  (orientation == "l") :
        final_width = max_size
        final_height = approx
    elif (orientation == "p") :
        final_width = approx
        final_height = max_size
    return (final_width, final_height)

def image_upload_event(im):
    if (im != None):
        if (im[0:11] != "data:image/"):
            image_out = Image.open(im)
        else :
            imbis = re.sub('^data:image/.+;base64,', '', im)
            image_out = Image.open(BytesIO(base64.b64decode(imbis)))
        return (image_out.size)
    else : 
        return (512, 512)

def image_upload_event_inpaint(im):
    type_image = type(im)
    rotation_img = 360            
    if (type_image == str):
        image_out = Image.open(im)
    else :
        imbis = re.sub('^data:image/.+;base64,', '', im["image"])
        image_out = Image.open(BytesIO(base64.b64decode(imbis)))
    try :
        exif_data = image_out._getexif()
        orientation_img = exif_data[274]
    except Exception as e:
        orientation_img = ""
    if (orientation_img == 3):
        image_out = image_out.rotate(180, expand=True)
        rotation_img = 180
    elif (orientation_img == 6):
        image_out = image_out.rotate(270, expand=True)
        rotation_img = 270 
    elif (orientation_img == 8):
        image_out = image_out.rotate(90, expand=True)
        rotation_img = 90
    dim = correct_size(image_out.size[0], image_out.size[1], 512)
    image_out = image_out.convert("RGB").resize(dim)
    return (image_out.size[0], image_out.size[1], image_out, rotation_img)
    
def image_upload_event_inpaint_b(im):
    if (im != None):
       imbis = re.sub('^data:image/.+;base64,', '', im)
       image_out = Image.open(BytesIO(base64.b64decode(imbis)))
       dim = correct_size(image_out.size[0], image_out.size[1], 512)
       return (dim)
    else : 
        return (512, 512)

def scale_image(im, size):
    max_size = int(size)
    if (im != None):
        type_image = type(im)
        if (type_image == str) :
            image_out = Image.open(im)
        else :
            imbis = re.sub('^data:image/.+;base64,', '', im["image"])
            image_out = Image.open(BytesIO(base64.b64decode(imbis)))
        if image_out.size[0] > max_size or image_out.size[1] > max_size :
            dim = correct_size(image_out.size[0], image_out.size[1], max_size)            
            image_out = image_out.convert("RGB").resize(dim)
        return (image_out.size[0], image_out.size[1], image_out)
    return (max_size, max_size, "")
        
def scale_resrgan_change(scale_resrgan):
    if (RESRGAN_SCALES[scale_resrgan] < 3):
        scale_model_resrgan: str = "RealESRGAN_x2.pth"
    elif (RESRGAN_SCALES[scale_resrgan] < 5):     
        scale_model_resrgan: str  = "RealESRGAN_x4.pth"
    else :     
        scale_model_resrgan: str = "RealESRGAN_x8.pth"  
    return scale_model_resrgan
        

def preview_image(step, timestep, latents, pipe):
    final_preview=[]
#    print(step, timestep, latents[0][0][0][0])
    with torch.no_grad():
        latents = 1 / 0.18215 * latents
        image = pipe.vae.decode(latents).sample
        image = (image / 2 + 0.5).clamp(0, 1)
        image = image.cpu().permute(0, 2, 3, 1).float().numpy()
        # convert to PIL Images
        image = pipe.numpy_to_pil(image)
#        for img in enumerate(image):
        for j in range(len(image)):
            timestamp = time.time()
            savename = f"/tmp/gradio/{timestamp}.png"
            image[j].save(savename)
            final_preview.append(savename)
    return final_preview

        # do something with the Images
        
#            output_gallery.update(value=img[i])
#             img.save(f"step_{step}_img{i}.png")

def clean_ram():
    gc.collect()
    if torch.cuda.is_available():
        torch.cuda.empty_cache()
        torch.cuda.ipc_collect()
    return

def ram_size():
    return int(psutil.virtual_memory().total/(1000**3))

# 0 = Pas de filtre (safety_checker=None)/ 1 = filtre actif
# Safety_Check : 
safety_checker_model = "CompVis/stable-diffusion-safety-checker"

def safety_checker_sd(model_path, device, nsfw_filter):
    if nsfw_filter == "0" :
        safecheck = None
        feat_ex = None
    elif nsfw_filter == "1" :
        safecheck = StableDiffusionSafetyChecker.from_pretrained(
            safety_checker_model, 
            cache_dir=model_path, 
            torch_dtype=torch.float32,
            resume_download=True,
            local_files_only=True if offline_test() else None
        ).to(device)
        feat_ex = AutoFeatureExtractor.from_pretrained(
            safety_checker_model, 
            cache_dir=model_path, 
            torch_dtype=torch.float32,
            resume_download=True,
            local_files_only=True if offline_test() else None            
            )
    return safecheck, feat_ex

def offline_test():
    try:
        rq.get("https://www.google.com", timeout=5)
        return False
    except rq.ConnectionError:
        return True

def write_file(*args) :
    timestamp = time.time()
    savename = f"outputs/{timestamp}.txt"
    content = ""
    for idx, data in enumerate(args):
        content += f"{data} \n"
    with open(savename, 'w') as savefile:
        savefile.write(content)
    return

def set_timestep_vid_ze(numstep) :
    factor = round(numstep/10)
    t1 = numstep-(factor+1)
    t0 = t1-factor
    return t0, t1

def set_num_beam_groups_img2txt_git(numbeam, numbeam_groups) :
    if numbeam>1 and numbeam_groups<2 :
        numbeam_groups = 2
    elif numbeam<2 and numbeam_groups>1 :
        numbeam_groups = 1
    return numbeam_groups

ressources/common.py
Woolverine94/biniou
# https://github.com/Woolverine94/biniou
# controlnet.py
import gradio as gr
import os
import cv2
import torch
from diffusers import StableDiffusionControlNetPipeline, StableDiffusionXLControlNetPipeline, ControlNetModel
from compel import Compel, ReturnedEmbeddingsType
import time
import random
from ressources.scheduler import *
from ressources.gfpgan import *
from controlnet_aux.processor import Processor
import tomesd

device_controlnet = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

# Gestion des modèles
model_path_controlnet = "./models/Stable_Diffusion/"
os.makedirs(model_path_controlnet, exist_ok=True)
model_list_controlnet = []

for filename in os.listdir(model_path_controlnet):
    f = os.path.join(model_path_controlnet, filename)
    if os.path.isfile(f) and (filename.endswith('.ckpt') or filename.endswith('.safetensors')):
        model_list_controlnet.append(f)

model_list_controlnet_builtin = [
    "SG161222/Realistic_Vision_V3.0_VAE",
#    "ckpt/anything-v4.5-vae-swapped",
    "stabilityai/stable-diffusion-xl-base-1.0",
    "runwayml/stable-diffusion-v1-5",
    "nitrosocke/Ghibli-Diffusion", 
]

for k in range(len(model_list_controlnet_builtin)):
    model_list_controlnet.append(model_list_controlnet_builtin[k])

model_path_base_controlnet = "./models/controlnet"
os.makedirs(model_path_base_controlnet, exist_ok=True)

base_controlnet = "lllyasviel/ControlNet-v1-1"

variant_list_controlnet = [
    "lllyasviel/control_v11p_sd15_canny",
    "lllyasviel/control_v11f1p_sd15_depth",
    "lllyasviel/control_v11p_sd15s2_lineart_anime",
    "lllyasviel/control_v11p_sd15_lineart",
    "lllyasviel/control_v11p_sd15_mlsd",
    "lllyasviel/control_v11p_sd15_normalbae",
    "lllyasviel/control_v11p_sd15_openpose",
    "lllyasviel/control_v11p_sd15_scribble",
    "lllyasviel/control_v11p_sd15_softedge",
    "patrickvonplaten/controlnet-canny-sdxl-1.0",
    "patrickvonplaten/controlnet-depth-sdxl-1.0",
    "zbulrush/controlnet-sd-xl-1.0-lineart",
    "thibaud/controlnet-openpose-sdxl-1.0",
    "SargeZT/controlnet-sd-xl-1.0-softedge-dexined",
]


preprocessor_list_controlnet = [
    "canny", 
    "depth_leres", 
    "depth_leres++", 
    "depth_midas", 
    "lineart_anime",
    "lineart_coarse", 
    "lineart_realistic", 
    "mlsd", 
    "normal_bae", 
    "openpose", 
    "openpose_face", 
    "openpose_faceonly", 
    "openpose_full", 
    "openpose_hand",
    "scribble_hed", 
    "scribble_pidinet", 
    "softedge_hed", 
    "softedge_hedsafe",
    "softedge_pidinet", 
    "softedge_pidsafe", 
]

# Bouton Cancel
stop_controlnet = False

def initiate_stop_controlnet() :
    global stop_controlnet
    stop_controlnet = True

def check_controlnet(step, timestep, latents) :
    global stop_controlnet
    if stop_controlnet == False :
        return
    elif stop_controlnet == True :
        stop_controlnet = False
        try:
            del ressources.controlnet.pipe_controlnet
        except NameError as e:
            raise Exception("Interrupting ...")
            return "Canceled ..."
    return

def dispatch_controlnet_preview(
    modelid_controlnet,
    low_threshold_controlnet, 
    high_threshold_controlnet, 
    img_source_controlnet, 
    preprocessor_controlnet, 
    progress_controlnet=gr.Progress(track_tqdm=True)
    ): 

    if ('xl' or 'XL' or 'Xl' or 'xL') in modelid_controlnet :
        is_xl_controlnet: bool = True
    else :        
        is_xl_controlnet: bool = False

    img_source_controlnet = Image.open(img_source_controlnet)
    img_source_controlnet = np.array(img_source_controlnet)
    processor_controlnet = Processor(preprocessor_controlnet)

    match preprocessor_controlnet:
        case "canny":
            result = canny_controlnet(img_source_controlnet, low_threshold_controlnet, high_threshold_controlnet)
            return result, result, variant_list_controlnet[9] if is_xl_controlnet else variant_list_controlnet[0]
        case "depth_leres":
            result = processor_controlnet(img_source_controlnet, to_pil=True)
            return result, result, variant_list_controlnet[10] if is_xl_controlnet else variant_list_controlnet[1]
        case "depth_leres++":
            result = processor_controlnet(img_source_controlnet, to_pil=True)
            return result, result, variant_list_controlnet[10] if is_xl_controlnet else variant_list_controlnet[1]
        case "depth_midas":
            result = processor_controlnet(img_source_controlnet, to_pil=True)
            return result, result, variant_list_controlnet[10] if is_xl_controlnet else variant_list_controlnet[1]
        case "depth_zoe":
            result = processor_controlnet(img_source_controlnet, to_pil=True)
            return result, result, variant_list_controlnet[10] if is_xl_controlnet else variant_list_controlnet[1]
        case "lineart_anime":
            result = processor_controlnet(img_source_controlnet, to_pil=True)
            return result, result, variant_list_controlnet[11] if is_xl_controlnet else variant_list_controlnet[2]
        case "lineart_coarse":
            result = processor_controlnet(img_source_controlnet, to_pil=True)
            return result, result, variant_list_controlnet[11] if is_xl_controlnet else variant_list_controlnet[3]
        case "lineart_realistic":
            result = processor_controlnet(img_source_controlnet, to_pil=True)
            return result, result, variant_list_controlnet[11] if is_xl_controlnet else variant_list_controlnet[3]
        case "mlsd":
            result = processor_controlnet(img_source_controlnet, to_pil=True)
            return result, result, variant_list_controlnet[9] if is_xl_controlnet else variant_list_controlnet[4]
        case "normal_bae":
            result = processor_controlnet(img_source_controlnet, to_pil=True)
            return result, result, variant_list_controlnet[10] if is_xl_controlnet else variant_list_controlnet[5]
        case "normal_midas":
            result = processor_controlnet(img_source_controlnet, to_pil=True)
            return result, result, variant_list_controlnet[10] if is_xl_controlnet else variant_list_controlnet[5]
        case "openpose":
            result = processor_controlnet(img_source_controlnet, to_pil=True)
            return result, result, variant_list_controlnet[12] if is_xl_controlnet else variant_list_controlnet[6]
        case "openpose_face":
            result = processor_controlnet(img_source_controlnet, to_pil=True)
            return result, result, variant_list_controlnet[12] if is_xl_controlnet else variant_list_controlnet[6]
        case "openpose_faceonly":
            result = processor_controlnet(img_source_controlnet, to_pil=True)
            return result, result, variant_list_controlnet[12] if is_xl_controlnet else variant_list_controlnet[6]
        case "openpose_full":
            result = processor_controlnet(img_source_controlnet, to_pil=True)
            return result, result, variant_list_controlnet[12] if is_xl_controlnet else variant_list_controlnet[6]
        case "openpose_hand":
            result = processor_controlnet(img_source_controlnet, to_pil=True)            
            return result, result, variant_list_controlnet[12] if is_xl_controlnet else variant_list_controlnet[6]
        case "scribble_hed":
            result = processor_controlnet(img_source_controlnet, to_pil=True)
            return result, result, variant_list_controlnet[13] if is_xl_controlnet else variant_list_controlnet[7]
        case "scribble_pidinet":
            result = processor_controlnet(img_source_controlnet, to_pil=True)
            return result, result, variant_list_controlnet[13] if is_xl_controlnet else variant_list_controlnet[7]
        case "softedge_hed":
            result = processor_controlnet(img_source_controlnet, to_pil=True)
            return result, result, variant_list_controlnet[13] if is_xl_controlnet else variant_list_controlnet[8]
        case "softedge_hedsafe":
            result = processor_controlnet(img_source_controlnet, to_pil=True)
            return result, result, variant_list_controlnet[13] if is_xl_controlnet else variant_list_controlnet[8]
        case "softedge_pidinet":
            result = processor_controlnet(img_source_controlnet, to_pil=True)
            return result, result, variant_list_controlnet[13] if is_xl_controlnet else variant_list_controlnet[8]
        case "softedge_pidsafe":
            result = processor_controlnet(img_source_controlnet, to_pil=True)
            return result, result, variant_list_controlnet[13] if is_xl_controlnet else variant_list_controlnet[8]
    
def canny_controlnet(image, low_threshold, high_threshold):
    image = cv2.Canny(image, low_threshold, high_threshold)
    image = image[:, :, None]
    image = np.concatenate([image, image, image], axis=2)
    canny_image = Image.fromarray(image)
    return canny_image

def image_controlnet(
    modelid_controlnet, 
    sampler_controlnet, 
    prompt_controlnet, 
    negative_prompt_controlnet, 
    num_images_per_prompt_controlnet, 
    num_prompt_controlnet, 
    guidance_scale_controlnet, 
    num_inference_step_controlnet, 
    height_controlnet, 
    width_controlnet, 
    seed_controlnet, 
    low_threshold_controlnet, 
    high_threshold_controlnet, 
    strength_controlnet, 
    start_controlnet, 
    stop_controlnet, 
    use_gfpgan_controlnet, 
    variant_controlnet, 
    img_preview_controlnet, 
    nsfw_filter, 
    tkme_controlnet,
    progress_controlnet=gr.Progress(track_tqdm=True)
    ):

    nsfw_filter_final, feat_ex = safety_checker_sd(model_path_controlnet, device_controlnet, nsfw_filter)
    
    controlnet = ControlNetModel.from_pretrained(
        variant_controlnet, 
        cache_dir=model_path_base_controlnet, 
        torch_dtype=torch.float32, 
        use_safetensors=True,
        resume_download=True,
        local_files_only=True if offline_test() else None        
        )
        
    img_preview_controlnet = Image.open(img_preview_controlnet)
    strength_controlnet = float(strength_controlnet)
    start_controlnet = float(start_controlnet)
    stop_controlnet = float(stop_controlnet)
    
    if ('xl' or 'XL' or 'Xl' or 'xL') in modelid_controlnet :
        is_xl_controlnet: bool = True
    else :        
        is_xl_controlnet: bool = False
        
    if (is_xl_controlnet == True) :
        if modelid_controlnet[0:9] == "./models/" :
            pipe_controlnet = StableDiffusionXLControlNetPipeline.from_single_file(
                modelid_controlnet, 
                controlnet=controlnet, 
                torch_dtype=torch.float32, 
                use_safetensors=True, 
                safety_checker=nsfw_filter_final, 
                feature_extractor=feat_ex
            )
        else :        
            pipe_controlnet = StableDiffusionXLControlNetPipeline.from_pretrained(
                modelid_controlnet, 
                controlnet=controlnet, 
                cache_dir=model_path_controlnet, 
                torch_dtype=torch.float32, 
                use_safetensors=True, 
                safety_checker=nsfw_filter_final, 
                feature_extractor=feat_ex,
                resume_download=True,
                local_files_only=True if offline_test() else None
            )
    else :
        if modelid_controlnet[0:9] == "./models/" :
            pipe_controlnet = StableDiffusionControlNetPipeline.from_single_file(
                modelid_controlnet, 
                controlnet=controlnet, 
                torch_dtype=torch.float32, 
                use_safetensors=True, 
                safety_checker=nsfw_filter_final, 
                feature_extractor=feat_ex
            )
        else :        
            pipe_controlnet = StableDiffusionControlNetPipeline.from_pretrained(
                modelid_controlnet, 
                controlnet=controlnet, 
                cache_dir=model_path_controlnet, 
                torch_dtype=torch.float32, 
                use_safetensors=True, 
                safety_checker=nsfw_filter_final, 
                feature_extractor=feat_ex,
                resume_download=True,
                local_files_only=True if offline_test() else None
            )
   
    pipe_controlnet = get_scheduler(pipe=pipe_controlnet, scheduler=sampler_controlnet)
    pipe_controlnet = pipe_controlnet.to(device_controlnet)
    pipe_controlnet.enable_attention_slicing("max")
    tomesd.apply_patch(pipe_controlnet, ratio=tkme_controlnet)

    if seed_controlnet == 0:
        random_seed = torch.randint(0, 10000000000, (1,))
        generator = torch.manual_seed(random_seed)
    else:
        generator = torch.manual_seed(seed_controlnet)

    prompt_controlnet = str(prompt_controlnet)
    negative_prompt_controlnet = str(negative_prompt_controlnet)
    if prompt_controlnet == "None":
        prompt_controlnet = ""
    if negative_prompt_controlnet == "None":
        negative_prompt_controlnet = ""

    if (is_xl_controlnet == True) :
        compel = Compel(
            tokenizer=[pipe_controlnet.tokenizer, pipe_controlnet.tokenizer_2], 
            text_encoder=[pipe_controlnet.text_encoder, pipe_controlnet.text_encoder_2], 
            returned_embeddings_type=ReturnedEmbeddingsType.PENULTIMATE_HIDDEN_STATES_NON_NORMALIZED, 
            requires_pooled=[False, True], 
        )
        conditioning, pooled = compel(prompt_controlnet)
        neg_conditioning, neg_pooled = compel(negative_prompt_controlnet)
        [conditioning, neg_conditioning] = compel.pad_conditioning_tensors_to_same_length([conditioning, neg_conditioning])
    else : 
        compel = Compel(tokenizer=pipe_controlnet.tokenizer, text_encoder=pipe_controlnet.text_encoder, truncate_long_prompts=False)
        conditioning = compel.build_conditioning_tensor(prompt_controlnet)
        neg_conditioning = compel.build_conditioning_tensor(negative_prompt_controlnet)    
        [conditioning, neg_conditioning] = compel.pad_conditioning_tensors_to_same_length([conditioning, neg_conditioning])
   
    final_image = []
    
    for i in range (num_prompt_controlnet):
        if (is_xl_controlnet == True) : 
            image = pipe_controlnet(
                prompt_embeds=conditioning, 
                pooled_prompt_embeds=pooled, 
                negative_prompt_embeds=neg_conditioning,
                negative_pooled_prompt_embeds=neg_pooled,            
                image=img_preview_controlnet,
                height=height_controlnet,
                width=width_controlnet,
                num_images_per_prompt=num_images_per_prompt_controlnet,
                num_inference_steps=num_inference_step_controlnet,
                guidance_scale=guidance_scale_controlnet,
                controlnet_conditioning_scale=strength_controlnet,
                control_guidance_start=start_controlnet,
                control_guidance_end=stop_controlnet,                
                generator=generator,
                callback=check_controlnet,
            ).images
        else :            
            image = pipe_controlnet(
                prompt_embeds=conditioning,
                negative_prompt_embeds=neg_conditioning,
                image=img_preview_controlnet,                
                height=height_controlnet,
                width=width_controlnet,
                num_images_per_prompt=num_images_per_prompt_controlnet,
                num_inference_steps=num_inference_step_controlnet,
                guidance_scale=guidance_scale_controlnet,
                controlnet_conditioning_scale=strength_controlnet,
                control_guidance_start=start_controlnet,
                control_guidance_end=stop_controlnet,
                generator=generator,
                callback=check_controlnet,
            ).images

        for j in range(len(image)):
            timestamp = time.time()
            savename = f"outputs/{timestamp}.png"
            if use_gfpgan_controlnet == True :
                image[j] = image_gfpgan_mini(image[j])
            image[j].save(savename)
            final_image.append(image[j])

    final_image.append(img_preview_controlnet)

    del nsfw_filter_final, feat_ex, controlnet, img_preview_controlnet, pipe_controlnet, generator, compel, conditioning, neg_conditioning, image 
    clean_ram()
    
    return final_image, final_image

ressources/controlnet.py
Woolverine94/biniou
# https://github.com/Woolverine94/biniou
# faceswapper.py
import os
import cv2
import copy
import insightface
import onnxruntime
import numpy as np
from PIL import Image
import time
import random
from ressources.scheduler import *
from ressources.common import *
from ressources.gfpgan import *
from huggingface_hub import snapshot_download, hf_hub_download

# Gestion des modèles
model_path_faceswap = "./models/faceswap/"
os.makedirs(model_path_faceswap, exist_ok=True)

model_list_faceswap = {}

# for filename in os.listdir(model_path_faceswap):
#     f = os.path.join(model_path_faceswap, filename)
#     if os.path.isfile(f) and filename.endswith('.onnx') :
#         print(filename, f)
#         model_list_faceswap.update({f: ""})

model_list_faceswap_builtin = {
    "thebiglaskowski/inswapper_128.onnx": "inswapper_128.onnx",
}

model_list_faceswap.update(model_list_faceswap_builtin)

def download_model(modelid_faceswap):
    if modelid_faceswap[0:9] != "./models/":
        hf_hub_path_faceswap = hf_hub_download(
            repo_id=modelid_faceswap, 
            filename=model_list_faceswap[modelid_faceswap], 
            repo_type="model", 
            cache_dir=model_path_faceswap, 
            local_dir=model_path_faceswap, 
            local_dir_use_symlinks=True, 
            resume_download=True,
            local_files_only=True if offline_test() else None
        )
        modelid_faceswap = hf_hub_path_faceswap
    return modelid_faceswap    

def image_faceswap(
    modelid_faceswap, 
    img_source_faceswap, 
    img_target_faceswap, 
    source_index_faceswap, 
    target_index_faceswap, 
    use_gfpgan_faceswap, 
    progress_txt2img_sd=gr.Progress(track_tqdm=True)
    ):
   
    if source_index_faceswap == "":
        source_index_faceswap = "0"
    if target_index_faceswap == "":
        target_index_faceswap = "0"
    
    model_path = os.path.join(model_path_faceswap, model_list_faceswap[modelid_faceswap])
    modelid_faceswap = download_model(modelid_faceswap)
    source_img = cv2.imread(img_source_faceswap)
    target_img = cv2.imread(img_target_faceswap)
    providers = onnxruntime.get_available_providers()
    face_analyser = insightface.app.FaceAnalysis(name="buffalo_l", root=model_path_faceswap, providers=providers)
    face_analyser.prepare(ctx_id=0, det_size=(320, 320))
    face_swapper = insightface.model_zoo.get_model(model_path)
    target_analyze = face_analyser.get(cv2.cvtColor(target_img, cv2.COLOR_RGB2BGR))
    target_faces = sorted(target_analyze, key=lambda x: x.bbox[0])
    num_target_faces = len(target_faces)
    source_analyze = face_analyser.get(cv2.cvtColor(source_img, cv2.COLOR_RGB2BGR))
    source_faces = sorted(source_analyze, key=lambda x: x.bbox[0])
    num_source_faces = len(source_faces)
    temp_frame = copy.deepcopy(target_img)
    source_index_faceswap = source_index_faceswap.split(',')    
    target_index_faceswap = target_index_faceswap.split(',')
    final_image = []
    
    for i in range(len(source_index_faceswap)):
        source_faces_final = int(source_index_faceswap[i])
        target_faces_final = int(target_index_faceswap[i])
        source_face = source_faces[source_faces_final]
        target_face = target_faces[target_faces_final]
        temp_frame = face_swapper.get(temp_frame, target_face, source_face, paste_back=True)
    
    temp_frame = Image.fromarray(cv2.cvtColor(temp_frame, cv2.COLOR_BGR2RGB))
    timestamp = time.time()
    savename = f"outputs/{timestamp}.png"
    
    if use_gfpgan_faceswap == True :
        temp_frame = image_gfpgan_mini(temp_frame)
    
    temp_frame.save(savename)
    final_image.append(temp_frame)
    
    del source_img, target_img, providers, face_analyser, face_swapper, target_analyze, target_faces, source_analyze, source_faces, temp_frame    
    clean_ram()
    
    return final_image, final_image 

ressources/faceswapper.py
Woolverine94/biniou
# https://github.com/Woolverine94/biniou
# gfpgan.py
import gradio as gr
import os
import cv2
import PIL
import time
import torch
import numpy as np
from gfpgan.utils import GFPGANer
from ressources.common import *
from huggingface_hub import snapshot_download, hf_hub_download

device_gfpgan = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

model_path_gfpgan = "./models/gfpgan/"
os.makedirs(model_path_gfpgan, exist_ok=True)

model_list_gfpgan = [
    "leonelhs/gfpgan",
]

variant_list_gfpgan = [
    "GFPGANv1.pth",
    "GFPGANCleanv1-NoCE-C2.pth",    
    "GFPGANv1.2.pth",
    "GFPGANv1.3.pth",
    "GFPGANv1.4.pth",
#    "RestoreFormer.pth",
]

def image_gfpgan(modelid_gfpgan, variantid_gfpgan, img_gfpgan, progress_gfpgan=gr.Progress(track_tqdm=True)):
    path_gfpgan = os.path.join(model_path_gfpgan, variantid_gfpgan)
    device = torch.device(device_gfpgan)
    
    if os.path.exists(path_gfpgan) == False :
        snapshot_path_gfpgan = snapshot_download(
            repo_id=modelid_gfpgan, 
            local_dir=model_path_gfpgan, 
            local_dir_use_symlinks=False,
            resume_download=True,
#            local_files_only=True if offline_test() else None
            )
        path_gfpgan = os.path.join(snapshot_path_gfpgan, variantid_gfpgan)    
    
    model_gfpgan = GFPGANer(
        model_path=path_gfpgan, 
        upscale=1, 
        arch='clean', 
        channel_multiplier=2
    )
    
    image_inter_gfpgan = np.array(Image.open(img_gfpgan).convert('RGB'))
    image_input_gfpgan = cv2.cvtColor(image_inter_gfpgan, cv2.COLOR_RGB2BGR)
    
    _, _, image_gfpgan = model_gfpgan.enhance(
        image_input_gfpgan, 
        has_aligned=False, 
        only_center_face=False, 
        paste_back=True
    )
    
    final_image = []
    timestamp = time.time()
    savename = f"outputs/{timestamp}.png"
    image_gfpgan = cv2.cvtColor(image_gfpgan, cv2.COLOR_BGR2RGB)
    image_gfpgan_save = Image.fromarray(image_gfpgan)
    image_gfpgan_save.save(savename)
    final_image.append(image_gfpgan_save)

    del model_gfpgan, image_inter_gfpgan, image_input_gfpgan, image_gfpgan
    clean_ram()
    return final_image, final_image

def image_gfpgan_mini(img_gfpgan):
    modelid_gfpgan = model_list_gfpgan[0]
    variantid_gfpgan = variant_list_gfpgan[4]
    path_gfpgan = os.path.join(model_path_gfpgan, variantid_gfpgan)
    device = torch.device(device_gfpgan)
    
    if os.path.exists(path_gfpgan) == False :
        snapshot_path_gfpgan = snapshot_download(
            repo_id=modelid_gfpgan, 
            local_dir=model_path_gfpgan, 
            local_dir_use_symlinks=False,
            resume_download=True,
#            local_files_only=True if offline_test() else None            
        )
        path_gfpgan = os.path.join(snapshot_path_gfpgan, variantid_gfpgan)
        
    model_gfpgan = GFPGANer(
        model_path=path_gfpgan, 
        upscale=1, 
        arch='clean', 
        channel_multiplier=2
    )
    
    if isinstance(img_gfpgan, np.ndarray):
        image_inter_gfpgan = img_gfpgan
    else :        
        image_inter_gfpgan = np.array(img_gfpgan.convert('RGB'))
    
    image_input_gfpgan = cv2.cvtColor(image_inter_gfpgan, cv2.COLOR_RGB2BGR)
    _, _, image_gfpgan = model_gfpgan.enhance(
        image_input_gfpgan, 
        has_aligned=False, 
        only_center_face=False, 
        paste_back=True
    )
    
    image_gfpgan = cv2.cvtColor(image_gfpgan, cv2.COLOR_BGR2RGB)    
    image_gfpgan_output = Image.fromarray(image_gfpgan)

    del model_gfpgan, image_inter_gfpgan, image_input_gfpgan, image_gfpgan
    clean_ram()
        
    return image_gfpgan_output

ressources/gfpgan.py
Woolverine94/biniou
# https://github.com/Woolverine94/biniou
# Harmonai.py
import gradio as gr
import os
from diffusers import DiffusionPipeline
import scipy.io.wavfile
import time
import torch
import random
from ressources.common import *

device_harmonai = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

model_path_harmonai = "./models/harmonai/"
os.makedirs(model_path_harmonai, exist_ok=True)

model_list_harmonai = []

for filename in os.listdir(model_path_harmonai):
    f = os.path.join(model_path_harmonai, filename)
    if os.path.isfile(f) and (filename.endswith('.ckpt') or filename.endswith('.safetensors') or filename.endswith('.bin')):
        model_list_harmonai.append(f)

model_list_harmonai_builtin = [
    "harmonai/glitch-440k",
    "harmonai/honk-140k",
    "harmonai/jmann-small-190k",
    "harmonai/jmann-large-580k",
    "harmonai/maestro-150k",
    "harmonai/unlocked-250k",
]

for k in range(len(model_list_harmonai_builtin)):
    model_list_harmonai.append(model_list_harmonai_builtin[k])

def music_harmonai(
    length_harmonai, 
    model_harmonai, 
    steps_harmonai, 
    seed_harmonai, 
    batch_size_harmonai, 
    batch_repeat_harmonai, 
    progress_harmonai=gr.Progress(track_tqdm=True)
    ):
        
    if model_harmonai[0:9] == "./models/" :
        pipe_harmonai = DiffusionPipeline.from_single_file(model_harmonai, torch_dtype=torch.float32)
    else : 
        pipe_harmonai = DiffusionPipeline.from_pretrained(
            model_harmonai, 
            cache_dir=model_path_harmonai, 
            torch_dtype=torch.float32, 
            resume_download=True,
            local_files_only=True if offline_test() else None
            )
    pipe_harmonai = pipe_harmonai.to(device_harmonai)

    if seed_harmonai == 0:
        random_seed = torch.randint(0, 10000000000, (1,))
        generator = torch.manual_seed(random_seed)
    else:
        generator = torch.manual_seed(seed_harmonai)

    for i in range (batch_repeat_harmonai):
        audios = pipe_harmonai(
            audio_length_in_s=length_harmonai,
            num_inference_steps=steps_harmonai,
            generator=generator,
            batch_size=batch_size_harmonai,
        ).audios

        for j, audio in enumerate(audios):
            timestamp = time.time()
            savename = f"outputs/{timestamp}_{j}.wav"
            scipy.io.wavfile.write(savename, pipe_harmonai.unet.config.sample_rate, audio.transpose())

    del pipe_harmonai, generator, audios
    clean_ram()

    return savename




ressources/harmonai.py
Woolverine94/biniou
# https://github.com/Woolverine94/biniou
# img2img.py
import gradio as gr
import os
import PIL
import torch
from diffusers import StableDiffusionImg2ImgPipeline, StableDiffusionXLImg2ImgPipeline
from compel import Compel, ReturnedEmbeddingsType
import time
import random
from ressources.scheduler import *
from ressources.common import *
from ressources.gfpgan import *
import tomesd

device_img2img = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

# Gestion des modèles
model_path_img2img = "./models/Stable_Diffusion/"
os.makedirs(model_path_img2img, exist_ok=True)

model_list_img2img = []

for filename in os.listdir(model_path_img2img):
    f = os.path.join(model_path_img2img, filename)
    if os.path.isfile(f) and (filename.endswith('.ckpt') or filename.endswith('.safetensors')):
        model_list_img2img.append(f)

model_list_img2img_builtin = [
    "SG161222/Realistic_Vision_V3.0_VAE",
#    "ckpt/anything-v4.5-vae-swapped",
    "stabilityai/stable-diffusion-xl-refiner-1.0",
    "runwayml/stable-diffusion-v1-5",
    "nitrosocke/Ghibli-Diffusion",     
]

for k in range(len(model_list_img2img_builtin)):
    model_list_img2img.append(model_list_img2img_builtin[k])

# Bouton Cancel
stop_img2img = False

def initiate_stop_img2img() :
    global stop_img2img
    stop_img2img = True

def check_img2img(step, timestep, latents) : 
    global stop_img2img
    if stop_img2img == False :
        return
    elif stop_img2img == True :
        stop_img2img = False
        try:
            del ressources.img2img.pipe_img2img
        except NameError as e:
            raise Exception("Interrupting ...")
    return

def image_img2img(
    modelid_img2img, 
    sampler_img2img, 
    img_img2img, 
    prompt_img2img, 
    negative_prompt_img2img, 
    num_images_per_prompt_img2img, 
    num_prompt_img2img, 
    guidance_scale_img2img, 
    denoising_strength_img2img, 
    num_inference_step_img2img, 
    height_img2img, 
    width_img2img, 
    seed_img2img, 
    source_type_img2img, 
    use_gfpgan_img2img, 
    nsfw_filter, 
    tkme_img2img,    
    progress_img2img=gr.Progress(track_tqdm=True)
    ):

    nsfw_filter_final, feat_ex = safety_checker_sd(model_path_img2img, device_img2img, nsfw_filter)

    if ('xl' or 'XL' or 'Xl' or 'xL') in modelid_img2img :
        is_xl_img2img: bool = True
    else :        
        is_xl_img2img: bool = False        

    if (is_xl_img2img == True) :
        if modelid_img2img[0:9] == "./models/" :
            pipe_img2img = StableDiffusionXLImg2ImgPipeline.from_single_file(
                modelid_img2img, 
                torch_dtype=torch.float32, 
                use_safetensors=True, 
                safety_checker=nsfw_filter_final, 
                feature_extractor=feat_ex,
            )
        else :        
            pipe_img2img = StableDiffusionXLImg2ImgPipeline.from_pretrained(
                modelid_img2img, 
                cache_dir=model_path_img2img, 
                torch_dtype=torch.float32, 
                use_safetensors=True, 
                safety_checker=nsfw_filter_final, 
                feature_extractor=feat_ex,
                resume_download=True,
                local_files_only=True if offline_test() else None                
            )
    else :
        if modelid_img2img[0:9] == "./models/" :
            pipe_img2img = StableDiffusionImg2ImgPipeline.from_single_file(
                modelid_img2img, 
                torch_dtype=torch.float32, 
                use_safetensors=True, 
                safety_checker=nsfw_filter_final, 
                feature_extractor=feat_ex,
            )
        else :        
            pipe_img2img = StableDiffusionImg2ImgPipeline.from_pretrained(
                modelid_img2img, 
                cache_dir=model_path_img2img, 
                torch_dtype=torch.float32, 
                use_safetensors=True, 
                safety_checker=nsfw_filter_final, 
                feature_extractor=feat_ex,
                resume_download=True,
                local_files_only=True if offline_test() else None                
            )

    pipe_img2img = get_scheduler(pipe=pipe_img2img, scheduler=sampler_img2img)
    pipe_img2img = pipe_img2img.to(device_img2img)
    pipe_img2img.enable_attention_slicing("max")  
    tomesd.apply_patch(pipe_img2img, ratio=tkme_img2img)
    
    if seed_img2img == 0:
        random_seed = torch.randint(0, 10000000000, (1,))
        generator = torch.manual_seed(random_seed)
    else:
        generator = torch.manual_seed(seed_img2img)

    if source_type_img2img == "sketch" :
        dim_size=[512, 512]
    else :        
        dim_size = correct_size(width_img2img, height_img2img, 512)
    image_input = PIL.Image.open(img_img2img)
    image_input = image_input.convert("RGB")
    image_input = image_input.resize((dim_size[0], dim_size[1]))

    prompt_img2img = str(prompt_img2img)
    negative_prompt_img2img = str(negative_prompt_img2img)
    if prompt_img2img == "None":
        prompt_img2img = ""
    if negative_prompt_img2img == "None":
        negative_prompt_img2img = ""

    if (is_xl_img2img == True) :
        compel = Compel(
            tokenizer=pipe_img2img.tokenizer_2, 
            text_encoder=pipe_img2img.text_encoder_2, 
            returned_embeddings_type=ReturnedEmbeddingsType.PENULTIMATE_HIDDEN_STATES_NON_NORMALIZED, 
            requires_pooled=[False, True], 
        )
        conditioning, pooled = compel(prompt_img2img)
        neg_conditioning, neg_pooled = compel(negative_prompt_img2img)
        [conditioning, neg_conditioning] = compel.pad_conditioning_tensors_to_same_length([conditioning, neg_conditioning])
    else :
        compel = Compel(tokenizer=pipe_img2img.tokenizer, text_encoder=pipe_img2img.text_encoder, truncate_long_prompts=False)
        conditioning = compel.build_conditioning_tensor(prompt_img2img)
        neg_conditioning = compel.build_conditioning_tensor(negative_prompt_img2img)
        [conditioning, neg_conditioning] = compel.pad_conditioning_tensors_to_same_length([conditioning, neg_conditioning])
    
    final_image = []
    
    for i in range (num_prompt_img2img):
        if (is_xl_img2img == True) :
            image = pipe_img2img(        
                image=image_input,
                prompt_embeds=conditioning, 
                pooled_prompt_embeds=pooled, 
                negative_prompt_embeds=neg_conditioning,
                negative_pooled_prompt_embeds=neg_pooled,
                num_images_per_prompt=num_images_per_prompt_img2img,
                guidance_scale=guidance_scale_img2img,
                strength=denoising_strength_img2img,
                num_inference_steps=num_inference_step_img2img,
                generator = generator,
                callback = check_img2img,                
            ).images
        else : 
            image = pipe_img2img(        
                image=image_input,
                prompt_embeds=conditioning,
                negative_prompt_embeds=neg_conditioning,
                num_images_per_prompt=num_images_per_prompt_img2img,
                guidance_scale=guidance_scale_img2img,
                strength=denoising_strength_img2img,
                num_inference_steps=num_inference_step_img2img,
                generator = generator,
                callback = check_img2img,                
            ).images        

        for j in range(len(image)):
            timestamp = time.time()
            savename = f"outputs/{timestamp}.png"
            if use_gfpgan_img2img == True :
                image[j] = image_gfpgan_mini(image[j])             
            image[j].save(savename)
            final_image.append(image[j])

    if source_type_img2img == "sketch" :
        final_image.append(image_input)

    del nsfw_filter_final, feat_ex, pipe_img2img, generator, image_input, compel, conditioning, neg_conditioning, image
    clean_ram()
   
    return final_image, final_image   


ressources/img2img.py
Woolverine94/biniou
# https://github.com/Woolverine94/biniou
# img2txt_git.py
import gradio as gr
import os
from transformers import AutoProcessor, AutoTokenizer, AutoImageProcessor, AutoModelForCausalLM, BlipForConditionalGeneration, VisionEncoderDecoderModel
import torch
import time
from ressources.common import *

device_img2txt_git = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

# Gestion des modèles
model_path_img2txt_git = "./models/GIT"
os.makedirs(model_path_img2txt_git, exist_ok=True)

model_list_img2txt_git = [
    "microsoft/git-large-coco",
]

def text_img2txt_git(
    modelid_img2txt_git, 
    max_tokens_img2txt_git, 
    min_tokens_img2txt_git, 
    num_beams_img2txt_git, 
    num_beam_groups_img2txt_git, 
    diversity_penalty_img2txt_git, 
    img_img2txt_git, 
    progress_img2txt_git=gr.Progress(track_tqdm=True)
    ):
    
    processor_img2txt_git = AutoProcessor.from_pretrained(modelid_img2txt_git, cache_dir=model_path_img2txt_git)    
    pipe_img2txt_git = AutoModelForCausalLM.from_pretrained(
        modelid_img2txt_git, 
        cache_dir=model_path_img2txt_git, 
        torch_dtype=torch.float32, 
        use_safetensors=True,
        resume_download=True,
        local_files_only=True if offline_test() else None
        )
    pipe_img2txt_git = pipe_img2txt_git.to(device_img2txt_git)
    inpipe_img2txt_git = processor_img2txt_git(images=img_img2txt_git, return_tensors="pt").to(device_img2txt_git)

    ids_img2txt_git = pipe_img2txt_git.generate(
        pixel_values=inpipe_img2txt_git.pixel_values, 
        max_new_tokens=max_tokens_img2txt_git, 
        min_new_tokens=min_tokens_img2txt_git, 
        early_stopping=False, 
        num_beams=num_beams_img2txt_git, 
        num_beam_groups=num_beam_groups_img2txt_git, 
        diversity_penalty=diversity_penalty_img2txt_git if num_beams_img2txt_git>1 else None
    )
    captions_img2txt_git = processor_img2txt_git.batch_decode(ids_img2txt_git, skip_special_tokens=True)[0]
    write_file(captions_img2txt_git)
    
    del processor_img2txt_git, pipe_img2txt_git, inpipe_img2txt_git, ids_img2txt_git
    clean_ram()
    
    return captions_img2txt_git

ressources/img2txt_git.py
Woolverine94/biniou
# https://github.com/Woolverine94/biniou
# img2var.py
import gradio as gr
import os
import PIL
import torch
from diffusers import StableDiffusionImageVariationPipeline
import time
import random
from ressources.scheduler import *
from ressources.common import *
from ressources.gfpgan import *
import tomesd

device_img2var = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

# Gestion des modèles
model_path_img2var = "./models/Stable_Diffusion/"
os.makedirs(model_path_img2var, exist_ok=True)

model_list_img2var = [
    "lambdalabs/sd-image-variations-diffusers",
]

# Bouton Cancel
stop_img2var = False

def initiate_stop_img2var() :
    global stop_img2var
    stop_img2var = True

def check_img2var(step, timestep, latents) : 
    global stop_img2var
    if stop_img2var == False :
        return
    elif stop_img2var == True :
        stop_img2var = False
        try:
            del ressources.img2var.pipe_img2var
        except NameError as e:
            raise Exception("Interrupting ...")
    return

def image_img2var(
    modelid_img2var, 
    sampler_img2var, 
    img_img2var, 
    num_images_per_prompt_img2var, 
    num_prompt_img2var, 
    guidance_scale_img2var, 
    num_inference_step_img2var, 
    height_img2var, 
    width_img2var, 
    seed_img2var, 
    use_gfpgan_img2var, 
    nsfw_filter, 
    tkme_img2var,    
    progress_img2var=gr.Progress(track_tqdm=True)
    ):

    nsfw_filter_final, feat_ex = safety_checker_sd(model_path_img2var, device_img2var, nsfw_filter)

    pipe_img2var = StableDiffusionImageVariationPipeline.from_pretrained(
        modelid_img2var,
        revision="v2.0", 
        cache_dir=model_path_img2var, 
        torch_dtype=torch.float32, 
        safety_checker=nsfw_filter_final,
        resume_download=True,
        local_files_only=True if offline_test() else None                
        )

    pipe_img2var = get_scheduler(pipe=pipe_img2var, scheduler=sampler_img2var)
    pipe_img2var = pipe_img2var.to(device_img2var)
    pipe_img2var.enable_attention_slicing("max")  
    tomesd.apply_patch(pipe_img2var, ratio=tkme_img2var)
    
    if seed_img2var == 0:
        random_seed = torch.randint(0, 10000000000, (1,))
        generator = torch.manual_seed(random_seed)
    else:
        generator = torch.manual_seed(seed_img2var)

    dim_size = correct_size(width_img2var, height_img2var, 512)
    image_input = PIL.Image.open(img_img2var)
    image_input = image_input.convert("RGB")
    image_input = image_input.resize((dim_size[0], dim_size[1]))
    
    final_image = []
    
    for i in range (num_prompt_img2var):
        image = pipe_img2var(        
            image=image_input,
            num_images_per_prompt=num_images_per_prompt_img2var,
            guidance_scale=guidance_scale_img2var,
            num_inference_steps=num_inference_step_img2var,
            width=dim_size[0],
            height=dim_size[1],             
            generator = generator,
            callback = check_img2var,                
        ).images

        for j in range(len(image)):
            timestamp = time.time()
            savename = f"outputs/{timestamp}.png"
            if use_gfpgan_img2var == True :
                image[j] = image_gfpgan_mini(image[j])             
            image[j].save(savename)
            final_image.append(image[j])

    del nsfw_filter_final, feat_ex, pipe_img2var, generator, image_input, image
    clean_ram()
   
    return final_image, final_image   


ressources/img2var.py
Woolverine94/biniou
# https://github.com/Woolverine94/biniou
# inpaint.py
import gradio as gr
import os
import PIL
import torch
from diffusers import StableDiffusionInpaintPipeline
from compel import Compel
import time
import random
from ressources.scheduler import *
from ressources.common import *
from ressources.gfpgan import *
import tomesd

device_inpaint = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

# Gestion des modèles
model_path_inpaint = "./models/inpaint/"
model_path_safety_checker = "./models/Stable_Diffusion/"
os.makedirs(model_path_inpaint, exist_ok=True)
os.makedirs(model_path_safety_checker, exist_ok=True)
model_list_inpaint = []

for filename in os.listdir(model_path_inpaint):
    f = os.path.join(model_path_inpaint, filename)
    if os.path.isfile(f) and (filename.endswith('.ckpt') or filename.endswith('.safetensors')):
        model_list_inpaint.append(f)

model_list_inpaint_builtin = [
    "Uminosachi/realisticVisionV30_v30VAE-inpainting",
    "runwayml/stable-diffusion-inpainting",
]

for k in range(len(model_list_inpaint_builtin)):
    model_list_inpaint.append(model_list_inpaint_builtin[k])

# Bouton Cancel
stop_inpaint = False

def initiate_stop_inpaint() :
    global stop_inpaint
    stop_inpaint = True

def check_inpaint(step, timestep, latents) : 
    global stop_inpaint
    if stop_inpaint == False :
        return
    elif stop_inpaint == True :
        stop_inpaint = False
        try:
            del ressources.inpaint.pipe_inpaint
        except NameError as e:
            raise Exception("Interrupting ...")
    return

def image_inpaint(
    modelid_inpaint, 
    sampler_inpaint, 
    img_inpaint, 
    rotation_img_inpaint, 
    prompt_inpaint, 
    negative_prompt_inpaint, 
    num_images_per_prompt_inpaint, 
    num_prompt_inpaint, 
    guidance_scale_inpaint,
    denoising_strength_inpaint, 
    num_inference_step_inpaint, 
    height_inpaint, 
    width_inpaint, 
    seed_inpaint, 
    use_gfpgan_inpaint, 
    nsfw_filter, 
    tkme_inpaint,
    progress_inpaint=gr.Progress(track_tqdm=True)
    ):
    
    nsfw_filter_final, feat_ex = safety_checker_sd(model_path_safety_checker, device_inpaint, nsfw_filter)

    if modelid_inpaint[0:9] == "./models/" :
        pipe_inpaint = StableDiffusionInpaintPipeline.from_single_file(
            modelid_inpaint, 
            torch_dtype=torch.float32, 
            use_safetensors=True, 
            safety_checker=nsfw_filter_final, 
            feature_extractor=feat_ex
        )
    else :        
        pipe_inpaint = StableDiffusionInpaintPipeline.from_pretrained(
            modelid_inpaint, 
            cache_dir=model_path_inpaint, 
            torch_dtype=torch.float32, 
            use_safetensors=True, 
            safety_checker=nsfw_filter_final, 
            feature_extractor=feat_ex,
            resume_download=True,
            local_files_only=True if offline_test() else None
        )

    pipe_inpaint = get_scheduler(pipe=pipe_inpaint, scheduler=sampler_inpaint)
    pipe_inpaint = pipe_inpaint.to(device_inpaint)
    pipe_inpaint.enable_attention_slicing("max")
    tomesd.apply_patch(pipe_inpaint, ratio=tkme_inpaint)
    
    if seed_inpaint == 0:
        random_seed = torch.randint(0, 10000000000, (1,))
        generator = torch.manual_seed(random_seed)
    else:
        generator = torch.manual_seed(seed_inpaint)

    angle_inpaint = 360 - rotation_img_inpaint   
    img_inpaint["image"] = img_inpaint["image"].rotate(angle_inpaint, expand=True)
    dim_size = correct_size(width_inpaint, height_inpaint, 512)
    image_input = img_inpaint["image"].convert("RGB")
    mask_image_input = img_inpaint["mask"].convert("RGB")
    image_input = image_input.resize((dim_size[0],dim_size[1]))
    mask_image_input = mask_image_input.resize((dim_size[0],dim_size[1]))    
    savename = f"outputs/mask.png"
    mask_image_input.save(savename)    
    
    prompt_inpaint = str(prompt_inpaint)
    negative_prompt_inpaint = str(negative_prompt_inpaint)
    if prompt_inpaint == "None":
        prompt_inpaint = ""
    if negative_prompt_inpaint == "None":
        negative_prompt_inpaint = ""

    compel = Compel(tokenizer=pipe_inpaint.tokenizer, text_encoder=pipe_inpaint.text_encoder, truncate_long_prompts=False)
    conditioning = compel.build_conditioning_tensor(prompt_inpaint)
    neg_conditioning = compel.build_conditioning_tensor(negative_prompt_inpaint)
    [conditioning, neg_conditioning] = compel.pad_conditioning_tensors_to_same_length([conditioning, neg_conditioning])
    
    final_image = []
    
    for i in range (num_prompt_inpaint):
        image = pipe_inpaint(
            image=image_input,
            mask_image=mask_image_input,            
            prompt_embeds=conditioning,
            negative_prompt_embeds=neg_conditioning,
            num_images_per_prompt=num_images_per_prompt_inpaint,
            guidance_scale=guidance_scale_inpaint,
            strength=denoising_strength_inpaint,
            width=dim_size[0],
            height=dim_size[1],
            num_inference_steps=num_inference_step_inpaint,
            generator = generator,
            callback = check_inpaint,              
        ).images

        for j in range(len(image)):
            timestamp = time.time()
            savename = f"outputs/{timestamp}.png"
            if use_gfpgan_inpaint == True :
                image[j] = image_gfpgan_mini(image[j])
            image[j].save(savename)
            final_image.append(image[j])

    final_image.append(mask_image_input)

    del nsfw_filter_final, feat_ex, pipe_inpaint, generator, image_input, mask_image_input, image
    clean_ram()

    return final_image, final_image

ressources/inpaint.py
Woolverine94/biniou
# https://github.com/Woolverine94/biniou
# llamacpp.py
from llama_cpp import Llama
import gradio as gr
import os
from huggingface_hub import snapshot_download, hf_hub_download
from ressources.common import *

# Gestion des modèles
model_path_llamacpp = "./models/llamacpp/"
os.makedirs(model_path_llamacpp, exist_ok=True)

model_list_llamacpp = {}

for filename in os.listdir(model_path_llamacpp):
    f = os.path.join(model_path_llamacpp, filename)
    if os.path.isfile(f) and filename.endswith('.gguf') :
        model_list_llamacpp.update(f)

model_list_llamacpp_builtin = {
    "TheBloke/CollectiveCognition-v1.1-Mistral-7B-GGUF": "collectivecognition-v1.1-mistral-7b.Q5_K_S.gguf", 
    "TheBloke/Airoboros-L2-13B-2.1-GGUF": "airoboros-l2-13b-2.1.Q5_K_S.gguf", 
    "TheBloke/Airoboros-L2-7B-2.1-GGUF": "airoboros-l2-7b-2.1.Q5_K_S.gguf", 
    "TheBloke/Vigogne-2-13B-Instruct-GGUF": "vigogne-2-13b-instruct.Q5_K_S.gguf", 
    "TheBloke/Vigogne-2-7B-Instruct-GGUF": "vigogne-2-7b-instruct.Q5_K_S.gguf", 
    "TheBloke/CodeLlama-13B-Instruct-GGUF": "codellama-13b-instruct.Q5_K_S.gguf", 
}

model_list_llamacpp.update(model_list_llamacpp_builtin)

def download_model(modelid_llamacpp):
    if modelid_llamacpp[0:9] != "./models/":
        hf_hub_path_llamacpp = hf_hub_download(
            repo_id=modelid_llamacpp, 
            filename=model_list_llamacpp[modelid_llamacpp], 
            repo_type="model", 
            cache_dir=model_path_llamacpp, 
            resume_download=True,
            local_files_only=True if offline_test() else None
        )
        modelid_llamacpp = hf_hub_path_llamacpp
    return modelid_llamacpp        

def text_llamacpp(
    modelid_llamacpp, 
    max_tokens_llamacpp, 
    seed_llamacpp, 
    stream_llamacpp, 
    n_ctx_llamacpp, 
    repeat_penalty_llamacpp, 
    temperature_llamacpp, 
    top_p_llamacpp, 
    top_k_llamacpp, 
    prompt_llamacpp, 
    history_llamacpp, 
    progress_txt2vid_ze=gr.Progress(track_tqdm=True)
    ):

    modelid_llamacpp = download_model(modelid_llamacpp)

    if history_llamacpp != "" :
        prompt_final_llamacpp = f"{history_llamacpp}\n\n{prompt_llamacpp}"
    else :
        prompt_final_llamacpp = prompt_llamacpp

    llm = Llama(model_path=modelid_llamacpp,  seed=seed_llamacpp, n_ctx=n_ctx_llamacpp)
    output_llamacpp = llm(
        f"{prompt_final_llamacpp}\n", 
        max_tokens=max_tokens_llamacpp, 
        stream=stream_llamacpp, 
        repeat_penalty=repeat_penalty_llamacpp, 
        temperature=temperature_llamacpp, 
        top_p=top_p_llamacpp, 
        top_k=top_k_llamacpp, 
        echo=True
    )    
    
    answer_llamacpp = (output_llamacpp['choices'][0]['text'])
    last_answer_llamacpp = answer_llamacpp.replace(f"{prompt_final_llamacpp}\n", "")
    write_file(answer_llamacpp)
    
    del llm, output_llamacpp
    clean_ram()
    
    return last_answer_llamacpp, answer_llamacpp
    
def text_llamacpp_continue(
    modelid_llamacpp, 
    max_tokens_llamacpp, 
    seed_llamacpp, 
    stream_llamacpp, 
    n_ctx_llamacpp, 
    repeat_penalty_llamacpp, 
    temperature_llamacpp, 
    top_p_llamacpp, 
    top_k_llamacpp, 
    history_llamacpp, 
    recent_history_llamacpp
    ):

    modelid_llamacpp = download_model(modelid_llamacpp)

    llm = Llama(model_path=modelid_llamacpp,  seed=seed_llamacpp, n_ctx=n_ctx_llamacpp)
    output_llamacpp = llm.create_completion(
        f"{recent_history_llamacpp}", 
        max_tokens=max_tokens_llamacpp, 
        stream=stream_llamacpp, 
        repeat_penalty=repeat_penalty_llamacpp, 
        temperature=temperature_llamacpp, 
        top_p=top_p_llamacpp, 
        top_k=top_k_llamacpp, 
    )    
    
    answer_llamacpp = (output_llamacpp['choices'][0]['text'])
    last_answer_llamacpp = f"{recent_history_llamacpp}{answer_llamacpp}"
    global_answer_llamacpp = f"{history_llamacpp}{answer_llamacpp}"
    write_file(global_answer_llamacpp)

    del llm, output_llamacpp
    clean_ram()
        
    return last_answer_llamacpp, global_answer_llamacpp

ressources/llamacpp.py
Woolverine94/biniou
# https://github.com/Woolverine94/biniou
# Musicgen.py
import os
import gradio as gr
import torch
import torchaudio
from audiocraft.models import MusicGen
from audiocraft.data.audio import audio_write
import time
import random
from ressources.common import *

device_musicgen = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

model_path_musicgen = "./models/Audiocraft/"
os.makedirs(model_path_musicgen, exist_ok=True)

modellist_musicgen = [
    "facebook/musicgen-small",
    "facebook/musicgen-medium",
    "facebook/musicgen-large",
    "facebook/musicgen-melody",    
]

# Bouton Cancel
stop_musicgen = False

def initiate_stop_musicgen() :
    global stop_musicgen
    stop_musicgen = True

def check_musicgen(generated_tokens, total_tokens) : 
    global stop_musicgen
    if stop_musicgen == False :
        return
    elif stop_musicgen == True :
        stop_musicgen = False
        try:
            del ressources.musicgen.pipe_musicgen
        except NameError as e:
            raise Exception("Interrupting ...")
    return

def music_musicgen(
    prompt_musicgen, 
    model_musicgen, 
    duration_musicgen, 
    num_batch_musicgen, 
    temperature_musicgen, 
    top_k_musicgen, 
    top_p_musicgen, 
    use_sampling_musicgen, 
    cfg_coef_musicgen, 
    progress_musicgen=gr.Progress(track_tqdm=True)
    ):

    pipe_musicgen = MusicGen.get_pretrained(model_musicgen, device=device_musicgen)
    pipe_musicgen.set_generation_params(
        duration=duration_musicgen, 
        use_sampling=use_sampling_musicgen, 
        temperature=temperature_musicgen, 
        top_k=top_k_musicgen, 
        top_p=top_p_musicgen, 
        cfg_coef=cfg_coef_musicgen
    )
    pipe_musicgen.set_custom_progress_callback(check_musicgen)
    prompt_musicgen_final = [f"{prompt_musicgen}"]
    for i in range (num_batch_musicgen):
        wav = pipe_musicgen.generate(prompt_musicgen_final, progress=True)
        for idx, one_wav in enumerate(wav):
            timestamp = time.time()
            savename = f"outputs/{timestamp}_{idx}"
            savename_final = savename+ ".wav" 
            audio_write(savename, one_wav.cpu(), pipe_musicgen.sample_rate, strategy="loudness", loudness_compressor=True)
            
    del pipe_musicgen
    clean_ram()      
            
    return savename_final           

ressources/musicgen.py
Woolverine94/biniou
# https://github.com/Woolverine94/biniou
# nllb.py
import gradio as gr
import os
import time
from huggingface_hub import snapshot_download, hf_hub_download
from transformers import NllbTokenizer, AutoModelForSeq2SeqLM, AutoTokenizer
from ressources.common import *

device_nllb = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

model_path_nllb = "./models/nllb/"
os.makedirs(model_path_nllb, exist_ok=True)

model_list_nllb = []

for filename in os.listdir(model_path_nllb):
    f = os.path.join(model_path_nllb, filename)
    if os.path.isfile(f) and (filename.endswith('.ckpt') or filename.endswith('.bin')):
        model_list_nllb.append(f)

model_list_nllb_builtin = [
    "facebook/nllb-200-distilled-600M",
]

for k in range(len(model_list_nllb_builtin)):
    model_list_nllb.append(model_list_nllb_builtin[k])

# Liste des langues
language_list_nllb = {
    "Acehnese (Arabic script) 	 	    ": "ace_Arab",
    "Acehnese (Latin script) 	 	 	": "ace_Latn",
    "Mesopotamian Arabic 	 	 	 	": "acm_Arab",
    "Ta’izzi-Adeni Arabic 	 	 	 	": "acq_Arab",
    "Tunisian Arabic 	 	 	 	 	": "aeb_Arab",
    "Afrikaans 	 	 	 	 	 	 	": "afr_Latn",
    "South Levantine Arabic 	 	 	": "ajp_Arab",
    "Akan 	 	 	 	 	 	 	 	": "aka_Latn",
    "Amharic 	 	 	 	 	 	 	": "amh_Ethi",
    "North Levantine Arabic 	 	 	": "apc_Arab",
    "Modern Standard Arabic 	 	 	": "arb_Arab",
    "Modern Standard Arabic (Romanized) ": "arb_Latn",
    "Najdi Arabic 	 	 	 	 	 	": "ars_Arab",
    "Moroccan Arabic 	 	 	 	 	": "ary_Arab",
    "Egyptian Arabic 	 	 	 	 	": "arz_Arab",
    "Assamese 	 	 	 	 	 	 	": "asm_Beng",
    "Asturian 	 	 	 	 	 	 	": "ast_Latn",
    "Awadhi 	 	 	 	 	 	 	": "awa_Deva",
    "Central Aymara 	 	 	 	 	": "ayr_Latn",
    "South Azerbaijani 	 	 	 	 	": "azb_Arab",
    "North Azerbaijani 	 	 	 	 	": "azj_Latn",
    "Bashkir 	 	 	 	 	 	 	": "bak_Cyrl",
    "Bambara 	 	 	 	 	 	 	": "bam_Latn",
    "Balinese 	 	 	 	 	 	 	": "ban_Latn",
    "Belarusian 	 	 	 	 	 	": "bel_Cyrl",
    "Bemba 	 	 	 	 	 	 	 	": "bem_Latn",
    "Bengali 	 	 	 	 	 	 	": "ben_Beng",
    "Bhojpuri 	 	 	 	 	 	 	": "bho_Deva",
    "Banjar (Arabic script) 	 	 	": "bjn_Arab",
    "Banjar (Latin script) 	 	 	 	": "bjn_Latn",
    "Standard Tibetan 	 	 	 	 	": "bod_Tibt",
    "Bosnian 	 	 	 	 	 	 	": "bos_Latn",
    "Buginese 	 	 	 	 	 	 	": "bug_Latn",
    "Bulgarian 	 	 	 	 	 	 	": "bul_Cyrl",
    "Catalan 	 	 	 	 	 	 	": "cat_Latn",
    "Cebuano 	 	 	 	 	 	 	": "ceb_Latn",
    "Czech 	 	 	 	 	 	 	 	": "ces_Latn",
    "Chokwe 	 	 	 	 	 	 	": "cjk_Latn",
    "Central Kurdish 	 	 	 	 	": "ckb_Arab",
    "Crimean Tatar 	 	 	 	 	 	": "crh_Latn",
    "Welsh 	 	 	 	 	 	 	 	": "cym_Latn",
    "Danish 	 	 	 	 	 	 	": "dan_Latn",
    "German 	 	 	 	 	 	 	": "deu_Latn",
    "Southwestern Dinka 	 	 	 	": "dik_Latn",
    "Dyula 	 	 	 	 	 	 	 	": "dyu_Latn",
    "Dzongkha 	 	 	 	 	 	 	": "dzo_Tibt",
    "Greek 	 	 	 	 	 	 	 	": "ell_Grek",
    "English 	 	 	 	 	 	 	": "eng_Latn",
    "Esperanto 	 	 	 	 	 	 	": "epo_Latn",
    "Estonian 	 	 	 	 	 	 	": "est_Latn",
    "Basque 	 	 	 	 	 	 	": "eus_Latn",
    "Ewe 	 	 	 	 	 	 	 	": "ewe_Latn",
    "Faroese 	 	 	 	 	 	 	": "fao_Latn",
    "Fijian 	 	 	 	 	 	 	": "fij_Latn",
    "Finnish 	 	 	 	 	 	 	": "fin_Latn",
    "Fon 	 	 	 	 	 	 	 	": "fon_Latn",
    "French 	 	 	 	 	 	 	": "fra_Latn",
    "Friulian 	 	 	 	 	 	 	": "fur_Latn",
    "Nigerian Fulfulde 	 	 	 	 	": "fuv_Latn",
    "Scottish Gaelic 	 	 	 	 	": "gla_Latn",
    "Irish 	 	 	 	 	 	 	 	": "gle_Latn",
    "Galician 	 	 	 	 	 	 	": "glg_Latn",
    "Guarani 	 	 	 	 	 	 	": "grn_Latn",
    "Gujarati 	 	 	 	 	 	 	": "guj_Gujr",
    "Haitian Creole 	 	 	 	 	": "hat_Latn",
    "Hausa 	 	 	 	 	 	 	 	": "hau_Latn",
    "Hebrew 	 	 	 	 	 	 	": "heb_Hebr",
    "Hindi 	 	 	 	 	 	 	 	": "hin_Deva",
    "Chhattisgarhi 	 	 	 	 	 	": "hne_Deva",
    "Croatian 	 	 	 	 	 	 	": "hrv_Latn",
    "Hungarian 	 	 	 	 	 	 	": "hun_Latn",
    "Armenian 	 	 	 	 	 	 	": "hye_Armn",
    "Igbo 	 	 	 	 	 	 	 	": "ibo_Latn",
    "Ilocano 	 	 	 	 	 	 	": "ilo_Latn",
    "Indonesian 	 	 	 	 	 	": "ind_Latn",
    "Icelandic 	 	 	 	 	 	 	": "isl_Latn",
    "Italian 	 	 	 	 	 	 	": "ita_Latn",
    "Javanese 	 	 	 	 	 	 	": "jav_Latn",
    "Japanese 	 	 	 	 	 	 	": "jpn_Jpan",
    "Kabyle 	 	 	 	 	 	 	": "kab_Latn",
    "Jingpho 	 	 	 	 	 	 	": "kac_Latn",
    "Kamba 	 	 	 	 	 	 	 	": "kam_Latn",
    "Kannada 	 	 	 	 	 	 	": "kan_Knda",
    "Kashmiri (Arabic script) 	 	 	": "kas_Arab",
    "Kashmiri (Devanagari script) 	 	": "kas_Deva",
    "Georgian 	 	 	 	 	 	 	": "kat_Geor",
    "Central Kanuri (Arabic script) 	": "knc_Arab",
    "Central Kanuri (Latin script) 	 	": "knc_Latn",
    "Kazakh 	 	 	 	 	 	 	": "kaz_Cyrl",
    "Kabiyè 	 	 	 	 	 	 	": "kbp_Latn",
    "Kabuverdianu 	 	 	 	 	 	": "kea_Latn",
    "Khmer 	 	 	 	 	 	 	 	": "khm_Khmr",
    "Kikuyu 	 	 	 	 	 	 	": "kik_Latn",
    "Kinyarwanda 	 	 	 	 	 	": "kin_Latn",
    "Kyrgyz 	 	 	 	 	 	 	": "kir_Cyrl",
    "Kimbundu 	 	 	 	 	 	 	": "kmb_Latn",
    "Northern Kurdish 	 	 	 	 	": "kmr_Latn",
    "Kikongo 	 	 	 	 	 	 	": "kon_Latn",
    "Korean 	 	 	 	 	 	 	": "kor_Hang",
    "Lao 	 	 	 	 	 	 	 	": "lao_Laoo",
    "Ligurian 	 	 	 	 	 	 	": "lij_Latn",
    "Limburgish 	 	 	 	 	 	": "lim_Latn",
    "Lingala 	 	 	 	 	 	 	": "lin_Latn",
    "Lithuanian 	 	 	 	 	 	": "lit_Latn",
    "Lombard 	 	 	 	 	 	 	": "lmo_Latn",
    "Latgalian 	 	 	 	 	 	 	": "ltg_Latn",
    "Luxembourgish 	 	 	 	 	 	": "ltz_Latn",
    "Luba-Kasai 	 	 	 	 	 	": "lua_Latn",
    "Ganda 	 	 	 	 	 	 	 	": "lug_Latn",
    "Luo 	 	 	 	 	 	 	 	": "luo_Latn",
    "Mizo 	 	 	 	 	 	 	 	": "lus_Latn",
    "Standard Latvian 	 	 	 	 	": "lvs_Latn",
    "Magahi 	 	 	 	 	 	 	": "mag_Deva",
    "Maithili 	 	 	 	 	 	 	": "mai_Deva",
    "Malayalam 	 	 	 	 	 	 	": "mal_Mlym",
    "Marathi 	 	 	 	 	 	 	": "mar_Deva",
    "Minangkabau (Arabic script) 	 	": "min_Arab",
    "Minangkabau (Latin script) 	 	": "min_Latn",
    "Macedonian 	 	 	 	 	 	": "mkd_Cyrl",
    "Plateau Malagasy 	 	 	 	 	": "plt_Latn",
    "Maltese 	 	 	 	 	 	 	": "mlt_Latn",
    "Meitei (Bengali script) 	 	 	": "mni_Beng",
    "Halh Mongolian 	 	 	 	 	": "khk_Cyrl",
    "Mossi 	 	 	 	 	 	 	 	": "mos_Latn",
    "Maori 	 	 	 	 	 	 	 	": "mri_Latn",
    "Burmese 	 	 	 	 	 	 	": "mya_Mymr",
    "Dutch 	 	 	 	 	 	 	 	": "nld_Latn",
    "Norwegian Nynorsk 	 	 	 	 	": "nno_Latn",
    "Norwegian Bokmål 	 	 	 	 	": "nob_Latn",
    "Nepali 	 	 	 	 	 	 	": "npi_Deva",
    "Northern Sotho 	 	 	 	 	": "nso_Latn",
    "Nuer 	 	 	 	 	 	 	 	": "nus_Latn",
    "Nyanja 	 	 	 	 	 	 	": "nya_Latn",
    "Occitan 	 	 	 	 	 	 	": "oci_Latn",
    "West Central Oromo 	 	 	 	": "gaz_Latn",
    "Odia 	 	 	 	 	 	 	 	": "ory_Orya",
    "Pangasinan 	 	 	 	 	 	": "pag_Latn",
    "Eastern Panjabi 	 	 	 	 	": "pan_Guru",
    "Papiamento 	 	 	 	 	 	": "pap_Latn",
    "Western Persian 	 	 	 	 	": "pes_Arab",
    "Polish 	 	 	 	 	 	 	": "pol_Latn",
    "Portuguese 	 	 	 	 	 	": "por_Latn",
    "Dari 	 	 	 	 	 	 	 	": "prs_Arab",
    "Southern Pashto 	 	 	 	 	": "pbt_Arab",
    "Ayacucho Quechua 	 	 	 	 	": "quy_Latn",
    "Romanian 	 	 	 	 	 	 	": "ron_Latn",
    "Rundi 	 	 	 	 	 	 	 	": "run_Latn",
    "Russian 	 	 	 	 	 	 	": "rus_Cyrl",
    "Sango 	 	 	 	 	 	 	 	": "sag_Latn",
    "Sanskrit 	 	 	 	 	 	 	": "san_Deva",
    "Santali 	 	 	 	 	 	 	": "sat_Olck",
    "Sicilian 	 	 	 	 	 	 	": "scn_Latn",
    "Shan 	 	 	 	 	 	 	 	": "shn_Mymr",
    "Sinhala 	 	 	 	 	 	 	": "sin_Sinh",
    "Slovak 	 	 	 	 	 	 	": "slk_Latn",
    "Slovenian 	 	 	 	 	 	 	": "slv_Latn",
    "Samoan 	 	 	 	 	 	 	": "smo_Latn",
    "Shona 	 	 	 	 	 	 	 	": "sna_Latn",
    "Sindhi 	 	 	 	 	 	 	": "snd_Arab",
    "Somali 	 	 	 	 	 	 	": "som_Latn",
    "Southern Sotho 	 	 	 	 	": "sot_Latn",
    "Spanish 	 	 	 	 	 	 	": "spa_Latn",
    "Tosk Albanian 	 	 	 	 	 	": "als_Latn",
    "Sardinian 	 	 	 	 	 	 	": "srd_Latn",
    "Serbian 	 	 	 	 	 	 	": "srp_Cyrl",
    "Swati 	 	 	 	 	 	 	 	": "ssw_Latn",
    "Sundanese 	 	 	 	 	 	 	": "sun_Latn",
    "Swedish 	 	 	 	 	 	 	": "swe_Latn",
    "Swahili 	 	 	 	 	 	 	": "swh_Latn",
    "Silesian 	 	 	 	 	 	 	": "szl_Latn",
    "Tamil 	 	 	 	 	 	 	 	": "tam_Taml",
    "Tatar 	 	 	 	 	 	 	 	": "tat_Cyrl",
    "Telugu 	 	 	 	 	 	 	": "tel_Telu",
    "Tajik 	 	 	 	 	 	 	 	": "tgk_Cyrl",
    "Tagalog 	 	 	 	 	 	 	": "tgl_Latn",
    "Thai 	 	 	 	 	 	 	 	": "tha_Thai",
    "Tigrinya 	 	 	 	 	 	 	": "tir_Ethi",
    "Tamasheq (Latin script) 	 	 	": "taq_Latn",
    "Tamasheq (Tifinagh script) 	 	": "taq_Tfng",
    "Tok Pisin 	 	 	 	 	 	 	": "tpi_Latn",
    "Tswana 	 	 	 	 	 	 	": "tsn_Latn",
    "Tsonga 	 	 	 	 	 	 	": "tso_Latn",
    "Turkmen 	 	 	 	 	 	 	": "tuk_Latn",
    "Tumbuka 	 	 	 	 	 	 	": "tum_Latn",
    "Turkish 	 	 	 	 	 	 	": "tur_Latn",
    "Twi 	 	 	 	 	 	 	 	": "twi_Latn",
    "Central Atlas Tamazight 	 	 	": "tzm_Tfng",
    "Uyghur 	 	 	 	 	 	 	": "uig_Arab",
    "Ukrainian 	 	 	 	 	 	 	": "ukr_Cyrl",
    "Umbundu 	 	 	 	 	 	 	": "umb_Latn",
    "Urdu 	 	 	 	 	 	 	 	": "urd_Arab",
    "Northern Uzbek 	 	 	 	 	": "uzn_Latn",
    "Venetian 	 	 	 	 	 	 	": "vec_Latn",
    "Vietnamese 	 	 	 	 	 	": "vie_Latn",
    "Waray 	 	 	 	 	 	 	 	": "war_Latn",
    "Wolof 	 	 	 	 	 	 	 	": "wol_Latn",
    "Xhosa 	 	 	 	 	 	 	 	": "xho_Latn",
    "Eastern Yiddish 	 	 	 	 	": "ydd_Hebr",
    "Yoruba 	 	 	 	 	 	 	": "yor_Latn",
    "Yue Chinese 	 	 	 	 	 	": "yue_Hant",
    "Chinese (Simplified) 	 	 	 	": "zho_Hans",
    "Chinese (Traditional) 	 	 	 	": "zho_Hant",
    "Standard Malay 	 	 	 	 	": "zsm_Latn",
    "Zulu 	 	 	 	 	 	 	 	": "zul_Latn",
}

# Bouton Cancel
stop_nllb = False

def initiate_stop_nllb() :
    global stop_nllb
    stop_nllb = True

def check_nllb(step, timestep, latents) :
    global stop_nllb
    if stop_nllb == False :
        return
    elif stop_nllb == True :
        stop_nllb = False
        try:
            del ressources.nllb.pipe_nllb
        except NameError as e:
            raise Exception("Interrupting ...")
    return

def text_nllb(
    modelid_nllb, 
    max_tokens_nllb, 
    source_language_nllb, 
    prompt_nllb, 
    output_language_nllb, 
    progress_nllb=gr.Progress(track_tqdm=True)
    ):
        
    source_language_nllb = language_list_nllb[source_language_nllb]
    output_language_nllb = language_list_nllb[output_language_nllb]
    model_nllb = snapshot_download(
        repo_id=modelid_nllb, 
        cache_dir=model_path_nllb, 
        resume_download=True,
        local_files_only=True if offline_test() else None
    )
    tokenizer_nllb = NllbTokenizer.from_pretrained(model_nllb, src_lang=source_language_nllb, tgt_lang=output_language_nllb)
    automodel_nllb = AutoModelForSeq2SeqLM.from_pretrained(model_nllb)
    inputs_nllb = tokenizer_nllb(prompt_nllb, return_tensors="pt").to(device_nllb)
    translated_tokens = automodel_nllb.generate(
        **inputs_nllb,
        forced_bos_token_id=tokenizer_nllb.lang_code_to_id[output_language_nllb],
        max_new_tokens=max_tokens_nllb, 
    )
    output_nllb = tokenizer_nllb.batch_decode(translated_tokens, skip_special_tokens=True)[0]
    write_file(output_nllb)
    
    del model_nllb, tokenizer_nllb, automodel_nllb, inputs_nllb, translated_tokens
    clean_ram()

    return output_nllb

ressources/nllb.py
Woolverine94/biniou
# https://github.com/Woolverine94/biniou
# pix2pix.py
import gradio as gr
import os
import PIL
import torch
from diffusers import StableDiffusionInstructPix2PixPipeline
from compel import Compel
import time
import random
from ressources.scheduler import *
from ressources.common import *
from ressources.gfpgan import *
import tomesd

device_pix2pix = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

# Gestion des modèles -> pas concerné (safetensors refusé)
model_path_pix2pix = "./models/pix2pix/"
model_path_safety_checker = "./models/Stable_Diffusion/"
os.makedirs(model_path_pix2pix, exist_ok=True)

model_list_pix2pix = []

for filename in os.listdir(model_path_pix2pix):
    f = os.path.join(model_path_pix2pix, filename)
    if os.path.isfile(f) and (filename.endswith('.ckpt') or filename.endswith('.safetensors')):
        model_list_pix2pix.append(f)

model_list_pix2pix_builtin = [
    "timbrooks/instruct-pix2pix",
]

for k in range(len(model_list_pix2pix_builtin)):
    model_list_pix2pix.append(model_list_pix2pix_builtin[k])

# Bouton Cancel
stop_pix2pix = False

def initiate_stop_pix2pix() :
    global stop_pix2pix
    stop_pix2pix = True

def check_pix2pix(step, timestep, latents) : 
    global stop_pix2pix
    if stop_pix2pix == False :
        return
    elif stop_pix2pix == True :
        stop_pix2pix = False
        try:
            del ressources.pix2pix.pipe_pix2pix
        except NameError as e:
            raise Exception("Interrupting ...")
    return

def image_pix2pix(
    modelid_pix2pix, 
    sampler_pix2pix, 
    img_pix2pix, 
    prompt_pix2pix, 
    negative_prompt_pix2pix, 
    num_images_per_prompt_pix2pix, 
    num_prompt_pix2pix, 
    guidance_scale_pix2pix, 
    image_guidance_scale_pix2pix, 
    num_inference_step_pix2pix, 
    height_pix2pix, 
    width_pix2pix, 
    seed_pix2pix, 
    use_gfpgan_pix2pix, 
    nsfw_filter, 
    tkme_pix2pix,
    progress_pix2pix=gr.Progress(track_tqdm=True)
    ):

    nsfw_filter_final, feat_ex = safety_checker_sd(model_path_safety_checker, device_pix2pix, nsfw_filter)
    
    pipe_pix2pix= StableDiffusionInstructPix2PixPipeline.from_pretrained(
        modelid_pix2pix, 
        cache_dir=model_path_pix2pix, 
        torch_dtype=torch.float32, 
        use_safetensors=True, 
        safety_checker=nsfw_filter_final, 
        feature_extractor=feat_ex,
        resume_download=True,
        local_files_only=True if offline_test() else None
    )
    
    pipe_pix2pix = get_scheduler(pipe=pipe_pix2pix, scheduler=sampler_pix2pix)
    pipe_pix2pix = pipe_pix2pix.to(device_pix2pix)
    pipe_pix2pix.enable_attention_slicing("max")
    tomesd.apply_patch(pipe_pix2pix, ratio=tkme_pix2pix)
    
    if seed_pix2pix == 0:
        random_seed = torch.randint(0, 10000000000, (1,))
        generator = torch.manual_seed(random_seed)
    else:
        generator = torch.manual_seed(seed_pix2pix)
        
    dim_size = correct_size(width_pix2pix, height_pix2pix, 512)
    image_input = PIL.Image.open(img_pix2pix)
    image_input = image_input.convert("RGB")
    image_input = image_input.resize((dim_size[0], dim_size[1]))
    
    prompt_pix2pix = str(prompt_pix2pix)
    negative_prompt_pix2pix = str(negative_prompt_pix2pix)
    if prompt_pix2pix == "None":
        prompt_pix2pix = ""
    if negative_prompt_pix2pix == "None":
        negative_prompt_pix2pix = ""

    compel = Compel(tokenizer=pipe_pix2pix.tokenizer, text_encoder=pipe_pix2pix.text_encoder, truncate_long_prompts=False)
    conditioning = compel.build_conditioning_tensor(prompt_pix2pix)
    neg_conditioning = compel.build_conditioning_tensor(negative_prompt_pix2pix)
    [conditioning, neg_conditioning] = compel.pad_conditioning_tensors_to_same_length([conditioning, neg_conditioning])

    final_image = []
    
    for i in range (num_prompt_pix2pix):
        image = pipe_pix2pix(
            image=image_input,
            prompt_embeds=conditioning,
            negative_prompt_embeds=neg_conditioning,
            num_images_per_prompt=num_images_per_prompt_pix2pix,
            guidance_scale=guidance_scale_pix2pix,
            image_guidance_scale=image_guidance_scale_pix2pix,
            num_inference_steps=num_inference_step_pix2pix,
            generator = generator,
            callback = check_pix2pix,             
        ).images

        for j in range(len(image)):
            timestamp = time.time()
            savename = f"outputs/{timestamp}.png"
            if use_gfpgan_pix2pix == True :
                image[j] = image_gfpgan_mini(image[j])             
            image[j].save(savename)
            final_image.append(image[j])
            
    del nsfw_filter_final, feat_ex, pipe_pix2pix, generator, image_input, image
    clean_ram()            

    return final_image, final_image


ressources/pix2pix.py
Woolverine94/biniou
# https://github.com/Woolverine94/biniou
# r_esrgan.py
import gradio as gr
import os
import PIL
import time
import torch
import numpy as np
from RealESRGAN import RealESRGAN
# from realesrgan import RealESRGANModel as RE
from ressources.common import *
from ressources.gfpgan import *

device_resrgan = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

model_path_resrgan = "./models/Real_ESRGAN/"
os.makedirs(model_path_resrgan, exist_ok=True)

model_list_resrgan = [
    "RealESRGAN_x2.pth",
    "RealESRGAN_x4.pth",
    "RealESRGAN_x8.pth",
]

def image_resrgan(
    modelid_resrgan, 
    scale_resrgan, 
    img_resrgan, 
    use_gfpgan_resrgan, 
    progress_resrgan=gr.Progress(track_tqdm=True)
    ):
        
    model_resrgan_path  = os.path.join(model_path_resrgan, modelid_resrgan)
    device = torch.device(device_resrgan)
    model_resrgan = RealESRGAN(device, scale=RESRGAN_SCALES[scale_resrgan])
    model_resrgan.load_weights(model_resrgan_path, download=True)
    image = Image.open(img_resrgan).convert('RGB')
    sr_image = model_resrgan.predict(image)
    final_image = [] 
    timestamp = time.time()
    savename = f"outputs/{timestamp}.png"
    
    if use_gfpgan_resrgan == True :
        sr_image = image_gfpgan_mini(sr_image)    
    sr_image.save(savename)
    final_image.append(sr_image)
    
    del model_resrgan, image, sr_image
    clean_ram()
    
    return final_image, final_image

ressources/r_esrgan.py
Woolverine94/biniou
# https://github.com/Woolverine94/biniou
#scheduler.py
from diffusers import (
    UniPCMultistepScheduler,
    DDIMScheduler,
    DDPMScheduler,
    PNDMScheduler,
    DEISMultistepScheduler,
    EulerDiscreteScheduler,
    EulerAncestralDiscreteScheduler,
    KDPM2DiscreteScheduler,
    KDPM2AncestralDiscreteScheduler,
    DPMSolverSinglestepScheduler,
    DPMSolverMultistepScheduler,
    HeunDiscreteScheduler,
    LMSDiscreteScheduler,
)

SCHEDULER_MAPPING = {
    "UniPC": UniPCMultistepScheduler,
    "DDIM": DDIMScheduler,
    "DDPM": DDPMScheduler,
    "PNDM": PNDMScheduler,
    "DEIS": DEISMultistepScheduler,
    "Euler": EulerDiscreteScheduler,
    "Euler a": EulerAncestralDiscreteScheduler,
    "DPM2": KDPM2DiscreteScheduler,
    "DPM2 a": KDPM2AncestralDiscreteScheduler,
    "DPM++ SDE": DPMSolverSinglestepScheduler,
    "DPM++ 2M": DPMSolverMultistepScheduler,
    "Heun":	HeunDiscreteScheduler,
    "LMS": LMSDiscreteScheduler,
}

def get_scheduler(pipe, scheduler):
    if scheduler in SCHEDULER_MAPPING:
        SchedulerClass = SCHEDULER_MAPPING[scheduler]
        pipe.scheduler = SchedulerClass.from_config(pipe.scheduler.config)
    else:
        raise ValueError(f"Invalid scheduler name {scheduler}")

    return pipe

ressources/scheduler.py
Woolverine94/biniou
# https://github.com/Woolverine94/biniou
# txt2img_kd.py
import gradio as gr
import os
from diffusers import AutoPipelineForText2Image
from compel import Compel
import torch
import time
import random
from ressources.scheduler import *
from ressources.gfpgan import *
import tomesd

device_txt2img_kd = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

# Gestion des modèles
model_path_txt2img_kd = "./models/Kandinsky/"
os.makedirs(model_path_txt2img_kd, exist_ok=True)

model_list_txt2img_kd = []

for filename in os.listdir(model_path_txt2img_kd):
    f = os.path.join(model_path_txt2img_kd, filename)
    if os.path.isfile(f) and (filename.endswith('.ckpt') or filename.endswith('.safetensors')):
        model_list_txt2img_kd.append(f)

model_list_txt2img_kd_builtin = [
    "kandinsky-community/kandinsky-2-2-decoder",
    "kandinsky-community/kandinsky-2-1",
]

for k in range(len(model_list_txt2img_kd_builtin)):
    model_list_txt2img_kd.append(model_list_txt2img_kd_builtin[k])

# Bouton Cancel
stop_txt2img_kd = False

def initiate_stop_txt2img_kd() :
    global stop_txt2img_kd
    stop_txt2img_kd = True

def check_txt2img_kd(step, timestep, latents) : 
    global stop_txt2img_kd
    if stop_txt2img_kd == False :
        return
    elif stop_txt2img_kd == True :
        stop_txt2img_kd = False
        try:
            del ressources.txt2img_kd.pipe_txt2img_kd
        except NameError as e:
            raise Exception("Interrupting ...")
    return

def image_txt2img_kd(
    modelid_txt2img_kd, 
    sampler_txt2img_kd, 
    prompt_txt2img_kd, 
    negative_prompt_txt2img_kd, 
    num_images_per_prompt_txt2img_kd, 
    num_prompt_txt2img_kd, 
    guidance_scale_txt2img_kd, 
    num_inference_step_txt2img_kd, 
    height_txt2img_kd, 
    width_txt2img_kd, 
    seed_txt2img_kd, 
    use_gfpgan_txt2img_kd, 
#    tkme_txt2img_kd,
    progress_txt2img_kd=gr.Progress(track_tqdm=True)
    ):
        
    if modelid_txt2img_kd[0:9] == "./models/" :
        pipe_txt2img_kd = AutoPipelineForText2Image.from_single_file(
            modelid_txt2img_kd, 
            torch_dtype=torch.float32, 
            use_safetensors=True,
        )
    else :        
        pipe_txt2img_kd = AutoPipelineForText2Image.from_pretrained(
            modelid_txt2img_kd, 
            cache_dir=model_path_txt2img_kd, 
            torch_dtype=torch.float32, 
            use_safetensors=True,
            resume_download=True,
            local_files_only=True if offline_test() else None
        )
        
    pipe_txt2img_kd = get_scheduler(pipe=pipe_txt2img_kd, scheduler=sampler_txt2img_kd)
    pipe_txt2img_kd = pipe_txt2img_kd.to(device_txt2img_kd)
    pipe_txt2img_kd.enable_attention_slicing("max")  
#    tomesd.apply_patch(pipe_txt2img_kd, ratio=tkme_txt2img_kd)

    if seed_txt2img_kd == 0:
        random_seed = torch.randint(0, 10000000000, (1,))
        generator = torch.manual_seed(random_seed)
    else:
        generator = torch.manual_seed(seed_txt2img_kd)

    final_image = []
    for i in range (num_prompt_txt2img_kd):
        image = pipe_txt2img_kd(
            prompt=prompt_txt2img_kd,
            negative_prompt=negative_prompt_txt2img_kd,
            height=height_txt2img_kd,
            width=width_txt2img_kd,
            num_inference_steps=num_inference_step_txt2img_kd,
            guidance_scale=guidance_scale_txt2img_kd,
            num_images_per_prompt=num_images_per_prompt_txt2img_kd,
            generator = generator,
            callback = check_txt2img_kd,            
        ).images

        for j in range(len(image)):
            timestamp = time.time()
            savename = f"outputs/{timestamp}.png"
            if use_gfpgan_txt2img_kd == True :
                image[j] = image_gfpgan_mini(image[j])            
            image[j].save(savename)
            final_image.append(image[j])
            
    del pipe_txt2img_kd, generator, image 
    clean_ram()            
    
    return final_image, final_image

ressources/txt2img_kd.py
Woolverine94/biniou
# https://github.com/Woolverine94/biniou
# txt2img_sd.py
import gradio as gr
import os
from diffusers import StableDiffusionPipeline, StableDiffusionXLPipeline
from compel import Compel, ReturnedEmbeddingsType
import torch
import time
import random
from ressources.scheduler import *
from ressources.gfpgan import *
import tomesd

device_txt2img_sd = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

# Gestion des modèles
model_path_txt2img_sd = "./models/Stable_Diffusion/"
os.makedirs(model_path_txt2img_sd, exist_ok=True)
model_list_txt2img_sd = []

for filename in os.listdir(model_path_txt2img_sd):
    f = os.path.join(model_path_txt2img_sd, filename)
    if os.path.isfile(f) and (filename.endswith('.ckpt') or filename.endswith('.safetensors')):
        model_list_txt2img_sd.append(f)

model_list_txt2img_sd_builtin = [
    "SG161222/Realistic_Vision_V3.0_VAE",
#    "ckpt/anything-v4.5-vae-swapped",
    "stabilityai/stable-diffusion-xl-base-1.0",
    "runwayml/stable-diffusion-v1-5",
    "nitrosocke/Ghibli-Diffusion", 
]

for k in range(len(model_list_txt2img_sd_builtin)):
    model_list_txt2img_sd.append(model_list_txt2img_sd_builtin[k])

# Bouton Cancel
stop_txt2img_sd = False

def initiate_stop_txt2img_sd() :
    global stop_txt2img_sd
    stop_txt2img_sd = True

def check_txt2img_sd(step, timestep, latents) :
    global stop_txt2img_sd
    if stop_txt2img_sd == False :
#        result_preview = preview_image(step, timestep, latents, pipe_txt2img_sd)
        return
    elif stop_txt2img_sd == True :
        stop_txt2img_sd = False
        try:
            del ressources.txt2img_sd.pipe_txt2img_sd
        except NameError as e:
            raise Exception("Interrupting ...")
    return

def image_txt2img_sd(modelid_txt2img_sd, 
    sampler_txt2img_sd, 
    prompt_txt2img_sd, 
    negative_prompt_txt2img_sd, 
    num_images_per_prompt_txt2img_sd, 
    num_prompt_txt2img_sd, 
    guidance_scale_txt2img_sd, 
    num_inference_step_txt2img_sd, 
    height_txt2img_sd, 
    width_txt2img_sd, 
    seed_txt2img_sd, 
    use_gfpgan_txt2img_sd, 
    nsfw_filter, 
    tkme_txt2img_sd,
    progress_txt2img_sd=gr.Progress(track_tqdm=True)
    ):
    
    global pipe_txt2img_sd
    nsfw_filter_final, feat_ex = safety_checker_sd(model_path_txt2img_sd, device_txt2img_sd, nsfw_filter)


    if ('xl' or 'XL' or 'Xl' or 'xL') in modelid_txt2img_sd :
        is_xl_txt2img_sd: bool = True
    else :        
        is_xl_txt2img_sd: bool = False

    if (is_xl_txt2img_sd == True) :
        if modelid_txt2img_sd[0:9] == "./models/" :
            pipe_txt2img_sd = StableDiffusionXLPipeline.from_single_file(
                modelid_txt2img_sd, 
                torch_dtype=torch.float32, 
                use_safetensors=True, 
                safety_checker=nsfw_filter_final, 
                feature_extractor=feat_ex,
            )
        else :        
            pipe_txt2img_sd = StableDiffusionXLPipeline.from_pretrained(
                modelid_txt2img_sd, 
                cache_dir=model_path_txt2img_sd, 
                torch_dtype=torch.float32, 
                use_safetensors=True, 
                safety_checker=nsfw_filter_final, 
                feature_extractor=feat_ex,
                resume_download=True,
                local_files_only=True if offline_test() else None
            )
    else :
        if modelid_txt2img_sd[0:9] == "./models/" :
            pipe_txt2img_sd = StableDiffusionPipeline.from_single_file(
                modelid_txt2img_sd, 
                torch_dtype=torch.float32, 
                use_safetensors=True, 
                safety_checker=nsfw_filter_final, 
                feature_extractor=feat_ex,
            )
        else :        
            pipe_txt2img_sd = StableDiffusionPipeline.from_pretrained(
                modelid_txt2img_sd, 
                cache_dir=model_path_txt2img_sd, 
                torch_dtype=torch.float32, 
                use_safetensors=True, 
                safety_checker=nsfw_filter_final, 
                feature_extractor=feat_ex,
                resume_download=True,
                local_files_only=True if offline_test() else None
            )

    pipe_txt2img_sd = get_scheduler(pipe=pipe_txt2img_sd, scheduler=sampler_txt2img_sd)
    pipe_txt2img_sd = pipe_txt2img_sd.to(device_txt2img_sd)
    pipe_txt2img_sd.enable_attention_slicing("max")
    tomesd.apply_patch(pipe_txt2img_sd, ratio=tkme_txt2img_sd)
    
    if seed_txt2img_sd == 0:
        random_seed = torch.randint(0, 10000000000, (1,))
        generator = torch.manual_seed(random_seed)
    else:
        generator = torch.manual_seed(seed_txt2img_sd)

    prompt_txt2img_sd = str(prompt_txt2img_sd)
    negative_prompt_txt2img_sd = str(negative_prompt_txt2img_sd)
    if prompt_txt2img_sd == "None":
        prompt_txt2img_sd = ""
    if negative_prompt_txt2img_sd == "None":
        negative_prompt_txt2img_sd = ""

    if (is_xl_txt2img_sd == True) :
        compel = Compel(
            tokenizer=[pipe_txt2img_sd.tokenizer, pipe_txt2img_sd.tokenizer_2], 
            text_encoder=[pipe_txt2img_sd.text_encoder, pipe_txt2img_sd.text_encoder_2], 
            returned_embeddings_type=ReturnedEmbeddingsType.PENULTIMATE_HIDDEN_STATES_NON_NORMALIZED, 
            requires_pooled=[False, True], 
        )
        conditioning, pooled = compel(prompt_txt2img_sd)
        neg_conditioning, neg_pooled = compel(negative_prompt_txt2img_sd)
        [conditioning, neg_conditioning] = compel.pad_conditioning_tensors_to_same_length([conditioning, neg_conditioning])
    else :
        compel = Compel(tokenizer=pipe_txt2img_sd.tokenizer, text_encoder=pipe_txt2img_sd.text_encoder, truncate_long_prompts=False)
        conditioning = compel.build_conditioning_tensor(prompt_txt2img_sd)
        neg_conditioning = compel.build_conditioning_tensor(negative_prompt_txt2img_sd)    
        [conditioning, neg_conditioning] = compel.pad_conditioning_tensors_to_same_length([conditioning, neg_conditioning])
   
    final_image = []
    for i in range (num_prompt_txt2img_sd):
        if (is_xl_txt2img_sd == True) :
            image = pipe_txt2img_sd(
                prompt_embeds=conditioning,
                pooled_prompt_embeds=pooled, 
                negative_prompt_embeds=neg_conditioning,
                negative_pooled_prompt_embeds=neg_pooled,
                height=height_txt2img_sd,
                width=width_txt2img_sd,
                num_images_per_prompt=num_images_per_prompt_txt2img_sd,
                num_inference_steps=num_inference_step_txt2img_sd,
                guidance_scale=guidance_scale_txt2img_sd,
                generator = generator,
                callback = check_txt2img_sd,
            ).images
        else :
            image = pipe_txt2img_sd(
                prompt_embeds=conditioning,
                negative_prompt_embeds=neg_conditioning,
                height=height_txt2img_sd,
                width=width_txt2img_sd,
                num_images_per_prompt=num_images_per_prompt_txt2img_sd,
                num_inference_steps=num_inference_step_txt2img_sd,
                guidance_scale=guidance_scale_txt2img_sd,
                generator = generator,
                callback = check_txt2img_sd,
            ).images

        for j in range(len(image)):
            timestamp = time.time()
            savename = f"outputs/{timestamp}.png"
            if use_gfpgan_txt2img_sd == True :
                image[j] = image_gfpgan_mini(image[j])
            image[j].save(savename)
            final_image.append(image[j])
    
    del nsfw_filter_final, feat_ex, pipe_txt2img_sd, generator, compel, conditioning, neg_conditioning, image
    clean_ram()
    
    return final_image, final_image

ressources/txt2img_sd.py
Woolverine94/biniou
# https://github.com/Woolverine94/biniou
# txt2vid_ms.py
import gradio as gr
import os
from diffusers import DiffusionPipeline
from diffusers.utils import export_to_video
from compel import Compel
import torch
import time
import random
import shutil
from ressources.scheduler import *
from ressources.common import *
import tomesd

device_txt2vid_ms = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

# Gestion des modèles
model_path_txt2vid_ms = "./models/Modelscope/"
os.makedirs(model_path_txt2vid_ms, exist_ok=True)

model_list_txt2vid_ms = [
    "cerspense/zeroscope_v2_576w",
    "camenduru/potat1",
    "damo-vilab/text-to-video-ms-1.7b",
]

# Bouton Cancel
stop_txt2vid_ms = False

def initiate_stop_txt2vid_ms() :
    global stop_txt2vid_ms
    stop_txt2vid_ms = True

def check_txt2vid_ms(step, timestep, latents) : 
    global stop_txt2vid_ms
    if stop_txt2vid_ms == False :
        return
    elif stop_txt2vid_ms == True :
        stop_txt2vid_ms = False
        try:
            del ressources.txt2vid_ms.pipe_txt2vid_ms
        except NameError as e:
            raise Exception("Interrupting ...")
    return

def video_txt2vid_ms(
    modelid_txt2vid_ms, 
    sampler_txt2vid_ms, 
    prompt_txt2vid_ms, 
    negative_prompt_txt2vid_ms, 
    num_frames_txt2vid_ms, 
    num_prompt_txt2vid_ms, 
    guidance_scale_txt2vid_ms, 
    num_inference_step_txt2vid_ms, 
    height_txt2vid_ms, 
    width_txt2vid_ms, 
    seed_txt2vid_ms, 
    use_gfpgan_txt2vid_ms,
#    tkme_txt2vid_ms,
    progress_txt2vid_ms=gr.Progress(track_tqdm=True)
    ):

    pipe_txt2vid_ms = DiffusionPipeline.from_pretrained(
        modelid_txt2vid_ms, 
        cache_dir=model_path_txt2vid_ms, 
        torch_dtype=torch.float32,
        resume_download=True,
        local_files_only=True if offline_test() else None         
    )
    pipe_txt2vid_ms = get_scheduler(pipe=pipe_txt2vid_ms, scheduler=sampler_txt2vid_ms)
    pipe_txt2vid_ms = pipe_txt2vid_ms.to(device_txt2vid_ms)
    pipe_txt2vid_ms.unet.enable_forward_chunking(chunk_size=1, dim=1)
    pipe_txt2vid_ms.enable_vae_slicing()

    if seed_txt2vid_ms == 0:
        random_seed = torch.randint(0, 10000000000, (1,))
        generator = torch.manual_seed(random_seed)
    else:
        generator = torch.manual_seed(seed_txt2vid_ms)

    prompt_txt2vid_ms = str(prompt_txt2vid_ms)
    negative_prompt_txt2vid_ms = str(negative_prompt_txt2vid_ms)
    if prompt_txt2vid_ms == "None":
        prompt_txt2vid_ms= ""
    if negative_prompt_txt2vid_ms == "None":
        negative_prompt_txt2vid_ms = ""

    compel = Compel(tokenizer=pipe_txt2vid_ms.tokenizer, text_encoder=pipe_txt2vid_ms.text_encoder, truncate_long_prompts=False)
    conditioning = compel.build_conditioning_tensor(prompt_txt2vid_ms)
    neg_conditioning = compel.build_conditioning_tensor(negative_prompt_txt2vid_ms)
    [conditioning, neg_conditioning] = compel.pad_conditioning_tensors_to_same_length([conditioning, neg_conditioning])    

    for i in range (num_prompt_txt2vid_ms):
        video_frames = pipe_txt2vid_ms(
            prompt_embeds=conditioning,            
            negative_prompt_embeds=neg_conditioning,
            height=height_txt2vid_ms,
            width=width_txt2vid_ms,
            num_inference_steps=num_inference_step_txt2vid_ms,
            guidance_scale=guidance_scale_txt2vid_ms,
            num_frames=num_frames_txt2vid_ms,
            generator = generator,
            callback = check_txt2vid_ms,            
        ).frames
        
        video_path = export_to_video(video_frames)
        timestamp = time.time()
        savename = f"outputs/{timestamp}.mp4"
        shutil.move(video_path, savename)
        
    del pipe_txt2vid_ms, generator, video_frames
    clean_ram()        

    return savename

ressources/txt2vid_ms.py
Woolverine94/biniou
# https://github.com/Woolverine94/biniou
# txt2vid_ze.py
import gradio as gr
import os
import imageio
from diffusers import TextToVideoZeroPipeline
import numpy as np
import torch
import time
import random
from ressources.scheduler import *
from ressources.common import *
from ressources.gfpgan import *
import tomesd

device_txt2vid_ze = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

model_path_txt2vid_ze = "./models/Stable_Diffusion/"
os.makedirs(model_path_txt2vid_ze, exist_ok=True)

model_list_txt2vid_ze = [
    "SG161222/Realistic_Vision_V3.0_VAE",
#    "ckpt/anything-v4.5-vae-swapped",
    "runwayml/stable-diffusion-v1-5",
    "nitrosocke/Ghibli-Diffusion",
]

# Bouton Cancel
stop_txt2vid_ze = False

def initiate_stop_txt2vid_ze() :
    global stop_txt2vid_ze
    stop_txt2vid_ze = True

def check_txt2vid_ze(step, timestep, latents) : 
    global stop_txt2vid_ze
    if stop_txt2vid_ze == False :
        return
    elif stop_txt2vid_ze == True :
        stop_txt2vid_ze = False
        try:
            del ressources.txt2vid_ze.pipe_txt2vid_ze
        except NameError as e:
            raise Exception("Interrupting ...")
    return

def video_txt2vid_ze(
    modelid_txt2vid_ze, 
    num_inference_step_txt2vid_ze, 
    sampler_txt2vid_ze, 
    guidance_scale_txt2vid_ze, 
    seed_txt2vid_ze, 
    num_frames_txt2vid_ze, 
    num_fps_txt2vid_ze, 
    height_txt2vid_ze, 
    width_txt2vid_ze, 
    num_videos_per_prompt_txt2vid_ze, 
    num_prompt_txt2vid_ze, 
    motion_field_strength_x_txt2vid_ze, 
    motion_field_strength_y_txt2vid_ze, 
    timestep_t0_txt2vid_ze :int, 
    timestep_t1_txt2vid_ze :int, 
    prompt_txt2vid_ze, 
    negative_prompt_txt2vid_ze, 
    nsfw_filter, 
    num_chunks_txt2vid_ze :int, 
    use_gfpgan_txt2vid_ze,
    tkme_txt2vid_ze,
    progress_txt2vid_ze=gr.Progress(track_tqdm=True)
    ):
    
    nsfw_filter_final, feat_ex = safety_checker_sd(model_path_txt2vid_ze, device_txt2vid_ze, nsfw_filter)

    pipe_txt2vid_ze = TextToVideoZeroPipeline.from_pretrained(
        modelid_txt2vid_ze, 
        cache_dir=model_path_txt2vid_ze, 
        torch_dtype=torch.float32, 
        use_safetensors=True, 
        safety_checker=nsfw_filter_final, 
        feature_extractor=feat_ex, 
        resume_download=True,
        local_files_only=True if offline_test() else None        
    )
    
    pipe_txt2vid_ze = get_scheduler(pipe=pipe_txt2vid_ze, scheduler=sampler_txt2vid_ze)
    pipe_txt2vid_ze = pipe_txt2vid_ze.to(device_txt2vid_ze)
    tomesd.apply_patch(pipe_txt2vid_ze, ratio=tkme_txt2vid_ze)
    
    if seed_txt2vid_ze == 0:
        random_seed = torch.randint(0, 10000000000, (1,))
        generator = torch.manual_seed(random_seed)
    else:
        generator = torch.manual_seed(seed_txt2vid_ze)

    for j in range (num_prompt_txt2vid_ze):
        if num_chunks_txt2vid_ze != 1 :
            result = []
            chunk_ids = np.arange(0, num_frames_txt2vid_ze, num_chunks_txt2vid_ze)
            generator = torch.Generator(device=device_txt2vid_ze)
            for i in range(len(chunk_ids)):
                print(f"Processing chunk {i + 1} / {len(chunk_ids)}")
                ch_start = chunk_ids[i]
                ch_end = num_frames_txt2vid_ze if i == len(chunk_ids) - 1 else chunk_ids[i + 1]
                if i == 0 :
                    frame_ids = [0] + list(range(ch_start, ch_end))
                else :
                    frame_ids = [ch_start -1] + list(range(ch_start, ch_end))
                generator = generator.manual_seed(seed_txt2vid_ze)
                output = pipe_txt2vid_ze(
                    prompt=prompt_txt2vid_ze,
                    negative_prompt=negative_prompt_txt2vid_ze,
                    height=height_txt2vid_ze,
                    width=width_txt2vid_ze,
                    num_inference_steps=num_inference_step_txt2vid_ze,
                    guidance_scale=guidance_scale_txt2vid_ze,
                    frame_ids=frame_ids,
                    video_length=len(frame_ids), 
                    num_videos_per_prompt=num_videos_per_prompt_txt2vid_ze,
                    motion_field_strength_x=motion_field_strength_x_txt2vid_ze,
                    motion_field_strength_y=motion_field_strength_y_txt2vid_ze,
                    t0=timestep_t0_txt2vid_ze,
                    t1=timestep_t1_txt2vid_ze,
                    generator = generator,
                    callback = check_txt2vid_ze,
                )
                result.append(output.images[1:])
            result = np.concatenate(result)
        else :
            result = pipe_txt2vid_ze(
                prompt=prompt_txt2vid_ze,
                negative_prompt=negative_prompt_txt2vid_ze,
                height=height_txt2vid_ze,
                width=width_txt2vid_ze,
                num_inference_steps=num_inference_step_txt2vid_ze,
                guidance_scale=guidance_scale_txt2vid_ze,
                video_length=num_frames_txt2vid_ze,
                num_videos_per_prompt=num_videos_per_prompt_txt2vid_ze,
                motion_field_strength_x=motion_field_strength_x_txt2vid_ze,
                motion_field_strength_y=motion_field_strength_y_txt2vid_ze,
                t0=timestep_t0_txt2vid_ze,
                t1=timestep_t1_txt2vid_ze,
                generator = generator,
                callback = check_txt2vid_ze,
            ).images

        result = [(r * 255).astype("uint8") for r in result]

        for j in range(len(result)):
            if use_gfpgan_txt2vid_ze == True :
                result[j] = image_gfpgan_mini(result[j])

        timestamp = time.time()
        savename = f"outputs/{timestamp}.mp4"
        imageio.mimsave(savename, result, fps=num_fps_txt2vid_ze)
        
    del nsfw_filter_final, feat_ex, pipe_txt2vid_ze, generator, result
    clean_ram()
    
    return savename


ressources/txt2vid_ze.py
Woolverine94/biniou
# https://github.com/Woolverine94/biniou
# vid2vid_ze.py
import gradio as gr
import os
import PIL
import torch
import imageio
import ffmpeg
from diffusers import StableDiffusionInstructPix2PixPipeline
from diffusers.pipelines.text_to_video_synthesis.pipeline_text_to_video_zero import CrossFrameAttnProcessor
import time
import random
from ressources.scheduler import *
from ressources.common import *
from ressources.gfpgan import *
import tomesd

device_vid2vid_ze = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

# Gestion des modèles -> pas concerné (safetensors refusé)
model_path_vid2vid_ze = "./models/pix2pix/"
model_path_safety_checker = "./models/Stable_Diffusion/"
os.makedirs(model_path_vid2vid_ze, exist_ok=True)

model_list_vid2vid_ze = []

for filename in os.listdir(model_path_vid2vid_ze):
    f = os.path.join(model_path_vid2vid_ze, filename)
    if os.path.isfile(f) and (filename.endswith('.ckpt') or filename.endswith('.safetensors')):
        model_list_vid2vid_ze.append(f)

model_list_vid2vid_ze_builtin = [
    "timbrooks/instruct-pix2pix",
]

for k in range(len(model_list_vid2vid_ze_builtin)):
    model_list_vid2vid_ze.append(model_list_vid2vid_ze_builtin[k])

# Bouton Cancel
stop_vid2vid_ze = False

def initiate_stop_vid2vid_ze() :
    global stop_vid2vid_ze
    stop_vid2vid_ze = True

def check_vid2vid_ze(step, timestep, latents) : 
    global stop_vid2vid_ze
    if stop_vid2vid_ze == False :
        return
    elif stop_vid2vid_ze == True :
        stop_vid2vid_ze = False
        try:
            del ressources.vid2vid_ze.pipe_vid2vid_ze
        except NameError as e:
            raise Exception("Interrupting ...")
    return

def image_vid2vid_ze(
    modelid_vid2vid_ze,
    sampler_vid2vid_ze,
    vid_vid2vid_ze,
    prompt_vid2vid_ze,
    negative_prompt_vid2vid_ze,
    num_images_per_prompt_vid2vid_ze,
    num_prompt_vid2vid_ze,
    guidance_scale_vid2vid_ze,
    image_guidance_scale_vid2vid_ze,
    num_inference_step_vid2vid_ze,
    height_vid2vid_ze,
    width_vid2vid_ze,
    seed_vid2vid_ze,
    num_frames_vid2vid_ze,
    num_fps_txt2vid_ze,
    use_gfpgan_vid2vid_ze,
    nsfw_filter,
    tkme_vid2vid_ze,
    progress_vid2vid_ze=gr.Progress(track_tqdm=True)
    ):

    nsfw_filter_final, feat_ex = safety_checker_sd(model_path_safety_checker, device_vid2vid_ze, nsfw_filter)
    
    probe = ffmpeg.probe(vid_vid2vid_ze)
    video_info = next(s for s in probe['streams'] if s['codec_type'] == 'video')
    num_frames_total = int(video_info['nb_frames'])
    if num_frames_total<num_frames_vid2vid_ze :
        num_frames_vid2vid_ze = num_frames_total
    reader = imageio.get_reader(vid_vid2vid_ze, "ffmpeg")
    video = [Image.fromarray(reader.get_data(i)) for i in range(num_frames_vid2vid_ze)]
    
    pipe_vid2vid_ze= StableDiffusionInstructPix2PixPipeline.from_pretrained(
        modelid_vid2vid_ze, 
        cache_dir=model_path_vid2vid_ze, 
        torch_dtype=torch.float32, 
        use_safetensors=True, 
        safety_checker=nsfw_filter_final, 
        feature_extractor=feat_ex,
        resume_download=True,
        local_files_only=True if offline_test() else None
    )
    
    pipe_vid2vid_ze = get_scheduler(pipe=pipe_vid2vid_ze, scheduler=sampler_vid2vid_ze)
    pipe_vid2vid_ze = pipe_vid2vid_ze.to(device_vid2vid_ze)
    pipe_vid2vid_ze.unet.set_attn_processor(CrossFrameAttnProcessor(batch_size=3))
    tomesd.apply_patch(pipe_vid2vid_ze, ratio=tkme_vid2vid_ze)
    
    if seed_vid2vid_ze == 0:
        random_seed = torch.randint(0, 10000000000, (1,))
        generator = torch.manual_seed(random_seed)
    else:
        generator = torch.manual_seed(seed_vid2vid_ze)
        
    prompt_vid2vid_ze = str(prompt_vid2vid_ze)
    negative_prompt_vid2vid_ze = str(negative_prompt_vid2vid_ze)
    if prompt_vid2vid_ze == "None":
        prompt_vid2vid_ze = ""
    if negative_prompt_vid2vid_ze == "None":
        negative_prompt_vid2vid_ze = ""

    final_image = []
    
    for i in range (num_prompt_vid2vid_ze):
        image = pipe_vid2vid_ze(
            image=video,
            prompt=[prompt_vid2vid_ze] * len(video),
            negative_prompt=[negative_prompt_vid2vid_ze] * len(video),
            num_images_per_prompt=num_images_per_prompt_vid2vid_ze,
            guidance_scale=guidance_scale_vid2vid_ze,
            image_guidance_scale=image_guidance_scale_vid2vid_ze,
            num_inference_steps=num_inference_step_vid2vid_ze,
            generator = generator,
            callback = check_vid2vid_ze,             
        ).images

        for j in range(len(image)):
            if use_gfpgan_vid2vid_ze == True :
                image[j] = image_gfpgan_mini(image[j])             
            final_image.append(image[j])
            
    timestamp = time.time()
    savename = f"outputs/{timestamp}.mp4"
    final_video = imageio.mimsave(savename, final_image, fps=num_fps_txt2vid_ze)            
            
    del nsfw_filter_final, feat_ex, pipe_vid2vid_ze, generator, image
    clean_ram()            

    return savename, savename

ressources/vid2vid_ze.py
Woolverine94/biniou
# https://github.com/Woolverine94/biniou
# whisper.py
import gradio as gr
import os
import time
import numpy as np
import torch
from transformers import WhisperProcessor, WhisperForConditionalGeneration
from transformers import pipeline
from pydub import AudioSegment
from huggingface_hub import snapshot_download, hf_hub_download
from transformers import AutoModel, AutoTokenizer, AutoFeatureExtractor
from ressources.common import *

device_whisper = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

model_path_whisper = "./models/whisper/"
os.makedirs(model_path_whisper, exist_ok=True)

model_list_whisper = {}

for filename in os.listdir(model_path_whisper):
    f = os.path.join(model_path_whisper, filename)
    if os.path.isfile(f) and (filename.endswith('.bin') or filename.endswith('.safetensors')) :
        model_list_whisper.update(f)

model_list_whisper_builtin = {
    "openai/whisper-tiny": "model.safetensors",
    "openai/whisper-base": "model.safetensors",        
    "openai/whisper-medium": "model.safetensors",
    "openai/whisper-large": "model.safetensors",
    "openai/whisper-large-v2": "model.safetensors", 
}

model_list_whisper.update(model_list_whisper_builtin)

language_list_whisper = [
    "afrikaans",
    "arabic",
    "armenian",
    "azerbaijani",
    "belarusian",
    "bosnian",
    "bulgarian",
    "catalan",
    "chinese",
    "croatian",
    "czech",
    "danish",
    "dutch",
    "english",
    "estonian",
    "finnish",
    "french",
    "galician",
    "german",
    "greek",
    "hebrew",
    "hindi",
    "hungarian",
    "icelandic",
    "indonesian",
    "italian",
    "japanese",
    "kannada",
    "kazakh",
    "korean",
    "latvian",
    "lithuanian",
    "macedonian",
    "malay",
    "marathi",
    "maori",
    "nepali",
    "norwegian",
    "persian",
    "polish",
    "portuguese",
    "romanian",
    "russian",
    "serbian",
    "slovak",
    "slovenian",
    "spanish",
    "swahili",
    "swedish",
    "tagalog",
    "tamil",
    "thai",
    "turkish",
    "ukrainian",
    "urdu",
    "vietnamese",
    "welsh",
]

# Bouton Cancel
stop_whisper = False

def initiate_stop_whisper() :
    global stop_whisper
    stop_whisper = True

def check_whisper(step, timestep, latents) :
    global stop_whisper
    if stop_whisper == False :
        return
    elif stop_whisper == True :
        stop_whisper = False
        try:
            del ressources.whisper.pipe_whisper
        except NameError as e:
            raise Exception("Interrupting ...")
    return

def convert_seconds_to_timestamp(seconds):
    RELIQUAT0 = int(seconds)
    MSECONDES = round(seconds-(RELIQUAT0), 3)
    MSECONDES_FINAL = str(int(MSECONDES*1000)).ljust(3, '0')
    RELIQUAT1 = int(seconds/60)
    SECONDES = RELIQUAT0-(RELIQUAT1*60)
    RELIQUAT2 = int(RELIQUAT1/60)
    MINUTES = int((RELIQUAT0-((RELIQUAT2*3600)+SECONDES))/60)
    RELIQUAT3 = RELIQUAT2/24
    HEURES = int((RELIQUAT0-((MINUTES*60)+SECONDES))/3600)
    total = f"{str(HEURES).zfill(2)}:{str(MINUTES).zfill(2)}:{str(SECONDES).zfill(2)},{MSECONDES_FINAL}"
    return total

# def download_model(modelid_whisper_final):
#     if modelid_whisper_final[0:9] != "./models/":
#         hf_hub_path_config_whisper = hf_hub_download(
#             repo_id=modelid_whisper_final, 
#             filename="config.json", 
#             repo_type="model", 
#             cache_dir=model_path_whisper, 
#             resume_download=True,
#             local_files_only=True if offline_test() else None
#         )
#         hf_hub_path_whisper = hf_hub_download(
#             repo_id=modelid_whisper_final, 
#             filename=model_list_whisper[modelid_whisper_final], 
#             repo_type="model", 
#             cache_dir=model_path_whisper, 
#             resume_download=True,
#             local_files_only=True if offline_test() else None
#         )
#     return hf_hub_path_config_whisper, hf_hub_path_whisper 

def text_whisper(
    modelid_whisper, 
    srt_output_whisper, 
    source_language_whisper, 
    source_audio_whisper, 
    output_type_whisper, 
    output_language_whisper, 
    progress_whisper=gr.Progress(track_tqdm=True)
    ):
        
    sample_rate_whisper = 16000    
    audio_whisper = AudioSegment.from_file(source_audio_whisper)
    audio_whisper = audio_whisper.set_frame_rate(sample_rate_whisper)
    audio_whisper = audio_whisper.set_channels(1)     
    audio_whisper = audio_whisper.get_array_of_samples()
    audio_whisper = np.array(audio_whisper)
    
    model_whisper = WhisperForConditionalGeneration.from_pretrained(
        modelid_whisper, 
        cache_dir=model_path_whisper, 
        resume_download=True, 
        local_files_only=True if offline_test() else None
    )
    
    tokenizer_whisper = AutoTokenizer.from_pretrained(
        modelid_whisper,
        cache_dir=model_path_whisper, 
        resume_download=True, 
        local_files_only=True if offline_test() else None
    )
    
    feat_ex_whisper = AutoFeatureExtractor.from_pretrained(
        modelid_whisper,
        cache_dir=model_path_whisper, 
        resume_download=True, 
        local_files_only=True if offline_test() else None        
    )
    
    pipe_whisper = pipeline(
        "automatic-speech-recognition", 
        model=model_whisper, 
        tokenizer=tokenizer_whisper, 
        feature_extractor=feat_ex_whisper, 
        chunk_length_s=30, 
        device=device_whisper, 
        torch_dtype=torch.float32
    )
    
    if srt_output_whisper == False :
        transcription_whisper_final = pipe_whisper(audio_whisper.copy(), generate_kwargs={"task": output_type_whisper}, batch_size=8)["text"]
    elif srt_output_whisper == True :
        transcription_whisper = pipe_whisper(
            audio_whisper.copy(), 
            batch_size=8, 
            generate_kwargs={"task": output_type_whisper}, 
            return_timestamps=True
        )["chunks"]
        
        transcription_whisper_final = ""
        
        for i in range(len(transcription_whisper)) : 
            timestamp_start, timestamp_end = transcription_whisper[i]["timestamp"]
            transcribe = transcription_whisper[i]["text"]
            timestamp_start_final = convert_seconds_to_timestamp(timestamp_start)
            timestamp_end_final = convert_seconds_to_timestamp(timestamp_end)
            transcription_whisper_final = transcription_whisper_final+ f"{i+1}"+ "\n"+ f"{timestamp_start_final}"+ " --> "+ f"{timestamp_end_final}"+ "\n"+ transcribe+ "\n"+ "\n"

    write_file(transcription_whisper_final)

    del audio_whisper, model_whisper, tokenizer_whisper, feat_ex_whisper, pipe_whisper
    clean_ram()

    return transcription_whisper_final


ressources/whisper.py
Woolverine94/biniou
# https://github.com/Woolverine94/biniou
# Webui.py
import os 
import gradio as gr
import numpy as np
import warnings
import shutil
from PIL import Image
from ressources.common import *
from ressources.llamacpp import *
from ressources.img2txt_git import *
from ressources.whisper import *
from ressources.nllb import *
from ressources.txt2img_sd import *
from ressources.txt2img_kd import *
from ressources.img2img import *
from ressources.img2var import *
from ressources.pix2pix import *
from ressources.inpaint import *
from ressources.controlnet import *
from ressources.faceswapper import *
from ressources.r_esrgan import *
from ressources.gfpgan import *
from ressources.musicgen import *
from ressources.audiogen import *
from ressources.harmonai import *
from ressources.bark import *
from ressources.txt2vid_ms import *
from ressources.txt2vid_ze import *
from ressources.vid2vid_ze import *

tmp_biniou="./.tmp"
if os.path.exists(tmp_biniou) :
    shutil.rmtree(tmp_biniou)
os.makedirs(tmp_biniou, exist_ok=True)

warnings.filterwarnings('ignore') 

get_window_url_params = """
    function(url_params) {
        const params = new URLSearchParams(window.location.search);
        url_params = Object.fromEntries(params);
        return url_params;
        }
    """

def split_url_params(url_params) :
    url_params = eval(url_params.replace("'", "\""))
    if "nsfw_filter" in url_params.keys():
        output_nsfw = url_params["nsfw_filter"]
        return output_nsfw
    else :         
        return "1"

## Fonctions communes
def dummy():
    return

def in_and_out(input_value):
    return input_value

# fonctions Exports Outputs 
def send_to_module(content, index, numtab, numtab_item):
    index = int(index)
    return content[index], gr.Tabs.update(selected=numtab), tabs_image.update(selected=numtab_item) # /!\ tabs_image = pas bon pour les autres modules

def send_to_module_inpaint(content, index, numtab, numtab_item):
    index = int(index)
    return content[index], content[index], gr.Tabs.update(selected=numtab), tabs_image.update(selected=numtab_item)
    
def send_to_module_text(content, index, numtab, numtab_item):
    index = int(index)
    return content[index], gr.Tabs.update(selected=numtab), tabs_text.update(selected=numtab_item)    
    
def send_to_module_video (content, numtab, numtab_item) : 
	return content, gr.Tabs.update(selected=numtab), tabs_video.update(selected=numtab_item)

def send_text_to_module_image (prompt, numtab, numtab_item):
    return prompt, gr.Tabs.update(selected=numtab), tabs_image.update(selected=numtab_item)
    
def send_audio_to_module_text(audio, numtab, numtab_item):
    return audio, gr.Tabs.update(selected=numtab), tabs_text.update(selected=numtab_item)    

def send_text_to_module_text(prompt, numtab, numtab_item):
    return prompt, gr.Tabs.update(selected=numtab), tabs_text.update(selected=numtab_item)

# fonctions Exports Inputs
def import_to_module(prompt, negative_prompt, numtab, numtab_item):
    return prompt, negative_prompt, gr.Tabs.update(selected=numtab), tabs_image.update(selected=numtab_item)
    
def import_to_module_audio(prompt, numtab, numtab_item):
    return prompt, gr.Tabs.update(selected=numtab), tabs_audio.update(selected=numtab_item)    
    
def import_to_module_video(prompt, negative_prompt, numtab, numtab_item):
    return prompt, negative_prompt, gr.Tabs.update(selected=numtab), tabs_video.update(selected=numtab_item)   

def import_text_to_module_image(prompt, numtab, numtab_item):
    return prompt, gr.Tabs.update(selected=numtab), tabs_image.update(selected=numtab_item)

def import_text_to_module_video(prompt, numtab, numtab_item):
    return prompt, gr.Tabs.update(selected=numtab), tabs_video.update(selected=numtab_item)    

# fonctions Exports Inputs + Outputs
def both_text_to_module_image (content, prompt, numtab, numtab_item):
    return content, prompt, gr.Tabs.update(selected=numtab), tabs_image.update(selected=numtab_item) 

def both_text_to_module_inpaint_image (content, prompt, numtab, numtab_item):
    return content, content, prompt, gr.Tabs.update(selected=numtab), tabs_image.update(selected=numtab_item) 
   
def both_to_module(prompt, negative_prompt, content, index, numtab, numtab_item):
    index = int(index)
    return prompt, negative_prompt, content[index], gr.Tabs.update(selected=numtab), tabs_image.update(selected=numtab_item) # /!\ tabs_image = pas bon pour les autres modules    

def both_to_module_inpaint(prompt, negative_prompt, content, index, numtab, numtab_item):
    index = int(index)
    return prompt, negative_prompt, content[index], content[index], gr.Tabs.update(selected=numtab), tabs_image.update(selected=numtab_item) # /!\ tabs_image = pas bon pour les autres modules    

def get_select_index(evt: gr.SelectData) :
    return evt.index

## Fonctions spécifiques à llamacpp
def llamacpp_inference_start() : 
    return out_llamacpp.update(visible=True)

def llamacpp_inference_end() :
    return out_llamacpp.update(visible=False)

## Fonctions spécifiques à Stable Diffusion 
def zip_download_file_txt2img_sd(content):
    savename = zipper(content)
    return savename, download_file_txt2img_sd.update(visible=True) 

def hide_download_file_txt2img_sd():
    return download_file_txt2img_sd.update(visible=False)
    
def update_preview_txt2img_sd(preview):
    return out_txt2img_sd.update(preview)     

## Fonctions spécifiques à Kandinsky 
def zip_download_file_txt2img_kd(content):
    savename = zipper(content)
    return savename, download_file_txt2img_kd.update(visible=True) 

def hide_download_file_txt2img_kd():
    return download_file_txt2img_kd.update(visible=False)
    
## Fonctions spécifiques à img2img 
def zip_download_file_img2img(content):
    savename = zipper(content)
    return savename, download_file_img2img.update(visible=True) 

def hide_download_file_img2img():
    return download_file_img2img.update(visible=False)        
    
def change_source_type_img2img(source_type_img2img):
    if source_type_img2img == "image" :
        return {"source": "upload", "tool": None, "__type__": "update"}
    elif source_type_img2img == "sketch" :
        return {"source": "canvas", "tool": "color-sketch", "__type__": "update"}

## Fonctions spécifiques à img2var 
def zip_download_file_img2var(content):
    savename = zipper(content)
    return savename, download_file_img2var.update(visible=True) 

def hide_download_file_img2var():
    return download_file_img2var.update(visible=False)        

## Fonctions spécifiques à pix2pix 
def zip_download_file_pix2pix(content):
    savename = zipper(content)
    return savename, download_file_pix2pix.update(visible=True) 

def hide_download_file_pix2pix():
    return download_file_pix2pix.update(visible=False) 
    
## Fonctions spécifiques à inpaint 
def zip_download_file_inpaint(content):
    savename = zipper(content)
    return savename, download_file_inpaint.update(visible=True) 

def hide_download_file_inpaint():
    return download_file_inpaint.update(visible=False) 

## Fonctions spécifiques à controlnet 
def zip_download_file_controlnet(content):
    savename = zipper(content)
    return savename, download_file_controlnet.update(visible=True) 

def hide_download_file_controlnet():
    return download_file_controlnet.update(visible=False) 

## Fonctions spécifiques à faceswap 
def zip_download_file_faceswap(content):
    savename = zipper(content)
    return savename, download_file_faceswap.update(visible=True) 

def hide_download_file_faceswap():
    return download_file_faceswap.update(visible=False) 

## Fonctions spécifiques à whisper
def change_source_type_whisper(source_type_whisper):
    if source_type_whisper == "audio" :
        return source_audio_whisper.update(source="upload")
    elif source_type_whisper == "micro" :
        return source_audio_whisper.update(source="microphone")
        
def change_output_type_whisper(output_type_whisper):
    if output_type_whisper == "transcribe" :
        return output_language_whisper.update(visible=False)
    elif output_type_whisper == "translate" :
        return output_language_whisper.update(visible=True)        

def stop_recording_whisper(source_audio_whisper):
    print(source_audio_whisper)
    print(type(source_audio_whisper))
    return source_audio_whisper.update(source="upload"), source_audio_whisper

color_label = "#7B43EE"
color_label_button = "#4361ee"

theme_gradio = gr.themes.Base().set(
    block_label_background_fill=color_label,
    block_label_background_fill_dark=color_label,
    block_label_border_color='#000000',
    block_label_border_color_dark='#000000',
    block_label_text_color='silver',
    block_label_text_color_dark='white',
    block_title_background_fill=color_label,
    block_title_background_fill_dark=color_label,
    block_title_text_color='white',
    block_title_text_color_dark='white',
    block_title_padding='5px',
    block_title_radius='*radius_lg',
    button_primary_background_fill=color_label_button,
    button_primary_background_fill_dark=color_label_button,
    button_primary_border_color=color_label_button,
    button_primary_border_color_dark=color_label_button,
    button_primary_text_color='white',
    button_primary_text_color_dark='white',
)

with gr.Blocks(theme=theme_gradio) as demo:
    gr.HTML(
        """<a href='https://github.com/Woolverine94/biniou' style='text-decoration: none;'><p style='float:left;'><img src='file/images/biniou_64.png' width='32' height='32'/></p>
        <p style='text-align: left; font-size: 32px; font-weight: bold; line-height:32px;'>biniou</p></a>"""
    )
    nsfw_filter = gr.Textbox(value="1", visible=False)
    with gr.Tabs() as tabs:
# Chat
        with gr.TabItem("Text ✍️", id=1) as tab_text:
            with gr.Tabs() as tabs_text:
# llamacpp
                with gr.TabItem("Chatbot Llama-cpp (gguf) 📝", id=12) as tab_llamacpp:
                    with gr.Accordion("About", open=False):
                        with gr.Box():                       
                            gr.HTML(
                                """
                                <h1 style='text-align: left'; text-decoration: underline;>Informations</h1>
                                <b>Module : </b>Llama-cpp</br>
                                <b>Function : </b>Chat with an AI using <a href='https://github.com/abetlen/llama-cpp-python' target='_blank'>llama-cpp-python</a></br>
                                <b>Input(s) : </b>Input text</br>
                                <b>Output(s) : </b>Output text</br>
                                <b>HF models pages : </b>
                                <a href='https://huggingface.co/TheBloke/CollectiveCognition-v1.1-Mistral-7B-GGUF' target='_blank'>TheBloke/CollectiveCognition-v1.1-Mistral-7B-GGUF</a>, 
                                <a href='https://huggingface.co/TheBloke/Vigogne-2-7B-Instruct-GGUF' target='_blank'>TheBloke/Vigogne-2-7B-Instruct-GGUF</a>, 
                                <a href='https://huggingface.co/TheBloke/Vigogne-2-13B-Instruct-GGUF' target='_blank'>TheBloke/Vigogne-2-13B-Instruct-GGUF</a>, 
                                <a href='https://huggingface.co/TheBloke/Airoboros-L2-7B-2.1-GGUF' target='_blank'>TheBloke/Airoboros-L2-7B-2.1-GGUF</a>, 
                                <a href='https://huggingface.co/TheBloke/Airoboros-L2-13B-2.1-GGUF' target='_blank'>TheBloke/Airoboros-L2-13B-2.1-GGUF</a>, 
                                <a href='https://huggingface.co/TheBloke/CodeLlama-13B-Instruct-GGUF' target='_blank'>TheBloke/CodeLlama-13B-Instruct-GGUF</a></br>
                                """
                            )
                        with gr.Box():
                            gr.HTML(
                                """
                                <h1 style='text-align: left'; text-decoration: underline;>Help</h1>
                                <div style='text-align: justified'>
                                <b>Usage :</b></br>
                                - Type your request in the <b>Input</b> textbox field</br>
                                - (optional) modify settings to use another model, change context size or modify maximum number of tokens generated.</br>
                                - Click the <b>Generate</b> button to generate a response to your input, using the chatbot history to keep a context.</br>
                                - Click the <b>Continue</b> button to complete the last reply.
                                </br>
                                <b>Models :</b></br>
                                - You could place llama-cpp compatible .gguf models in the directory ./biniou/models/llamacpp. Restart Biniou to see them in the models list.                                  
                                </div>
                                """
                            )
                    with gr.Accordion("Settings", open=False):
                        with gr.Row():
                            with gr.Column():
                                model_llamacpp = gr.Dropdown(choices=list(model_list_llamacpp.keys()), value=list(model_list_llamacpp.keys())[0], label="Model", info="Choose model to use for inference")
                            with gr.Column():
                                max_tokens_llamacpp = gr.Slider(0, 65536, step=16, value=1024, label="Max tokens", info="Maximum number of tokens to generate")
                            with gr.Column():
                                seed_llamacpp = gr.Slider(0, 10000000000, step=1, value=1337, label="Seed(0 for random)", info="Seed to use for generation.")    
                        with gr.Row():
                            with gr.Column():
                                stream_llamacpp = gr.Checkbox(value=False, label="Stream", info="Stream results", interactive=False)                            
                            with gr.Column():
                                n_ctx_llamacpp = gr.Slider(0, 65536, step=128, value=2048, label="n_ctx", info="Maximum context size")
                            with gr.Column():
                                repeat_penalty_llamacpp = gr.Slider(0.0, 10.0, step=0.1, value=1.1, label="Repeat penalty", info="The penalty to apply to repeated tokens")
                        with gr.Row():
                            with gr.Column():
                                temperature_llamacpp = gr.Slider(0.0, 10.0, step=0.1, value=0.8, label="Temperature", info="Temperature to use for sampling")
                            with gr.Column():
                                top_p_llamacpp = gr.Slider(0.0, 10.0, step=0.05, value=0.95, label="top_p", info="The top-p value to use for sampling")
                            with gr.Column():
                                top_k_llamacpp = gr.Slider(0, 500, step=1, value=40, label="top_k", info="The top-k value to use for sampling")
                    with gr.Row():                            
                        history_llamacpp = gr.Textbox(label="Chatbot history", lines=13, max_lines=13, autoscroll=True, show_copy_button=True, interactive=False)
                        hidden_history_llamacpp = gr.Textbox(label="Chatbot history", visible=False)
                    with gr.Row():                        
                        out_llamacpp = gr.Textbox(label="Chatbot last reply", lines=1, max_lines=1, interactive=False, visible=False)
                    with gr.Row():
                            prompt_llamacpp = gr.Textbox(label="Input", lines=1, max_lines=1, placeholder="Type your request here ...")                            
                            hidden_prompt_llamacpp = gr.Textbox(value="", visible=False)
                            out_llamacpp.change(fn=llamacpp_inference_end, outputs=out_llamacpp)
                            out_llamacpp.change(fn=in_and_out, inputs=hidden_history_llamacpp, outputs=history_llamacpp)
                            out_llamacpp.change(fn=in_and_out, inputs=hidden_prompt_llamacpp, outputs=prompt_llamacpp)
                    with gr.Row():
                        with gr.Column():
                            btn_llamacpp = gr.Button("Generate 🚀", variant="primary")
                        with gr.Column():
                            btn_llamacpp_continue = gr.Button("Continue ➕")                            
                        with gr.Column():
                            btn_llamacpp_clear_input = gr.ClearButton(components=[prompt_llamacpp], value="Clear inputs 🧹")
                        with gr.Column():                      
                            btn_llamacpp_clear_output = gr.ClearButton(components=[out_llamacpp, history_llamacpp, hidden_history_llamacpp], value="Clear all 🧹") 
                        btn_llamacpp.click(
                            fn=text_llamacpp,
                            inputs=[
                                model_llamacpp, 
                                max_tokens_llamacpp,                                 
                                seed_llamacpp, 
                                stream_llamacpp, 
                                n_ctx_llamacpp, 
                                repeat_penalty_llamacpp, 
                                temperature_llamacpp, 
                                top_p_llamacpp, 
                                top_k_llamacpp, 
                                prompt_llamacpp, 
                                history_llamacpp, 
                            ],
                            outputs=[
                                out_llamacpp, 
                                hidden_history_llamacpp, 
                            ],
                            show_progress="full",
                        )
                        prompt_llamacpp.submit(
                            fn=text_llamacpp,
                            inputs=[
                                model_llamacpp, 
                                max_tokens_llamacpp,                                 
                                seed_llamacpp, 
                                stream_llamacpp, 
                                n_ctx_llamacpp, 
                                repeat_penalty_llamacpp, 
                                temperature_llamacpp, 
                                top_p_llamacpp, 
                                top_k_llamacpp,
                                prompt_llamacpp,                             
                                history_llamacpp,                                
                            ],
                            outputs=[
                                out_llamacpp, 
                                hidden_history_llamacpp, 
                            ],
                            show_progress="full",
                        )
                        btn_llamacpp_continue.click(
                            fn=text_llamacpp_continue,
                            inputs=[
                                model_llamacpp, 
                                max_tokens_llamacpp,                                 
                                seed_llamacpp, 
                                stream_llamacpp, 
                                n_ctx_llamacpp, 
                                repeat_penalty_llamacpp, 
                                temperature_llamacpp, 
                                top_p_llamacpp, 
                                top_k_llamacpp,
                                history_llamacpp,                                
                                out_llamacpp,                                   
                            ],
                            outputs=[
                                out_llamacpp, 
                                hidden_history_llamacpp, 
                            ],
                            show_progress="full",
                        )                        
                        btn_llamacpp.click(fn=llamacpp_inference_start, outputs=out_llamacpp)
                        prompt_llamacpp.submit(fn=llamacpp_inference_start, outputs=out_llamacpp)
                        btn_llamacpp_continue.click(fn=llamacpp_inference_start, outputs=out_llamacpp)
                    with gr.Accordion("Send ...", open=False):
                        with gr.Row():
                            with gr.Column():
                                with gr.Box():                                
                                    with gr.Group():
                                        gr.HTML(value='... selected output to ...')
                                        gr.HTML(value='... text module ...')
                                        llamacpp_nllb = gr.Button("✍️ >> Nllb translation")
                                        gr.HTML(value='... image module ...')                                        
                                        llamacpp_txt2img_sd = gr.Button("✍️ >> Stable Diffusion")
                                        llamacpp_txt2img_kd = gr.Button("✍️ >> Kandinsky")                                        
                                        llamacpp_img2img = gr.Button("✍️ >> img2img")
                                        llamacpp_pix2pix = gr.Button("✍️ >> Instruct pix2pix")
                                        llamacpp_inpaint = gr.Button("✍️ >> inpaint")
                                        llamacpp_controlnet = gr.Button("✍️ >> ControlNet")
                                        gr.HTML(value='... audio module ...')
                                        llamacpp_musicgen = gr.Button("✍️ >> Musicgen")                                        
                                        llamacpp_audiogen = gr.Button("✍️ >> Audiogen")
                                        llamacpp_bark = gr.Button("✍️ >> Bark")                                        
                                        gr.HTML(value='... video module ...')                                               
                                        llamacpp_txt2vid_ms = gr.Button("✍️ >> Modelscope")
                                        llamacpp_txt2vid_ze = gr.Button("✍️ >> Text2Video-Zero")                                        
                            with gr.Column():
                                with gr.Box():
                                    with gr.Group():
                                        gr.HTML(value='... input prompt(s) to ...')
                            with gr.Column():
                                with gr.Box():                                
                                    with gr.Group():
                                        gr.HTML(value='... both to ...')
# Image captioning                                        
                with gr.TabItem("Image captioning 👁️", id=13) as tab_img2txt_git:
                    with gr.Accordion("About", open=False):
                        with gr.Box():                       
                            gr.HTML(
                                """
                                <h1 style='text-align: left'; text-decoration: underline;>Informations</h1>
                                <b>Module : </b>Image Captioning</br>
                                <b>Function : </b>Caption an image by a simple description of it using GIT</br>
                                <b>Input(s) : </b>Input image</br>
                                <b>Output(s) : </b>Caption text</br>
                                <b>HF model page : </b><a href='https://huggingface.co/microsoft/git-large-coco' target='_blank'>microsoft/git-large-coco</a></br>
                                """
                            )
                        with gr.Box():
                            gr.HTML(
                                """
                                <h1 style='text-align: left'; text-decoration: underline;>Help</h1>
                                <div style='text-align: justified'>
                                <b>Usage :</b></br>
                                - Upload an input image by clicking on the <b>Input image</b> field</br>
                                - (optional) modify settings to use another model, change min. and/or max. number of tokens generated.</br>
                                - Click the <b>Generate button</b></br>
                                - After generation, captions of the image are displayed in the Generated captions field                                 
                                </div>
                                """
                            )
                    with gr.Accordion("Settings", open=False):
                        with gr.Row():
                            with gr.Column():
                                model_img2txt_git = gr.Dropdown(choices=model_list_img2txt_git, value=model_list_img2txt_git[0], label="Model", info="Choose model to use for inference")
                            with gr.Column():
                                min_tokens_img2txt_git = gr.Slider(0, 128, step=1, value=0, label="Min tokens", info="Minimum number of tokens in output")
                            with gr.Column():
                                max_tokens_img2txt_git = gr.Slider(0, 256, step=1, value=20, label="Max tokens", info="Maximum number of tokens in output")
                        with gr.Row():
                            with gr.Column():
                                num_beams_img2txt_git = gr.Slider(1, 16, step=1, value=1, label="Num beams", info="Number of total beams")
                            with gr.Column():
                                num_beam_groups_img2txt_git = gr.Slider(1, 8, step=1, value=1, label="Num beam groups", info="Number of beam groups")
                                num_beams_img2txt_git.change(set_num_beam_groups_img2txt_git, inputs=[num_beams_img2txt_git, num_beam_groups_img2txt_git], outputs=num_beam_groups_img2txt_git)
                            with gr.Column():
                                diversity_penalty_img2txt_git = gr.Slider(0.0, 5.0, step=0.01, value=0.5, label="Diversity penalty", info="Penalty score value for a beam")
                    with gr.Row():
                        with gr.Column():
                            img_img2txt_git = gr.Image(label="Input image", type="pil", height=400)
                        with gr.Column():
                            out_img2txt_git = gr.Textbox(label="Generated captions", lines=15, interactive=False)
                    with gr.Row():
                        with gr.Column():
                            btn_img2txt_git = gr.Button("Generate 🚀", variant="primary")
                        with gr.Column():
                            btn_img2txt_git_clear_input = gr.ClearButton(components=[img_img2txt_git], value="Clear inputs 🧹")
                        with gr.Column():                            
                            btn_img2txt_git_clear_output = gr.ClearButton(components=[out_img2txt_git], value="Clear outputs 🧹") 
                        btn_img2txt_git.click(
                            fn=text_img2txt_git,
                            inputs=[
                                model_img2txt_git,
                                max_tokens_img2txt_git,
                                min_tokens_img2txt_git,
                                num_beams_img2txt_git, 
                                num_beam_groups_img2txt_git, 
                                diversity_penalty_img2txt_git,
                                img_img2txt_git,                                
                            ],
                            outputs=out_img2txt_git,
                            show_progress="full",
                        )
                    with gr.Accordion("Send ...", open=False):
                        with gr.Row():
                            with gr.Column():
                                with gr.Box():                                
                                    with gr.Group():
                                        gr.HTML(value='... selected output to ...')
                                        gr.HTML(value='... text module ...')
                                        img2txt_git_nllb = gr.Button("✍️ >> Nllb translation")
                                        gr.HTML(value='... image module ...')                                        
                                        img2txt_git_txt2img_sd = gr.Button("✍️ >> Stable Diffusion")
                                        img2txt_git_txt2img_kd = gr.Button("✍️ >> Kandinsky")                                        
                                        img2txt_git_img2img = gr.Button("✍️ >> img2img")
                                        img2txt_git_pix2pix = gr.Button("✍️ >> Instruct pix2pix")
                                        img2txt_git_inpaint = gr.Button("✍️ >> inpaint")
                                        img2txt_git_controlnet = gr.Button("✍️ >> ControlNet")                                        
                                        gr.HTML(value='... audio module ...')
                                        img2txt_git_musicgen = gr.Button("✍️ >> Musicgen")                                        
                                        img2txt_git_audiogen = gr.Button("✍️ >> Audiogen")
                                        gr.HTML(value='... video module ...')                                               
                                        img2txt_git_txt2vid_ms = gr.Button("✍️ >> Modelscope")
                                        img2txt_git_txt2vid_ze = gr.Button("✍️ >> Text2Video-Zero")                                        
                            with gr.Column():
                                with gr.Box():
                                    with gr.Group():
                                        gr.HTML(value='... input prompt(s) to ...')
                            with gr.Column():
                                with gr.Box():                                
                                    with gr.Group():
                                        gr.HTML(value='... both to ...')
                                        img2txt_git_img2img_both = gr.Button("🖼️+✍️ >> img2img")
                                        img2txt_git_pix2pix_both = gr.Button("🖼️+✍️ >> Instruct pix2pix")
                                        img2txt_git_inpaint_both = gr.Button("🖼️+✍️ >> inpaint")

# Whisper 
                with gr.TabItem("Whisper 👂", id=14) as tab_whisper:
                    with gr.Accordion("About", open=False):
                        with gr.Box():                       
                            gr.HTML(
                                """
                                <h1 style='text-align: left'; text-decoration: underline;>Informations</h1>
                                <b>Module : </b>Whisper</br>
                                <b>Function : </b>Transcribe/translate audio to text with <a href='https://openai.com/research/whisper' target='_blank'>whisper</a></br>
                                <b>Input(s) : </b>Input audio</br>
                                <b>Output(s) : </b>Transcribed/translated text</br>
                                <b>HF model page : </b>
                                <a href='https://huggingface.co/openai/whisper-tiny' target='_blank'>openai/whisper-tiny</a>, 
                                <a href='https://huggingface.co/openai/whisper-base' target='_blank'>openai/whisper-base</a>, 
                                <a href='https://huggingface.co/openai/whisper-medium' target='_blank'>openai/whisper-medium</a>,
                                <a href='https://huggingface.co/openai/whisper-large' target='_blank'>openai/whisper-large</a>,
                                <a href='https://huggingface.co/openai/whisper-large-v2' target='_blank'>openai/whisper-large-v2</a></br>
                                """
                            )
                        with gr.Box():
                            gr.HTML(
                                """
                                <h1 style='text-align: left'; text-decoration: underline;>Help</h1>
                                <div style='text-align: justified'>
                                <b>Usage :</b></br>
                                - Upload an input audio file by clicking on the <b>Source audio</b> field or select the <b>micro</b> input type and record your voice</br>
                                - Select the source language of the audio</br>
                                - Select the task to execute : transcribe in source language or translate to english</br>
                                - (optional) modify settings to use another model, or generate SRT-formated subtitles</br>
                                - Click the <b>Generate</b> button</br>
                                - After generation, audio transcription is displayed in the <b>Output text</b> field
                                </div>
                                """
                            )
                    with gr.Accordion("Settings", open=False):
                        with gr.Row():
                            with gr.Column():
                                model_whisper = gr.Dropdown(choices=list(model_list_whisper.keys()), value=list(model_list_whisper.keys())[4], label="Model", info="Choose model to use for inference")
                            with gr.Column():
                                srt_output_whisper = gr.Checkbox(value=False, label=".srt format output", info="Generate an output in .srt format")
                    with gr.Row():
                        with gr.Column():
                            with gr.Row():
                                with gr.Column():
                                    source_type_whisper = gr.Radio(choices=["audio", "micro"], value="audio", label="Input type", info="Choose input type")
                                with gr.Column():
                                    source_language_whisper = gr.Dropdown(choices=language_list_whisper, value=language_list_whisper[13], label="Input language", info="Select input language")    
                            with gr.Row():
                                source_audio_whisper = gr.Audio(label="Source audio", source="upload", type="filepath")
                                source_type_whisper.change(fn=change_source_type_whisper, inputs=source_type_whisper, outputs=source_audio_whisper)
                        with gr.Column():
                            with gr.Row():
                                with gr.Column():
                                    output_type_whisper = gr.Radio(choices=["transcribe", "translate"], value="transcribe", label="Task", info="Choose task to execute")
                                with gr.Column():
                                    output_language_whisper = gr.Dropdown(choices=language_list_whisper, value=language_list_whisper[13], label="Output language", info="Select output language", visible=False, interactive=False)
                            with gr.Row():
                                out_whisper = gr.Textbox(label="Output text", lines=9, max_lines=9, interactive=False)
                                output_type_whisper.change(fn=change_output_type_whisper, inputs=output_type_whisper, outputs=output_language_whisper)
                    with gr.Row():
                        with gr.Column():
                            btn_whisper = gr.Button("Generate 🚀", variant="primary")
                        with gr.Column():
                            btn_whisper_clear_input = gr.ClearButton(components=[source_audio_whisper], value="Clear inputs 🧹")
                        with gr.Column():                            
                            btn_whisper_clear_output = gr.ClearButton(components=[out_whisper], value="Clear outputs 🧹") 
                        btn_whisper.click(
                            fn=text_whisper,
                            inputs=[
                                model_whisper,
                                srt_output_whisper,
                                source_language_whisper,
                                source_audio_whisper,
                                output_type_whisper,
                                output_language_whisper,
                            ],
                            outputs=out_whisper,
                            show_progress="full",
                        )
                    with gr.Accordion("Send ...", open=False):
                        with gr.Row():
                            with gr.Column():
                                with gr.Box():                                
                                    with gr.Group():
                                        gr.HTML(value='... output text to ...')
                                        gr.HTML(value='... text module ...')
                                        whisper_nllb = gr.Button("✍️ >> Nllb translation")
                                        gr.HTML(value='... image module ...')
                                        whisper_txt2img_sd = gr.Button("✍️ >> Stable Diffusion")
                                        whisper_txt2img_kd = gr.Button("✍️ >> Kandinsky")
                                        whisper_img2img = gr.Button("✍️ >> img2img")
                                        whisper_pix2pix = gr.Button("✍️ >> Instruct pix2pix")
                                        whisper_inpaint = gr.Button("✍️ >> inpaint")
                                        whisper_controlnet = gr.Button("✍️ >> ControlNet")
                                        gr.HTML(value='... audio module ...')
                                        whisper_musicgen = gr.Button("✍️ >> Musicgen")
                                        whisper_audiogen = gr.Button("✍️ >> Audiogen")
                                        whisper_bark = gr.Button("✍️ >> Bark")
                                        gr.HTML(value='... video module ...')
                                        whisper_txt2vid_ms = gr.Button("✍️ >> Modelscope")
                                        whisper_txt2vid_ze = gr.Button("✍️ >> Text2Video-Zero")
                            with gr.Column():
                                with gr.Box():
                                    with gr.Group():
                                        gr.HTML(value='... input prompt(s) to ...')
                            with gr.Column():
                                with gr.Box():                                
                                    with gr.Group():
                                        gr.HTML(value='... both to ...')



# nllb 
                with gr.TabItem("nllb translation 👥", id=15) as tab_nllb:
                    with gr.Accordion("About", open=False):
                        with gr.Box():                       
                            gr.HTML(
                                """
                                <h1 style='text-align: left'; text-decoration: underline;>Informations</h1>
                                <b>Module : </b>nllb translation</br>
                                <b>Function : </b>Translate text with <a href='https://ai.meta.com/research/no-language-left-behind/' target='_blank'>nllb</a></br>
                                <b>Input(s) : </b>Input text</br>
                                <b>Output(s) : </b>Translated text</br>
                                <b>HF model page : </b>
                                <a href='https://huggingface.co/facebook/nllb-200-distilled-600M' target='_blank'>facebook/nllb-200-distilled-600M</a>
                                </br>
                                """
                            )
                        with gr.Box():
                            gr.HTML(
                                """
                                <h1 style='text-align: left'; text-decoration: underline;>Help</h1>
                                <div style='text-align: justified'>
                                <b>Usage :</b></br>
                                - Select an <b>input language</b></br>
                                - Type or copy/paste the text to translate in the <b>source text</b> field</br>
                                - Select an <b>output language</b></br>
                                - (optional) modify settings to use another model, or reduce the maximum number of tokens in the output</br>
                                - Click the <b>Generate</b> button</br>
                                - After generation, translation is displayed in the <b>Output text</b> field
                                </div>
                                """
                            )
                    with gr.Accordion("Settings", open=False):
                        with gr.Row():
                            with gr.Column():
                                model_nllb = gr.Dropdown(choices=model_list_nllb, value=model_list_nllb[0], label="Model", info="Choose model to use for inference")
                            with gr.Column():
                                max_tokens_nllb = gr.Slider(0, 1024, step=1, value=1024, label="Max tokens", info="Maximum number of tokens in output")
                    with gr.Row():
                        with gr.Column():
                            with gr.Row():
                                source_language_nllb = gr.Dropdown(choices=list(language_list_nllb.keys()), value=list(language_list_nllb.keys())[47], label="Input language", info="Select input language")    
                            with gr.Row():
                                prompt_nllb = gr.Textbox(label="Source text", lines=9, max_lines=9, placeholder="Type or paste here the text to translate")
                        with gr.Column():
                            with gr.Row():
                                output_language_nllb = gr.Dropdown(choices=list(language_list_nllb.keys()), value=list(language_list_nllb.keys())[47], label="Output language", info="Select output language")
                            with gr.Row():
                                out_nllb = gr.Textbox(label="Output text", lines=9, max_lines=9, interactive=False)
                    with gr.Row():
                        with gr.Column():
                            btn_nllb = gr.Button("Generate 🚀", variant="primary")
                        with gr.Column():
                            btn_nllb_clear_input = gr.ClearButton(components=[prompt_nllb], value="Clear inputs 🧹")
                        with gr.Column():                            
                            btn_nllb_clear_output = gr.ClearButton(components=[out_nllb], value="Clear outputs 🧹") 
                        btn_nllb.click(
                            fn=text_nllb,
                            inputs=[
                                model_nllb,
                                max_tokens_nllb, 
                                source_language_nllb, 
                                prompt_nllb, 
                                output_language_nllb,
                            ],
                            outputs=out_nllb,
                            show_progress="full",
                        )
                    with gr.Accordion("Send ...", open=False):
                        with gr.Row():
                            with gr.Column():
                                with gr.Box():                                
                                    with gr.Group():
                                        gr.HTML(value='... output text to ...')
                                        gr.HTML(value='... image module ...')                                        
                                        nllb_txt2img_sd = gr.Button("✍️ >> Stable Diffusion")
                                        nllb_txt2img_kd = gr.Button("✍️ >> Kandinsky")                                        
                                        nllb_img2img = gr.Button("✍️ >> img2img")
                                        nllb_pix2pix = gr.Button("✍️ >> Instruct pix2pix")
                                        nllb_inpaint = gr.Button("✍️ >> inpaint")
                                        nllb_controlnet = gr.Button("✍️ >> ControlNet")                                        
                                        gr.HTML(value='... audio module ...')
                                        nllb_musicgen = gr.Button("✍️ >> Musicgen")                                        
                                        nllb_audiogen = gr.Button("✍️ >> Audiogen")
                                        nllb_bark = gr.Button("✍️ >> Bark")                                        
                                        gr.HTML(value='... video module ...')                                               
                                        nllb_txt2vid_ms = gr.Button("✍️ >> Modelscope")
                                        nllb_txt2vid_ze = gr.Button("✍️ >> Text2Video-Zero")                                        
                            with gr.Column():
                                with gr.Box():
                                    with gr.Group():
                                        gr.HTML(value='... input prompt(s) to ...')
                            with gr.Column():
                                with gr.Box():                                
                                    with gr.Group():
                                        gr.HTML(value='... both to ...')



# Image
        with gr.TabItem("Image 🖼️", id=2) as tab_image:
            with gr.Tabs() as tabs_image:
# Stable Diffusion
                with gr.TabItem("Stable Diffusion 🖼️", id=21) as tab_txt2img_sd:
                    with gr.Accordion("About", open=False):                
                        with gr.Box():                       
                            gr.HTML(
                                """
                                <h1 style='text-align: left'; text-decoration: underline;>Informations</h1>
                                <b>Module : </b>Stable diffusion</br>
                                <b>Function : </b>Generate images from a prompt and a negative prompt using <a href='https://stability.ai/stablediffusion' target='_blank'>Stable Diffusion</a></br>
                                <b>Input(s) : </b>Prompt, negative prompt</br>
                                <b>Output(s) : </b>Image(s)</br>
                                <b>HF model page : </b>
                                <a href='https://huggingface.co/SG161222/Realistic_Vision_V3.0_VAE' target='_blank'>SG161222/Realistic_Vision_V3.0_VAE</a>,
                                <a href='https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0' target='_blank'>stabilityai/stable-diffusion-xl-base-1.0</a>,
                                <a href='https://huggingface.co/runwayml/stable-diffusion-v1-5' target='_blank'>runwayml/stable-diffusion-v1-5</a>,
                                <a href='https://huggingface.co/nitrosocke/Ghibli-Diffusion' target='_blank'>nitrosocke/Ghibli-Diffusion</a>
                                </br>
                                """
#                                <a href='https://huggingface.co/ckpt/anything-v4.5-vae-swapped' target='_blank'>ckpt/anything-v4.5-vae-swapped</a>,
                            )
                        with gr.Box():
                            gr.HTML(
                                """
                                <h1 style='text-align: left'; text-decoration: underline;>Help</h1>
                                <div style='text-align: justified'>
                                <b>Usage :</b></br>
                                - Fill the <b>prompt</b> with what you want to see in your output image</br>
                                - Fill the <b>negative prompt</b> with what you DO NOT want to see in your output image</br>
                                - (optional) Modify the settings to use another model, generate several images in a single run or change dimensions of the outputs</br>
                                - Click the <b>Generate</b> button</br>
                                - After generation, generated images are displayed in the gallery. Save them individually or create a downloadable zip of the whole gallery.
                                </br>
                                <b>Models :</b></br>
                                - You could place <a href='https://huggingface.co/' target='_blank'>huggingface.co</a> or  <a href='https://www.civitai.com/' target='_blank'>civitai.com</a> Stable diffusion based safetensors models in the directory ./biniou/models/Stable Diffusion. Restart Biniou to see them in the models list.  
                                </div>
                                """
                            )                
                    with gr.Accordion("Settings", open=False):
                        with gr.Row():
                            with gr.Column():
                                model_txt2img_sd = gr.Dropdown(choices=model_list_txt2img_sd, value=model_list_txt2img_sd[0], label="Model", info="Choose model to use for inference")
                            with gr.Column():
                                num_inference_step_txt2img_sd = gr.Slider(1, 100, step=1, value=10, label="Steps", info="Number of iterations per image. Results and speed depends of sampler")
                            with gr.Column():
                                sampler_txt2img_sd = gr.Dropdown(choices=list(SCHEDULER_MAPPING.keys()), value=list(SCHEDULER_MAPPING.keys())[0], label="Sampler", info="Sampler to use for inference")
                        with gr.Row():
                            with gr.Column():
                                guidance_scale_txt2img_sd = gr.Slider(0.1, 20.0, step=0.1, value=7.0, label="CFG scale", info="Low values : more creativity. High values : more corresponding to the prompts")
                            with gr.Column():
                                num_images_per_prompt_txt2img_sd = gr.Slider(minimum=1, maximum=4, step=1, value=1, label="Batch size", info ="Number of images to generate in a single run")
                            with gr.Column():
                                num_prompt_txt2img_sd = gr.Slider(1, 32, step=1, value=1, label="Batch count", info="Number of batch to run successively")
                        with gr.Row():
                            with gr.Column():
                                width_txt2img_sd = gr.Slider(128, 1280, step=64, value=512, label="Image Width", info="Width of outputs")
                            with gr.Column():
                                height_txt2img_sd = gr.Slider(128, 1280, step=64, value=512, label="Image Height", info="Height of outputs")
                            with gr.Column():
                                seed_txt2img_sd = gr.Slider(0, 10000000000, step=1, value=0, label="Seed(0 for random)", info="Seed to use for generation. Depending on scheduler, may permit reproducibility")    
                        with gr.Row():
                            with gr.Column():    
                                use_gfpgan_txt2img_sd = gr.Checkbox(value=True, label="Use GFPGAN to restore faces", info="Use GFPGAN to enhance faces in the outputs")
                            with gr.Column():
                                tkme_txt2img_sd = gr.Slider(0.0, 1.0, step=0.01, value=0.6, label="Token merging ratio", info="0=slow,best quality, 1=fast,worst quality")
                    with gr.Row():
                        with gr.Column():
                            with gr.Row():
                                with gr.Column():                        
                                    prompt_txt2img_sd = gr.Textbox(lines=6, max_lines=6, label="Prompt", info="Describe what you want in your image", placeholder="a cute kitten playing with a ball, dynamic pose, close-up cinematic still, photo realistic, ultra quality, 4k uhd, perfect lighting, HDR, bokeh")
                            with gr.Row():
                                with gr.Column(): 
                                    negative_prompt_txt2img_sd = gr.Textbox(lines=6, max_lines=6, label="Negative Prompt", info="Describe what you DO NOT want in your image", placeholder="out of frame, bad quality, medium quality, blurry, ugly, duplicate, text, characters, logo")
                        with gr.Column(scale=2):
                            out_txt2img_sd = gr.Gallery(
                                label="Generated images",
                                show_label=True,
                                elem_id="gallery",
                                columns=3,
                                height=400,
                            )    
                            gs_out_txt2img_sd = gr.State()
                            sel_out_txt2img_sd = gr.Number(precision=0, visible=False)
                            out_txt2img_sd.select(get_select_index, None, sel_out_txt2img_sd)
                            with gr.Row():
                                with gr.Column():
                                    download_btn_txt2img_sd = gr.Button("Zip gallery 💾")
                                with gr.Column():
                                    download_file_txt2img_sd = gr.File(label="Output", height=30, interactive=False, visible=False)
                                    download_btn_txt2img_sd.click(fn=zip_download_file_txt2img_sd, inputs=out_txt2img_sd, outputs=[download_file_txt2img_sd, download_file_txt2img_sd])
                    with gr.Row():
                        with gr.Column():
                            btn_txt2img_sd = gr.Button("Generate 🚀", variant="primary")
                        with gr.Column():                            
                            btn_txt2img_sd_cancel = gr.Button("Cancel 🛑", variant="stop")
                            btn_txt2img_sd_cancel.click(fn=initiate_stop_txt2img_sd, inputs=None, outputs=None)                              
                        with gr.Column():
                            btn_txt2img_sd_clear_input = gr.ClearButton(components=[prompt_txt2img_sd, negative_prompt_txt2img_sd], value="Clear inputs 🧹")
                        with gr.Column():                            
                            btn_txt2img_sd_clear_output = gr.ClearButton(components=[out_txt2img_sd, gs_out_txt2img_sd], value="Clear outputs 🧹")   
                            btn_txt2img_sd.click(fn=hide_download_file_txt2img_sd, inputs=None, outputs=download_file_txt2img_sd)   
                            btn_txt2img_sd.click(
                            fn=image_txt2img_sd, 
                            inputs=[
                                model_txt2img_sd,
                                sampler_txt2img_sd,
                                prompt_txt2img_sd,
                                negative_prompt_txt2img_sd,
                                num_images_per_prompt_txt2img_sd,
                                num_prompt_txt2img_sd,
                                guidance_scale_txt2img_sd,
                                num_inference_step_txt2img_sd,
                                height_txt2img_sd,
                                width_txt2img_sd,
                                seed_txt2img_sd,
                                use_gfpgan_txt2img_sd,
                                nsfw_filter,
                                tkme_txt2img_sd,
                            ],
                                outputs=[out_txt2img_sd, gs_out_txt2img_sd],
                                show_progress="full",
                            )
                    with gr.Accordion("Send ...", open=False):
                        with gr.Row():
                            with gr.Column():
                                with gr.Box():                                
                                    with gr.Group():
                                        gr.HTML(value='... selected output to ...')
                                        gr.HTML(value='... text module ...')                                        
                                        txt2img_sd_img2txt_git = gr.Button("🖼️ >> GIT Captioning")      
                                        gr.HTML(value='... image module ...')
                                        txt2img_sd_img2img = gr.Button("🖼️ >> img2img")
                                        txt2img_sd_img2var = gr.Button("🖼️ >> Image variation")
                                        txt2img_sd_pix2pix = gr.Button("🖼️ >> Instruct pix2pix")
                                        txt2img_sd_inpaint = gr.Button("🖼️ >> inpaint")
                                        txt2img_sd_controlnet = gr.Button("🖼️ >> ControlNet")
                                        txt2img_sd_faceswap = gr.Button("🖼️ >> Faceswap target")
                                        txt2img_sd_resrgan = gr.Button("🖼️ >> Real ESRGAN")
                                        txt2img_sd_gfpgan = gr.Button("🖼️ >> GFPGAN")
                            with gr.Column():
                                with gr.Box():
                                    with gr.Group():
                                        gr.HTML(value='... input prompt(s) to ...')
                                        gr.HTML(value='... image module ...')
                                        txt2img_sd_txt2img_kd_input = gr.Button("✍️ >> Kandinsky")                                        
                                        txt2img_sd_img2img_input = gr.Button("✍️ >> img2img")
                                        txt2img_sd_pix2pix_input = gr.Button("✍️ >> Instruct pix2pix")
                                        txt2img_sd_inpaint_input = gr.Button("✍️ >> inpaint")
                                        txt2img_sd_controlnet_input = gr.Button("✍️ >> ControlNet")
                                        gr.HTML(value='... video module ...')                                        
                                        txt2img_sd_txt2vid_ms_input = gr.Button("✍️ >> Modelscope")
                                        txt2img_sd_txt2vid_ze_input = gr.Button("✍️ >> Text2Video-Zero")                                        
                            with gr.Column():
                                with gr.Box():                                
                                    with gr.Group():
                                        gr.HTML(value='... both to ...')
                                        gr.HTML(value='... image module ...')                                                                            
                                        txt2img_sd_img2img_both = gr.Button("🖼️ + ✍️ >> img2img")
                                        txt2img_sd_pix2pix_both = gr.Button("🖼️ + ✍️ >> Instruct pix2pix")
                                        txt2img_sd_inpaint_both = gr.Button("🖼️ + ✍️ >> inpaint")
                                        txt2img_sd_controlnet_both = gr.Button("🖼️ + ✍️️ >> ControlNet")                                        

# Kandinsky
                if ram_size() >= 16 :
                    titletab_txt2img_kd = "Kandinsky 🖼️"
                else :
                    titletab_txt2img_kd = "Kandinsky 🖼️ ⛔"

                with gr.TabItem(titletab_txt2img_kd, id=22) as tab_txt2img_kd:                    
                    with gr.Accordion("About", open=False):                
                        with gr.Box():                       
                            gr.HTML(
                                """
                                <h1 style='text-align: left; text-decoration: underline;'>Informations</h1>
                                <b>Module : </b>Kandinsky</br>
                                <b>Function : </b>Generate images from a prompt and a negative prompt using <a href='https://github.com/ai-forever/Kandinsky-2' target='_blank'>Kandinsky</a></br>
                                <b>Input(s) : </b>Prompt, negative prompt</br>
                                <b>Output(s) : </b>Image(s)</br>
                                <b>HF model page : </b>
                                <a href='https://huggingface.co/kandinsky-community/kandinsky-2-2-decoder' target='_blank'>kandinsky-community/kandinsky-2-2-decoder</a>,
                                <a href='https://huggingface.co/kandinsky-community/kandinsky-2-1' target='_blank'>kandinsky-community/kandinsky-2-1</a></br>
                                """
                            )
                        with gr.Box():
                            gr.HTML(
                                """
                                <h1 style='text-align: left; text-decoration: underline;'>Help</h1>
                                <div style='text-align: justified'>
                                <b>Usage :</b></br>
                                - Fill the <b>prompt</b> with what you want to see in your output image</br>
                                - Fill the <b>negative prompt</b> with what you DO NOT want to see in your output image</br>
                                - (optional) Modify the settings to use another model, generate several images in a single run or change dimensions of the outputs</br>
                                - Click the <b>generate</b> button</br>
                                - After generation, generated images are displayed in the gallery. Save them individually or create a downloadable zip of the whole gallery.                                
                                </div>
                                """
                            )                                
                    with gr.Accordion("Settings", open=False):
                        with gr.Row():
                            with gr.Column():
                                model_txt2img_kd = gr.Dropdown(choices=model_list_txt2img_kd, value=model_list_txt2img_kd[0], label="Model", info="Choose model to use for inference")
                            with gr.Column():
                                num_inference_step_txt2img_kd = gr.Slider(1, 100, step=1, value=25, label="Steps", info="Number of iterations per image. Results and speed depends of sampler")
                            with gr.Column():
                                sampler_txt2img_kd = gr.Dropdown(choices=list(SCHEDULER_MAPPING.keys()), value=list(SCHEDULER_MAPPING.keys())[5], label="Sampler", info="Sampler to use for inference")
                        with gr.Row():
                            with gr.Column():
                                guidance_scale_txt2img_kd = gr.Slider(0.1, 20.0, step=0.1, value=4.0, label="CFG scale", info="Low values : more creativity. High values : more corresponding to the prompts")
                            with gr.Column():
                                num_images_per_prompt_txt2img_kd = gr.Slider(1, 4, step=1, value=1, label="Batch size", info ="Number of images to generate in a single run")
                            with gr.Column():
                                num_prompt_txt2img_kd = gr.Slider(1, 32, step=1, value=1, label="Batch count", info="Number of batch to run successively")
                        with gr.Row():
                            with gr.Column():
                                width_txt2img_kd = gr.Slider(128, 1280, step=64, value=512, label="Image Width", info="Width of outputs")
                            with gr.Column():
                                height_txt2img_kd = gr.Slider(128, 1280, step=64, value=512, label="Image Height", info="Height of outputs")
                            with gr.Column():
                                seed_txt2img_kd = gr.Slider(0, 10000000000, step=1, value=0, label="Seed(0 for random)", info="Seed to use for generation. Depending on scheduler, may permit reproducibility")
                        with gr.Row():
                            with gr.Column():    
                                use_gfpgan_txt2img_kd = gr.Checkbox(value=True, label="Use GFPGAN to restore faces", info="Use GFPGAN to enhance faces in the outputs")
                    with gr.Row():
                        with gr.Column():
                            with gr.Row():
                                with gr.Column():   
                                    prompt_txt2img_kd = gr.Textbox(lines=6, max_lines=6, label="Prompt", info="Describe what you want in your image", placeholder="An alien cheeseburger creature eating itself, claymation, cinematic, moody lighting")
                            with gr.Row():
                                with gr.Column():
                                    negative_prompt_txt2img_kd = gr.Textbox(lines=6, max_lines=6, label="Negative Prompt", info="Describe what you DO NOT want in your image", placeholder="low quality, bad quality")
                        with gr.Column(scale=2):
                            out_txt2img_kd = gr.Gallery(
                                label="Generated images",
                                show_label=True,
                                elem_id="gallery_k",
                                columns=3,
                                height=400,
                            )
                            gs_out_txt2img_kd = gr.State()
                            sel_out_txt2img_kd = gr.Number(precision=0, visible=False)                        
                            out_txt2img_kd.select(get_select_index, None, sel_out_txt2img_kd)
                            with gr.Row():
                                with gr.Column():
                                    download_btn_txt2img_kd = gr.Button("Zip gallery 💾")
                                with gr.Column():
                                    download_file_txt2img_kd = gr.File(label="Output", height=30, interactive=False, visible=False)
                                    download_btn_txt2img_kd.click(fn=zip_download_file_txt2img_kd, inputs=out_txt2img_kd, outputs=[download_file_txt2img_kd, download_file_txt2img_kd])                            
                    with gr.Row():
                        with gr.Column():
                            btn_txt2img_kd = gr.Button("Generate 🚀", variant="primary")
                        with gr.Column():                            
                            btn_txt2img_kd_cancel = gr.Button("Cancel 🛑", variant="stop")
                            btn_txt2img_kd_cancel.click(fn=initiate_stop_txt2img_kd, inputs=None, outputs=None)                              
                        with gr.Column():
                            btn_txt2img_kd_clear_input = gr.ClearButton(components=[prompt_txt2img_kd, negative_prompt_txt2img_kd], value="Clear inputs 🧹")
                        with gr.Column():                            
                            btn_txt2img_kd_clear_output = gr.ClearButton(components=[out_txt2img_kd, gs_out_txt2img_kd], value="Clear outputs 🧹")
                            btn_txt2img_kd.click(fn=hide_download_file_txt2img_kd, inputs=None, outputs=download_file_txt2img_kd)                              
                            btn_txt2img_kd.click(
                                fn=image_txt2img_kd,
                                inputs=[
                                    model_txt2img_kd,
                                    sampler_txt2img_kd,
                                    prompt_txt2img_kd,
                                    negative_prompt_txt2img_kd,
                                    num_images_per_prompt_txt2img_kd,
                                    num_prompt_txt2img_kd,
                                    guidance_scale_txt2img_kd,
                                    num_inference_step_txt2img_kd,
                                    height_txt2img_kd,
                                    width_txt2img_kd,
                                    seed_txt2img_kd,
                                    use_gfpgan_txt2img_kd,
                                ],
                                outputs=[out_txt2img_kd, gs_out_txt2img_kd],
                                show_progress="full",
                            )                    
                    with gr.Accordion("Send ...", open=False):
                        with gr.Row():
                            with gr.Column():
                                with gr.Box():                                
                                    with gr.Group():
                                        gr.HTML(value='... selected output to ...')
                                        gr.HTML(value='... text module ...')
                                        txt2img_kd_img2txt_git = gr.Button("🖼️ >> GIT Captioning")
                                        gr.HTML(value='... image module ...')
                                        txt2img_kd_img2img = gr.Button("🖼️ >> img2img")
                                        txt2img_kd_img2var = gr.Button("🖼️ >> Image variation")
                                        txt2img_kd_pix2pix = gr.Button("🖼️ >> Instruct pix2pix")
                                        txt2img_kd_inpaint = gr.Button("🖼️ >> inpaint")
                                        txt2img_kd_controlnet = gr.Button("🖼️ >> ControlNet")
                                        txt2img_kd_faceswap = gr.Button("🖼️ >> Faceswap target")
                                        txt2img_kd_resrgan = gr.Button("🖼️ >> Real ESRGAN")
                                        txt2img_kd_gfpgan = gr.Button("🖼️ >> GFPGAN")
                            with gr.Column():
                                with gr.Box():
                                    with gr.Group():
                                        gr.HTML(value='... input prompt(s) to ...')
                                        gr.HTML(value='... image module ...')
                                        txt2img_kd_txt2img_sd_input = gr.Button("✍️ >> Stable Diffusion")
                                        txt2img_kd_img2img_input = gr.Button("✍️ >> img2img")
                                        txt2img_kd_pix2pix_input = gr.Button("✍️ >> Instruct pix2pix")
                                        txt2img_kd_inpaint_input = gr.Button("✍️ >> inpaint")
                                        txt2img_kd_controlnet_input = gr.Button("✍️ >> ControlNet")                                        
                                        gr.HTML(value='... video module ...')                                                                                
                                        txt2img_kd_txt2vid_ms_input = gr.Button("✍️ >> Modelscope")
                                        txt2img_kd_txt2vid_ze_input = gr.Button("✍️ >> Text2Video-Zero")
                            with gr.Column():
                                with gr.Box():                                
                                    with gr.Group():
                                        gr.HTML(value='... both to ...')
                                        gr.HTML(value='... image module ...')
                                        txt2img_kd_img2img_both = gr.Button("🖼️ + ✍️ >> img2img")
                                        txt2img_kd_pix2pix_both = gr.Button("🖼️ + ✍️ >> Instruct pix2pix")
                                        txt2img_kd_inpaint_both = gr.Button("🖼️ + ✍️ >> inpaint")
                                        txt2img_kd_controlnet_both = gr.Button("🖼️ + ✍️ >> ControlNet")                                        
# img2img    
                with gr.TabItem("img2img 🖌️", id=23) as tab_img2img:
                    with gr.Accordion("About", open=False):                
                        with gr.Box():                       
                            gr.HTML(
                                """
                                <h1 style='text-align: left'; text-decoration: underline;>Informations</h1>
                                <b>Module : </b>Img2img</br>
                                <b>Function : </b>Generate images variations of an input image, from a prompt and a negative prompt using <a href='https://stability.ai/stablediffusion' target='_blank'>Stable Diffusion</a></br>
                                You could use this module to refine an image produced by another module.</br>
                                <b>Input(s) : </b>Input image, prompt, negative prompt</br>
                                <b>Output(s) : </b>Image(s)</br>
                                <b>HF model page : </b>
                                <a href='https://huggingface.co/SG161222/Realistic_Vision_V3.0_VAE' target='_blank'>SG161222/Realistic_Vision_V3.0_VAE</a>,
                                <a href='https://huggingface.co/stabilityai/stable-diffusion-xl-refiner-1.0' target='_blank'>stabilityai/stable-diffusion-xl-refiner-1.0</a>,
                                <a href='https://huggingface.co/runwayml/stable-diffusion-v1-5' target='_blank'>runwayml/stable-diffusion-v1-5</a>,
                                <a href='https://huggingface.co/nitrosocke/Ghibli-Diffusion' target='_blank'>nitrosocke/Ghibli-Diffusion</a>
                                """
#                                <a href='https://huggingface.co/ckpt/anything-v4.5-vae-swapped' target='_blank'>ckpt/anything-v4.5-vae-swapped</a>,
                            )
                        with gr.Box():
                            gr.HTML(
                                """
                                <h1 style='text-align: left'; text-decoration: underline;>Help</h1>
                                <div style='text-align: justified'>
                                <b>Usage :</b></br>
                                - Upload, import an image or draw a sketch as an <b>Input image</b></br>
                                - Set the balance between the input image and the prompt (<b>denoising strength</b>) to a value between 0 and 1 : 0 will completely ignore the prompt, 1 will completely ignore the input image</br>                                
                                - Fill the <b>prompt</b> with what you want to see in your output image</br>
                                - Fill the <b>negative prompt</b> with what you DO NOT want to see in your output image</br>
                                - (optional) Modify the settings to use another model or generate several images in a single run</br>
                                - Click the <b>Generate</b> button</br>
                                - After generation, generated images are displayed in the gallery. Save them individually or create a downloadable zip of the whole gallery.
                                </br>
                                <b>Models :</b></br>
                                - You could place <a href='https://huggingface.co/' target='_blank'>huggingface.co</a> or  <a href='https://www.civitai.com/' target='_blank'>civitai.com</a> Stable diffusion based safetensors models in the directory /biniou/models/Stable Diffusion. Restart Biniou to see them in the models list. 
                                </div>
                                """
                            )               
                    with gr.Accordion("Settings", open=False):
                        with gr.Row():
                            with gr.Column():
                                model_img2img = gr.Dropdown(choices=model_list_img2img, value=model_list_img2img[0], label="Model", info="Choose model to use for inference")
                            with gr.Column():
                                num_inference_step_img2img = gr.Slider(1, 100, step=1, value=10, label="Steps", info="Number of iterations per image. Results and speed depends of sampler")
                            with gr.Column():
                                sampler_img2img = gr.Dropdown(choices=list(SCHEDULER_MAPPING.keys()), value=list(SCHEDULER_MAPPING.keys())[0], label="Sampler", info="Sampler to use for inference")
                        with gr.Row():
                            with gr.Column():
                                guidance_scale_img2img = gr.Slider(0.0, 10.0, step=0.1, value=7.5, label="CFG Scale", info="Low values : more creativity. High values : more corresponding to the prompts")
                            with gr.Column():
                                num_images_per_prompt_img2img = gr.Slider(1, 4, step=1, value=1, label="Batch size", info ="Number of images to generate in a single run")
                            with gr.Column():
                                num_prompt_img2img = gr.Slider(1, 32, step=1, value=1, label="Batch count", info="Number of batch to run successively")
                        with gr.Row():
                            with gr.Column():
                                width_img2img = gr.Slider(128, 8192, step=64, value=512, label="Image Width", info="Width of outputs", interactive=False)
                            with gr.Column():
                                height_img2img = gr.Slider(128, 8192, step=64, value=512, label="Image Height", info="Height of outputs", interactive=False)
                            with gr.Column():
                                seed_img2img = gr.Slider(0, 10000000000, step=1, value=0, label="Seed(0 for random)", info="Seed to use for generation. Depending on scheduler, may permit reproducibility")
                        with gr.Row():
                            with gr.Column():    
                                use_gfpgan_img2img = gr.Checkbox(value=True, label="Use GFPGAN to restore faces", info="Use GFPGAN to enhance faces in the outputs")
                            with gr.Column():
                                tkme_img2img = gr.Slider(0.0, 1.0, step=0.01, value=0.6, label="Token merging ratio", info="0=slow,best quality, 1=fast,worst quality")                          
                    with gr.Row():
                        with gr.Column():
                            img_img2img = gr.Image(label="Input image", height=400, type="filepath")
                            with gr.Row():
                                source_type_img2img = gr.Radio(choices=["image", "sketch"], value="image", label="Input type", info="Choose input type")
                                img_img2img.change(image_upload_event, inputs=img_img2img, outputs=[width_img2img, height_img2img])
                                source_type_img2img.change(fn=change_source_type_img2img, inputs=source_type_img2img, outputs=img_img2img)                                
                        with gr.Column():
                            with gr.Row(): 
                                with gr.Column():
                                    denoising_strength_img2img = gr.Slider(0.0, 1.0, step=0.01, value=0.75, label="Denoising strength", info="Balance between input image (0) and prompts (1)")  
                            with gr.Row():
                                with gr.Column():
                                    prompt_img2img = gr.Textbox(lines=5, max_lines=5, label="Prompt", info="Describe what you want in your image", placeholder="a cute kitten playing with a ball, dynamic pose, close-up cinematic still, photo realistic, ultra quality, 4k uhd, perfect lighting, HDR, bokeh")
                            with gr.Row():                                    
                                with gr.Column():
                                    negative_prompt_img2img = gr.Textbox(lines=5, max_lines=5, label="Negative Prompt", info="Describe what you DO NOT want in your image", placeholder="out of frame, bad quality, medium quality, blurry, ugly, duplicate, text, characters, logo")
                        with gr.Column():
                            with gr.Row():
                                with gr.Column():                            
                                    out_img2img = gr.Gallery(
                                        label="Generated images",
                                        show_label=True,
                                        elem_id="gallery_i2i",
                                        columns=2,
                                        height=400,
                                        preview=True,
                                )
                                gs_out_img2img = gr.State()
                                sel_out_img2img = gr.Number(precision=0, visible=False)                              
                                out_img2img.select(get_select_index, None, sel_out_img2img)
                                with gr.Row():
                                    with gr.Column():
                                        download_btn_img2img = gr.Button("Zip gallery 💾")
                                    with gr.Column():
                                        download_file_img2img = gr.File(label="Output", height=30, interactive=False, visible=False)
                                        download_btn_img2img.click(fn=zip_download_file_img2img, inputs=out_img2img, outputs=[download_file_img2img, download_file_img2img])                                
                    with gr.Row():
                        with gr.Column():
                            btn_img2img = gr.Button("Generate 🚀", variant="primary")
                        with gr.Column():                            
                            btn_img2img_cancel = gr.Button("Cancel 🛑", variant="stop")
                            btn_img2img_cancel.click(fn=initiate_stop_img2img, inputs=None, outputs=None)                              
                        with gr.Column():
                            btn_img2img_clear_input = gr.ClearButton(components=[img_img2img, prompt_img2img, negative_prompt_img2img], value="Clear inputs 🧹")
                        with gr.Column():                            
                            btn_img2img_clear_output = gr.ClearButton(components=[out_img2img, gs_out_img2img], value="Clear outputs 🧹")
                            btn_img2img.click(fn=hide_download_file_img2img, inputs=None, outputs=download_file_img2img)                             
                            btn_img2img.click(
                                fn=image_img2img,
                                inputs=[
                                    model_img2img,
                                    sampler_img2img,
                                    img_img2img,
                                    prompt_img2img,
                                    negative_prompt_img2img,
                                    num_images_per_prompt_img2img,
                                    num_prompt_img2img,
                                    guidance_scale_img2img,
                                    denoising_strength_img2img,
                                    num_inference_step_img2img,
                                    height_img2img,
                                    width_img2img,
                                    seed_img2img,
                                    source_type_img2img,
                                    use_gfpgan_img2img,
                                    nsfw_filter,
                                    tkme_img2img,
                                ],
                                outputs=[out_img2img, gs_out_img2img], 
                                show_progress="full",
                            )  
                    with gr.Accordion("Send ...", open=False):
                        with gr.Row():
                            with gr.Column():
                                with gr.Box():                                
                                    with gr.Group():
                                        gr.HTML(value='... selected output to ...')
                                        gr.HTML(value='... text module ...')
                                        img2img_img2txt_git = gr.Button("🖼️ >> GIT Captioning")
                                        gr.HTML(value='... image module ...')
                                        img2img_img2img = gr.Button("🖼️ >> img2img")
                                        img2img_img2var = gr.Button("🖼️ >> Image variation")
                                        img2img_pix2pix = gr.Button("🖼️ >> Instruct pix2pix")
                                        img2img_inpaint = gr.Button("🖼️ >> inpaint")
                                        img2img_controlnet = gr.Button("🖼️ >> ControlNet")
                                        img2img_faceswap = gr.Button("🖼️ >> Faceswap target")
                                        img2img_resrgan = gr.Button("🖼️ >> Real ESRGAN")
                                        img2img_gfpgan = gr.Button("🖼️ >> GFPGAN")
                            with gr.Column():
                                with gr.Box():
                                    with gr.Group():
                                        gr.HTML(value='... input prompt(s) to ...')
                                        gr.HTML(value='... image module ...')
                                        img2img_txt2img_sd_input = gr.Button("✍️ >> Stable Diffusion")
                                        img2img_txt2img_kd_input = gr.Button("✍️ >> Kandinsky")
                                        img2img_pix2pix_input = gr.Button("✍️ >> Instruct pix2pix")
                                        img2img_inpaint_input = gr.Button("✍️ >> inpaint")
                                        img2img_controlnet_input = gr.Button("✍️ >> ControlNet")                                        
                            with gr.Column():
                                with gr.Box():                                
                                    with gr.Group():
                                        gr.HTML(value='... both to ...')
                                        gr.HTML(value='... image module ...')
                                        img2img_pix2pix_both = gr.Button("🖼️ + ✍️ >> Instruct pix2pix")
                                        img2img_inpaint_both = gr.Button("🖼️ + ✍️ >> inpaint")
                                        img2img_controlnet_both = gr.Button("🖼️ + ✍️ >> ControlNet")
                                        
# img2var    
                if ram_size() >= 16 :
                    titletab_img2var = "Image variation 🖌️"
                else :
                    titletab_img2var = "Image variation 🖌️ ⛔"

                with gr.TabItem(titletab_img2var, id=24) as tab_img2var: 
                    with gr.Accordion("About", open=False):                
                        with gr.Box():                       
                            gr.HTML(
                                """
                                <h1 style='text-align: left'; text-decoration: underline;>Informations</h1>
                                <b>Module : </b>Image variation</br>
                                <b>Function : </b>Generate variations of an input image using <a href='https://stability.ai/stablediffusion' target='_blank'>Stable Diffusion</a></br>
                                <b>Input(s) : </b>Input image</br>
                                <b>Output(s) : </b>Image(s)</br>
                                <b>HF model page : </b>
                                <a href='https://huggingface.co/lambdalabs/sd-image-variations-diffusers' target='_blank'>lambdalabs/sd-image-variations-diffusers</a>
                                """
                            )
                        with gr.Box():
                            gr.HTML(
                                """
                                <h1 style='text-align: left'; text-decoration: underline;>Help</h1>
                                <div style='text-align: justified'>
                                <b>Usage :</b></br>
                                - Upload or import an image as an <b>Input image</b></br>
                                - (optional) Modify the settings to generate several images in a single run</br>
                                - Click the <b>Generate</b> button</br>
                                - After generation, generated images are displayed in the gallery. Save them individually or create a downloadable zip of the whole gallery.
                                </br>
                                """
                            )               
                    with gr.Accordion("Settings", open=False):
                        with gr.Row():
                            with gr.Column():
                                model_img2var = gr.Dropdown(choices=model_list_img2var, value=model_list_img2var[0], label="Model", info="Choose model to use for inference")
                            with gr.Column():
                                num_inference_step_img2var = gr.Slider(1, 100, step=1, value=10, label="Steps", info="Number of iterations per image. Results and speed depends of sampler")
                            with gr.Column():
                                sampler_img2var = gr.Dropdown(choices=list(SCHEDULER_MAPPING.keys()), value=list(SCHEDULER_MAPPING.keys())[0], label="Sampler", info="Sampler to use for inference")
                        with gr.Row():
                            with gr.Column():
                                guidance_scale_img2var = gr.Slider(0.0, 10.0, step=0.1, value=7.5, label="CFG Scale", info="Low values : more creativity. High values : more corresponding to the prompts")
                            with gr.Column():
                                num_images_per_prompt_img2var = gr.Slider(1, 4, step=1, value=1, label="Batch size", info ="Number of images to generate in a single run")
                            with gr.Column():
                                num_prompt_img2var = gr.Slider(1, 32, step=1, value=1, label="Batch count", info="Number of batch to run successively")
                        with gr.Row():
                            with gr.Column():
                                width_img2var = gr.Slider(128, 8192, step=64, value=512, label="Image Width", info="Width of outputs", interactive=False)
                            with gr.Column():
                                height_img2var = gr.Slider(128, 8192, step=64, value=512, label="Image Height", info="Height of outputs", interactive=False)
                            with gr.Column():
                                seed_img2var = gr.Slider(0, 10000000000, step=1, value=0, label="Seed(0 for random)", info="Seed to use for generation. Depending on scheduler, may permit reproducibility")
                        with gr.Row():
                            with gr.Column():    
                                use_gfpgan_img2var = gr.Checkbox(value=True, label="Use GFPGAN to restore faces", info="Use GFPGAN to enhance faces in the outputs")
                            with gr.Column():
                                tkme_img2var = gr.Slider(0.0, 1.0, step=0.01, value=0.6, label="Token merging ratio", info="0=slow,best quality, 1=fast,worst quality")                          
                    with gr.Row():
                        with gr.Column():
                            img_img2var = gr.Image(label="Input image", height=400, type="filepath")
                            img_img2var.change(image_upload_event, inputs=img_img2var, outputs=[width_img2var, height_img2var])
                        with gr.Column():
                            with gr.Row():
                                out_img2var = gr.Gallery(
                                    label="Generated images",
                                    show_label=True,
                                    elem_id="gallery_i2v",
                                    columns=2,
                                    height=400,
                                    preview=True,
                                )
                                gs_out_img2var = gr.State()
                                sel_out_img2var = gr.Number(precision=0, visible=False)                              
                                out_img2var.select(get_select_index, None, sel_out_img2var)
                            with gr.Row():
                                with gr.Column():
                                    download_btn_img2var = gr.Button("Zip gallery 💾")
                                with gr.Column():
                                    download_file_img2var = gr.File(label="Output", height=30, interactive=False, visible=False)
                                    download_btn_img2var.click(fn=zip_download_file_img2var, inputs=out_img2var, outputs=[download_file_img2var, download_file_img2var])                                
                    with gr.Row():
                        with gr.Column():
                            btn_img2var = gr.Button("Generate 🚀", variant="primary")
                        with gr.Column():                            
                            btn_img2var_cancel = gr.Button("Cancel 🛑", variant="stop")
                            btn_img2var_cancel.click(fn=initiate_stop_img2var, inputs=None, outputs=None)                              
                        with gr.Column():
                            btn_img2var_clear_input = gr.ClearButton(components=[img_img2var], value="Clear inputs 🧹")
                        with gr.Column():                            
                            btn_img2var_clear_output = gr.ClearButton(components=[out_img2var, gs_out_img2var], value="Clear outputs 🧹")
                            btn_img2var.click(fn=hide_download_file_img2var, inputs=None, outputs=download_file_img2var)                             
                            btn_img2var.click(
                                fn=image_img2var,
                                inputs=[
                                    model_img2var,
                                    sampler_img2var,
                                    img_img2var,
                                    num_images_per_prompt_img2var,
                                    num_prompt_img2var,
                                    guidance_scale_img2var,
                                    num_inference_step_img2var,
                                    height_img2var,
                                    width_img2var,
                                    seed_img2var,
                                    use_gfpgan_img2var,
                                    nsfw_filter,
                                    tkme_img2var,
                                ],
                                outputs=[out_img2var, gs_out_img2var], 
                                show_progress="full",
                            )  
                    with gr.Accordion("Send ...", open=False):
                        with gr.Row():
                            with gr.Column():
                                with gr.Box():                                
                                    with gr.Group():
                                        gr.HTML(value='... selected output to ...')
                                        gr.HTML(value='... text module ...')
                                        img2var_img2txt_git = gr.Button("🖼️ >> GIT Captioning")
                                        gr.HTML(value='... image module ...')
                                        img2var_img2img = gr.Button("🖼️ >> img2img")
                                        img2var_img2var = gr.Button("🖼️ >> Image variation")
                                        img2var_pix2pix = gr.Button("🖼️ >> Instruct pix2pix")
                                        img2var_inpaint = gr.Button("🖼️ >> inpaint")
                                        img2var_controlnet = gr.Button("🖼️ >> ControlNet")
                                        img2var_faceswap = gr.Button("🖼️ >> Faceswap target")
                                        img2var_resrgan = gr.Button("🖼️ >> Real ESRGAN")
                                        img2var_gfpgan = gr.Button("🖼️ >> GFPGAN")
                            with gr.Column():
                                with gr.Box():
                                    with gr.Group():
                                        gr.HTML(value='... input prompt(s) to ...')
                            with gr.Column():
                                with gr.Box():                                
                                    with gr.Group():
                                        gr.HTML(value='... both to ...')
                                       

# pix2pix    
                with gr.TabItem("Instruct pix2pix 🖌️", id=25) as tab_pix2pix:
                    with gr.Accordion("About", open=False):                
                        with gr.Box():                       
                            gr.HTML(
                                """
                                <h1 style='text-align: left'; text-decoration: underline;>Informations</h1>
                                <b>Module : </b>Instruct pix2pix</br>
                                <b>Function : </b>Edit an input image with instructions from a prompt and a negative prompt using <a href='https://github.com/timothybrooks/instruct-pix2pix' target='_blank'>Instructpix2pix</a></br>
                                <b>Input(s) : </b>Input image, prompt, negative prompt</br>
                                <b>Output(s) : </b>Image(s)</br>
                                <b>HF model page : </b>
                                <a href='https://huggingface.co/timbrooks/instruct-pix2pix' target='_blank'>timbrooks/instruct-pix2pix</a></br>
                                """
                            )
                        with gr.Box():
                            gr.HTML(
                                """
                                <h1 style='text-align: left'; text-decoration: underline;>Help</h1>
                                <div style='text-align: justified'>
                                <b>Usage :</b></br>
                                - Upload or import an image using the <b>Input image</b> field</br>
                                - Fill the <b>prompt</b> with the instructions for modifying your input image</br>
                                - Fill the <b>negative prompt</b> with what you DO NOT want to see in your output image</br>
                                - (optional) Modify the settings to generate several images in a single run or generate several images in a single run</br>
                                - Click the <b>Generate</b> button</br>
                                - After generation, generated images are displayed in the gallery. Save them individually or create a downloadable zip of the whole gallery</br></br>
                                <b>Examples : </b><a href='https://www.timothybrooks.com/instruct-pix2pix/' target='_blank'>InstructPix2Pix : Learning to Follow Image Editing Instructions</a>
                                </div>
                                """
                            )                
                    with gr.Accordion("Settings", open=False):
                        with gr.Row():
                            with gr.Column():
                                model_pix2pix = gr.Dropdown(choices=model_list_pix2pix, value=model_list_pix2pix[0], label="Model", info="Choose model to use for inference")
                            with gr.Column():
                                num_inference_step_pix2pix = gr.Slider(1, 100, step=1, value=10, label="Steps", info="Number of iterations per image. Results and speed depends of sampler")
                            with gr.Column():
                                sampler_pix2pix = gr.Dropdown(choices=list(SCHEDULER_MAPPING.keys()), value=list(SCHEDULER_MAPPING.keys())[0], label="Sampler", info="Sampler to use for inference")
                        with gr.Row():
                            with gr.Column():
                                guidance_scale_pix2pix = gr.Slider(0.0, 10.0, step=0.1, value=7.5, label="CFG Scale", info="Low values : more creativity. High values : more corresponding to the prompts")
                            with gr.Column():
                                image_guidance_scale_pix2pix = gr.Slider(0.0, 10.0, step=0.1, value=1.5, label="Img CFG Scale", info="Low values : more creativity. High values : more corresponding to the input image")
                            with gr.Column():
                                num_images_per_prompt_pix2pix = gr.Slider(1, 4, step=1, value=1, label="Batch size", info ="Number of images to generate in a single run")
                            with gr.Column():
                                num_prompt_pix2pix = gr.Slider(1, 32, step=1, value=1, label="Batch count", info="Number of batch to run successively")
                        with gr.Row():
                            with gr.Column():
                                width_pix2pix = gr.Slider(128, 8192, step=64, value=512, label="Image Width", info="Width of outputs", interactive=False)
                            with gr.Column():
                                height_pix2pix = gr.Slider(128, 8192, step=64, value=512, label="Image Height", info="Height of outputs", interactive=False)
                            with gr.Column():
                                seed_pix2pix = gr.Slider(0, 10000000000, step=1, value=0, label="Seed(0 for random)", info="Seed to use for generation. Depending on scheduler, may permit reproducibility")
                        with gr.Row():
                            with gr.Column():    
                                use_gfpgan_pix2pix = gr.Checkbox(value=True, label="Use GFPGAN to restore faces", info="Use GFPGAN to enhance faces in the outputs")
                            with gr.Column():
                                tkme_pix2pix = gr.Slider(0.0, 1.0, step=0.01, value=0.6, label="Token merging ratio", info="0=slow,best quality, 1=fast,worst quality")
                    with gr.Row():
                        with gr.Column():
                             img_pix2pix = gr.Image(label="Input image", height=400, type="filepath")
                             img_pix2pix.change(image_upload_event, inputs=img_pix2pix, outputs=[width_pix2pix, height_pix2pix])
                        with gr.Column():
                            with gr.Row():
                                with gr.Column():
                                    prompt_pix2pix = gr.Textbox(lines=6, max_lines=6, label="Prompt", info="Describe what you want to modify in your input image", placeholder="make it a Rembrandt painting")
                                with gr.Column():
                                    negative_prompt_pix2pix = gr.Textbox(lines=6, max_lines=6, label="Negative Prompt", info="Describe what you DO NOT want in your output image", placeholder="out of frame, bad quality, medium quality, blurry, ugly, duplicate, text, characters, logo")
                        with gr.Column():
                            with gr.Row():
                                with gr.Column():
                                    out_pix2pix = gr.Gallery(
                                        label="Generated images",
                                        show_label=True,
                                        elem_id="gallery_p2p",
                                        columns=2,
                                        height=400,
                                        preview=True,                                        
                                    )
                                    gs_out_pix2pix = gr.State()
                                    sel_out_pix2pix = gr.Number(precision=0, visible=False)                        
                                    out_pix2pix.select(get_select_index, None, sel_out_pix2pix)
                                    with gr.Row():
                                        with gr.Column():
                                            download_btn_pix2pix = gr.Button("Zip gallery 💾")
                                        with gr.Column():
                                            download_file_pix2pix = gr.File(label="Output", height=30, interactive=False, visible=False)
                                            download_btn_pix2pix.click(fn=zip_download_file_pix2pix, inputs=out_pix2pix, outputs=[download_file_pix2pix, download_file_pix2pix])                                       
                    with gr.Row():
                        with gr.Column():
                            btn_pix2pix = gr.Button("Generate 🚀", variant="primary")
                        with gr.Column():                            
                            btn_pix2pix_cancel = gr.Button("Cancel 🛑", variant="stop")
                            btn_pix2pix_cancel.click(fn=initiate_stop_pix2pix, inputs=None, outputs=None)                              
                        with gr.Column():
                            btn_pix2pix_clear_input = gr.ClearButton(components=[img_pix2pix, prompt_pix2pix, negative_prompt_pix2pix], value="Clear inputs 🧹")
                        with gr.Column():                            
                            btn_pix2pix_clear_output = gr.ClearButton(components=[out_pix2pix, gs_out_pix2pix], value="Clear outputs 🧹")
                            btn_pix2pix.click(fn=hide_download_file_pix2pix, inputs=None, outputs=download_file_pix2pix)                               
                            btn_pix2pix.click(
                                fn=image_pix2pix,
                                inputs=[
                                    model_pix2pix,
                                    sampler_pix2pix,
                                    img_pix2pix,
                                    prompt_pix2pix,
                                    negative_prompt_pix2pix,
                                    num_images_per_prompt_pix2pix,
                                    num_prompt_pix2pix,
                                    guidance_scale_pix2pix,
                                    image_guidance_scale_pix2pix,
                                    num_inference_step_pix2pix,
                                    height_pix2pix,
                                    width_pix2pix,
                                    seed_pix2pix,
                                    use_gfpgan_pix2pix,
                                    nsfw_filter,
                                    tkme_pix2pix,
                                ],
                                outputs=[out_pix2pix, gs_out_pix2pix],
                                show_progress="full",
                            )  
                    with gr.Accordion("Send ...", open=False):
                        with gr.Row():
                            with gr.Column():
                                with gr.Box():                                
                                    with gr.Group():
                                        gr.HTML(value='... selected output to ...')
                                        gr.HTML(value='... text module ...')
                                        pix2pix_img2txt_git = gr.Button("🖼️ >> GIT Captioning")
                                        gr.HTML(value='... image module ...')
                                        pix2pix_img2img = gr.Button("🖼️ >> img2img")
                                        pix2pix_img2var = gr.Button("🖼️ >> Image variation")
                                        pix2pix_pix2pix = gr.Button("🖼️ >> Instruct pix2pix")
                                        pix2pix_inpaint = gr.Button("🖼️ >> inpaint")
                                        pix2pix_controlnet = gr.Button("🖼️ >> ControlNet")
                                        pix2pix_faceswap = gr.Button("🖼️ >> Faceswap target")
                                        pix2pix_resrgan = gr.Button("🖼️ >> Real ESRGAN")
                                        pix2pix_gfpgan = gr.Button("🖼️ >> GFPGAN")
                            with gr.Column():
                                with gr.Box():
                                    with gr.Group():
                                        gr.HTML(value='... input prompt(s) to ...')
                                        gr.HTML(value='... image module ...')                                        
                                        pix2pix_txt2img_sd_input = gr.Button("✍️ >> Stable Diffusion")
                                        pix2pix_txt2img_kd_input = gr.Button("✍️ >> Kandinsky")                                        
                                        pix2pix_img2img_input = gr.Button("✍️ >> img2img")
                                        pix2pix_inpaint_input = gr.Button("✍️ >> inpaint")
                                        pix2pix_controlnet_input = gr.Button("✍️ >> ControlNet")
                                        gr.HTML(value='... video module ...')                                        
                                        pix2pix_vid2vid_ze_input = gr.Button("✍️ >> Video Instruct-pix2pix")
                            with gr.Column():
                                with gr.Box():                                
                                    with gr.Group():
                                        gr.HTML(value='... both to ...')
                                        gr.HTML(value='... image module ...')                                        
                                        pix2pix_img2img_both = gr.Button("🖼️ + ✍️ >> img2img")
                                        pix2pix_inpaint_both = gr.Button("🖼️ + ✍️ >> inpaint")
                                        pix2pix_controlnet_both = gr.Button("🖼️ + ✍️ >> ControlNet")
# inpaint    
                with gr.TabItem("inpaint 🖌️", id=26) as tab_inpaint:
                    with gr.Accordion("About", open=False):                
                        with gr.Box():                       
                            gr.HTML(
                                """
                                <h1 style='text-align: left'; text-decoration: underline;>Informations</h1>
                                <b>Module : </b>Inpaint</br>
                                <b>Function : </b>Inpaint the masked area of an input image, from a prompt and a negative prompt using <a href='https://stability.ai/stablediffusion' target='_blank'>Stable Diffusion</a></br>
                                <b>Input(s) : </b>Input image, inpaint masked area, prompt, negative prompt</br>
                                <b>Output(s) : </b>Image(s)</br>
                                <b>HF model page : </b>
                                <a href='https://huggingface.co/Uminosachi/realisticVisionV30_v30VAE-inpainting' target='_blank'>Uminosachi/realisticVisionV30_v30VAE-inpainting</a> ,     
                                <a href='https://huggingface.co/runwayml/stable-diffusion-inpainting' target='_blank'>runwayml/stable-diffusion-inpainting</a></br>
                                """
                            )
                        with gr.Box():
                            gr.HTML(
                                """
                                <h1 style='text-align: left'; text-decoration: underline;>Help</h1>
                                <div style='text-align: justified'>
                                <b>Usage :</b></br>
                                - Upload or import an image using the <b>Input image</b> field</br>
                                - Using the sketch tool of the <b>inpaint field</b>, mask the area to be modified</br>
                                - Modify the <b>denoising strength of the inpainted area</b> : 0 will keep the original content, 1 will ignore it</br>
                                - Fill <b>the prompt</b> with what you want to see in your WHOLE (not only the inpaint area) output image</br>
                                - Fill the <b>negative prompt</b> with what you DO NOT want to see in your output image</br>
                                - (optional) Modify the settings to use another model or generate several images in a single run</br>
                                - Click the <b>Generate button</b></br>
                                - After generation, generated images are displayed in the gallery. Save them individually or create a downloadable zip of the whole gallery.
                                </br>
                                <b>Models :</b></br>
                                - You could place <a href='https://huggingface.co/' target='_blank'>huggingface.co</a> or  <a href='https://www.civitai.com/' target='_blank'>civitai.com</a> Stable diffusion based safetensors models in the directory /biniou/models/Stable Diffusion. Restart Biniou to see them in the models list.
                                </div>
                                """
                            )                   
                    with gr.Accordion("Settings", open=False):
                        with gr.Row():
                            with gr.Column():
                                model_inpaint = gr.Dropdown(choices=model_list_inpaint, value=model_list_inpaint[0], label="Model", info="Choose model to use for inference")
                            with gr.Column():
                                num_inference_step_inpaint = gr.Slider(1, 100, step=1, value=10, label="Steps", info="Number of iterations per image. Results and speed depends of sampler")
                            with gr.Column():
                                sampler_inpaint = gr.Dropdown(choices=list(SCHEDULER_MAPPING.keys()), value=list(SCHEDULER_MAPPING.keys())[0], label="Sampler", info="Sampler to use for inference")
                        with gr.Row():
                            with gr.Column():
                                guidance_scale_inpaint = gr.Slider(0.0, 10.0, step=0.1, value=7.5, label="CFG Scale", info="Low values : more creativity. High values : more corresponding to the prompts")
                            with gr.Column():
                                num_images_per_prompt_inpaint= gr.Slider(1, 4, step=1, value=1, label="Batch size", info ="Number of images to generate in a single run")
                            with gr.Column():
                                num_prompt_inpaint = gr.Slider(1, 32, step=1, value=1, label="Batch count", info="Number of batch to run successively")
                        with gr.Row():
                            with gr.Column():
                                width_inpaint = gr.Slider(128, 8192, step=64, value=512, label="Image Width", info="Width of outputs", interactive=False)
                            with gr.Column():
                                height_inpaint = gr.Slider(128, 8192, step=64, value=512, label="Image Height", info="Height of outputs", interactive=False)
                            with gr.Column():
                                seed_inpaint = gr.Slider(0, 10000000000, step=1, value=0, label="Seed(0 for random)", info="Seed to use for generation. Depending on scheduler, may permit reproducibility")
                        with gr.Row():
                            with gr.Column():    
                                use_gfpgan_inpaint = gr.Checkbox(value=True, label="Use GFPGAN to restore faces", info="Use GFPGAN to enhance faces in the outputs")
                            with gr.Column():
                                tkme_inpaint = gr.Slider(0.0, 1.0, step=0.01, value=0.6, label="Token merging ratio", info="0=slow,best quality, 1=fast,worst quality")
                    with gr.Row():
                        with gr.Column(scale=2):
                             rotation_img_inpaint = gr.Number(value=0, visible=False)
                             img_inpaint = gr.Image(label="Input image", type="pil", height=400, tool="sketch")
                             img_inpaint.upload(image_upload_event_inpaint, inputs=img_inpaint, outputs=[width_inpaint, height_inpaint, img_inpaint, rotation_img_inpaint], preprocess=False)
                             gs_img_inpaint = gr.Image(type="pil", visible=False)
                             gs_img_inpaint.change(image_upload_event_inpaint_b, inputs=gs_img_inpaint, outputs=[width_inpaint, height_inpaint], preprocess=False)
                        with gr.Column():
                            with gr.Row(): 
                                with gr.Column():
                                    denoising_strength_inpaint = gr.Slider(0.0, 1.0, step=0.01, value=1.0, label="Denoising strength", info="Balance between input image (0) and prompts (1)")                                
                            with gr.Row():
                                with gr.Column():
                                    prompt_inpaint = gr.Textbox(lines=3, max_lines=3, label="Prompt", info="Describe what you want in your image", placeholder="a cute kitten playing with a ball, dynamic pose, close-up cinematic still, photo realistic, ultra quality, 4k uhd, perfect lighting, HDR, bokeh")
                                with gr.Column():
                                    negative_prompt_inpaint = gr.Textbox(lines=3, max_lines=3, label="Negative Prompt", info="Describe what you DO NOT want in your image", placeholder="out of frame, bad quality, medium quality, blurry, ugly, duplicate, text, characters, logo")
                        with gr.Column():
                            with gr.Row(): 
                                with gr.Column():
                                    out_inpaint = gr.Gallery(
                                        label="Generated images",
                                        show_label=True,
                                        elem_id="gallery_inpaint",
                                        columns=2,
                                        height=400,
                                        preview=True,                                        
                                    )
                                    gs_out_inpaint = gr.State()                                    
                                    sel_out_inpaint = gr.Number(precision=0, visible=False)
                                    out_inpaint.select(get_select_index, None, sel_out_inpaint)   
                                    with gr.Row():
                                        with gr.Column():
                                            download_btn_inpaint = gr.Button("Zip gallery 💾")
                                        with gr.Column():
                                            download_file_inpaint = gr.File(label="Output", height=30, interactive=False, visible=False)
                                            download_btn_inpaint.click(fn=zip_download_file_inpaint, inputs=out_inpaint, outputs=[download_file_inpaint, download_file_inpaint])                                       
                    with gr.Row():
                        with gr.Column():
                            btn_inpaint = gr.Button("Generate 🚀", variant="primary")
                        with gr.Column():                            
                            btn_inpaint_cancel = gr.Button("Cancel 🛑", variant="stop")
                            btn_inpaint_cancel.click(fn=initiate_stop_inpaint, inputs=None, outputs=None)                              
                        with gr.Column():
                            btn_inpaint_clear_input = gr.ClearButton(components=[img_inpaint, gs_img_inpaint, prompt_inpaint, negative_prompt_inpaint], value="Clear inputs 🧹")
                        with gr.Column():                            
                            btn_inpaint_clear_output = gr.ClearButton(components=[out_inpaint, gs_out_inpaint], value="Clear outputs 🧹")  
                            btn_inpaint.click(fn=hide_download_file_inpaint, inputs=None, outputs=download_file_inpaint)                             
                            btn_inpaint.click(
                                fn=image_inpaint,
                                inputs=[
                                    model_inpaint,
                                    sampler_inpaint,
                                    img_inpaint,
                                    rotation_img_inpaint,
                                    prompt_inpaint,
                                    negative_prompt_inpaint,
                                    num_images_per_prompt_inpaint,
                                    num_prompt_inpaint,
                                    guidance_scale_inpaint,
                                    denoising_strength_inpaint,
                                    num_inference_step_inpaint,
                                    height_inpaint,
                                    width_inpaint,
                                    seed_inpaint,
                                    use_gfpgan_inpaint,
                                    nsfw_filter,
                                    tkme_inpaint,
                                ],
                                outputs=[out_inpaint, gs_out_inpaint], 
                                show_progress="full",
                            )  
                    with gr.Accordion("Send ...", open=False):
                        with gr.Row():
                            with gr.Column():
                                with gr.Box():                                
                                    with gr.Group():
                                        gr.HTML(value='... selected output to ...')
                                        gr.HTML(value='... text module ...')
                                        inpaint_img2txt_git = gr.Button("🖼️ >> GIT Captioning")      
                                        gr.HTML(value='... image module ...')
                                        inpaint_img2img = gr.Button("🖼️ >> img2img")
                                        inpaint_img2var = gr.Button("🖼️ >> Image variation")
                                        inpaint_pix2pix = gr.Button("🖼️ >> Instruct pix2pix")
                                        inpaint_inpaint = gr.Button("🖼️ >> inpaint")
                                        inpaint_controlnet = gr.Button("🖼️ >> ControlNet")
                                        inpaint_faceswap = gr.Button("🖼️ >> Faceswap target")
                                        inpaint_resrgan = gr.Button("🖼️ >> Real ESRGAN")
                                        inpaint_gfpgan = gr.Button("🖼️ >> GFPGAN")
                            with gr.Column():
                                with gr.Box():
                                    with gr.Group():
                                        gr.HTML(value='... input prompt(s) to ...')
                                        gr.HTML(value='... image module ...')
                                        inpaint_txt2img_sd_input = gr.Button("✍️ >> Stable Diffusion")
                                        inpaint_txt2img_kd_input = gr.Button("✍️ >> Kandinsky")                                        
                                        inpaint_img2img_input = gr.Button("✍️ >> img2img")
                                        inpaint_pix2pix_input = gr.Button("✍️ >> Instruct pix2pix")
                                        inpaint_controlnet_input = gr.Button("✍️ >> ControlNet")
                            with gr.Column():
                                with gr.Box():                                
                                    with gr.Group():
                                        gr.HTML(value='... both to ...')                                    
                                        gr.HTML(value='... image module ...')                                        
                                        inpaint_img2img_both = gr.Button("🖼️ + ✍️ >> img2img")
                                        inpaint_pix2pix_both = gr.Button("🖼️ + ✍️ >> Instruct pix2pix")
                                        inpaint_controlnet_both = gr.Button("🖼️ + ✍️ >> ControlNet")
# ControlNet
                with gr.TabItem("ControlNet 🖼️", id=27) as tab_controlnet:
                    with gr.Accordion("About", open=False):                
                        with gr.Box():                       
                            gr.HTML(
                                """
                                <h1 style='text-align: left'; text-decoration: underline;>Informations</h1>
                                <b>Module : </b>ControlNet</br>
                                <b>Function : </b>Generate images from a prompt, a negative prompt and a control image using <a href='https://stability.ai/stablediffusion' target='_blank'>Stable Diffusion</a> and <a href='https://stablediffusionweb.com/ControlNet' target='_blank'>ControlNet</a></br>
                                <b>Input(s) : </b>Prompt, negative prompt, ControlNet input</br>
                                <b>Output(s) : </b>Image(s)</br>
                                <b>HF Stable Diffusion models pages : </b>
                                <a href='https://huggingface.co/SG161222/Realistic_Vision_V3.0_VAE' target='_blank'>SG161222/Realistic_Vision_V3.0_VAE</a>,  

                                <a href='https://huggingface.co/stabilityai/stable-diffusion-xl-refiner-1.0' target='_blank'>stabilityai/stable-diffusion-xl-refiner-1.0</a>, 
                                <a href='https://huggingface.co/runwayml/stable-diffusion-v1-5' target='_blank'>runwayml/stable-diffusion-v1-5</a>, 
                                <a href='https://huggingface.co/nitrosocke/Ghibli-Diffusion' target='_blank'>nitrosocke/Ghibli-Diffusion</a></br>
                                <b>HF ControlNet models pages : </b>
                                <a href='https://huggingface.co/lllyasviel/control_v11p_sd15_canny' target='_blank'>lllyasviel/control_v11p_sd15_canny</a>, 
                                <a href='https://huggingface.co/lllyasviel/control_v11f1p_sd15_depth' target='_blank'>lllyasviel/control_v11f1p_sd15_depth</a>, 
                                <a href='https://huggingface.co/lllyasviel/control_v11p_sd15s2_lineart_anime' target='_blank'>lllyasviel/control_v11p_sd15s2_lineart_anime</a>, 
                                <a href='https://huggingface.co/lllyasviel/control_v11p_sd15_lineart' target='_blank'>lllyasviel/control_v11p_sd15_lineart</a>, 
                                <a href='https://huggingface.co/lllyasviel/control_v11p_sd15_mlsd' target='_blank'>lllyasviel/control_v11p_sd15_mlsd</a>, 
                                <a href='https://huggingface.co/lllyasviel/control_v11p_sd15_normalbae' target='_blank'>lllyasviel/control_v11p_sd15_normalbae</a>, 
                                <a href='https://huggingface.co/lllyasviel/control_v11p_sd15_openpose' target='_blank'>lllyasviel/control_v11p_sd15_openpose</a>, 
                                <a href='https://huggingface.co/lllyasviel/control_v11p_sd15_scribble' target='_blank'>lllyasviel/control_v11p_sd15_scribble</a>, 
                                <a href='https://huggingface.co/lllyasviel/control_v11p_sd15_softedge' target='_blank'>lllyasviel/control_v11p_sd15_softedge</a>
                                </br>
                                """
#                                <a href='https://huggingface.co/ckpt/anything-v4.5-vae-swapped' target='_blank'>ckpt/anything-v4.5-vae-swapped</a>, 
                            )
                        with gr.Box():
                            gr.HTML(
                                """
                                <h1 style='text-align: left'; text-decoration: underline;>Help</h1>
                                <div style='text-align: justified'>
                                <b>Usage :</b></br>
                                - (optional) Modify the settings to use another model, change the settings for ControlNet or adjust threshold on canny</br>
                                - Select a <b>Source image</b> that will be used to generate the control image</br>
                                - Select a <b>pre-processor</b> for the control image</br> 
                                - Click the <b>Preview</b> button</br>
                                - If the <b>Control image</b> generated suits your needs, continue. Else, you could modify the settings and generate a new one</br> 
                                - You should not modifiy the value in the <b>ControlNet Model</b> field, as it is automatically selected from the used pre-processor</br>
                                - Fill the <b>prompt</b> with what you want to see in your output image</br>
                                - Fill the <b>negative prompt</b> with what you DO NOT want to see in your output image</br>
                                - Click the <b>Generate button</b></br>
                                - After generation, generated images are displayed in the gallery. Save them individually or create a downloadable zip of the whole gallery</br>
                                <b>Models :</b></br>
                                - You could place <a href='https://huggingface.co/' target='_blank'>huggingface.co</a> or <a href='https://www.civitai.com/' target='_blank'>civitai.com</a> Stable diffusion based safetensors models in the directory ./biniou/models/Stable Diffusion. Restart Biniou to see them in the models list.
                                </div>
                                """
                            )                
                    with gr.Accordion("Settings", open=False):
                        with gr.Row():
                            with gr.Column():
                                model_controlnet = gr.Dropdown(choices=model_list_controlnet, value=model_list_controlnet[0], label="Model", info="Choose model to use for inference")
                            with gr.Column():
                                num_inference_step_controlnet = gr.Slider(1, 100, step=1, value=10, label="Steps", info="Number of iterations per image. Results and speed depends of sampler")
                            with gr.Column():
                                sampler_controlnet = gr.Dropdown(choices=list(SCHEDULER_MAPPING.keys()), value=list(SCHEDULER_MAPPING.keys())[0], label="Sampler", info="Sampler to use for inference")
                        with gr.Row():
                            with gr.Column():
                                guidance_scale_controlnet = gr.Slider(0.1, 20.0, step=0.1, value=7.0, label="CFG scale", info="Low values : more creativity. High values : more corresponding to the prompts")
                            with gr.Column():
                                num_images_per_prompt_controlnet = gr.Slider(minimum=1, maximum=4, step=1, value=1, label="Batch size", info ="Number of images to generate in a single run")
                            with gr.Column():
                                num_prompt_controlnet = gr.Slider(1, 32, step=1, value=1, label="Batch count", info="Number of batch to run successively")
                        with gr.Row():
                            with gr.Column():
                                width_controlnet = gr.Slider(128, 1280, step=64, value=512, label="Image Width", info="Width of outputs", interactive=False)
                            with gr.Column():
                                height_controlnet = gr.Slider(128, 1280, step=64, value=512, label="Image Height", info="Height of outputs", interactive=False)
                            with gr.Column():
                                seed_controlnet = gr.Slider(0, 10000000000, step=1, value=0, label="Seed(0 for random)", info="Seed to use for generation. Depending on scheduler, may permit reproducibility")    
                        with gr.Row():
                            with gr.Column():
                                low_threshold_controlnet = gr.Slider(0, 255, step=1, value=100, label="Canny low threshold", info="ControlNet Low threshold")  
                            with gr.Column():
                                high_threshold_controlnet = gr.Slider(0, 255, step=1, value=200, label="Canny high threshold", info="ControlNet high threshold")
                            with gr.Column():                                    
                                strength_controlnet = gr.Slider(0.0, 2.0, step=0.01, value=1.0, label="ControlNet strength", info="ControlNet strength")
                        with gr.Row():
                            with gr.Column():
                                start_controlnet = gr.Slider(0.0, 1.0, step=0.01, value=0.0, label="Start ControlNet", info="Start ControlNet at % step")
                            with gr.Column():
                                stop_controlnet = gr.Slider(0.0, 1.0, step=0.01, value=1.0, label="Stop ControlNet", info="Stop ControlNet at % step")
                        with gr.Row():
                            with gr.Column():    
                                use_gfpgan_controlnet = gr.Checkbox(value=True, label="Use GFPGAN to restore faces", info="Use GFPGAN to enhance faces in the outputs")
                            with gr.Column():
                                tkme_controlnet = gr.Slider(0.0, 1.0, step=0.01, value=0.6, label="Token merging ratio", info="0=slow,best quality, 1=fast,worst quality")
                    with gr.Row():
                        with gr.Column():
                            with gr.Row():
                                with gr.Column():                        
                                    img_source_controlnet = gr.Image(label="Source image", height=250, type="filepath")
                                    img_source_controlnet.change(fn=image_upload_event, inputs=img_source_controlnet, outputs=[width_controlnet, height_controlnet])                                            
                                    gs_img_source_controlnet = gr.Image(type="pil", visible=False)
                                    gs_img_source_controlnet.change(fn=image_upload_event, inputs=gs_img_source_controlnet, outputs=[width_controlnet, height_controlnet], preprocess=False)                                    
                            with gr.Row():
                                with gr.Column(): 
                                    preprocessor_controlnet = gr.Dropdown(choices=preprocessor_list_controlnet, value=preprocessor_list_controlnet[0], label="Pre-processor", info="Choose pre-processor to use")
                                    btn_controlnet_preview = gr.Button("Preview 👁️")
                        with gr.Column():
                            with gr.Row():
                                with gr.Column():                                                            
                                    img_preview_controlnet = gr.Image(label="Control image preview", height=250, type="filepath")
                                    gs_img_preview_controlnet = gr.Image(type="pil", visible=False)
                            with gr.Row():
                                with gr.Column(): 
                                    variant_controlnet = gr.Dropdown(choices=variant_list_controlnet, value=variant_list_controlnet[0], label="ControlNet Model", info="Choose ControlNet model to use")                                    
                                    gs_variant_controlnet = gr.Textbox(visible=False)
                                    gs_variant_controlnet.change(fn=in_and_out, inputs=gs_variant_controlnet, outputs=variant_controlnet) 
                                    scale_preview_controlnet = gr.Number(value=2048, visible=False)
                                    img_preview_controlnet.upload(fn=scale_image, inputs=[img_preview_controlnet, scale_preview_controlnet], outputs=[width_controlnet, height_controlnet, img_preview_controlnet])
                                    gs_img_preview_controlnet.change(image_upload_event_inpaint_b, inputs=gs_img_preview_controlnet, outputs=[width_controlnet, height_controlnet], preprocess=False)
                                    btn_controlnet_preview.click(fn=dispatch_controlnet_preview, inputs=[model_controlnet, low_threshold_controlnet, high_threshold_controlnet, img_source_controlnet, preprocessor_controlnet], outputs=[img_preview_controlnet, gs_img_preview_controlnet, gs_variant_controlnet], show_progress="full")
                            with gr.Row():
                                with gr.Column(): 
                                    btn_controlnet_clear_preview = gr.ClearButton(components=[img_preview_controlnet, gs_img_preview_controlnet], value="Clear preview 🧹")  
                        with gr.Column():
                            with gr.Row():
                                with gr.Column():                        
                                    prompt_controlnet = gr.Textbox(lines=6, max_lines=6, label="Prompt", info="Describe what you want in your image", placeholder="a cute kitten playing with a ball, dynamic pose, close-up cinematic still, photo realistic, ultra quality, 4k uhd, perfect lighting, HDR, bokeh")
                            with gr.Row():
                                with gr.Column(): 
                                    negative_prompt_controlnet = gr.Textbox(lines=6, max_lines=6, label="Negative Prompt", info="Describe what you DO NOT want in your image", placeholder="out of frame, bad quality, medium quality, blurry, ugly, duplicate, text, characters, logo")
                        with gr.Column():
                            out_controlnet = gr.Gallery(
                                label="Generated images",
                                show_label=True,
                                elem_id="gallery",
                                columns=1,
                                height=400,
                                preview=True,                                 
                            )    
                            gs_out_controlnet = gr.State()
                            sel_out_controlnet = gr.Number(precision=0, visible=False)
                            out_controlnet.select(get_select_index, None, sel_out_controlnet)
                            with gr.Row():
                                with gr.Column():
                                    download_btn_controlnet = gr.Button("Zip gallery 💾")
                                with gr.Column():
                                    download_file_controlnet = gr.File(label="Output", height=30, interactive=False, visible=False)
                                    download_btn_controlnet.click(fn=zip_download_file_controlnet, inputs=out_controlnet, outputs=[download_file_controlnet, download_file_controlnet])
                    with gr.Row():
                        with gr.Column():
                            btn_controlnet = gr.Button("Generate 🚀", variant="primary")
                        with gr.Column():                            
                            btn_controlnet_cancel = gr.Button("Cancel 🛑", variant="stop")
                            btn_controlnet_cancel.click(fn=initiate_stop_controlnet, inputs=None, outputs=None)                              
                        with gr.Column():
                            btn_controlnet_clear_input = gr.ClearButton(components=[prompt_controlnet, negative_prompt_controlnet, img_source_controlnet, gs_img_source_controlnet, img_preview_controlnet], value="Clear inputs 🧹")
                        with gr.Column():                            
                            btn_controlnet_clear_output = gr.ClearButton(components=[out_controlnet, gs_out_controlnet], value="Clear outputs 🧹")   
                            btn_controlnet.click(fn=hide_download_file_controlnet, inputs=None, outputs=download_file_controlnet)   
                            btn_controlnet.click(
                            fn=image_controlnet, 
                            inputs=[
                                model_controlnet, 
                                sampler_controlnet, 
                                prompt_controlnet, 
                                negative_prompt_controlnet, 
                                num_images_per_prompt_controlnet, 
                                num_prompt_controlnet, 
                                guidance_scale_controlnet, 
                                num_inference_step_controlnet, 
                                height_controlnet, 
                                width_controlnet, 
                                seed_controlnet, 
                                low_threshold_controlnet, 
                                high_threshold_controlnet, 
                                strength_controlnet, 
                                start_controlnet, 
                                stop_controlnet,                                 
                                use_gfpgan_controlnet, 
                                variant_controlnet, 
                                img_preview_controlnet,
                                nsfw_filter,
                                tkme_controlnet,
                            ],
                                outputs=[out_controlnet, gs_out_controlnet],
                                show_progress="full",
                            )
                    with gr.Accordion("Send ...", open=False):
                        with gr.Row():
                            with gr.Column():
                                with gr.Box():                                
                                    with gr.Group():
                                        gr.HTML(value='... selected output to ...')
                                        gr.HTML(value='... text module ...')                                        
                                        controlnet_img2txt_git = gr.Button("🖼️ >> GIT Captioning")         
                                        gr.HTML(value='... image module ...')
                                        controlnet_img2img = gr.Button("🖼️ >> img2img")
                                        controlnet_img2var = gr.Button("🖼️ >> Image variation")
                                        controlnet_pix2pix = gr.Button("🖼️ >> Instruct pix2pix")
                                        controlnet_inpaint = gr.Button("🖼️ >> inpaint")
                                        controlnet_controlnet = gr.Button("🖼️ >> ControlNet")
                                        controlnet_faceswap = gr.Button("🖼️ >> Faceswap target")
                                        controlnet_resrgan = gr.Button("🖼️ >> Real ESRGAN")
                                        controlnet_gfpgan = gr.Button("🖼️ >> GFPGAN")
                            with gr.Column():
                                with gr.Box():
                                    with gr.Group():
                                        gr.HTML(value='... input prompt(s) to ...')
                                        gr.HTML(value='... image module ...')
                                        controlnet_txt2img_sd_input = gr.Button("✍️ >> Stable Diffusion")
                                        controlnet_txt2img_kd_input = gr.Button("✍️ >> Kandinsky")                                        
                                        controlnet_img2img_input = gr.Button("✍️ >> img2img")
                                        controlnet_pix2pix_input = gr.Button("✍️ >> Instruct pix2pix")
                                        controlnet_inpaint_input = gr.Button("✍️ >> inpaint")         
                                        gr.HTML(value='... video module ...')                                        
                                        controlnet_txt2vid_ms_input = gr.Button("✍️ >> Modelscope")
                                        controlnet_txt2vid_ze_input = gr.Button("✍️ >> Text2Video-Zero")                                        
                            with gr.Column():
                                with gr.Box():                                
                                    with gr.Group():
                                        gr.HTML(value='... both to ...')
                                        gr.HTML(value='... image module ...')                                                                            
                                        controlnet_img2img_both = gr.Button("🖼️ + ✍️ >> img2img")
                                        controlnet_pix2pix_both = gr.Button("🖼️ + ✍️ >> Instruct pix2pix")
                                        controlnet_inpaint_both = gr.Button("🖼️ + ✍️ >> inpaint")                                        
# faceswap    
                with gr.TabItem("Faceswap 🎭", id=28) as tab_faceswap:
                    with gr.Accordion("About", open=False):                
                        with gr.Box():                       
                            gr.HTML(
                                """
                                <h1 style='text-align: left'; text-decoration: underline;>Informations</h1>
                                <b>Module : </b>Faceswap</br>
                                <b>Function : </b>Swap faces between images (source -> target) using <a href='https://github.com/deepinsight/insightface' target='_blank'>Insight Face</a> et <a href='https://github.com/microsoft/onnxruntime' target='_blank'>Onnx runtime</a></br>
                                <b>Input(s) : </b>Source image, target image</br>
                                <b>Output(s) : </b>Image(s)</br>
                                <b>HF model page : </b>
                                <a href='https://huggingface.co/thebiglaskowski/inswapper_128.onnx' target='_blank'>thebiglaskowski/inswapper_128.onnx</a></br>
                                """
                            )
                        with gr.Box():
                            gr.HTML(
                                """
                                <h1 style='text-align: left'; text-decoration: underline;>Help</h1>
                                <div style='text-align: justified'>
                                <b>Usage :</b></br>
                                - Upload a <b>Source image</b>. The face(s) in this image will replaces face(s) in the target image.</br>
                                - Upload or import a <b>target image</b>. The face(s) in this image will be replaced with the source one(s)</br>
                                - Set the <b>source index</b> list to choose which face(s) to extract from source image and in which order. From left to right and starting from 0, id comma separated list of faces number. For example, if there is 3 faces in a picture '0,2' will select the face on the left, then on the right, but not on the one in the middle. If set to 0, take only the first face from the left.</br>
                                - Set the <b>target index</b> list to choose which face(s) to replace in target image and in which order. From left to right and starting from 0, id comma separated list of faces number. For example, if there is 3 faces in a picture '2,1' will select the faces on the right, then in the middle, but not the one on the left. The source index list is used to create a mapping between the faces to extract and to replace. If set to 0, replace only the first face from the left.</br>
                                - (optional) Modify the settings to desactivate GFPGAN faces restoration</br>
                                - Click the <b>Generate</b> button</br>
                                - After generation, generated images are displayed in the gallery. Save them individually or create a downloadable zip of the whole gallery.
                                </div>
                                """
                            )                
                    with gr.Accordion("Settings", open=False):
                        with gr.Row():
                            with gr.Column():
                                model_faceswap = gr.Dropdown(choices=list(model_list_faceswap.keys()), value=list(model_list_faceswap.keys())[0], label="Model", info="Choose model to use for inference")
                            with gr.Column():
                                width_faceswap = gr.Slider(128, 8192, step=64, value=512, label="Image Width", info="Width of outputs", interactive=False)
                            with gr.Column():
                                height_faceswap = gr.Slider(128, 8192, step=64, value=512, label="Image Height", info="Height of outputs", interactive=False)
                        with gr.Row():
                            with gr.Column():    
                                use_gfpgan_faceswap = gr.Checkbox(value=True, label="Use GFPGAN to restore faces", info="Use GFPGAN to enhance faces in the outputs")    
                    with gr.Row():
                        with gr.Column():
                             img_source_faceswap = gr.Image(label="Source image", height=400, type="filepath")
                             with gr.Row():
                                 source_index_faceswap = gr.Textbox(value=0, lines=1, label="Source index", info="Use a comma separated list of faces to export to target (numbers from left to right)")
                        with gr.Column():
                             img_target_faceswap = gr.Image(label="Target image", type="filepath", height=400)
                             gs_img_target_faceswap = gr.Image(type="pil", visible=False)
                             img_target_faceswap.change(image_upload_event, inputs=img_target_faceswap, outputs=[width_faceswap, height_faceswap])                             
                             with gr.Row():
                                 target_index_faceswap = gr.Textbox(value=0, lines=1, label="Target index", info="Use a comma separated list of faces to replace in target (numbers from left to right)")                             
                        with gr.Column():
                            with gr.Row():
                                with gr.Column():
                                    out_faceswap = gr.Gallery(
                                        label="Generated images",
                                        show_label=True,
                                        elem_id="gallery_fsw",
                                        columns=2,
                                        height=400,
                                        preview=True,                                   
                                    )
                                    gs_out_faceswap = gr.State()
                                    sel_out_faceswap = gr.Number(precision=0, visible=False)                        
                                    out_faceswap.select(get_select_index, None, sel_out_faceswap)
                                    with gr.Row():
                                        with gr.Column():
                                            download_btn_faceswap = gr.Button("Zip gallery 💾")
                                        with gr.Column():
                                            download_file_faceswap = gr.File(label="Output", height=30, interactive=False, visible=False)
                                            download_btn_faceswap.click(fn=zip_download_file_faceswap, inputs=out_faceswap, outputs=[download_file_faceswap, download_file_faceswap])                                       
                    with gr.Row():
                        with gr.Column():
                            btn_faceswap = gr.Button("Generate 🚀", variant="primary")
                        with gr.Column():
                            btn_faceswap_clear_input = gr.ClearButton(components=[img_source_faceswap, img_target_faceswap, source_index_faceswap, target_index_faceswap, gs_img_target_faceswap], value="Clear inputs 🧹")
                        with gr.Column():                            
                            btn_faceswap_clear_output = gr.ClearButton(components=[out_faceswap, gs_out_faceswap], value="Clear outputs 🧹")
                            btn_faceswap.click(fn=hide_download_file_faceswap, inputs=None, outputs=download_file_faceswap)                               
                            btn_faceswap.click(
                                fn=image_faceswap,
                                inputs=[
                                    model_faceswap, 
                                    img_source_faceswap, 
                                    img_target_faceswap, 
                                    source_index_faceswap, 
                                    target_index_faceswap,
                                    use_gfpgan_faceswap,                                
                                ],
                                outputs=[out_faceswap, gs_out_faceswap],
                                show_progress="full",
                            )  
                    with gr.Accordion("Send ...", open=False):
                        with gr.Row():
                            with gr.Column():
                                with gr.Box():                                
                                    with gr.Group():
                                        gr.HTML(value='... selected output to ...')
                                        gr.HTML(value='... text module ...')                                        
                                        faceswap_img2txt_git = gr.Button("🖼️ >> GIT Captioning")
                                        gr.HTML(value='... image module ...')
                                        faceswap_img2img = gr.Button("🖼️ >> img2img")
                                        faceswap_img2var = gr.Button("🖼️ >> Image variation")
                                        faceswap_pix2pix = gr.Button("🖼️ >> Instruct pix2pix")
                                        faceswap_inpaint = gr.Button("🖼️ >> inpaint")
                                        faceswap_controlnet = gr.Button("🖼️ >> ControlNet")
                                        faceswap_faceswap = gr.Button("🖼️ >> Faceswap target")
                                        faceswap_resrgan = gr.Button("🖼️ >> Real ESRGAN")
                                        faceswap_gfpgan = gr.Button("🖼️ >> GFPGAN")
                            with gr.Column():
                                with gr.Box():
                                    with gr.Group():
                                        gr.HTML(value='... prompt(s) to ...')
                            with gr.Column():
                                with gr.Box():                                
                                    with gr.Group():
                                        gr.HTML(value='... both to ...')                                    

# Real ESRGAN    
                with gr.TabItem("Real ESRGAN 🔎", id=29) as tab_resrgan:
                    with gr.Accordion("About", open=False):                
                        with gr.Box():                       
                            gr.HTML(
                                """
                                <h1 style='text-align: left'; text-decoration: underline;>Informations</h1>
                                <b>Module : </b>Real ESRGAN</br>
                                <b>Function : </b>Upscale x4 using <a href='https://github.com/xinntao/Real-ESRGAN' target='_blank'>Real ESRGAN</a></br>
                                <b>Input(s) : </b>Input image</br>
                                <b>Output(s) : </b>Upscaled image</br>
                                <b>HF model page : </b>
                                <a href='https://huggingface.co/ai-forever/Real-ESRGAN' target='_blank'>ai-forever/Real-ESRGAN</a></br>
                                """
                            )
                        with gr.Box():
                            gr.HTML(
                                """
                                <h1 style='text-align: left'; text-decoration: underline;>Help</h1>
                                <div style='text-align: justified'>
                                <b>Usage :</b></br>
                                - Upload or import an <b>Input image</b> </br>
                                - (optional) Modify the settings to change scale factor or use another model</br>
                                - Click the <b>Generate</b> button</br>
                                - After generation, upscaled image is displayed in the gallery.
                                </div>
                                """
                            )                
                    with gr.Accordion("Settings", open=False):
                        with gr.Row():
                            with gr.Column():
                                model_resrgan = gr.Dropdown(choices=model_list_resrgan, value=model_list_resrgan[1], label="Model", info="Choose model to use for inference")
                            with gr.Column():
                                scale_resrgan = gr.Dropdown(choices=list(RESRGAN_SCALES.keys()), value=list(RESRGAN_SCALES.keys())[1], label="Upscale factor", info="Choose upscale factor")  
                                scale_resrgan.change(scale_resrgan_change, inputs=scale_resrgan, outputs=model_resrgan)                                
                        with gr.Row():
                            with gr.Column():
                                width_resrgan = gr.Slider(128, 8192, step=64, value=512, label="Image Width", info="Width of input", interactive=False)
                            with gr.Column():
                                height_resrgan = gr.Slider(128, 8192, step=64, value=512, label="Image Height", info="Height of input", interactive=False)
                        with gr.Row():
                            with gr.Column():    
                                use_gfpgan_resrgan = gr.Checkbox(value=True, label="Use GFPGAN to restore faces", info="Use GFPGAN to enhance faces in the outputs")                                 
                    with gr.Row():
                        with gr.Column():
                             img_resrgan = gr.Image(label="Input image", type="filepath", height=400)
                             img_resrgan.change(image_upload_event, inputs=img_resrgan, outputs=[width_resrgan, height_resrgan])
                        with gr.Column():
                            out_resrgan = gr.Gallery(
                                label="Generated image",
                                show_label=True,
                                elem_id="gallery_resr",
                                columns=1,
                                height=400,
                                preview=True,
                            )
                        gs_out_resrgan = gr.State()
                        sel_out_resrgan = gr.Number(precision=0, visible=False)                                   
                    with gr.Row():
                        with gr.Column():
                            btn_resrgan = gr.Button("Generate 🚀", variant="primary")
                        with gr.Column():
                            btn_resrgan_clear_input = gr.ClearButton(components=[img_resrgan], value="Clear inputs 🧹")
                        with gr.Column():                            
                            btn_resrgan_clear_output = gr.ClearButton(components=[out_resrgan, gs_out_resrgan], value="Clear outputs 🧹") 
                            btn_resrgan.click(
                                fn=image_resrgan,
                                inputs=[
                                    model_resrgan,
                                    scale_resrgan,                                    
                                    img_resrgan,
                                    use_gfpgan_resrgan,
                                ],
                                outputs=[out_resrgan, gs_out_resrgan],
                                show_progress="full",
                            )
                    with gr.Accordion("Send ...", open=False):
                        with gr.Row():
                            with gr.Column():
                                with gr.Box():                                
                                    with gr.Group():
                                        gr.HTML(value='... selected output to ...')
                                        gr.HTML(value='... text module ...')
                                        resrgan_img2txt_git = gr.Button("🖼️ >> GIT Captioning") 
                                        gr.HTML(value='... image module ...')
                                        resrgan_img2img = gr.Button("🖼️ >> img2img")
                                        resrgan_img2var = gr.Button("🖼️ >> Image variation")
                                        resrgan_pix2pix = gr.Button("🖼️ >> Instruct pix2pix")
                                        resrgan_inpaint = gr.Button("🖼️ >> inpaint")
                                        resrgan_controlnet = gr.Button("🖼️ >> ControlNet")
                                        resrgan_faceswap = gr.Button("🖼️ >> Faceswap target")
                                        resrgan_gfpgan = gr.Button("🖼️ >> GFPGAN")
                            with gr.Column():
                                with gr.Box():
                                    with gr.Group():
                                        gr.HTML(value='... input prompt(s) to ...')
                            with gr.Column():
                                with gr.Box():                                
                                    with gr.Group():
                                        gr.HTML(value='... both to ...')                         
# GFPGAN    
                with gr.TabItem("GFPGAN 🔎", id=291) as tab_gfpgan:
                    with gr.Accordion("About", open=False):                
                        with gr.Box():                       
                            gr.HTML(
                                """
                                <h1 style='text-align: left'; text-decoration: underline;>Informations</h1>
                                <b>Module : </b>GFPGAN</br>
                                <b>Function : </b>Restore and enhance faces in an image using <a href='https://github.com/TencentARC/GFPGAN' target='_blank'>GFPGAN</a></br>
                                <b>Input(s) : </b>Input image</br>
                                <b>Output(s) : </b>Enhanced Image</br>
                                <b>HF model page : </b>
                                <a href='https://huggingface.co/leonelhs/gfpgan' target='_blank'>leonelhs/gfpgan</a></br>
                                """
                            )
                        with gr.Box():
                            gr.HTML(
                                """
                                <h1 style='text-align: left'; text-decoration: underline;>Help</h1>
                                <div style='text-align: justified'>
                                <b>Usage :</b></br>
                                - Upload or import an <b>Input image</b></br>
                                - (optional) Modify the settings to use another variant of the GFPGAN model</br> 
                                - Click the <b>Generate</b> button</br>
                                - After generation, enhanced image is displayed in the gallery
                                </div>
                                """
                            )                     
                    with gr.Accordion("Settings", open=False):
                        with gr.Row():
                            with gr.Column():
                                model_gfpgan = gr.Dropdown(choices=model_list_gfpgan, value=model_list_gfpgan[0], label="Model", info="Choose model to use for inference")
                            with gr.Column():
                                variant_gfpgan = gr.Dropdown(choices=variant_list_gfpgan, value=variant_list_gfpgan[4], label="Variant", info="Variant of GPFGAN to use")
                        with gr.Row():
                            with gr.Column():
                                width_gfpgan = gr.Slider(128, 8192, step=64, value=512, label="Image Width", info="Width of outputs", interactive=False)
                            with gr.Column():
                                height_gfpgan = gr.Slider(128, 8192, step=64, value=512, label="Image Height", info="Height of outputs", interactive=False)
                    with gr.Row():
                        with gr.Column():
                            img_gfpgan = gr.Image(label="Input image", type="filepath", height=400)
                            img_gfpgan.change(image_upload_event, inputs=img_gfpgan, outputs=[width_gfpgan, height_gfpgan])
                        with gr.Column():
                            out_gfpgan = gr.Gallery(
                                label="Generated image",
                                show_label=True,
                                elem_id="gallery_gfp",
                                columns=1,
                                height=400,
                                preview=True,
                            )
                        gs_out_gfpgan = gr.State()
                        sel_out_gfpgan = gr.Number(precision=0, visible=False)                            
                    with gr.Row():
                        with gr.Column():
                            btn_gfpgan = gr.Button("Generate 🚀", variant="primary")
                        with gr.Column():
                            btn_gfpgan_clear_input = gr.ClearButton(components=[img_gfpgan], value="Clear inputs 🧹")
                        with gr.Column():                            
                            btn_gfpgan_clear_output = gr.ClearButton(components=[out_gfpgan, gs_out_gfpgan], value="Clear outputs 🧹") 
                        btn_gfpgan.click(
                            fn=image_gfpgan,
                            inputs=[
                                model_gfpgan,
                                variant_gfpgan,
                                img_gfpgan,
                            ],
                            outputs=[out_gfpgan, gs_out_gfpgan],
                            show_progress="full",
                        )
                    with gr.Accordion("Send ...", open=False):
                        with gr.Row():
                            with gr.Column():
                                with gr.Box():                                
                                    with gr.Group():
                                        gr.HTML(value='... selected output to ...')
                                        gr.HTML(value='... text module ...')                                        
                                        gfpgan_img2txt_git = gr.Button("🖼️ >> GIT Captioning")   
                                        gr.HTML(value='... image module ...')
                                        gfpgan_img2img = gr.Button("🖼️ >> img2img")
                                        gfpgan_img2var = gr.Button("🖼️ >> Image variation")
                                        gfpgan_pix2pix = gr.Button("🖼️ >> Instruct pix2pix")
                                        gfpgan_inpaint = gr.Button("🖼️ >> inpaint")
                                        gfpgan_controlnet = gr.Button("🖼️ >> ControlNet")
                                        gfpgan_faceswap = gr.Button("🖼️ >> Faceswap target")
                                        gfpgan_resrgan = gr.Button("🖼️ >> Real ESRGAN")
                            with gr.Column():
                                with gr.Box():
                                    with gr.Group():
                                        gr.HTML(value='... input prompt(s) to ...')
                            with gr.Column():
                                with gr.Box():                                
                                    with gr.Group():
                                        gr.HTML(value='... both to ...')                        
# Audio
        with gr.TabItem("Audio 🎵", id=3) as tab_audio:
            with gr.Tabs() as tabs_audio:        
# Musicgen
                with gr.TabItem("MusicGen 🎶", id=31) as tab_musicgen:
                    with gr.Accordion("About", open=False):                
                        with gr.Box():                       
                            gr.HTML(
                                """
                                <h1 style='text-align: left'; text-decoration: underline;>Informations</h1>
                                <b>Module : </b>MusicGen</br>
                                <b>Function : </b>Generate music from a prompt, using <a href='https://github.com/facebookresearch/audiocraft' target='_blank'>MusicGen</a></br>
                                <b>Input(s) : </b>Input prompt</br>
                                <b>Output(s) : </b>Generated music</br>
                                <b>HF model page : </b>
                                <a href='https://huggingface.co/facebook/musicgen-small' target='_blank'>facebook/musicgen-small</a>, 
                                <a href='https://huggingface.co/facebook/musicgen-medium' target='_blank'>facebook/musicgen-medium</a>, 
                                <a href='https://huggingface.co/facebook/musicgen-large' target='_blank'>facebook/musicgen-large</a>, 
                                <a href='https://huggingface.co/facebook/musicgen-melody' target='_blank'>facebook/musicgen-melody</a></br>                                
                                """
                            )
                        with gr.Box():
                            gr.HTML(
                                """
                                <h1 style='text-align: left'; text-decoration: underline;>Help</h1>
                                <div style='text-align: justified'>
                                <b>Usage :</b></br>
                                - Fill the <b>prompt</b> by describing the music you want to generate</br>
                                - (optional) Modify the settings to use another model or change audio duration</br>                                
                                - Click the <b>Generate<b> button</br>
                                - After generation, generated music is available to listen in the <b>Generated music<b> field.
                                </div>
                                """
                            )                           
                    with gr.Accordion("Settings", open=False):
                        with gr.Row():
                            with gr.Column():
                                model_musicgen= gr.Dropdown(choices=modellist_musicgen, value=modellist_musicgen[0], label="Model", info="Choose model to use for inference")
                            with gr.Column():    
                                duration_musicgen = gr.Slider(1, 160, step=1, value=5, label="Audio length (sec)")
                            with gr.Column():
                                cfg_coef_musicgen = gr.Slider(0.1, 20.0, step=0.1, value=3.0, label="CFG scale", info="Low values : more creativity. High values : more corresponding to the prompts")
                            with gr.Column():
                                num_batch_musicgen = gr.Slider(1, 32, step=1, value=1, label="Batch count", info="Number of batch to run successively")  
                        with gr.Row():
                            with gr.Column():    
                                use_sampling_musicgen = gr.Checkbox(value=True, label="Use sampling")
                            with gr.Column():    
                                temperature_musicgen = gr.Slider(0.0, 10.0, step=0.1, value=1.0, label="temperature")
                            with gr.Column():
                                top_k_musicgen = gr.Slider(0, 500, step=1, value=250, label="top_k")
                            with gr.Column():
                                top_p_musicgen = gr.Slider(0.0, 500.0, step=1.0, value=0.0, label="top_p")
                    with gr.Row():
                        with gr.Column():
                            prompt_musicgen = gr.Textbox(label="Describe your music", lines=2, max_lines=2, placeholder="90s rock song with loud guitars and heavy drums")
                        with gr.Column():
                            out_musicgen = gr.Audio(label="Generated music", type="filepath", show_download_button=True, interactive=False)
                    with gr.Row():
                        with gr.Column():
                            btn_musicgen = gr.Button("Generate 🚀", variant="primary")
                        with gr.Column():                            
                            btn_musicgen_cancel = gr.Button("Cancel 🛑", variant="stop")
                            btn_musicgen_cancel.click(fn=initiate_stop_musicgen, inputs=None, outputs=None)                              
                        with gr.Column():
                            btn_musicgen_clear_input = gr.ClearButton(components=prompt_musicgen, value="Clear inputs 🧹")
                        with gr.Column():                            
                            btn_musicgen_clear_output = gr.ClearButton(components=out_musicgen, value="Clear outputs 🧹")
                        btn_musicgen.click(
                            fn=music_musicgen, 
                            inputs=[
                                prompt_musicgen, 
                                model_musicgen,
                                duration_musicgen,
                                num_batch_musicgen,
                                temperature_musicgen,
                                top_k_musicgen,
                                top_p_musicgen,
                                use_sampling_musicgen,
                                cfg_coef_musicgen,
                            ], 
                            outputs=out_musicgen,
                            show_progress="full",                            
                        )
                    with gr.Accordion("Send ...", open=False):
                        with gr.Row():
                            with gr.Column():
                                with gr.Box():                                
                                    with gr.Group():
                                        gr.HTML(value='... selected output to ...')
                            with gr.Column():
                                with gr.Box():
                                    with gr.Group():
                                        gr.HTML(value='... input prompt(s) to ...')
                                        gr.HTML(value='... audio module ...')                                        
                                        musicgen_audiogen_input = gr.Button("✍️ >> Audiogen")                                        
                            with gr.Column():
                                with gr.Box():                                
                                    with gr.Group():
                                        gr.HTML(value='... both to ...')                                    

# Audiogen
                if ram_size() >= 16 :
                    titletab_audiogen = "AudioGen 🔊"
                else :
                    titletab_audiogen = "AudioGen 🔊 ⛔"
                
                with gr.TabItem(titletab_audiogen, id=32) as tab_audiogen:

                    with gr.Accordion("About", open=False):                
                        with gr.Box():                       
                            gr.HTML(
                                """
                                <h1 style='text-align: left'; text-decoration: underline;>Informations</h1>
                                <b>Module : </b>AudioGen</br>
                                <b>Function : </b>Generate sound from a prompt, using <a href='https://github.com/facebookresearch/audiocraft' target='_blank'>Audiogen</a></br>
                                <b>Input(s) : </b>Input prompt</br>
                                <b>Output(s) : </b>Generated sound</br>
                                <b>HF model page : </b>
                                <a href='https://huggingface.co/facebook/audiogen-medium' target='_blank'>facebook/audiogen-medium</a></br>                                
                                """
                            )
                        with gr.Box():
                            gr.HTML(
                                """
                                <h1 style='text-align: left'; text-decoration: underline;>Help</h1>
                                <div style='text-align: justified'>
                                <b>Usage :</b></br>
                                - Fill the <b>Prompt</b> by describing the sound you want to generate</br>
                                - (optional) Modify the settings to change audio duration</br>                                
                                - Click the <b>Generate</b> button</br>
                                - After generation, generated sound is available to listen in the <b>Generated sound</b> field.
                                </div>
                                """
                            )                       
                    with gr.Accordion("Settings", open=False):
                        with gr.Row():
                            with gr.Column():
                                model_audiogen= gr.Dropdown(choices=modellist_audiogen, value=modellist_audiogen[0], label="Model", info="Choose model to use for inference")
                            with gr.Column():    
                                duration_audiogen = gr.Slider(1, 160, step=1, value=5, label="Audio length (sec)")
                            with gr.Column():
                                cfg_coef_audiogen = gr.Slider(0.1, 20.0, step=0.1, value=3.0, label="CFG scale", info="Low values : more creativity. High values : more corresponding to the prompts")
                            with gr.Column():
                                num_batch_audiogen = gr.Slider(1, 32, step=1, value=1, label="Batch count", info="Number of batch to run successively")  
                        with gr.Row():
                            with gr.Column():    
                                use_sampling_audiogen = gr.Checkbox(value=True, label="Use sampling")
                            with gr.Column():    
                                temperature_audiogen = gr.Slider(0.0, 10.0, step=0.1, value=1.0, label="temperature")
                            with gr.Column():
                                top_k_audiogen = gr.Slider(0, 500, step=1, value=250, label="top_k")
                            with gr.Column():
                                top_p_audiogen = gr.Slider(0.0, 500.0, step=1.0, value=0.0, label="top_p")
                    with gr.Row():
                        with gr.Column():
                            prompt_audiogen = gr.Textbox(label="Describe your sound", lines=2, max_lines=2, placeholder="dog barking, sirens of an emergency vehicle, footsteps in a corridor")
                        with gr.Column():
                            out_audiogen = gr.Audio(label="Generated sound", type="filepath", show_download_button=True, interactive=False)
                    with gr.Row():
                        with gr.Column():
                            btn_audiogen = gr.Button("Generate 🚀", variant="primary")
                        with gr.Column():                            
                            btn_audiogen_cancel = gr.Button("Cancel 🛑", variant="stop")
                            btn_audiogen_cancel.click(fn=initiate_stop_audiogen, inputs=None, outputs=None)                              
                        with gr.Column():
                            btn_audiogen_clear_input = gr.ClearButton(components=prompt_audiogen, value="Clear inputs 🧹")
                        with gr.Column():                            
                            btn_audiogen_clear_output = gr.ClearButton(components=out_audiogen, value="Clear outputs 🧹")                        
                        btn_audiogen.click(
                            fn=music_audiogen, 
                            inputs=[
                                prompt_audiogen, 
                                model_audiogen,
                                duration_audiogen,
                                num_batch_audiogen,
                                temperature_audiogen,
                                top_k_audiogen,
                                top_p_audiogen,
                                use_sampling_audiogen,
                                cfg_coef_audiogen,
                            ],
                            outputs=out_audiogen,
                            show_progress="full",                            
                        )
                    with gr.Accordion("Send ...", open=False):
                        with gr.Row():
                            with gr.Column():
                                with gr.Box():                                
                                    with gr.Group():
                                        gr.HTML(value='... selected output to ...')
                            with gr.Column():
                                with gr.Box():
                                    with gr.Group():
                                        gr.HTML(value='... input prompt(s) to ...')
                                        gr.HTML(value='... audio module ...')
                                        audiogen_musicgen_input = gr.Button("✍️ >> Musicgen")
                            with gr.Column():
                                with gr.Box():                                
                                    with gr.Group():
                                        gr.HTML(value='... both to ...')                                    

# Harmonai
                with gr.TabItem("Harmonai 🔊", id=33) as tab_harmonai:
                    with gr.Accordion("About", open=False):                
                        with gr.Box():                       
                            gr.HTML(
                                """
                                <h1 style='text-align: left'; text-decoration: underline;>Informations</h1>
                                <b>Module : </b>Harmonai</br>
                                <b>Function : </b>Generate audio from a specific model using <a href='https://www.harmonai.org/' target='_blank'>Harmonai</a></br>
                                <b>Input(s) : </b>None</br>
                                <b>Output(s) : </b>Generated audio</br>
                                <b>HF model page : </b>
                                <a href='https://huggingface.co/harmonai/glitch-440k' target='_blank'>harmonai/glitch-440k</a> ,
                                <a href='https://huggingface.co/harmonai/honk-140k' target='_blank'>harmonai/honk-140k</a> ,
                                <a href='https://huggingface.co/harmonai/jmann-small-190k' target='_blank'>harmonai/jmann-small-190k</a> ,
                                <a href='https://huggingface.co/harmonai/jmann-large-580k' target='_blank'>harmonai/jmann-large-580k</a> ,
                                <a href='https://huggingface.co/harmonai/maestro-150k' target='_blank'>harmonai/maestro-150k</a> ,
                                <a href='https://huggingface.co/harmonai/unlocked-250k' target='_blank'>harmonai/unlocked-250k</a></br>                                
                                """
                            )
                        with gr.Box():
                            gr.HTML(
                                """
                                <h1 style='text-align: left'; text-decoration: underline;>Help</h1>
                                <div style='text-align: justified'>
                                <b>Usage :</b></br>
                                - (optional) Modify the settings to change audio duration</br>                                
                                - Click the <b>Generate<b> button</br>
                                - After generation, generated audio is available to listen in the <b>Output<b> field.
                                </div>
                                """
                            )                                       
                    with gr.Accordion("Settings", open=False):
                        with gr.Row():
                            with gr.Column():
                                model_harmonai = gr.Dropdown(choices=model_list_harmonai, value=model_list_harmonai[4], label="Model", info="Choose model to use for inference")
                            with gr.Column():
                                steps_harmonai = gr.Slider(1, 100, step=1, value=50, label="Steps", info="Number of iterations per audio. Results and speed depends of sampler")
                            with gr.Column():
                                seed_harmonai = gr.Slider(0, 10000000000, value=0, label="Seed(0 for random)", info="Seed to use for generation. Depending on scheduler, may permit reproducibility")
                        with gr.Row():
                            with gr.Column():
                                length_harmonai = gr.Slider(1, 1200, value=5, step=1, label="Audio length (sec)")
                            with gr.Column():
                                batch_size_harmonai = gr.Slider(1, 4, step=1, value=1, label="Batch size", info ="Number of audios to generate in a single run")
                            with gr.Column():
                                batch_repeat_harmonai = gr.Slider(1, 32, step=1, value=1, label="Batch count", info="Number of batch to run successively")
                    with gr.Row():
                        out_harmonai = gr.Audio(label="Output", type="filepath", show_download_button=True, interactive=False)
                    with gr.Row():
                        with gr.Column():
                            btn_harmonai = gr.Button("Generate 🚀", variant="primary")
                        with gr.Column():                            
                            btn_harmonai_clear_output = gr.ClearButton(components=out_harmonai, value="Clear outputs 🧹")                           
                        btn_harmonai.click(
                            fn=music_harmonai,
                            inputs=[
                            length_harmonai,
                            model_harmonai,
                            steps_harmonai,
                            seed_harmonai,
                            batch_size_harmonai,
                            batch_repeat_harmonai,
                            ],
                            outputs=out_harmonai,
                            show_progress="full",    
                        )
                    with gr.Accordion("Send ...", open=False):
                        with gr.Row():
                            with gr.Column():
                                with gr.Box():                                
                                    with gr.Group():
                                        gr.HTML(value='... selected output to ...')
                            with gr.Column():
                                with gr.Box():
                                    with gr.Group():
                                        gr.HTML(value='... input prompt(s) to ...')
                            with gr.Column():
                                with gr.Box():                                
                                    with gr.Group():
                                        gr.HTML(value='... both to ...')                       
# Bark
                with gr.TabItem("Bark 🗣️", id=34) as tab_bark:
                    with gr.Accordion("About", open=False):                
                        with gr.Box():                       
                            gr.HTML(
                                """
                                <h1 style='text-align: left'; text-decoration: underline;>Informations</h1>
                                <b>Module : </b>Bark</br>
                                <b>Function : </b>Generate high quality text-to-speech in several languages with <a href='https://github.com/suno-ai/bark' target='_blank'>Bark</a></br>
                                <b>Input(s) : </b>Prompt</br>
                                <b>Output(s) : </b>Generated speech</br>
                                <b>HF model page : </b>
                                <a href='https://huggingface.co/suno/bark' target='_blank'>suno/bark</a> ,
                                <a href='https://huggingface.co/suno/bark-small' target='_blank'>suno/bark-small</a></br>               
                                """
                            )
                        with gr.Box():
                            gr.HTML(
                                """
                                <h1 style='text-align: left'; text-decoration: underline;>Help</h1>
                                <div style='text-align: justified'>
                                <b>Usage :</b></br>
                                - Fill the <b>prompt</b> with the text you want to hear</br>                                
                                - (optional) Modify the settings to select a model and a voice</br>                                
                                - Click the <b>Generate</b> button</br>
                                - After generation, generated audio is available to listen in the <b>Generated speech</b> field.</br>
                                <b>Tips : </b>You can add modifications to the generated voices, by adding the following in your prompts : 
                                [laughter]</br>
                                [laughs]</br>
                                [sighs]</br>
                                [music]</br>
                                [gasps]</br>
                                [clears throat]</br>
                                — or ... for hesitations</br>
                                ♪ for song lyrics</br>
                                CAPITALIZATION for emphasis of a word</br>
                                [MAN] and [WOMAN] to bias Bark toward male and female speakers, respectively</br>
                                </div>
                                """
                            )                                                       
                    with gr.Accordion("Settings", open=False):
                        with gr.Row():
                            with gr.Column():
                                model_bark = gr.Dropdown(choices=model_list_bark, value=model_list_bark[0], label="Model", info="Choose model to use for inference")
                            with gr.Column():
                                voice_preset_bark = gr.Dropdown(choices=list(voice_preset_list_bark.keys()), value=list(voice_preset_list_bark.keys())[2], label="Voice")
                    with gr.Row():
                        with gr.Column():                    
                            prompt_bark = gr.Textbox(label="Text to speech", lines=2, max_lines=2, placeholder="Type or past here what you want to hear ...")
                        with gr.Column():
                            out_bark = gr.Audio(label="Generated speech", type="filepath", show_download_button=True, interactive=False)
                    with gr.Row():
                        with gr.Column():
                            btn_bark = gr.Button("Generate 🚀", variant="primary")
                        with gr.Column():
                            btn_bark_clear_input = gr.ClearButton(components=prompt_bark, value="Clear inputs 🧹")
                        with gr.Column():                            
                            btn_bark_clear_output = gr.ClearButton(components=out_bark, value="Clear outputs 🧹")                        
                        btn_bark.click(
                            fn=music_bark,
                            inputs=[
                                prompt_bark,
                                model_bark,
                                voice_preset_bark,
                            ],
                            outputs=out_bark,
                            show_progress="full",    
                        )
                    with gr.Accordion("Send ...", open=False):
                        with gr.Row():
                            with gr.Column():
                                with gr.Box():                                
                                    with gr.Group():
                                        gr.HTML(value='... selected output to ...')
                                        gr.HTML(value='... text module ...')                                        
                                        bark_whisper = gr.Button("🗣️ >> Whisper")                                      
                            with gr.Column():
                                with gr.Box():
                                    with gr.Group():
                                        gr.HTML(value='... input prompt(s) to ...')
                            with gr.Column():
                                with gr.Box():                                
                                    with gr.Group():
                                        gr.HTML(value='... both to ...')                                    
# Video
        with gr.TabItem("Video 🎬", id=4) as tab_video:
            with gr.Tabs() as tabs_video:           
# Modelscope            
                if ram_size() >= 16 :
                    titletab_txt2vid_ms = "Modelscope 📼"
                else :
                    titletab_txt2vid_ms = "Modelscope 📼 ⛔"
                    
                with gr.TabItem(titletab_txt2vid_ms, id=41) as tab_txt2vid_ms:                        
                        
                    with gr.Accordion("About", open=False):                
                        with gr.Box():                       
                            gr.HTML(
                                """
                                <h1 style='text-align: left'; text-decoration: underline;>Informations</h1>
                                <b>Module : </b>Modelscope</br>
                                <b>Function : </b>Generate video from a prompt and a negative prompt using <a href='https://github.com/modelscope/modelscope' target='_blank'>Modelscope</a></br>
                                <b>Input(s) : </b>Prompt, negative prompt</br>
                                <b>Output(s) : </b>Video</br>
                                <b>HF model page : </b>
                                <a href='https://huggingface.co/cerspense/zeroscope_v2_576w' target='_blank'>cerspense/zeroscope_v2_576w</a>, 
                                <a href='https://huggingface.co/camenduru/potat1' target='_blank'>camenduru/potat1</a>, 
                                <a href='https://huggingface.co/damo-vilab/text-to-video-ms-1.7b' target='_blank'>damo-vilab/text-to-video-ms-1.7b</a></br>
                                """
                            )
                        with gr.Box():
                            gr.HTML(
                                """
                                <h1 style='text-align: left'; text-decoration: underline;>Help</h1>
                                <div style='text-align: justified'>
                                <b>Usage :</b></br>
                                - Fill the <b>prompt</b> with what you want to see in your output video</br>
                                - Fill the <b>negative prompt</b> with what you DO NOT want to see in your output video</br>
                                - (optional) Modify the settings to use another model, modify the number of frames to generate, or change dimensions of the outputs</br>
                                - Click the <b>Generate</b> button</br>
                                - After generation, generated video is displayed in the <b>Generated video</b> field.
                                </br>
                                </div>
                                """
                            )                
                    with gr.Accordion("Settings", open=False):
                        with gr.Row():
                            with gr.Column():
                                model_txt2vid_ms = gr.Dropdown(choices=model_list_txt2vid_ms, value=model_list_txt2vid_ms[0], label="Model", info="Choose model to use for inference")
                            with gr.Column():
                                num_inference_step_txt2vid_ms = gr.Slider(1, 100, step=1, value=10, label="Steps", info="Number of iterations per video. Results and speed depends of sampler")
                            with gr.Column():
                                sampler_txt2vid_ms = gr.Dropdown(choices=list(SCHEDULER_MAPPING.keys()), value=list(SCHEDULER_MAPPING.keys())[0], label="Sampler", info="Sampler to use for inference")
                        with gr.Row():
                            with gr.Column():
                                guidance_scale_txt2vid_ms = gr.Slider(0.1, 20.0, step=0.1, value=4.0, label="CFG scale", info="Low values : more creativity. High values : more corresponding to the prompts")
                            with gr.Column():
                                num_frames_txt2vid_ms = gr.Slider(1, 1200, step=1, value=8, label="Video Length (frames)", info="Number of frames in the output video")
                            with gr.Column():
                                num_prompt_txt2vid_ms = gr.Slider(1, 32, step=1, value=1, label="Batch count", info="Number of batch to run successively")
                        with gr.Row():
                            with gr.Column():
                                width_txt2vid_ms = gr.Slider(128, 1280, step=64, value=576, label="Video Width", info="Width of outputs")
                            with gr.Column():
                                height_txt2vid_ms = gr.Slider(128, 1280, step=64, value=320, label="Video Height", info="Height of outputs")
                            with gr.Column():
                                seed_txt2vid_ms = gr.Slider(0, 10000000000, step=1, value=0, label="Seed(0 for random)", info="Seed to use for generation. Depending on scheduler, may permit reproducibility")
                        with gr.Row():
                            with gr.Column():    
                                use_gfpgan_txt2vid_ms = gr.Checkbox(value=True, label="Use GFPGAN to restore faces", info="Use GFPGAN to enhance faces in the outputs")
                    with gr.Row():
                        with gr.Column():
                            with gr.Row():
                                with gr.Column():
                                    prompt_txt2vid_ms = gr.Textbox(lines=4, max_lines=4, label="Prompt", info="Describe what you want in your video", placeholder="Darth vader is surfing on waves, photo realistic, best quality")
                            with gr.Row():
                                with gr.Column():
                                    negative_prompt_txt2vid_ms = gr.Textbox(lines=4, max_lines=4, label="Negative Prompt", info="Describe what you DO NOT want in your video", placeholder="out of frame, ugly")
                        with gr.Column():
                            out_txt2vid_ms = gr.Video(label="Generated video", height=400, interactive=False)
                    with gr.Row():
                        with gr.Column():
                            btn_txt2vid_ms = gr.Button("Generate 🚀", variant="primary")
                        with gr.Column():                            
                            btn_txt2vid_ms_cancel = gr.Button("Cancel 🛑", variant="stop")
                            btn_txt2vid_ms_cancel.click(fn=initiate_stop_txt2vid_ms, inputs=None, outputs=None)                              
                        with gr.Column():
                            btn_txt2vid_ms_clear_input = gr.ClearButton(components=[prompt_txt2vid_ms, negative_prompt_txt2vid_ms], value="Clear inputs 🧹")
                        with gr.Column():                            
                            btn_txt2vid_ms_clear_output = gr.ClearButton(components=[out_txt2vid_ms], value="Clear outputs 🧹")                                                   
                            btn_txt2vid_ms.click(
                                fn=video_txt2vid_ms,
                                inputs=[
                                    model_txt2vid_ms,
                                    sampler_txt2vid_ms,
                                    prompt_txt2vid_ms,
                                    negative_prompt_txt2vid_ms,
                                    num_frames_txt2vid_ms,
                                    num_prompt_txt2vid_ms,
                                    guidance_scale_txt2vid_ms,
                                    num_inference_step_txt2vid_ms,
                                    height_txt2vid_ms,
                                    width_txt2vid_ms,
                                    seed_txt2vid_ms,
                                    use_gfpgan_txt2vid_ms,
                                ],
                                outputs=out_txt2vid_ms,
                                show_progress="full",
                            )
                    with gr.Accordion("Send ...", open=False):
                        with gr.Row():
                            with gr.Column():
                                with gr.Box():                                
                                    with gr.Group():
                                        gr.HTML(value='... selected output to ...')
                                        gr.HTML(value='... video module ...')
                                        txt2vid_ms_vid2vid_ze = gr.Button("📼 >> Video Instruct-pix2pix")
                            with gr.Column():
                                with gr.Box():
                                    with gr.Group():
                                        gr.HTML(value='... input prompt(s) to ...')
                                        gr.HTML(value='... image module ...')
                                        txt2vid_ms_txt2img_sd_input = gr.Button("✍️ >> Stable Diffusion")
                                        txt2vid_ms_txt2img_kd_input = gr.Button("✍️ >> Kandinsky")
                                        gr.HTML(value='... video module ...')
                                        txt2vid_ms_txt2vid_ze_input = gr.Button("✍️ >> Text2Video-Zero")
                            with gr.Column():
                                with gr.Box():                                
                                    with gr.Group():
                                        gr.HTML(value='... both to ...')
# Txt2vid_zero            
                with gr.TabItem("Text2Video-Zero 📼", id=42) as tab_txt2vid_ze:
                    with gr.Accordion("About", open=False):                
                        with gr.Box():                       
                            gr.HTML(
                                """
                                <h1 style='text-align: left'; text-decoration: underline;>Informations</h1>
                                <b>Module : </b>Text2Video-Zero</br>
                                <b>Function : </b>Generate video from a prompt and a negative prompt using <a href='https://github.com/Picsart-AI-Research/Text2Video-Zero' target='_blank'>Text2Video-Zero</a> with <a href='https://stability.ai/stablediffusion' target='_blank'>Stable Diffusion</a> Models</br>
                                <b>Input(s) : </b>Prompt, negative prompt</br>
                                <b>Output(s) : </b>Video</br>
                                <b>HF model page : </b>
                                <a href='https://huggingface.co/SG161222/Realistic_Vision_V3.0_VAE' target='_blank'>SG161222/Realistic_Vision_V3.0_VAE</a>, 
                                <a href='https://huggingface.co/runwayml/stable-diffusion-v1-5' target='_blank'>runwayml/stable-diffusion-v1-5</a>, 
                                <a href='https://huggingface.co/nitrosocke/Ghibli-Diffusion' target='_blank'>nitrosocke/Ghibli-Diffusion</a></br>
                                """
#                                 <a href='https://huggingface.co/ckpt/anything-v4.5-vae-swapped' target='_blank'>ckpt/anything-v4.5-vae-swapped</a>, 
                            )
                        with gr.Box():
                            gr.HTML(
                                """
                                <h1 style='text-align: left'; text-decoration: underline;>Help</h1>
                                <div style='text-align: justified'>
                                <b>Usage :</b></br>
                                - Fill the <b>prompt</b> with what you want to see in your output video</br>
                                - Fill the <b>negative prompt</b> with what you DO NOT want to see in your output video</br>
                                - (optional) Modify the settings to use another model, modify the number of frames to generate, fps of the output video or change dimensions of the outputs</br>
                                - Click the <b>Generate</b> button</br>
                                - After generation, generated video is displayed in the <b>Generated video</b> field.
                                </br>
                                <b>Models :</b></br>
                                - You could place <a href='https://huggingface.co/' target='_blank'>huggingface.co</a> or  <a href='https://www.civitai.com/' target='_blank'>civitai.com</a> Stable diffusion based safetensors models in the directory /biniou/models/Stable Diffusion. Restart Biniou to see them in the models list.
                                </div>
                                """
                            )                      
                    with gr.Accordion("Settings", open=False):
                        with gr.Row():
                            with gr.Column():
                                model_txt2vid_ze = gr.Dropdown(choices=model_list_txt2vid_ze, value=model_list_txt2vid_ze[0], label="Model", info="Choose model to use for inference")
                            with gr.Column():
                                num_inference_step_txt2vid_ze = gr.Slider(1, 100, step=1, value=10, label="Steps", info="Number of iterations per video. Results and speed depends of sampler")
                            with gr.Column():
                                sampler_txt2vid_ze = gr.Dropdown(choices=list(SCHEDULER_MAPPING.keys()), value=list(SCHEDULER_MAPPING.keys())[0], label="Sampler", info="Sampler to use for inference")
                            with gr.Column():
                                guidance_scale_txt2vid_ze = gr.Slider(0.1, 20.0, step=0.1, value=7.5, label="CFG scale", info="Low values : more creativity. High values : more corresponding to the prompts")
                        with gr.Row():
                            with gr.Column():
                                seed_txt2vid_ze = gr.Slider(0, 10000000000, step=1, value=0, label="Seed(0 for random)", info="Seed to use for generation. Depending on scheduler, may permit reproducibility")
                            with gr.Column():
                                num_frames_txt2vid_ze = gr.Slider(1, 1200, step=1, value=8, label="Video Length (frames)", info="Number of frames in the output video")
                            with gr.Column():
                                num_fps_txt2vid_ze = gr.Slider(1, 120, step=1, value=4, label="Frames per second", info="Number of frames per second")
                            with gr.Column():
                                num_chunks_txt2vid_ze = gr.Slider(1, 32, step=1, value=1, label="Chunk size", info="Number of frames processed in a chunk. 1 = no chunks.")
                        with gr.Row():
                            with gr.Column():
                                width_txt2vid_ze = gr.Slider(128, 1280, step=64, value=576, label="Video Width", info="Width of outputs")
                            with gr.Column():
                                height_txt2vid_ze = gr.Slider(128, 1280, step=64, value=320, label="Video Height", info="Height of outputs")
                            with gr.Column():
                                num_videos_per_prompt_txt2vid_ze = gr.Slider(1, 4, step=1, value=1, label="Batch size", info ="Number of videos to generate in a single run")
                            with gr.Column():
                                num_prompt_txt2vid_ze = gr.Slider(1, 32, step=1, value=1, label="Batch count", info="Number of batch to run successively")                            
                        with gr.Accordion("Advanced Settings", open=False):
                            with gr.Row():
                                with gr.Column():
                                    motion_field_strength_x_txt2vid_ze = gr.Slider(0, 50, step=1, value=12, label="Motion field strength x", info="Horizontal motion strength")
                                with gr.Column():
                                    motion_field_strength_y_txt2vid_ze = gr.Slider(0, 50, step=1, value=12, label="Motion field strength y", info="Vertical motion strength")
                                with gr.Column():
                                    timestep_t0_txt2vid_ze = gr.Slider(0, 100, step=1, value=7, label="Timestep t0", interactive=False)
                                with gr.Column():
                                    timestep_t1_txt2vid_ze = gr.Slider(1, 100, step=1, value=8, label="Timestep t1", interactive=False)
                                    num_inference_step_txt2vid_ze.change(set_timestep_vid_ze, inputs=num_inference_step_txt2vid_ze, outputs=[timestep_t0_txt2vid_ze, timestep_t1_txt2vid_ze])
                        with gr.Row():
                            with gr.Column():    
                                use_gfpgan_txt2vid_ze = gr.Checkbox(value=True, label="Use GFPGAN to restore faces", info="Use GFPGAN to enhance faces in the outputs")
                            with gr.Column():    
                                tkme_txt2vid_ze = gr.Slider(0.0, 1.0, step=0.01, value=0.6, label="Token Merging ratio", info="0=slow,best quality, 1=fast,worst quality")
                    with gr.Row():
                        with gr.Column():
                            with gr.Row():
                                with gr.Column():
                                    prompt_txt2vid_ze = gr.Textbox(lines=4, max_lines=4, label="Prompt", info="Describe what you want in your video", placeholder="a panda is playing guitar on times square")
                            with gr.Row():
                                with gr.Column():
                                    negative_prompt_txt2vid_ze = gr.Textbox(lines=4, max_lines=4, label="Negative Prompt", info="Describe what you DO NOT want in your video", placeholder="out of frame, ugly")
                        with gr.Column():
                            out_txt2vid_ze = gr.Video(label="Generated video", height=400, interactive=False)
                    with gr.Row():
                        with gr.Column():
                            btn_txt2vid_ze = gr.Button("Generate 🚀", variant="primary")
                        with gr.Column():                            
                            btn_txt2vid_ze_cancel = gr.Button("Cancel 🛑", variant="stop")
                            btn_txt2vid_ze_cancel.click(fn=initiate_stop_txt2vid_ze, inputs=None, outputs=None)                              
                        with gr.Column():
                            btn_txt2vid_ze_clear_input = gr.ClearButton(components=[prompt_txt2vid_ze, negative_prompt_txt2vid_ze], value="Clear inputs 🧹")
                        with gr.Column():                            
                            btn_txt2vid_ze_clear_output = gr.ClearButton(components=[out_txt2vid_ze], value="Clear outputs 🧹")                           
                            btn_txt2vid_ze.click(
                                fn=video_txt2vid_ze,
                                inputs=[
                                    model_txt2vid_ze,
                                    num_inference_step_txt2vid_ze,
                                    sampler_txt2vid_ze,
                                    guidance_scale_txt2vid_ze,
                                    seed_txt2vid_ze,
                                    num_frames_txt2vid_ze,
                                    num_fps_txt2vid_ze,
                                    height_txt2vid_ze,
                                    width_txt2vid_ze,
                                    num_videos_per_prompt_txt2vid_ze,
                                    num_prompt_txt2vid_ze,
                                    motion_field_strength_x_txt2vid_ze,
                                    motion_field_strength_y_txt2vid_ze,
                                    timestep_t0_txt2vid_ze,
                                    timestep_t1_txt2vid_ze,
                                    prompt_txt2vid_ze,
                                    negative_prompt_txt2vid_ze,
                                    nsfw_filter,
                                    num_chunks_txt2vid_ze,
                                    use_gfpgan_txt2vid_ze,
                                    tkme_txt2vid_ze,
                                ],
                                outputs=out_txt2vid_ze,
                                show_progress="full",
                            )
                    with gr.Accordion("Send ...", open=False):
                        with gr.Row():
                            with gr.Column():
                                with gr.Box():                                
                                    with gr.Group():
                                        gr.HTML(value='... selected output to ...')
                                        gr.HTML(value='... video module ...')
                                        txt2vid_ze_vid2vid_ze = gr.Button("📼 >> Video Instruct-pix2pix")
                            with gr.Column():
                                with gr.Box():
                                    with gr.Group():
                                        gr.HTML(value='... input prompt(s) to ...')
                                        gr.HTML(value='... image module ...')
                                        txt2vid_ze_txt2img_sd_input = gr.Button("✍️ >> Stable Diffusion")
                                        txt2vid_ze_txt2img_kd_input = gr.Button("✍️ >> Kandinsky")
                                        gr.HTML(value='... video module ...')                                        
                                        txt2vid_ze_txt2vid_ms_input = gr.Button("✍️ >> Modelscope")
                            with gr.Column():
                                with gr.Box():                                
                                    with gr.Group():
                                        gr.HTML(value='... both to ...')        
# vid2vid_ze    
                if ram_size() >= 16 :
                    titletab_vid2vid_ze = "Video Instruct-Pix2Pix 🖌️"
                else :
                    titletab_vid2vid_ze = "Video Instruct-Pix2Pix 🖌️ ⛔"

                with gr.TabItem(titletab_vid2vid_ze, id=43) as tab_vid2vid_ze:
                    with gr.Accordion("About", open=False):                
                        with gr.Box():                       
                            gr.HTML(
                                """
                                <h1 style='text-align: left'; text-decoration: underline;>Informations</h1>
                                <b>Module : </b>Video Instruct-Pix2Pix</br>
                                <b>Function : </b>Edit an input video with instructions from a prompt and a negative prompt using <a href='https://github.com/timothybrooks/instruct-pix2pix' target='_blank'>Instructpix2pix</a> and <a href='https://github.com/Picsart-AI-Research/Text2Video-Zero' target='_blank'>Text2Video-Zero</a></br>
                                <b>Input(s) : </b>Input video, prompt, negative prompt</br>
                                <b>Output(s) : </b>Video(s)</br>
                                <b>HF model page : </b>
                                <a href='https://huggingface.co/timbrooks/instruct-pix2pix' target='_blank'>timbrooks/instruct-pix2pix</a></br>
                                """
                            )
                        with gr.Box():
                            gr.HTML(
                                """
                                <h1 style='text-align: left'; text-decoration: underline;>Help</h1>
                                <div style='text-align: justified'>
                                <b>Usage :</b></br>
                                - Upload or import a video using the <b>Input video</b> field</br>
                                - Fill the <b>prompt</b> with the instructions for modifying your input video</br>
                                - Fill the <b>negative prompt</b> with what you DO NOT want to see in your output video</br>
                                - (optional) Modify the settings to change the number of frames to process (default=8) or the fps of the output</br>
                                - Click the <b>Generate</b> button</br>
                                - After generation, generated video is displayed in the Generated video field.</br></br>
                                <b>Examples : </b><a href='https://www.timothybrooks.com/instruct-pix2pix/' target='_blank'>Instructpix2pix : Learning to Follow Image Editing Instructions</a>
                                </div>
                                """
                            )                
                    with gr.Accordion("Settings", open=False):
                        with gr.Row():
                            with gr.Column():
                                model_vid2vid_ze = gr.Dropdown(choices=model_list_vid2vid_ze, value=model_list_vid2vid_ze[0], label="Model", info="Choose model to use for inference")
                            with gr.Column():
                                num_inference_step_vid2vid_ze = gr.Slider(1, 100, step=1, value=10, label="Steps", info="Number of iterations per image. Results and speed depends of sampler")
                            with gr.Column():
                                sampler_vid2vid_ze = gr.Dropdown(choices=list(SCHEDULER_MAPPING.keys()), value=list(SCHEDULER_MAPPING.keys())[0], label="Sampler", info="Sampler to use for inference")
                        with gr.Row():
                            with gr.Column():
                                guidance_scale_vid2vid_ze = gr.Slider(0.0, 10.0, step=0.1, value=7.5, label="CFG Scale", info="Low values : more creativity. High values : more corresponding to the prompts")
                            with gr.Column():
                                image_guidance_scale_vid2vid_ze = gr.Slider(0.0, 10.0, step=0.1, value=1.5, label="Img CFG Scale", info="Low values : more creativity. High values : more corresponding to the input video")
                            with gr.Column():
                                num_images_per_prompt_vid2vid_ze = gr.Slider(1, 4, step=1, value=1, label="Batch size", info ="Number of videos to generate in a single run")
                            with gr.Column():
                                num_prompt_vid2vid_ze = gr.Slider(1, 32, step=1, value=1, label="Batch count", info="Number of batch to run successively")
                        with gr.Row():
                            with gr.Column():
                                width_vid2vid_ze = gr.Slider(128, 8192, step=64, value=512, label="Image Width", info="Width of outputs", interactive=False)
                            with gr.Column():
                                height_vid2vid_ze = gr.Slider(128, 8192, step=64, value=512, label="Image Height", info="Height of outputs", interactive=False)
                            with gr.Column():
                                seed_vid2vid_ze = gr.Slider(0, 10000000000, step=1, value=0, label="Seed(0 for random)", info="Seed to use for generation. Depending on scheduler, may permit reproducibility")
                        with gr.Row():
                            with gr.Column():
                                num_frames_vid2vid_ze = gr.Slider(0, 1200, step=1, value=8, label="Video Length (frames)", info="Number of frames to process")
                            with gr.Column():
                                num_fps_txt2vid_ze = gr.Slider(1, 120, step=1, value=4, label="Frames per second", info="Number of frames per second")
                        with gr.Row():
                            with gr.Column():    
                                use_gfpgan_vid2vid_ze = gr.Checkbox(value=True, label="Use GFPGAN to restore faces", info="Use GFPGAN to enhance faces in the outputs")
                            with gr.Column():
                                tkme_vid2vid_ze = gr.Slider(0.0, 1.0, step=0.01, value=0.6, label="Token merging ratio", info="0=slow,best quality, 1=fast,worst quality")
                    with gr.Row():
                        with gr.Column():
                             vid_vid2vid_ze = gr.Video(label="Input video", height=400)
                        with gr.Column():
                            with gr.Row():
                                with gr.Column():
                                    prompt_vid2vid_ze = gr.Textbox(lines=5, max_lines=5, label="Prompt", info="Describe what you want to modify in your input video", placeholder="make it Van Gogh Starry Night style")
                                with gr.Column():
                                    negative_prompt_vid2vid_ze = gr.Textbox(lines=5, max_lines=5, label="Negative Prompt", info="Describe what you DO NOT want in your output video", placeholder="out of frame, bad quality, blurry, ugly, text, characters, logo")
                        with gr.Column():
                            with gr.Row():
                                with gr.Column():
                                    out_vid2vid_ze = gr.Video(label="Generated video", height=400, interactive=False)
                                    gs_out_vid2vid_ze = gr.State()
                    with gr.Row():
                        with gr.Column():
                            btn_vid2vid_ze = gr.Button("Generate 🚀", variant="primary")
                        with gr.Column():                            
                            btn_vid2vid_ze_cancel = gr.Button("Cancel 🛑", variant="stop")
                            btn_vid2vid_ze_cancel.click(fn=initiate_stop_vid2vid_ze, inputs=None, outputs=None)                              
                        with gr.Column():
                            btn_vid2vid_ze_clear_input = gr.ClearButton(components=[vid_vid2vid_ze, prompt_vid2vid_ze, negative_prompt_vid2vid_ze], value="Clear inputs 🧹")
                        with gr.Column():                            
                            btn_vid2vid_ze_clear_output = gr.ClearButton(components=[out_vid2vid_ze, gs_out_vid2vid_ze], value="Clear outputs 🧹")
                            btn_vid2vid_ze.click(
                                fn=image_vid2vid_ze,
                                inputs=[
                                    model_vid2vid_ze,
                                    sampler_vid2vid_ze,
                                    vid_vid2vid_ze,
                                    prompt_vid2vid_ze,
                                    negative_prompt_vid2vid_ze,
                                    num_images_per_prompt_vid2vid_ze,
                                    num_prompt_vid2vid_ze,
                                    guidance_scale_vid2vid_ze,
                                    image_guidance_scale_vid2vid_ze,
                                    num_inference_step_vid2vid_ze,
                                    height_vid2vid_ze,
                                    width_vid2vid_ze,
                                    seed_vid2vid_ze,
                                    num_frames_vid2vid_ze,
                                    num_fps_txt2vid_ze,
                                    use_gfpgan_vid2vid_ze,
                                    nsfw_filter,
                                    tkme_vid2vid_ze,
                                ],
                                outputs=[out_vid2vid_ze, gs_out_vid2vid_ze],
                                show_progress="full",
                            )  
                    with gr.Accordion("Send ...", open=False):
                        with gr.Row():
                            with gr.Column():
                                with gr.Box():                                
                                    with gr.Group():
                                        gr.HTML(value='... selected output to ...')
                            with gr.Column():
                                with gr.Box():
                                    with gr.Group():
                                        gr.HTML(value='... input prompt(s) to ...')
                                        gr.HTML(value='... image module ...')                                        
                                        vid2vid_ze_pix2pix = gr.Button("✍️ >> Instruct pix2pix")
                            with gr.Column():
                                with gr.Box():                                
                                    with gr.Group():
                                        gr.HTML(value='... both to ...')

    tab_text_num = gr.Number(value=tab_text.id, precision=0, visible=False)
    tab_image_num = gr.Number(value=tab_image.id, precision=0, visible=False)
    tab_audio_num = gr.Number(value=tab_audio.id, precision=0, visible=False)    
    tab_video_num = gr.Number(value=tab_video.id, precision=0, visible=False)

    tab_llamacpp_num = gr.Number(value=tab_llamacpp.id, precision=0, visible=False)    
    tab_img2txt_git_num = gr.Number(value=tab_img2txt_git.id, precision=0, visible=False)    
    tab_whisper_num = gr.Number(value=tab_whisper.id, precision=0, visible=False)        
    tab_nllb_num = gr.Number(value=tab_nllb.id, precision=0, visible=False)    
    tab_txt2img_sd_num = gr.Number(value=tab_txt2img_sd.id, precision=0, visible=False)
    tab_txt2img_kd_num = gr.Number(value=tab_txt2img_kd.id, precision=0, visible=False)
    tab_img2img_num = gr.Number(value=tab_img2img.id, precision=0, visible=False)
    tab_img2var_num = gr.Number(value=tab_img2var.id, precision=0, visible=False)    
    tab_pix2pix_num = gr.Number(value=tab_pix2pix.id, precision=0, visible=False)
    tab_inpaint_num = gr.Number(value=tab_inpaint.id, precision=0, visible=False)
    tab_controlnet_num = gr.Number(value=tab_controlnet.id, precision=0, visible=False)    
    tab_faceswap_num = gr.Number(value=tab_faceswap.id, precision=0, visible=False)
    tab_resrgan_num = gr.Number(value=tab_resrgan.id, precision=0, visible=False)
    tab_gfpgan_num = gr.Number(value=tab_gfpgan.id, precision=0, visible=False)
    tab_musicgen_num = gr.Number(value=tab_musicgen.id, precision=0, visible=False)
    tab_audiogen_num = gr.Number(value=tab_audiogen.id, precision=0, visible=False)
    tab_harmonai_num = gr.Number(value=tab_harmonai.id, precision=0, visible=False)
    tab_bark_num = gr.Number(value=tab_bark.id, precision=0, visible=False)
    tab_txt2vid_ms_num = gr.Number(value=tab_txt2vid_ms.id, precision=0, visible=False)
    tab_txt2vid_ze_num = gr.Number(value=tab_txt2vid_ze.id, precision=0, visible=False)    
    tab_vid2vid_ze_num = gr.Number(value=tab_vid2vid_ze.id, precision=0, visible=False)        

# Llamacpp outputs   
    llamacpp_nllb.click(fn=send_text_to_module_text, inputs=[out_llamacpp, tab_text_num, tab_nllb_num], outputs=[prompt_nllb, tabs, tabs_text])
    llamacpp_txt2img_sd.click(fn=send_text_to_module_image, inputs=[out_llamacpp, tab_image_num, tab_txt2img_sd_num], outputs=[prompt_txt2img_sd, tabs, tabs_image])
    llamacpp_txt2img_kd.click(fn=send_text_to_module_image, inputs=[out_llamacpp, tab_image_num, tab_txt2img_kd_num], outputs=[prompt_txt2img_kd, tabs, tabs_image])
    llamacpp_img2img.click(fn=send_text_to_module_image, inputs=[out_llamacpp, tab_image_num, tab_img2img_num], outputs=[prompt_img2img, tabs, tabs_image])
    llamacpp_pix2pix.click(fn=send_text_to_module_image, inputs=[out_llamacpp, tab_image_num, tab_pix2pix_num], outputs=[prompt_pix2pix, tabs, tabs_image])
    llamacpp_inpaint.click(fn=send_text_to_module_image, inputs=[out_llamacpp, tab_image_num, tab_inpaint_num], outputs=[prompt_inpaint, tabs, tabs_image])
    llamacpp_controlnet.click(fn=send_text_to_module_image, inputs=[out_llamacpp, tab_image_num, tab_controlnet_num], outputs=[prompt_controlnet, tabs, tabs_image])    
    llamacpp_musicgen.click(fn=import_to_module_audio, inputs=[out_llamacpp, tab_audio_num, tab_musicgen_num], outputs=[prompt_musicgen, tabs, tabs_audio])    
    llamacpp_audiogen.click(fn=import_to_module_audio, inputs=[out_llamacpp, tab_audio_num, tab_audiogen_num], outputs=[prompt_audiogen, tabs, tabs_audio])
    llamacpp_bark.click(fn=import_to_module_audio, inputs=[out_llamacpp, tab_audio_num, tab_bark_num], outputs=[prompt_bark, tabs, tabs_audio])    
    llamacpp_txt2vid_ms.click(fn=import_text_to_module_video, inputs=[out_llamacpp, tab_video_num, tab_txt2vid_ms_num], outputs=[prompt_txt2vid_ms, tabs, tabs_video])
    llamacpp_txt2vid_ze.click(fn=import_text_to_module_video, inputs=[out_llamacpp, tab_video_num, tab_txt2vid_ze_num], outputs=[prompt_txt2vid_ze, tabs, tabs_video])    

# GIT Captions outputs
    img2txt_git_nllb.click(fn=send_text_to_module_text, inputs=[out_img2txt_git, tab_text_num, tab_nllb_num], outputs=[prompt_nllb, tabs, tabs_text])    
    img2txt_git_txt2img_sd.click(fn=send_text_to_module_image, inputs=[out_img2txt_git, tab_image_num, tab_txt2img_sd_num], outputs=[prompt_txt2img_sd, tabs, tabs_image])
    img2txt_git_txt2img_kd.click(fn=send_text_to_module_image, inputs=[out_img2txt_git, tab_image_num, tab_txt2img_kd_num], outputs=[prompt_txt2img_kd, tabs, tabs_image])
    img2txt_git_img2img.click(fn=send_text_to_module_image, inputs=[out_img2txt_git, tab_image_num, tab_img2img_num], outputs=[prompt_img2img, tabs, tabs_image])
    img2txt_git_pix2pix.click(fn=send_text_to_module_image, inputs=[out_img2txt_git, tab_image_num, tab_pix2pix_num], outputs=[prompt_pix2pix, tabs, tabs_image])
    img2txt_git_inpaint.click(fn=send_text_to_module_image, inputs=[out_img2txt_git, tab_image_num, tab_inpaint_num], outputs=[prompt_inpaint, tabs, tabs_image])
    img2txt_git_controlnet.click(fn=send_text_to_module_image, inputs=[out_img2txt_git, tab_image_num, tab_controlnet_num], outputs=[prompt_controlnet, tabs, tabs_image])    
    img2txt_git_musicgen.click(fn=import_to_module_audio, inputs=[out_img2txt_git, tab_audio_num, tab_musicgen_num], outputs=[prompt_musicgen, tabs, tabs_audio])    
    img2txt_git_audiogen.click(fn=import_to_module_audio, inputs=[out_img2txt_git, tab_audio_num, tab_audiogen_num], outputs=[prompt_audiogen, tabs, tabs_audio])
    img2txt_git_txt2vid_ms.click(fn=import_text_to_module_video, inputs=[out_img2txt_git, tab_video_num, tab_txt2vid_ms_num], outputs=[prompt_txt2vid_ms, tabs, tabs_video])
    img2txt_git_txt2vid_ze.click(fn=import_text_to_module_video, inputs=[out_img2txt_git, tab_video_num, tab_txt2vid_ze_num], outputs=[prompt_txt2vid_ze, tabs, tabs_video])    

# GIT Captions both
    img2txt_git_img2img_both.click(fn=both_text_to_module_image, inputs=[img_img2txt_git, out_img2txt_git, tab_image_num, tab_img2img_num], outputs=[img_img2img, prompt_img2img, tabs, tabs_image])
    img2txt_git_pix2pix_both.click(fn=both_text_to_module_image, inputs=[img_img2txt_git, out_img2txt_git, tab_image_num, tab_pix2pix_num], outputs=[img_pix2pix, prompt_pix2pix, tabs, tabs_image])
    img2txt_git_inpaint_both.click(fn=both_text_to_module_inpaint_image, inputs=[img_img2txt_git, out_img2txt_git, tab_image_num, tab_inpaint_num], outputs=[img_inpaint, gs_img_inpaint, prompt_pix2pix, tabs, tabs_image])
    
# Whisper outputs
    whisper_nllb.click(fn=send_text_to_module_text, inputs=[out_whisper, tab_text_num, tab_nllb_num], outputs=[prompt_nllb, tabs, tabs_text])
    whisper_txt2img_sd.click(fn=send_text_to_module_image, inputs=[out_whisper, tab_image_num, tab_txt2img_sd_num], outputs=[prompt_txt2img_sd, tabs, tabs_image])
    whisper_txt2img_kd.click(fn=send_text_to_module_image, inputs=[out_whisper, tab_image_num, tab_txt2img_kd_num], outputs=[prompt_txt2img_kd, tabs, tabs_image])
    whisper_img2img.click(fn=send_text_to_module_image, inputs=[out_whisper, tab_image_num, tab_img2img_num], outputs=[prompt_img2img, tabs, tabs_image])
    whisper_pix2pix.click(fn=send_text_to_module_image, inputs=[out_whisper, tab_image_num, tab_pix2pix_num], outputs=[prompt_pix2pix, tabs, tabs_image])
    whisper_inpaint.click(fn=send_text_to_module_image, inputs=[out_whisper, tab_image_num, tab_inpaint_num], outputs=[prompt_inpaint, tabs, tabs_image])
    whisper_controlnet.click(fn=send_text_to_module_image, inputs=[out_whisper, tab_image_num, tab_controlnet_num], outputs=[prompt_controlnet, tabs, tabs_image])    
    whisper_musicgen.click(fn=import_to_module_audio, inputs=[out_whisper, tab_audio_num, tab_musicgen_num], outputs=[prompt_musicgen, tabs, tabs_audio])    
    whisper_audiogen.click(fn=import_to_module_audio, inputs=[out_whisper, tab_audio_num, tab_audiogen_num], outputs=[prompt_audiogen, tabs, tabs_audio])
    whisper_bark.click(fn=import_to_module_audio, inputs=[out_whisper, tab_audio_num, tab_bark_num], outputs=[prompt_bark, tabs, tabs_audio])    
    whisper_txt2vid_ms.click(fn=import_text_to_module_video, inputs=[out_whisper, tab_video_num, tab_txt2vid_ms_num], outputs=[prompt_txt2vid_ms, tabs, tabs_video])
    whisper_txt2vid_ze.click(fn=import_text_to_module_video, inputs=[out_whisper, tab_video_num, tab_txt2vid_ze_num], outputs=[prompt_txt2vid_ze, tabs, tabs_video])       

# Nllb outputs
    nllb_txt2img_sd.click(fn=send_text_to_module_image, inputs=[out_nllb, tab_image_num, tab_txt2img_sd_num], outputs=[prompt_txt2img_sd, tabs, tabs_image])
    nllb_txt2img_kd.click(fn=send_text_to_module_image, inputs=[out_nllb, tab_image_num, tab_txt2img_kd_num], outputs=[prompt_txt2img_kd, tabs, tabs_image])
    nllb_img2img.click(fn=send_text_to_module_image, inputs=[out_nllb, tab_image_num, tab_img2img_num], outputs=[prompt_img2img, tabs, tabs_image])
    nllb_pix2pix.click(fn=send_text_to_module_image, inputs=[out_nllb, tab_image_num, tab_pix2pix_num], outputs=[prompt_pix2pix, tabs, tabs_image])
    nllb_inpaint.click(fn=send_text_to_module_image, inputs=[out_nllb, tab_image_num, tab_inpaint_num], outputs=[prompt_inpaint, tabs, tabs_image])
    nllb_controlnet.click(fn=send_text_to_module_image, inputs=[out_nllb, tab_image_num, tab_controlnet_num], outputs=[prompt_controlnet, tabs, tabs_image])    
    nllb_musicgen.click(fn=import_to_module_audio, inputs=[out_nllb, tab_audio_num, tab_musicgen_num], outputs=[prompt_musicgen, tabs, tabs_audio])    
    nllb_audiogen.click(fn=import_to_module_audio, inputs=[out_nllb, tab_audio_num, tab_audiogen_num], outputs=[prompt_audiogen, tabs, tabs_audio])
    nllb_bark.click(fn=import_to_module_audio, inputs=[out_nllb, tab_audio_num, tab_bark_num], outputs=[prompt_bark, tabs, tabs_audio])    
    nllb_txt2vid_ms.click(fn=import_text_to_module_video, inputs=[out_nllb, tab_video_num, tab_txt2vid_ms_num], outputs=[prompt_txt2vid_ms, tabs, tabs_video])
    nllb_txt2vid_ze.click(fn=import_text_to_module_video, inputs=[out_nllb, tab_video_num, tab_txt2vid_ze_num], outputs=[prompt_txt2vid_ze, tabs, tabs_video])
      
# txt2img_sd outputs
    txt2img_sd_img2img.click(fn=send_to_module, inputs=[gs_out_txt2img_sd, sel_out_txt2img_sd, tab_image_num, tab_img2img_num], outputs=[img_img2img, tabs, tabs_image])
    txt2img_sd_img2var.click(fn=send_to_module, inputs=[gs_out_txt2img_sd, sel_out_txt2img_sd, tab_image_num, tab_img2var_num], outputs=[img_img2var, tabs, tabs_image])  
    txt2img_sd_pix2pix.click(fn=send_to_module, inputs=[gs_out_txt2img_sd, sel_out_txt2img_sd, tab_image_num, tab_pix2pix_num], outputs=[img_pix2pix, tabs, tabs_image])
    txt2img_sd_inpaint.click(fn=send_to_module_inpaint, inputs=[gs_out_txt2img_sd, sel_out_txt2img_sd, tab_image_num, tab_inpaint_num], outputs=[img_inpaint, gs_img_inpaint, tabs, tabs_image])    
    txt2img_sd_controlnet.click(fn=send_to_module_inpaint, inputs=[gs_out_txt2img_sd, sel_out_txt2img_sd, tab_image_num, tab_controlnet_num], outputs=[img_source_controlnet, gs_img_source_controlnet, tabs, tabs_image])
    txt2img_sd_faceswap.click(fn=send_to_module_inpaint, inputs=[gs_out_txt2img_sd, sel_out_txt2img_sd, tab_image_num, tab_faceswap_num], outputs=[img_target_faceswap, gs_img_target_faceswap, tabs, tabs_image])        
    txt2img_sd_resrgan.click(fn=send_to_module, inputs=[gs_out_txt2img_sd, sel_out_txt2img_sd, tab_image_num, tab_resrgan_num], outputs=[img_resrgan, tabs, tabs_image])
    txt2img_sd_gfpgan.click(fn=send_to_module, inputs=[gs_out_txt2img_sd, sel_out_txt2img_sd, tab_image_num, tab_gfpgan_num], outputs=[img_gfpgan, tabs, tabs_image])
    txt2img_sd_img2txt_git.click(fn=send_to_module_text, inputs=[gs_out_txt2img_sd, sel_out_txt2img_sd, tab_text_num, tab_img2txt_git_num], outputs=[img_img2txt_git, tabs, tabs_text])    

# txt2img_sd inputs
    txt2img_sd_txt2img_kd_input.click(fn=import_to_module, inputs=[prompt_txt2img_sd, negative_prompt_txt2img_sd, tab_image_num, tab_txt2img_kd_num], outputs=[prompt_txt2img_kd, negative_prompt_txt2img_kd, tabs, tabs_image])
    txt2img_sd_img2img_input.click(fn=import_to_module, inputs=[prompt_txt2img_sd, negative_prompt_txt2img_sd, tab_image_num, tab_img2img_num], outputs=[prompt_img2img, negative_prompt_img2img, tabs, tabs_image])
    txt2img_sd_pix2pix_input.click(fn=import_to_module, inputs=[prompt_txt2img_sd, negative_prompt_txt2img_sd, tab_image_num, tab_pix2pix_num], outputs=[prompt_pix2pix, negative_prompt_pix2pix, tabs, tabs_image])
    txt2img_sd_inpaint_input.click(fn=import_to_module, inputs=[prompt_txt2img_sd, negative_prompt_txt2img_sd, tab_image_num, tab_inpaint_num], outputs=[prompt_inpaint, negative_prompt_inpaint, tabs, tabs_image])
    txt2img_sd_controlnet_input.click(fn=import_to_module, inputs=[prompt_txt2img_sd, negative_prompt_txt2img_sd, tab_image_num, tab_controlnet_num], outputs=[prompt_controlnet, negative_prompt_controlnet, tabs, tabs_image])    
    txt2img_sd_txt2vid_ms_input.click(fn=import_to_module_video, inputs=[prompt_txt2img_sd, negative_prompt_txt2img_sd, tab_video_num, tab_txt2vid_ms_num], outputs=[prompt_txt2vid_ms, negative_prompt_txt2vid_ms, tabs, tabs_video])
    txt2img_sd_txt2vid_ze_input.click(fn=import_to_module_video, inputs=[prompt_txt2img_sd, negative_prompt_txt2img_sd, tab_video_num, tab_txt2vid_ze_num], outputs=[prompt_txt2vid_ze, negative_prompt_txt2vid_ze, tabs, tabs_video])    
    
# txt2img_sd both
    txt2img_sd_img2img_both.click(fn=both_to_module, inputs=[prompt_txt2img_sd, negative_prompt_txt2img_sd, gs_out_txt2img_sd, sel_out_txt2img_sd, tab_image_num, tab_img2img_num], outputs=[prompt_img2img, negative_prompt_img2img, img_img2img, tabs, tabs_image])
    txt2img_sd_pix2pix_both.click(fn=both_to_module, inputs=[prompt_txt2img_sd, negative_prompt_txt2img_sd, gs_out_txt2img_sd, sel_out_txt2img_sd, tab_image_num, tab_pix2pix_num], outputs=[prompt_pix2pix, negative_prompt_pix2pix, img_pix2pix, tabs, tabs_image])
    txt2img_sd_inpaint_both.click(fn=both_to_module_inpaint, inputs=[prompt_txt2img_sd, negative_prompt_txt2img_sd, gs_out_txt2img_sd, sel_out_txt2img_sd, tab_image_num, tab_inpaint_num], outputs=[prompt_inpaint, negative_prompt_inpaint,img_inpaint, gs_img_inpaint, tabs, tabs_image])
    txt2img_sd_controlnet_both.click(fn=both_to_module_inpaint, inputs=[prompt_txt2img_sd, negative_prompt_txt2img_sd, gs_out_txt2img_sd, sel_out_txt2img_sd, tab_image_num, tab_controlnet_num], outputs=[prompt_controlnet, negative_prompt_controlnet, img_source_controlnet, gs_img_source_controlnet, tabs, tabs_image])

# txt2img_kd outputs
    txt2img_kd_img2img.click(fn=send_to_module, inputs=[gs_out_txt2img_kd, sel_out_txt2img_kd, tab_image_num, tab_img2img_num], outputs=[img_img2img, tabs, tabs_image])
    txt2img_kd_img2var.click(fn=send_to_module, inputs=[gs_out_txt2img_kd, sel_out_txt2img_kd, tab_image_num, tab_img2var_num], outputs=[img_img2var, tabs, tabs_image])    
    txt2img_kd_pix2pix.click(fn=send_to_module, inputs=[gs_out_txt2img_kd, sel_out_txt2img_kd, tab_image_num, tab_pix2pix_num], outputs=[img_pix2pix, tabs, tabs_image])
    txt2img_kd_inpaint.click(fn=send_to_module_inpaint, inputs=[gs_out_txt2img_kd, sel_out_txt2img_kd, tab_image_num, tab_inpaint_num], outputs=[img_inpaint, gs_img_inpaint, tabs, tabs_image])    
    txt2img_kd_controlnet.click(fn=send_to_module_inpaint, inputs=[gs_out_txt2img_kd, sel_out_txt2img_kd, tab_image_num, tab_controlnet_num], outputs=[img_source_controlnet, gs_img_source_controlnet, tabs, tabs_image])
    txt2img_kd_faceswap.click(fn=send_to_module_inpaint, inputs=[gs_out_txt2img_kd, sel_out_txt2img_kd, tab_image_num, tab_faceswap_num], outputs=[img_target_faceswap, gs_img_target_faceswap, tabs, tabs_image])        
    txt2img_kd_resrgan.click(fn=send_to_module, inputs=[gs_out_txt2img_kd, sel_out_txt2img_kd, tab_image_num, tab_resrgan_num], outputs=[img_resrgan, tabs, tabs_image])
    txt2img_kd_gfpgan.click(fn=send_to_module, inputs=[gs_out_txt2img_kd, sel_out_txt2img_kd, tab_image_num, tab_gfpgan_num], outputs=[img_gfpgan, tabs, tabs_image])
    txt2img_kd_img2txt_git.click(fn=send_to_module_text, inputs=[gs_out_txt2img_kd, sel_out_txt2img_kd, tab_text_num, tab_img2txt_git_num], outputs=[img_img2txt_git, tabs, tabs_text])
    
# txt2img_kd inputs
    txt2img_kd_txt2img_sd_input.click(fn=import_to_module, inputs=[prompt_txt2img_kd, negative_prompt_txt2img_kd, tab_image_num, tab_txt2img_sd_num], outputs=[prompt_txt2img_sd, negative_prompt_txt2img_sd, tabs, tabs_image])
    txt2img_kd_img2img_input.click(fn=import_to_module, inputs=[prompt_txt2img_kd, negative_prompt_txt2img_kd, tab_image_num, tab_img2img_num], outputs=[prompt_img2img, negative_prompt_img2img, tabs, tabs_image])
    txt2img_kd_pix2pix_input.click(fn=import_to_module, inputs=[prompt_txt2img_kd, negative_prompt_txt2img_kd, tab_image_num, tab_pix2pix_num], outputs=[prompt_pix2pix, negative_prompt_pix2pix, tabs, tabs_image])
    txt2img_kd_inpaint_input.click(fn=import_to_module, inputs=[prompt_txt2img_kd, negative_prompt_txt2img_kd, tab_image_num, tab_inpaint_num], outputs=[prompt_inpaint, negative_prompt_inpaint, tabs, tabs_image])
    txt2img_kd_controlnet_input.click(fn=import_to_module, inputs=[prompt_txt2img_kd, negative_prompt_txt2img_kd, tab_image_num, tab_controlnet_num], outputs=[prompt_controlnet, negative_prompt_controlnet, tabs, tabs_image])
    txt2img_kd_txt2vid_ms_input.click(fn=import_to_module_video, inputs=[prompt_txt2img_kd, negative_prompt_txt2img_kd, tab_video_num, tab_txt2vid_ms_num], outputs=[prompt_txt2vid_ms, negative_prompt_txt2vid_ms, tabs, tabs_video])
    txt2img_kd_txt2vid_ze_input.click(fn=import_to_module_video, inputs=[prompt_txt2img_kd, negative_prompt_txt2img_kd, tab_video_num, tab_txt2vid_ze_num], outputs=[prompt_txt2vid_ze, negative_prompt_txt2vid_ze, tabs, tabs_video])        
    
# txt2img_kd both
    txt2img_kd_img2img_both.click(fn=both_to_module, inputs=[prompt_txt2img_kd, negative_prompt_txt2img_kd, gs_out_txt2img_kd, sel_out_txt2img_kd, tab_image_num, tab_img2img_num], outputs=[prompt_img2img, negative_prompt_img2img, img_img2img, tabs, tabs_image])
    txt2img_kd_pix2pix_both.click(fn=both_to_module, inputs=[prompt_txt2img_kd, negative_prompt_txt2img_kd, gs_out_txt2img_kd, sel_out_txt2img_kd, tab_image_num, tab_pix2pix_num], outputs=[prompt_pix2pix, negative_prompt_pix2pix, img_pix2pix, tabs, tabs_image])
    txt2img_kd_inpaint_both.click(fn=both_to_module_inpaint, inputs=[prompt_txt2img_kd, negative_prompt_txt2img_kd, gs_out_txt2img_kd, sel_out_txt2img_kd, tab_image_num, tab_inpaint_num], outputs=[prompt_inpaint, negative_prompt_inpaint,img_inpaint, gs_img_inpaint, tabs, tabs_image])
    txt2img_kd_controlnet_both.click(fn=both_to_module_inpaint, inputs=[prompt_txt2img_kd, negative_prompt_txt2img_kd, gs_out_txt2img_kd, sel_out_txt2img_kd, tab_image_num, tab_controlnet_num], outputs=[prompt_controlnet, negative_prompt_controlnet, img_source_controlnet, gs_img_source_controlnet, tabs, tabs_image])    

# img2img outputs
    img2img_img2img.click(fn=send_to_module, inputs=[gs_out_img2img, sel_out_img2img, tab_image_num, tab_img2img_num], outputs=[img_img2img, tabs, tabs_image])
    img2var_img2var.click(fn=send_to_module, inputs=[gs_out_img2var, sel_out_img2var, tab_image_num, tab_img2var_num], outputs=[img_img2var, tabs, tabs_image])    
    img2img_pix2pix.click(fn=send_to_module, inputs=[gs_out_img2img, sel_out_img2img, tab_image_num, tab_pix2pix_num], outputs=[img_pix2pix, tabs, tabs_image])
    img2img_inpaint.click(fn=send_to_module_inpaint, inputs=[gs_out_img2img, sel_out_img2img, tab_image_num, tab_inpaint_num], outputs=[img_inpaint, gs_img_inpaint, tabs, tabs_image])
    img2img_controlnet.click(fn=send_to_module_inpaint, inputs=[gs_out_img2img, sel_out_img2img, tab_image_num, tab_controlnet_num], outputs=[img_source_controlnet, gs_img_source_controlnet, tabs, tabs_image])    
    img2img_faceswap.click(fn=send_to_module_inpaint, inputs=[gs_out_img2img, sel_out_img2img, tab_image_num, tab_faceswap_num], outputs=[img_target_faceswap, gs_img_target_faceswap, tabs, tabs_image])    
    img2img_resrgan.click(fn=send_to_module, inputs=[gs_out_img2img, sel_out_img2img, tab_image_num, tab_resrgan_num], outputs=[img_resrgan, tabs, tabs_image])
    img2img_gfpgan.click(fn=send_to_module, inputs=[gs_out_img2img, sel_out_img2img, tab_image_num, tab_gfpgan_num], outputs=[img_gfpgan, tabs, tabs_image])
    img2img_img2txt_git.click(fn=send_to_module_text, inputs=[gs_out_img2img, sel_out_img2img, tab_text_num, tab_img2txt_git_num], outputs=[img_img2txt_git, tabs, tabs_text])    

# img2img inputs
    img2img_txt2img_sd_input.click(fn=import_to_module, inputs=[prompt_img2img, negative_prompt_img2img, tab_image_num, tab_txt2img_sd_num], outputs=[prompt_txt2img_sd, negative_prompt_txt2img_sd, tabs, tabs_image])
    img2img_txt2img_kd_input.click(fn=import_to_module, inputs=[prompt_img2img, negative_prompt_img2img, tab_image_num, tab_txt2img_kd_num], outputs=[prompt_txt2img_kd, negative_prompt_txt2img_kd, tabs, tabs_image])    
    img2img_pix2pix_input.click(fn=import_to_module, inputs=[prompt_img2img, negative_prompt_img2img, tab_image_num, tab_pix2pix_num], outputs=[prompt_pix2pix, negative_prompt_pix2pix, tabs, tabs_image])
    img2img_inpaint_input.click(fn=import_to_module, inputs=[prompt_img2img, negative_prompt_img2img, tab_image_num, tab_inpaint_num], outputs=[prompt_inpaint, negative_prompt_inpaint, tabs, tabs_image])
    img2img_controlnet_input.click(fn=import_to_module, inputs=[prompt_img2img, negative_prompt_img2img, tab_image_num, tab_controlnet_num], outputs=[prompt_controlnet, negative_prompt_controlnet, tabs, tabs_image])    
    
# img2img both
    img2img_pix2pix_both.click(fn=both_to_module, inputs=[prompt_img2img, negative_prompt_img2img, gs_out_img2img, sel_out_img2img, tab_image_num, tab_pix2pix_num], outputs=[prompt_pix2pix, negative_prompt_pix2pix, img_pix2pix, tabs, tabs_image])
    img2img_inpaint_both.click(fn=both_to_module_inpaint, inputs=[prompt_img2img, negative_prompt_img2img, gs_out_img2img, sel_out_img2img, tab_image_num, tab_inpaint_num], outputs=[prompt_inpaint, negative_prompt_inpaint,img_inpaint, gs_img_inpaint, tabs, tabs_image])
    img2img_controlnet_both.click(fn=both_to_module_inpaint, inputs=[prompt_img2img, negative_prompt_img2img, gs_out_img2img, sel_out_img2img, tab_image_num, tab_controlnet_num], outputs=[prompt_controlnet, negative_prompt_controlnet, img_source_controlnet, gs_img_source_controlnet, tabs, tabs_image])    

# img2var outputs
    img2var_img2img.click(fn=send_to_module, inputs=[gs_out_img2var, sel_out_img2var, tab_image_num, tab_img2img_num], outputs=[img_img2img, tabs, tabs_image])
    img2var_img2var.click(fn=send_to_module, inputs=[gs_out_img2var, sel_out_img2var, tab_image_num, tab_img2var_num], outputs=[img_img2var, tabs, tabs_image])    
    img2var_pix2pix.click(fn=send_to_module, inputs=[gs_out_img2var, sel_out_img2var, tab_image_num, tab_pix2pix_num], outputs=[img_pix2pix, tabs, tabs_image])
    img2var_inpaint.click(fn=send_to_module_inpaint, inputs=[gs_out_img2var, sel_out_img2var, tab_image_num, tab_inpaint_num], outputs=[img_inpaint, gs_img_inpaint, tabs, tabs_image])
    img2var_controlnet.click(fn=send_to_module_inpaint, inputs=[gs_out_img2var, sel_out_img2var, tab_image_num, tab_controlnet_num], outputs=[img_source_controlnet, gs_img_source_controlnet, tabs, tabs_image])    
    img2var_faceswap.click(fn=send_to_module_inpaint, inputs=[gs_out_img2var, sel_out_img2var, tab_image_num, tab_faceswap_num], outputs=[img_target_faceswap, gs_img_target_faceswap, tabs, tabs_image])    
    img2var_resrgan.click(fn=send_to_module, inputs=[gs_out_img2var, sel_out_img2var, tab_image_num, tab_resrgan_num], outputs=[img_resrgan, tabs, tabs_image])
    img2var_gfpgan.click(fn=send_to_module, inputs=[gs_out_img2var, sel_out_img2var, tab_image_num, tab_gfpgan_num], outputs=[img_gfpgan, tabs, tabs_image])
    img2var_img2txt_git.click(fn=send_to_module_text, inputs=[gs_out_img2var, sel_out_img2var, tab_text_num, tab_img2txt_git_num], outputs=[img_img2txt_git, tabs, tabs_text])    

# pix2pix outputs
    pix2pix_img2img.click(fn=send_to_module, inputs=[gs_out_pix2pix, sel_out_pix2pix, tab_image_num, tab_img2img_num], outputs=[img_img2img, tabs, tabs_image])
    pix2pix_img2var.click(fn=send_to_module, inputs=[gs_out_pix2pix, sel_out_pix2pix, tab_image_num, tab_img2var_num], outputs=[img_img2var, tabs, tabs_image])    
    pix2pix_pix2pix.click(fn=send_to_module, inputs=[gs_out_pix2pix, sel_out_pix2pix, tab_image_num, tab_pix2pix_num], outputs=[img_pix2pix, tabs, tabs_image])    
    pix2pix_inpaint.click(fn=send_to_module_inpaint, inputs=[gs_out_pix2pix, sel_out_pix2pix, tab_image_num, tab_inpaint_num], outputs=[img_inpaint, gs_img_inpaint, tabs, tabs_image])
    pix2pix_controlnet.click(fn=send_to_module_inpaint, inputs=[gs_out_pix2pix, sel_out_pix2pix, tab_image_num, tab_controlnet_num], outputs=[img_source_controlnet, gs_img_source_controlnet, tabs, tabs_image])        
    pix2pix_faceswap.click(fn=send_to_module_inpaint, inputs=[gs_out_pix2pix, sel_out_pix2pix, tab_image_num, tab_faceswap_num], outputs=[img_target_faceswap, gs_img_target_faceswap, tabs, tabs_image])    
    pix2pix_resrgan.click(fn=send_to_module, inputs=[gs_out_pix2pix, sel_out_pix2pix, tab_image_num, tab_resrgan_num], outputs=[img_resrgan, tabs, tabs_image])
    pix2pix_gfpgan.click(fn=send_to_module, inputs=[gs_out_pix2pix, sel_out_pix2pix, tab_image_num, tab_gfpgan_num], outputs=[img_gfpgan, tabs, tabs_image])
    pix2pix_img2txt_git.click(fn=send_to_module_text, inputs=[gs_out_pix2pix, sel_out_pix2pix, tab_text_num, tab_img2txt_git_num], outputs=[img_img2txt_git, tabs, tabs_text])

# pix2pix inputs
    pix2pix_txt2img_sd_input.click(fn=import_to_module, inputs=[prompt_pix2pix, negative_prompt_pix2pix, tab_image_num, tab_txt2img_sd_num], outputs=[prompt_txt2img_sd, negative_prompt_txt2img_sd, tabs, tabs_image])
    pix2pix_txt2img_kd_input.click(fn=import_to_module, inputs=[prompt_pix2pix, negative_prompt_pix2pix, tab_image_num, tab_txt2img_kd_num], outputs=[prompt_txt2img_kd, negative_prompt_txt2img_kd, tabs, tabs_image])    
    pix2pix_img2img_input.click(fn=import_to_module, inputs=[prompt_pix2pix, negative_prompt_pix2pix, tab_image_num, tab_img2img_num], outputs=[prompt_img2img, negative_prompt_img2img, tabs, tabs_image])
    pix2pix_inpaint_input.click(fn=import_to_module, inputs=[prompt_pix2pix, negative_prompt_pix2pix, tab_image_num, tab_inpaint_num], outputs=[prompt_inpaint, negative_prompt_inpaint, tabs, tabs_image])
    pix2pix_controlnet_input.click(fn=import_to_module, inputs=[prompt_pix2pix, negative_prompt_pix2pix, tab_image_num, tab_controlnet_num], outputs=[prompt_controlnet, negative_prompt_controlnet, tabs, tabs_image])
    pix2pix_vid2vid_ze_input.click(fn=import_to_module_video, inputs=[prompt_pix2pix, negative_prompt_pix2pix, tab_video_num, tab_vid2vid_ze_num], outputs=[prompt_vid2vid_ze, negative_prompt_vid2vid_ze, tabs, tabs_video])
# pix2pix both
    pix2pix_img2img_both.click(fn=both_to_module, inputs=[prompt_pix2pix, negative_prompt_pix2pix, gs_out_pix2pix, sel_out_pix2pix, tab_image_num, tab_img2img_num], outputs=[prompt_img2img, negative_prompt_img2img, img_img2img, tabs, tabs_image])
    pix2pix_inpaint_both.click(fn=both_to_module_inpaint, inputs=[prompt_pix2pix, negative_prompt_pix2pix, gs_out_pix2pix, sel_out_pix2pix, tab_image_num, tab_inpaint_num], outputs=[prompt_inpaint, negative_prompt_inpaint,img_inpaint, gs_img_inpaint, tabs, tabs_image])
    pix2pix_controlnet_both.click(fn=both_to_module_inpaint, inputs=[prompt_pix2pix, negative_prompt_pix2pix, gs_out_pix2pix, sel_out_pix2pix, tab_image_num, tab_controlnet_num], outputs=[prompt_controlnet, negative_prompt_controlnet,img_source_controlnet, gs_img_source_controlnet, tabs, tabs_image])

# inpaint outputs
    inpaint_img2img.click(fn=send_to_module, inputs=[gs_out_inpaint, sel_out_inpaint, tab_image_num, tab_img2img_num], outputs=[img_img2img, tabs, tabs_image])
    inpaint_img2var.click(fn=send_to_module, inputs=[gs_out_inpaint, sel_out_inpaint, tab_image_num, tab_img2var_num], outputs=[img_img2var, tabs, tabs_image])    
    inpaint_pix2pix.click(fn=send_to_module, inputs=[gs_out_inpaint, sel_out_inpaint, tab_image_num, tab_pix2pix_num], outputs=[img_pix2pix, tabs, tabs_image])    
    inpaint_inpaint.click(fn=send_to_module_inpaint, inputs=[gs_out_inpaint, sel_out_inpaint, tab_image_num, tab_inpaint_num], outputs=[img_inpaint, gs_img_inpaint, tabs, tabs_image])    
    inpaint_controlnet.click(fn=send_to_module_inpaint, inputs=[gs_out_inpaint, sel_out_inpaint, tab_image_num, tab_controlnet_num], outputs=[img_source_controlnet, gs_img_source_controlnet, tabs, tabs_image])        
    inpaint_faceswap.click(fn=send_to_module_inpaint, inputs=[gs_out_inpaint, sel_out_inpaint, tab_image_num, tab_faceswap_num], outputs=[img_target_faceswap, gs_img_target_faceswap, tabs, tabs_image])        
    inpaint_resrgan.click(fn=send_to_module, inputs=[gs_out_inpaint, sel_out_inpaint, tab_image_num, tab_resrgan_num], outputs=[img_resrgan, tabs, tabs_image])
    inpaint_gfpgan.click(fn=send_to_module, inputs=[gs_out_inpaint, sel_out_inpaint, tab_image_num, tab_gfpgan_num], outputs=[img_gfpgan, tabs, tabs_image])
    inpaint_img2txt_git.click(fn=send_to_module_text, inputs=[gs_out_inpaint, sel_out_inpaint, tab_text_num, tab_img2txt_git_num], outputs=[img_img2txt_git, tabs, tabs_text])    

# inpaint inputs
    inpaint_txt2img_sd_input.click(fn=import_to_module, inputs=[prompt_inpaint, negative_prompt_inpaint, tab_image_num, tab_txt2img_sd_num], outputs=[prompt_txt2img_sd, negative_prompt_txt2img_sd, tabs, tabs_image])
    inpaint_txt2img_kd_input.click(fn=import_to_module, inputs=[prompt_inpaint, negative_prompt_inpaint, tab_image_num, tab_txt2img_kd_num], outputs=[prompt_txt2img_kd, negative_prompt_txt2img_kd, tabs, tabs_image])    
    inpaint_img2img_input.click(fn=import_to_module, inputs=[prompt_inpaint, negative_prompt_inpaint, tab_image_num, tab_img2img_num], outputs=[prompt_img2img, negative_prompt_img2img, tabs, tabs_image])
    inpaint_pix2pix_input.click(fn=import_to_module, inputs=[prompt_inpaint, negative_prompt_inpaint, tab_image_num, tab_pix2pix_num], outputs=[prompt_pix2pix, negative_prompt_pix2pix, tabs, tabs_image])
    inpaint_controlnet_input.click(fn=import_to_module, inputs=[prompt_inpaint, negative_prompt_inpaint, tab_image_num, tab_controlnet_num], outputs=[prompt_controlnet, negative_prompt_controlnet, tabs, tabs_image])    
    
# inpaint both
    inpaint_img2img_both.click(fn=both_to_module, inputs=[prompt_inpaint, negative_prompt_inpaint, gs_out_inpaint, sel_out_inpaint, tab_image_num, tab_img2img_num], outputs=[prompt_img2img, negative_prompt_img2img, img_img2img, tabs, tabs_image])
    inpaint_pix2pix_both.click(fn=both_to_module, inputs=[prompt_inpaint, negative_prompt_inpaint, gs_out_inpaint, sel_out_inpaint, tab_image_num, tab_pix2pix_num], outputs=[prompt_pix2pix, negative_prompt_pix2pix, img_pix2pix, tabs, tabs_image])
    inpaint_controlnet_both.click(fn=both_to_module_inpaint, inputs=[prompt_inpaint, negative_prompt_inpaint, gs_out_inpaint, sel_out_inpaint, tab_image_num, tab_controlnet_num], outputs=[prompt_controlnet, negative_prompt_controlnet, img_source_controlnet, gs_img_source_controlnet, tabs, tabs_image])

# ControlNet outputs
    controlnet_img2img.click(fn=send_to_module, inputs=[gs_out_controlnet, sel_out_controlnet, tab_image_num, tab_img2img_num], outputs=[img_img2img, tabs, tabs_image])
    controlnet_img2var.click(fn=send_to_module, inputs=[gs_out_controlnet, sel_out_controlnet, tab_image_num, tab_img2var_num], outputs=[img_img2var, tabs, tabs_image])    
    controlnet_pix2pix.click(fn=send_to_module, inputs=[gs_out_controlnet, sel_out_controlnet, tab_image_num, tab_pix2pix_num], outputs=[img_pix2pix, tabs, tabs_image])
    controlnet_inpaint.click(fn=send_to_module_inpaint, inputs=[gs_out_controlnet, sel_out_controlnet, tab_image_num, tab_inpaint_num], outputs=[img_inpaint, gs_img_inpaint, tabs, tabs_image]) 
    controlnet_controlnet.click(fn=send_to_module_inpaint, inputs=[gs_out_controlnet, sel_out_controlnet, tab_image_num, tab_controlnet_num], outputs=[img_source_controlnet, gs_img_source_controlnet, tabs, tabs_image])    
    controlnet_faceswap.click(fn=send_to_module_inpaint, inputs=[gs_out_controlnet, sel_out_controlnet, tab_image_num, tab_faceswap_num], outputs=[img_target_faceswap, gs_img_target_faceswap, tabs, tabs_image])
    controlnet_resrgan.click(fn=send_to_module, inputs=[gs_out_controlnet, sel_out_controlnet, tab_image_num, tab_resrgan_num], outputs=[img_resrgan, tabs, tabs_image])
    controlnet_gfpgan.click(fn=send_to_module, inputs=[gs_out_controlnet, sel_out_controlnet, tab_image_num, tab_gfpgan_num], outputs=[img_gfpgan, tabs, tabs_image])
    controlnet_img2txt_git.click(fn=send_to_module_text, inputs=[gs_out_controlnet, sel_out_controlnet, tab_text_num, tab_img2txt_git_num], outputs=[img_img2txt_git, tabs, tabs_text])

# controlnet inputs
    controlnet_txt2img_sd_input.click(fn=import_to_module, inputs=[prompt_controlnet, negative_prompt_controlnet, tab_image_num, tab_txt2img_sd_num], outputs=[prompt_txt2img_sd, negative_prompt_txt2img_sd, tabs, tabs_image])
    controlnet_txt2img_kd_input.click(fn=import_to_module, inputs=[prompt_controlnet, negative_prompt_controlnet, tab_image_num, tab_txt2img_kd_num], outputs=[prompt_txt2img_kd, negative_prompt_txt2img_kd, tabs, tabs_image])
    controlnet_img2img_input.click(fn=import_to_module, inputs=[prompt_controlnet, negative_prompt_controlnet, tab_image_num, tab_img2img_num], outputs=[prompt_img2img, negative_prompt_img2img, tabs, tabs_image])
    controlnet_pix2pix_input.click(fn=import_to_module, inputs=[prompt_controlnet, negative_prompt_controlnet, tab_image_num, tab_pix2pix_num], outputs=[prompt_pix2pix, negative_prompt_pix2pix, tabs, tabs_image])
    controlnet_inpaint_input.click(fn=import_to_module, inputs=[prompt_controlnet, negative_prompt_controlnet, tab_image_num, tab_inpaint_num], outputs=[prompt_inpaint, negative_prompt_inpaint, tabs, tabs_image])
    controlnet_txt2vid_ms_input.click(fn=import_to_module_video, inputs=[prompt_controlnet, negative_prompt_controlnet, tab_video_num, tab_txt2vid_ms_num], outputs=[prompt_txt2vid_ms, negative_prompt_txt2vid_ms, tabs, tabs_video])
    controlnet_txt2vid_ze_input.click(fn=import_to_module_video, inputs=[prompt_controlnet, negative_prompt_controlnet, tab_video_num, tab_txt2vid_ze_num], outputs=[prompt_txt2vid_ze, negative_prompt_txt2vid_ze, tabs, tabs_video])    

# ControlNet both 
    controlnet_img2img_both.click(fn=both_to_module, inputs=[prompt_controlnet, negative_prompt_controlnet, gs_out_controlnet, sel_out_controlnet, tab_image_num, tab_img2img_num], outputs=[prompt_img2img, negative_prompt_img2img, img_img2img, tabs, tabs_image])
    controlnet_pix2pix_both.click(fn=both_to_module, inputs=[prompt_controlnet, negative_prompt_controlnet, gs_out_controlnet, sel_out_controlnet, tab_image_num, tab_pix2pix_num], outputs=[prompt_pix2pix, negative_prompt_pix2pix, img_pix2pix, tabs, tabs_image])
    controlnet_inpaint_both.click(fn=both_to_module_inpaint, inputs=[prompt_controlnet, negative_prompt_controlnet, gs_out_controlnet, sel_out_controlnet, tab_image_num, tab_inpaint_num], outputs=[prompt_inpaint, negative_prompt_inpaint,img_inpaint, gs_img_inpaint, tabs, tabs_image])

# Faceswap outputs
    faceswap_img2img.click(fn=send_to_module, inputs=[gs_out_faceswap, sel_out_faceswap, tab_image_num, tab_img2img_num], outputs=[img_img2img, tabs, tabs_image])
    faceswap_img2var.click(fn=send_to_module, inputs=[gs_out_faceswap, sel_out_faceswap, tab_image_num, tab_img2var_num], outputs=[img_img2var, tabs, tabs_image])    
    faceswap_pix2pix.click(fn=send_to_module, inputs=[gs_out_faceswap, sel_out_faceswap, tab_image_num, tab_pix2pix_num], outputs=[img_pix2pix, tabs, tabs_image])
    faceswap_inpaint.click(fn=send_to_module_inpaint, inputs=[gs_out_faceswap, sel_out_faceswap, tab_image_num, tab_inpaint_num], outputs=[img_inpaint, gs_img_inpaint, tabs, tabs_image]) 
    faceswap_controlnet.click(fn=send_to_module_inpaint, inputs=[gs_out_faceswap, sel_out_faceswap, tab_image_num, tab_controlnet_num], outputs=[img_source_controlnet,  gs_img_source_controlnet, tabs, tabs_image])     
    faceswap_faceswap.click(fn=send_to_module_inpaint, inputs=[gs_out_faceswap, sel_out_faceswap, tab_image_num, tab_faceswap_num], outputs=[img_target_faceswap, gs_img_target_faceswap, tabs, tabs_image])
    faceswap_resrgan.click(fn=send_to_module, inputs=[gs_out_faceswap, sel_out_faceswap, tab_image_num, tab_resrgan_num], outputs=[img_resrgan, tabs, tabs_image])
    faceswap_gfpgan.click(fn=send_to_module, inputs=[gs_out_faceswap, sel_out_faceswap, tab_image_num, tab_gfpgan_num], outputs=[img_gfpgan, tabs, tabs_image])
    faceswap_img2txt_git.click(fn=send_to_module_text, inputs=[gs_out_faceswap, sel_out_faceswap, tab_text_num, tab_img2txt_git_num], outputs=[img_img2txt_git, tabs, tabs_text])

# resrgan outputs
    resrgan_img2img.click(fn=send_to_module, inputs=[gs_out_resrgan, sel_out_resrgan, tab_image_num, tab_img2img_num], outputs=[img_img2img, tabs, tabs_image])
    resrgan_img2var.click(fn=send_to_module, inputs=[gs_out_resrgan, sel_out_resrgan, tab_image_num, tab_img2var_num], outputs=[img_img2var, tabs, tabs_image])    
    resrgan_pix2pix.click(fn=send_to_module, inputs=[gs_out_resrgan, sel_out_resrgan, tab_image_num, tab_pix2pix_num], outputs=[img_pix2pix, tabs, tabs_image])
    resrgan_inpaint.click(fn=send_to_module_inpaint, inputs=[gs_out_resrgan, sel_out_resrgan, tab_image_num, tab_inpaint_num], outputs=[img_inpaint, gs_img_inpaint, tabs, tabs_image])    
    resrgan_controlnet.click(fn=send_to_module, inputs=[gs_out_resrgan, sel_out_resrgan, tab_image_num, tab_controlnet_num], outputs=[img_source_controlnet, gs_img_source_controlnet, tabs, tabs_image])            
    resrgan_faceswap.click(fn=send_to_module_inpaint, inputs=[gs_out_resrgan, sel_out_resrgan, tab_faceswap_num, tab_inpaint_num], outputs=[img_target_faceswap, gs_img_target_faceswap, tabs, tabs_image])       
    resrgan_gfpgan.click(fn=send_to_module, inputs=[gs_out_resrgan, sel_out_resrgan, tab_image_num, tab_gfpgan_num], outputs=[img_gfpgan, tabs, tabs_image])
    resrgan_img2txt_git.click(fn=send_to_module_text, inputs=[gs_out_resrgan, sel_out_resrgan, tab_text_num, tab_img2txt_git_num], outputs=[img_img2txt_git, tabs, tabs_text])        

# gfpgan outputs
    gfpgan_img2img.click(fn=send_to_module, inputs=[gs_out_gfpgan, sel_out_gfpgan, tab_image_num, tab_img2img_num], outputs=[img_img2img, tabs, tabs_image])
    gfpgan_img2var.click(fn=send_to_module, inputs=[gs_out_gfpgan, sel_out_gfpgan, tab_image_num, tab_img2var_num], outputs=[img_img2var, tabs, tabs_image])    
    gfpgan_pix2pix.click(fn=send_to_module, inputs=[gs_out_gfpgan, sel_out_gfpgan, tab_image_num, tab_pix2pix_num], outputs=[img_pix2pix, tabs, tabs_image])
    gfpgan_inpaint.click(fn=send_to_module_inpaint, inputs=[gs_out_gfpgan, sel_out_gfpgan, tab_image_num, tab_inpaint_num], outputs=[img_inpaint, gs_img_inpaint, gs_img_source_controlnet, tabs, tabs_image])  
    gfpgan_controlnet.click(fn=send_to_module, inputs=[gs_out_gfpgan, sel_out_gfpgan, tab_image_num, tab_controlnet_num], outputs=[img_source_controlnet, tabs, tabs_image])                  
    gfpgan_faceswap.click(fn=send_to_module_inpaint, inputs=[gs_out_gfpgan, sel_out_gfpgan, tab_faceswap_num, tab_inpaint_num], outputs=[img_target_faceswap, gs_img_target_faceswap, tabs, tabs_image])    
    gfpgan_resrgan.click(fn=send_to_module, inputs=[gs_out_gfpgan, sel_out_gfpgan, tab_image_num, tab_resrgan_num], outputs=[img_resrgan, tabs, tabs_image])
    gfpgan_img2txt_git.click(fn=send_to_module_text, inputs=[gs_out_gfpgan, sel_out_gfpgan, tab_text_num, tab_img2txt_git_num], outputs=[img_img2txt_git, tabs, tabs_text])

# Musicgen inputs
    musicgen_audiogen_input.click(fn=import_to_module_audio, inputs=[prompt_musicgen, tab_audio_num, tab_audiogen_num], outputs=[prompt_audiogen, tabs, tabs_audio])
    
# Audiogen inputs    
    audiogen_musicgen_input.click(fn=import_to_module_audio, inputs=[prompt_audiogen, tab_audio_num, tab_musicgen_num], outputs=[prompt_musicgen, tabs, tabs_audio])

# Bark inputs
    bark_whisper.click(fn=send_audio_to_module_text, inputs=[out_bark, tab_text_num, tab_whisper_num], outputs=[source_audio_whisper, tabs, tabs_text])

# Modelscope outputs
    txt2vid_ms_vid2vid_ze.click(fn=send_to_module_video, inputs=[out_txt2vid_ms, tab_video_num, tab_vid2vid_ze_num], outputs=[vid_vid2vid_ze, tabs, tabs_video])

# Modelscope inputs    
    txt2vid_ms_txt2vid_ze_input.click(fn=import_to_module_video, inputs=[prompt_txt2vid_ms, negative_prompt_txt2vid_ms, tab_video_num, tab_txt2vid_ze_num], outputs=[prompt_txt2vid_ze, negative_prompt_txt2vid_ze, tabs, tabs_video])
    txt2vid_ms_txt2img_sd_input.click(fn=import_to_module_video, inputs=[prompt_txt2vid_ms, negative_prompt_txt2vid_ms, tab_image_num, tab_txt2img_sd_num], outputs=[prompt_txt2img_sd, negative_prompt_txt2img_sd, tabs, tabs_image])
    txt2vid_ms_txt2img_kd_input.click(fn=import_to_module_video, inputs=[prompt_txt2vid_ms, negative_prompt_txt2vid_ms, tab_image_num, tab_txt2img_kd_num], outputs=[prompt_txt2img_kd, negative_prompt_txt2img_kd, tabs, tabs_image])

# Text2Video-Zero outputs
    txt2vid_ze_vid2vid_ze.click(fn=send_to_module_video, inputs=[out_txt2vid_ze, tab_video_num, tab_vid2vid_ze_num], outputs=[vid_vid2vid_ze, tabs, tabs_video])

# Text2Video-Zero inputs    
    txt2vid_ze_txt2vid_ms_input.click(fn=import_to_module_video, inputs=[prompt_txt2vid_ze, negative_prompt_txt2vid_ze, tab_video_num, tab_txt2vid_ms_num], outputs=[prompt_txt2vid_ms, negative_prompt_txt2vid_ms, tabs, tabs_video])
    txt2vid_ze_txt2img_sd_input.click(fn=import_to_module_video, inputs=[prompt_txt2vid_ze, negative_prompt_txt2vid_ze, tab_image_num, tab_txt2img_sd_num], outputs=[prompt_txt2img_sd, negative_prompt_txt2img_sd, tabs, tabs_image])    
    txt2vid_ze_txt2img_kd_input.click(fn=import_to_module_video, inputs=[prompt_txt2vid_ze, negative_prompt_txt2vid_ze, tab_image_num, tab_txt2img_kd_num], outputs=[prompt_txt2img_kd, negative_prompt_txt2img_kd, tabs, tabs_image])

# Video Instruct-pix2pix inputs
    vid2vid_ze_pix2pix.click(fn=import_to_module, inputs=[prompt_vid2vid_ze, negative_prompt_vid2vid_ze, tab_image_num, tab_pix2pix_num], outputs=[prompt_pix2pix, negative_prompt_pix2pix, tabs, tabs_image]) 

# Exécution de l'UI :
    demo.load(split_url_params, nsfw_filter, nsfw_filter, _js=get_window_url_params)
if __name__ == "__main__":
    demo.queue(concurrency_count=8).launch(server_name="0.0.0.0", server_port=7860, ssl_certfile="./ssl/cert.pem", favicon_path="./images/biniou_64.ico", ssl_keyfile="./ssl/key.pem", ssl_verify=False)

# Fin du fichier

webui.py
Woolverine94/biniou