File size: 5,999 Bytes
807cf27
 
 
 
 
 
 
 
44a24ae
 
807cf27
5b48c5b
a377ac3
 
 
 
 
807cf27
 
5b48c5b
807cf27
5b48c5b
a75c8c5
 
807cf27
 
 
 
 
 
 
 
 
 
 
69097d1
807cf27
 
 
 
 
 
 
 
 
 
 
 
 
5b48c5b
 
 
 
 
 
 
 
 
 
807cf27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a24347
807cf27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50fe9fa
807cf27
 
 
 
fdf2d4e
 
807cf27
 
 
 
 
 
 
 
 
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import gradio as gr
import requests
import json
import PIL.Image
from io import BytesIO
import os
import random
import datetime
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

def generate_image(prompt, negative_prompt, use_negative_embedding, scheduler, steps, width, height, cfg, restore_faces, seed):
    forbidden_words = os.getenv("FORBIDDEN_WORDS").split(", ")
    # Check if the prompt contains any of the forbidden words
    for word in forbidden_words:
        if word in prompt:
            raise Exception(f"The prompt contains a forbidden word: {word}")
    request_time = datetime.datetime.now()
    restore_faces = bool(restore_faces)
    use_negative_embedding = bool(use_negative_embedding)
    print(f"restore_faces: {restore_faces}, type: {type(restore_faces)}")
    print(f"use_negative_embedding: {use_negative_embedding}, type: {type(use_negative_embedding)}")
    if use_negative_embedding:
        negative_prompt += ", easynegative, ng_deepnegative_v1_75t"
    # Define the API endpoint
    apiUrl = os.getenv("API_URL")
    # Define the request headers
    headers = {
        "Content-Type": "application/json",
        "token": os.getenv("API_TOKEN")
    }

    # Define the request body
    body = {
        "mode": "url",
        "model": "LiberteRedmond.ckpt",
        "tiling": False,
        "batch_size": 1,
        "prompt": prompt,
        "negative_prompt": negative_prompt,
        "seed":random.randint(0, 999999999),
        "scheduler": scheduler,
        "n_iter": 1,
        "steps": steps,
        "cfg": cfg,
        "offset_noise": 0.0,
        "width": width,
        "height": height,
        "clip_skip": 1,
        "embeddings": [
            {
                "name": "easynegative",
                "strength": 1.0 if use_negative_embedding else 0
            },
            {
                "name": "ng_deepnegative_v1_75t",
                "strength": 1.0 if use_negative_embedding else 0
            }
        ],
        "vae": "vae-ft-mse-840000-ema-pruned.ckpt",
        "restore_faces": restore_faces,
        "fr_model": "CodeFormer",
        "codeformer_weight": 0.5,
        "enable_hr": False,
        "denoising_strength": 0.75,
        "hr_scale": 2,
        "hr_upscale": "None",
        "img2img_ref_img_type": "piece",
        "img2img_resize_mode": 0,
        "img2img_denoising_strength": 0.75,
    }

    # Send the request
    response = requests.post(apiUrl, headers=headers, data=json.dumps(body), verify=False)
    # Print the response body if the status code is not 200
    if response.status_code != 200:
        print(response.text)

    # Check the response status
    if response.status_code == 200:
        
       # Get the image URL from the response
       response_json = response.json()
       if 'results' in response_json and isinstance(response_json['results'], list) and len(response_json['results']) > 0:
           image_url = response_json['results'][0]

           # Get the image from the URL           
           image_response = requests.get(image_url)
           image = PIL.Image.open(BytesIO(image_response.content))

           # Log the information together
           print(f"Request time: {request_time}\n"
                  f"Prompt: {prompt}\n"
                  f"Negative Prompt: {negative_prompt}\n"
                  f"Seed: {seed}\n"
                  f"Res(width x height): {width} x {height}\n"
                  f"Image URL: {image_url}")

           return image
       else:
           raise Exception("Unexpected API response format")
    else:
        raise Exception("API request failed with status code " + str(response.status_code))

# Define the Gradio interface
iface = gr.Interface(
    fn=generate_image, 
    inputs=[
        gr.components.Textbox(label="Prompt"),
        gr.components.Textbox(value="ugly, tiling, poorly drawn hands, poorly drawn feet, poorly drawn face, out of frame, extra limbs, disfigured, deformed, body out of frame, blurry, bad anatomy, blurred, watermark, grainy, signature, cut off, draft", label="Negative Prompt"),
        gr.inputs.Checkbox(label="Use Negative Embedding", default=True),
        gr.components.Dropdown(choices=[
            "Euler a",
            "Euler",
            "LMS",
            "Heun",
            "DPM2",
            "DPM2 a",
            "DPM++ 2S a",
            "DPM++ 2M",
            "DPM++ SDE",
            "DPM fast",
            "DPM adaptive",
            "LMS Karras",
            "DPM2 Karras",
            "DPM2 a Karras",
            "DPM++ 2S a Karras",
            "DPM++ 2M Karras",
            "DPM++ SDE Karras",
            "DDIM",
            "PLMS"
        ], label="Scheduler", value="DPM++ SDE Karras"),
        gr.components.Slider(minimum=10, maximum=100, step=1.0,value=30, label="Steps"),
        gr.components.Slider(minimum=512, maximum=1600, value=512, label="Width"),
        gr.components.Slider(minimum=512, maximum=1600, value=512, label="Height"),
        gr.components.Slider(minimum=4, maximum=12, step=0.5, value=7.0, label="CFG"),
        gr.inputs.Checkbox(label="Restore Faces", default=False),
    ], 
    outputs=gr.components.Image(),
    title="Liberte.Redmond Demonstration",
    description = """
## Finetuned model of SD 1.5 produced by [@artificialguybr](https://twitter.com/artificialguybr).
## Resources
- The weights were released [here](https://civitai.com/models/94123/liberte-generalist-model) with example prompts in CIVITAI and [here in HF](https://huggingface.co/artificialguybr/liberte).
## Demonstration
This demonstration is running on the [makeai.run API](https://www.makeai.run/).
## Acknowledgements
Thanks to [Redmond.ai](https://redmond.ai/) for providing GPU Time and sponsoring this model.

## Test my 2.1 Finetuned Model (Freedom) [here](https://huggingface.co/spaces/artificialguybr/freedom).
""",
    allow_flagging='never'
)

#Adding queue
iface.queue(concurrency_count=12)

# Launch the app
iface.launch()