File size: 7,949 Bytes
3cbf1cd
 
 
 
2786a41
 
 
 
 
 
 
 
 
 
c58c4f8
 
 
2786a41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aaf6d52
2786a41
2c96486
2786a41
 
 
c58c4f8
2786a41
c58c4f8
2786a41
 
 
 
 
 
 
 
 
 
94c9ba6
 
 
 
 
910f25b
 
 
 
e1d53b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94c9ba6
2786a41
94c9ba6
5533ca9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
910f25b
5533ca9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
714b860
5533ca9
 
 
 
94c9ba6
2786a41
e941316
e1d53b4
4374e49
3e8991a
e1d53b4
4374e49
 
 
e1d53b4
4374e49
 
 
 
 
 
 
2786a41
 
 
3e8991a
2786a41
 
 
 
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# This space used model: stabilityai/stable-diffusion-xl-base-1.0
# and model: stabilityai/stable-diffusion-xl-refiner-1.0


import numpy as np
import gradio as gr
import requests
import time
import json
import base64
import os
from PIL import Image
from io import BytesIO

batch_size=1
batch_count=1

class Prodia:
    def __init__(self, api_key, base=None):
        self.base = base or "https://api.prodia.com/v1"
        self.headers = {
            "X-Prodia-Key": api_key
        }

    def generate(self, params):
        response = self._post(f"{self.base}/sdxl/generate", params)
        return response.json()

    def get_job(self, job_id):
        response = self._get(f"{self.base}/job/{job_id}")
        return response.json()

    def wait(self, job):
        job_result = job

        while job_result['status'] not in ['succeeded', 'failed']:
            time.sleep(0.25)
            job_result = self.get_job(job['job'])

        return job_result

    def list_models(self):
        response = self._get(f"{self.base}/sdxl/models")
        return response.json()

    def list_samplers(self):
        response = self._get(f"{self.base}/sdxl/samplers")
        return response.json()

    def _post(self, url, params):
        headers = {
            **self.headers,
            "Content-Type": "application/json"
        }
        response = requests.post(url, headers=headers, data=json.dumps(params))

        if response.status_code != 200:
            raise Exception(f"Bad Prodia Response: {response.status_code}")

        return response

    def _get(self, url):
        response = requests.get(url, headers=self.headers)

        if response.status_code != 200:
            raise Exception(f"Bad Prodia Response: {response.status_code}")

        return response


def image_to_base64(image_path):
    # Open the image with PIL
    with Image.open(image_path) as image:
        # Convert the image to bytes
        buffered = BytesIO()
        image.save(buffered, format="PNG")  # You can change format to PNG if needed

        # Encode the bytes to base64
        img_str = base64.b64encode(buffered.getvalue())

    return img_str.decode('utf-8')  # Convert bytes to string



prodia_client = Prodia(api_key=os.getenv("API_KEY"))

def flip_text(prompt, negative_prompt, steps, cfg_scale, width, height, seed):
    result = prodia_client.generate({
        "prompt": prompt,
        "negative_prompt": negative_prompt,
        "model": "sd_xl_base_1.0.safetensors [be9edd61]",
        "steps": steps,
        "sampler": "DPM++ 2M Karras",
        "cfg_scale": cfg_scale,
        "width": width,
        "height": height,
        "seed": seed
    })

    job = prodia_client.wait(result)

    return job["imageUrl"]

css = """
        #prompt-container .form{
        border-top-right-radius: 0;
        border-bottom-right-radius: 0;
        }
        #prompt-text-input, #negative-prompt-text-input{padding: .45rem 0.625rem}
        #component-16{border-top-width: 1px!important;margin-top: 1em}
        .image_duplication{position: absolute; width: 100px; left: 50px}
        .tabitem{border: 0 !important}.style(mobile_collapse=False, equal_height=True).style(mobile_collapse=False, equal_height=True).style(mobile_collapse=False, equal_height=True).style(mobile_collapse=False, equal_height=True
        #gen-button{
        border-top-left-radius:0;
        border-bottom-left-radius:0;
        }
        #gallery {
            min-height: 22rem;
            margin-bottom: 15px;
            margin-left: auto;
            margin-right: auto;
            border-bottom-right-radius: .5rem !important;
            border-bottom-left-radius: .5rem !important;
        }
        #gallery>div>.h-full {
            min-height: 20rem;
        }
"""

