CoolFace
Apppublic

openskyml/HuggingDiffusion

sourceHugging Facemitupdated 3y agoView on Hugging Face
4likes
app.py222 linesDownload Raw Back to root
1import gradio as gr2import requests3import io4import random5import os6from PIL import Image7 8list_models = [9    "SD-1.5",10    "SDXL-1.0",11    "OpenJourney-V4",12    "Anything-V4",13    "Disney-Pixar-Cartoon",14    "Pixel-Art-XL",15]16 17def generate_txt2img(current_model, prompt, is_negative=False, image_style="None style", steps=50, cfg_scale=7,18                     seed=None):19 20    if current_model == "SD-1.5":21        API_URL = "https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5"22    elif current_model == "SDXL-1.0":23        API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"24    elif current_model == "OpenJourney-V4":25        API_URL = "https://api-inference.huggingface.co/models/prompthero/openjourney"26    elif current_model == "Anything-V4":27        API_URL = "https://api-inference.huggingface.co/models/xyn-ai/anything-v4.0" 28    elif current_model == "Disney-Pixar-Cartoon":29        API_URL = "https://api-inference.huggingface.co/models/stablediffusionapi/disney-pixar-cartoon"30    elif current_model == "Pixel-Art-XL":31        API_URL = "https://api-inference.huggingface.co/models/nerijs/pixel-art-xl"32 33 34    API_TOKEN = os.environ.get("HF_READ_TOKEN")35    headers = {"Authorization": f"Bearer {API_TOKEN}"}36 37 38    if image_style == "None style":39        payload = {40            "inputs": prompt + ", 8k",41            "is_negative": is_negative,42            "steps": steps,43            "cfg_scale": cfg_scale,44            "seed": seed if seed is not None else random.randint(-1, 2147483647)45        }46    elif image_style == "Cinematic":47        payload = {48            "inputs": prompt + ", realistic, detailed, textured, skin, hair, eyes, by Alex Huguet, Mike Hill, Ian Spriggs, JaeCheol Park, Marek Denko",49            "is_negative": is_negative + ", abstract, cartoon, stylized",50            "steps": steps,51            "cfg_scale": cfg_scale,52            "seed": seed if seed is not None else random.randint(-1, 2147483647)53        }54    elif image_style == "Digital Art":55        payload = {56            "inputs": prompt + ", faded , vintage , nostalgic , by Jose Villa , Elizabeth Messina , Ryan Brenizer , Jonas Peterson , Jasmine Star",57            "is_negative": is_negative + ", sharp , modern , bright",58            "steps": steps,59            "cfg_scale": cfg_scale,60            "seed": seed if seed is not None else random.randint(-1, 2147483647)61        }62    elif image_style == "Portrait":63        payload = {64            "inputs": prompt + ", soft light, sharp, exposure blend, medium shot, bokeh, (hdr:1.4), high contrast, (cinematic, teal and orange:0.85), (muted colors, dim colors, soothing tones:1.3), low saturation, (hyperdetailed:1.2), (noir:0.4), (natural skin texture, hyperrealism, soft light, sharp:1.2)",65            "is_negative": is_negative,66            "steps": steps,67            "cfg_scale": cfg_scale,68            "seed": seed if seed is not None else random.randint(-1, 2147483647)69        }70 71    image_bytes = requests.post(API_URL, headers=headers, json=payload).content72    image = Image.open(io.BytesIO(image_bytes))73    return image74 75 76css = """77        .gradio-container {78            font-family: 'IBM Plex Sans', sans-serif;79        }80        .gr-button {81            color: white;82            border-color: black;83            background: black;84        }85        input[type='range'] {86            accent-color: black;87        }88        .dark input[type='range'] {89            accent-color: #dfdfdf;90        }91        .gradio-container {92            max-width: 730px !important;93            margin: auto;94            padding-top: 1.5rem;95        }96        #gallery {97            min-height: 22rem;98            margin-bottom: 15px;99            margin-left: auto;100            margin-right: auto;101            border-bottom-right-radius: .5rem !important;102            border-bottom-left-radius: .5rem !important;103        }104        #gallery>div>.h-full {105            min-height: 20rem;106        }107        .details:hover {108            text-decoration: underline;109        }110        .gr-button {111            white-space: nowrap;112        }113        .gr-button:focus {114            border-color: rgb(147 197 253 / var(--tw-border-opacity));115            outline: none;116            box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);117            --tw-border-opacity: 1;118            --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);119            --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);120            --tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));121            --tw-ring-opacity: .5;122        }123        #advanced-btn {124            font-size: .7rem !important;125            line-height: 19px;126            margin-top: 12px;127            margin-bottom: 12px;128            padding: 2px 8px;129            border-radius: 14px !important;130        }131        #advanced-options {132            display: none;133            margin-bottom: 20px;134        }135        .footer {136            margin-bottom: 45px;137            margin-top: 35px;138            text-align: center;139            border-bottom: 1px solid #e5e5e5;140        }141        .footer>p {142            font-size: .8rem;143            display: inline-block;144            padding: 0 10px;145            transform: translateY(10px);146            background: white;147        }148        .dark .footer {149            border-color: #303030;150        }151        .dark .footer>p {152            background: #0b0f19;153        }154        .acknowledgments h4{155            margin: 1.25em 0 .25em 0;156            font-weight: bold;157            font-size: 115%;158        }159        .animate-spin {160            animation: spin 1s linear infinite;161        }162        @keyframes spin {163            from {164                transform: rotate(0deg);165            }166            to {167                transform: rotate(360deg);168            }169        }170        #share-btn-container {padding-left: 0.5rem !important; padding-right: 0.5rem !important; background-color: #000000; justify-content: center; align-items: center; border-radius: 9999px !important; max-width: 13rem; margin-left: auto;}171        div#share-btn-container > div {flex-direction: row;background: black;align-items: center}172        #share-btn-container:hover {background-color: #060606}173        #share-btn {all: initial; color: #ffffff;font-weight: 600; cursor:pointer; font-family: 'IBM Plex Sans', sans-serif; margin-left: 0.5rem !important; padding-top: 0.5rem !important; padding-bottom: 0.5rem !important;right:0;}174        #share-btn * {all: unset}175        #share-btn-container div:nth-child(-n+2){width: auto !important;min-height: 0px !important;}176        #share-btn-container .wrap {display: none !important}177        #share-btn-container.hidden {display: none!important}178        .gr-form{179            flex: 1 1 50%; border-top-right-radius: 0; border-bottom-right-radius: 0;180        }181        #prompt-container{182            gap: 0;183        }184        #prompt-container .form{185        border-top-right-radius: 0;186        border-bottom-right-radius: 0;187        }188        #gen-button{189        border-top-left-radius:0;190        border-bottom-left-radius:0;191        }192        #prompt-text-input, #negative-prompt-text-input{padding: .45rem 0.625rem}193        #component-16{border-top-width: 1px!important;margin-top: 1em}194        .image_duplication{position: absolute; width: 100px; left: 50px}195        .tabitem{border: 0 !important}196"""197 198with gr.Blocks(css=css) as demo:199    200    favicon = '<img src="https://bit.ly/49fkpJc" width="48px" style="display: inline">'201    gr.Markdown(202        f"""<h1><center>{favicon} HuggingDiffusion</center></h1>203            """204    )205    206    with gr.Row(elem_id="prompt-container"):207        current_model = gr.Dropdown(label="Current Model", choices=list_models, value=list_models[1])208        209    with gr.Row(elem_id="prompt-container"):210        text_prompt = gr.Textbox(label="Prompt", placeholder="a cute cat", lines=1, elem_id="prompt-text-input")211        text_button = gr.Button("Generate", variant='primary', elem_id="gen-button")212        213    with gr.Row():214        image_output = gr.Image(type="pil", label="Output Image", elem_id="gallery")215        216    with gr.Accordion("Advanced settings", open=False):217        negative_prompt = gr.Textbox(label="Negative Prompt", value="text, blurry, fuzziness", lines=1, elem_id="negative-prompt-text-input")218        image_style = gr.Dropdown(label="Style", choices=["None style", "Cinematic", "Digital Art", "Portrait"], value="None style", allow_custom_value=False)219 220    text_button.click(generate_txt2img, inputs=[current_model, text_prompt, negative_prompt, image_style], outputs=image_output)221 222demo.launch(show_api=False)