Team Ai
Apppublic

FallnAI/DiffusersUI

sourceHugging Faceapache-2.0updated 2y agoView on Hugging Face
2likes
app.py168 linesDownload Raw Back to root
1import streamlit as st2from PIL import Image3from diffusers import StableDiffusionPipeline, ControlNetModel, DDIMScheduler, LMSDiscreteScheduler, UNet2DConditionModel, DiffusionPipeline4from diffusers import DDPMScheduler, DDPMSchedulerV2, PNDMScheduler5from transformers import AutoModelForCausalLM, AutoTokenizer, AutoModelForSeq2SeqLM, LlamaTokenizerFast, LlamaForCausalLM6from accelerate import Accelerator7import torch8from peft import PeftModel, LoraConfig, get_peft_model, prepare_model_for_int8_training, prepare_model_for_int8_bf16_training9 10# Define a dictionary with all available models, schedulers, features, weights, and adapters11model_dict = {12    "Stable Diffusion": {13        "Models": [14            "stabilityai/stable-diffusion-3-medium"15            "CompVis/stable-diffusion-v1-4",16            "stabilityai/stable-diffusion-2-1",17            "runwayml/stable-diffusion-v1-5",18            "runwayml/stable-diffusion-inpainting",19            "runwayml/stable-diffusion-video-v1-5",20            "stabilityai/stable-diffusion-2-base"21        ],22        "Schedulers": [23            "DDIMScheduler",24            "LMSDiscreteScheduler"25        ],26        "Features": [27            "Unconditional image generation",28            "Text-to-image",29            "Image-to-image",30            "Inpainting",31            "Text or image-to-video",32            "Depth-to-image"33        ],34        "Adapters": [35            "ControlNet",36            "T2I-Adapter"37        ],38        "Weights": [39            "Stable Diffusion XL",40            "SDXL Turbo",41            "Kandinsky",42            "IP-Adapter",43            "ControlNet",44            "Latent Consistency Model",45            "Textual inversion",46            "Shap-E",47            "DiffEdit",48            "Trajectory Consistency Distillation-LoRA",49            "Stable Video Diffusion",50            "Marigold Computer Vision"51        ]52    },53    "Llama": {54        "Models": [55            "decapoda-research/llama-7b-hf",56            "decapoda-research/llama-13b-hf",57            "decapoda-research/llama-30b-hf",58            "decapoda-research/llama-65b-hf"59        ],60        "Tokenizers": [61            "LlamaTokenizerFast"62        ],63        "Features": [64            "AutoPipeline",65            "Train a diffusion model",66            "Load LoRAs for inference",67            "Accelerate inference of text-to-image diffusion models",68            "LOAD PIPELINES AND ADAPTERS",69            "Load community pipelines and components",70            "Load schedulers and models",71            "Model files and layouts",72            "Load adapters",73            "Push files to the Hub",74            "GENERATIVE TASKS",75            "Unconditional image generation",76            "Text-to-image",77            "Image-to-image",78            "Inpainting",79            "Text or image-to-video",80            "Depth-to-image",81            "INFERENCE TECHNIQUES",82            "Overview",83            "Distributed inference with multiple GPUs",84            "Merge LoRAs",85            "Scheduler features",86            "Pipeline callbacks",87            "Reproducible pipelines",88            "Controlling image quality",89            "Prompt techniques",90            "ADVANCED INFERENCE",91            "Outpainting",92            "SPECIFIC PIPELINE EXAMPLES",93            "Stable Diffusion XL",94            "SDXL Turbo",95            "Kandinsky",96            "IP-Adapter",97            "ControlNet",98            "T2I-Adapter",99            "Latent Consistency Model",100            "Textual inversion",101            "Shap-E",102            "DiffEdit",103            "Trajectory Consistency Distillation-LoRA",104            "Stable Video Diffusion",105            "Marigold Computer Vision"106        ],107        "Weights": [108            "LoRA weights"109        ]110    }111}112 113model_type = st.selectbox("Select a model type:", list(model_dict.keys()))114 115if model_type == "Stable Diffusion":116    model = st.selectbox("Select a Stable Diffusion model:", model_dict[model_type]["Models"])117    scheduler = st.selectbox("Select a scheduler:", model_dict[model_type]["Schedulers"])118    feature = st.selectbox("Select a feature:", model_dict[model_type]["Features"])119    adapter = st.selectbox("Select an adapter:", model_dict[model_type]["Adapters"])120    weight = st.selectbox("Select a weight:", model_dict[model_type]["Weights"])121 122    if st.button("Generate Images"):123        st.write("Generating images...")124 125        pipe = StableDiffusionPipeline.from_pretrained(model)126        pipe.scheduler = eval(scheduler)()127 128        if adapter == "ControlNet":129            controlnet = ControlNetModel.from_pretrained("lllyasviel/control_v11e_sd15_openpose")130            pipe = pipe.to_controlnet(controlnet)131 132        # Define the prompt and number of images to generate133        prompt = st.text_input("Enter a prompt:")134        num_images = st.slider("Number of images to generate", min_value=1, max_value=10, value=1)135 136        # Generate the images137        images = pipe(prompt, num_images=num_images, guidance_scale=7.5).images138 139        # Display the generated images140        cols = st.columns(num_images)141        for i, image in enumerate(images):142            cols[i].image(image, caption=f"Image {i+1}", use_column_width=True)143 144if model_type == "Llama":145    model = st.selectbox("Select a Llama model:", model_dict[model_type]["Models"])146    tokenizer = st.selectbox("Select a tokenizer:", model_dict[model_type]["Tokenizers"])147    feature = st.selectbox("Select a feature:", model_dict[model_type]["Features"])148    weight = st.selectbox("Select a weight:", model_dict[model_type]["Weights"])149 150    if st.button("Generate Text"):151        st.write("Generating text...")152 153        tokenizer = AutoTokenizer.from_pretrained(tokenizer)154        model = AutoModelForCausalLM.from_pretrained(model)155 156        input_text = st.text_area("Enter a prompt:")157 158        # Tokenize the input text159        inputs = tokenizer(input_text, return_tensors="pt")160 161        # Generate the text162        output = model.generate(**inputs)163 164        # Decode the generated text165        generated_text = tokenizer.decode(output[0], skip_special_tokens=True)166 167        st.write("Generated Text:")168        st.write(generated_text)