with gr.Blocks(css=css) as demo:
    gr.HTML(
        """
            <div style="text-align: center; margin: 0 auto;">
              <div
                style="
                  display: inline-flex;
                  align-items: center;
                  gap: 0.8rem;
                  font-size: 1.75rem;
                "
              >
                <svg
                  width="0.65em"
                  height="0.65em"
                  viewBox="0 0 115 115"
                  fill="none"
                  xmlns="http://www.w3.org/2000/svg"
                >
                  <rect width="23" height="23" fill="white"></rect>
                  <rect y="69" width="23" height="23" fill="white"></rect>
                  <rect x="23" width="23" height="23" fill="#AEAEAE"></rect>
                  <rect x="23"
                  <rect x="69" width="23" height="23" fill="black"></rect>
                  <rect x="69" y="69" width="23" height="23" fill="black"></rect>
                  <rect x="92" width="23" height="23" fill="#D9D9D9"></rect>
                  <rect x="92" y="69" width="23" height="23" fill="#AEAEAE"></rect>
                  <rect x="115" y="46" width="23" height="23" fill="white"></rect>
                  <rect x="115" y="115" width="23" height="23" fill="white"></rect>
                  <rect x="115" y="69" width="23" height="23" fill="#D9D9D9"></rect>
                  <rect x="92" y="46" width="23" height="23" fill="#AEAEAE"></rect>
                  <rect x="92" y="115" width="23" height="23" fill="#AEAEAE"></rect>
                  <rect x="92" y="69" width="23" height="23" fill="white"></rect>
                  <rect x="69" y="46" width="23" height="23" fill="white"></rect>
                  <rect x="69" y="115" width="23" height="23" fill="white"></rect>
                  <rect x="69" y="69" width="23" height="23" fill="#D9D9D9"></rect>
                  <rect x="46" y="46" width="23" height="23" fill="black"></rect>
                  <rect x="46" y="115" width="23" height="23" fill="black"></rect>
                  <rect x="46" y="69" width="23" height="23" fill="black"></rect>
                  <rect x="23" y="46" width="23" height="23" fill="#D9D9D9"></rect>
                  <rect x="23" y="115" width="23" height="23" fill="#AEAEAE"></rect>
                  <rect x="23" y="69" width="23" height="23" fill="black"></rect>
                </svg>
                <h1 style="font-weight: 900; margin-bottom: 7px;margin-top:5px">
                  Fast SDXL
                </h1> 
              </div>
            </div>
        """)
    with gr.Row(elem_id="prompt-container").style(mobile_collapse=False, equal_height=True):    
        with gr.Column(scale=6, min_width=600):  
            prompt = gr.Textbox(label="Prompt", placeholder="a cute cat, 8k", show_label=True, lines=1, elem_id="prompt-text-input")
            text_button = gr.Button("Generate", variant='primary', elem_id="gen-button")
    with gr.Row():    
        with gr.Column(scale=1):
            image_output = gr.Image(elem_id="gallery")
    with gr.Row():
        with gr.Accordion("Additionals inputs", open=False):
            with gr.Column(scale=1):
                negative_prompt = gr.Textbox(label="Negative Prompt", value="text, blurry", placeholder="What you don't want to see in the image", show_label=True, lines=1, elem_id="negative-prompt-text-input")
            with gr.Column(scale=1):
                steps = gr.Slider(label="Sampling Steps", minimum=1, maximum=30, value=25, step=1)
                cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, value=7, step=1)
                seed = gr.Number(label="Seed", value=-1)
            with gr.Column(scale=1):
                width = gr.Slider(label="↔️ Width", minimum=1024, maximum=1024, value=1024, step=8)
                height = gr.Slider(label="↕️ Height", minimum=1024, maximum=1024, value=1024, step=8)





        text_button.click(flip_text, inputs=[prompt, negative_prompt, steps, cfg_scale, width, height, seed], outputs=image_output)

demo.queue(concurrency_count=16, max_size=20, api_open=False).launch(max_threads=64)