File size: 4,630 Bytes
ccec886
eb1e067
afc2013
86775a6
eb1e067
ee56298
afc2013
86775a6
 
 
 
0847be2
86775a6
 
eb1e067
86775a6
eb1e067
 
f15034b
 
86775a6
f15034b
 
 
eb1e067
 
91bbd67
1666a8e
 
 
 
 
 
 
 
 
 
 
 
 
eb1e067
1666a8e
 
 
 
 
 
 
ea0fbdd
eb1e067
 
 
 
 
 
 
 
 
 
 
91bbd67
ea0fbdd
eb1e067
 
 
 
 
 
 
9d64fb0
91bbd67
afc2013
eb1e067
096fcbf
eb1e067
096fcbf
eb1e067
ee5e394
096fcbf
ee5e394
 
096fcbf
 
ee5e394
096fcbf
ee5e394
 
 
f9110f2
ee5e394
096fcbf
ee5e394
51c10b3
eb1e067
0937f1b
add3705
 
eb1e067
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3110228
e464d27
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import gradio as gr
import os

from huggingface_hub import HfApi, ModelFilter, list_liked_repos, SpaceHardware
from diffusers import DiffusionPipeline
import torch

def gpu_enabled() -> bool:
    # If cloned, fill in SPACE_ID with your own space
    SPACE_ID = "aroffe/comparing-diffusion-models"
    runtime = api.get_space_runtime(repo_id=SPACE_ID)
    return (runtime.hardware != SpaceHardware.CPU_BASIC and runtime.hardware != SpaceHardware.CPU_UPGRADE)

    
def image_mod(prompt: str, model: str, image_0: gr.Image, image_1: gr.Image) -> list[gr.Image]:
    gpu_enabled()
    images = [image_0, image_1]
    for i, diffusion_model in enumerate(model):
        pipeline = DiffusionPipeline.from_pretrained(
            pretrained_model_name_or_path=diffusion_model,
            torch_dtype=torch.float16 if gpu_enabled() else torch.float32,
            use_safetensors=True,
            device_map="auto"
        )
        images[i] = pipeline(prompt).images[0]
    return images


def get_text_to_image_models():
    api = HfApi()

    # Out app utilizers Diffusers and Safetensors
    model_filter = ModelFilter(
        library=["diffusers" , "safetensors"],
        tags=["text-to-image"]
    )

    return api.list_models(filter=model_filter)


def get_liked_models(username: str) -> list:
    user_liked_models = list_liked_repos(username)
    text_to_image_models = get_text_to_image_models()
    text_to_image_models_ids = [model.id for model in list(text_to_image_models)]

    # find intersection between user liked models and models pulled from huggingface
    user_liked_text_to_image_models = [i for i in user_liked_models.models if i in text_to_image_models_ids]
    return user_liked_text_to_image_models


def get_dropdown_for_huggingface(profile: gr.OAuthProfile):
    return gr.Dropdown(
        choices=get_liked_models(profile.username),
        multiselect=True,
        max_choices=2,
        label="Select a Model from Hugginface",
        info="Select up to two models to compare. If you want to see more models populated in the dropdown, just like a model on huggingface! The dropdown is refreshed every minute.",
        every=60,
        visible=True
    )


def change_model(choice): 
    images = [gr.Image(visible=False), gr.Image(visible=False)]
    for i, model in enumerate(choice):
        images[i] = gr.Image(label=model, visible=True, interactive=False)
    
    return images


with gr.Blocks() as demo:
    gr.Markdown(
    """
    # Comparing Stable Diffusion Models

    ## Motivation

    Much of the motivation of this project was to compare open-source text-to-image models (I'd like to expand this beyond open-source as well).
    In the process of building this, I thought of the use case where a developer is developing there own model -- or maybe fine-tuning an existing one -- and wants to compare the output of said model against a published one. A side-by-side tool can be adapted to that!

    I see this space evolving in the future to include a comprehensive list of popular text-to-image model, saving the user from going into Hugging Face and liking models to see them in this app. (Anything but providing models by free text was my goal).
    
    I'd love to hear your feedback!

    ## How to use this app

    This app uses a [DiffusionPipeline](https://huggingface.co/docs/diffusers/main/en/api/diffusion_pipeline) to generate images.

    In the dropdown you will see models that you have liked on HuggingFace. Only models that you've likes and fit the criteria for this app will display in the dropdown. You can navigate [here](https://huggingface.co/models?pipeline_tag=text-to-image&library=diffusers,safetensors&sort=trending) to get models that will work with this app. Go to the model card and like it; then upon refreshing this app you will see this model in the dropdown.

    ## Speed up Inference
    
    For faster inference, you can duplicate the space and upgrade the hardware resources!
    """)
    # Make the buttons the same size for aesthetics
    gr.DuplicateButton()
    gr.LoginButton()
    dropdown = gr.Dropdown(visible=False)
    demo.load(get_dropdown_for_huggingface, inputs=None, outputs=dropdown)
    
    textbox = gr.Textbox(label="Prompt for Image Generation")
    
    with gr.Row() as row:
        image_0 = gr.Image(visible=False)
        image_1 = gr.Image(visible=False)
        dropdown.change(
            fn=change_model,
            inputs=dropdown,
            outputs=[image_0, image_1]
        )
    
    textbox.submit(
        image_mod, [textbox, dropdown, image_0, image_1], [image_0, image_1]
    )

demo.launch()