File size: 5,893 Bytes
0f3978b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5683ad2
0f3978b
 
5bb086c
6dbc9d1
 
 
0f3978b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1ee90c
0f3978b
 
 
 
5683ad2
0f3978b
 
d333df2
0f3978b
 
 
 
e49a43d
 
0f3978b
 
 
 
 
 
 
 
6dbc9d1
0f3978b
 
97c155f
0f3978b
 
 
9d23916
 
 
0f3978b
 
97ce691
c559134
0f3978b
ed0da0d
 
0f3978b
 
 
9f0ff31
e49a43d
dafd661
0f3978b
7e38602
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
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

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("PRODIA_API_KEY"))

def flip_text(prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed):
    result = prodia_client.generate({
        "prompt": prompt,
        "negative_prompt": negative_prompt,
        "model": model,
        "steps": steps,
        "sampler": sampler,
        "cfg_scale": cfg_scale,
        "width": width,
        "height": height,
        "seed": seed
    })

    job = prodia_client.wait(result)

    return job["imageUrl"]

css = """
#generate {
    height: 100%;
}
"""

with gr.Blocks(css=css) as demo:


    with gr.Row():
        with gr.Column(scale=6):
            model = gr.Dropdown(interactive=True,value="sd_xl_base_1.0.safetensors [be9edd61]", show_label=True, label="Stable Diffusion Checkpoint", choices=prodia_client.list_models())

        with gr.Column(scale=1):
            gr.Markdown(elem_id="powered-by-prodia", value="AUTOMATIC1111 Stable Diffusion Web UI for SDXL V1.0.<br> Powered by Prodia. <br> It will not automatically save the image you generated so please don't forget the imnage that you like.")

    with gr.Tab("txt2img"):
        with gr.Row():
            with gr.Column(scale=6, min_width=600):
                prompt = gr.Textbox(placeholder="Prompt (What you want to see in the image)", show_label=False, lines=3)
                negative_prompt = gr.Textbox(placeholder="Negative Prompt (What you don't want to see in the image)", show_label=False, lines=3,)
            with gr.Column():
                text_button = gr.Button("Generate", variant='primary', elem_id="generate")

        with gr.Row():
            with gr.Column(scale=3):
                with gr.Tab("Generation"):
                    with gr.Row():
                        with gr.Column(scale=1):
                            sampler = gr.Dropdown(value="DPM++ 2M Karras", show_label=True, label="Sampling Method", choices=prodia_client.list_samplers())

                        with gr.Column(scale=1):
                            steps = gr.Slider(label="Sampling Steps(How many times the AI is going to improve the initial image)", minimum=1, maximum=50, value=25, step=1)

                    with gr.Row():
                        with gr.Column(scale=1):
                            width = gr.Slider(label="Width", minimum=512, maximum=1536, value=1024, step=8)
                            height = gr.Slider(label="Height", minimum=512, maximum=1536, value=1024, step=8)
                            gr.Markdown(elem_id="resolution", value="*Resolution Maximum: 1MP (1048576 px)*")

                        with gr.Column(scale=1):
                            batch_size = gr.Slider(label="Batch Size", maximum=1, value=1)
                            batch_count = gr.Slider(label="Batch Count", maximum=1, value=1)

                    cfg_scale = gr.Slider(label="CFG Scale(How much does the AI follow the promts)", minimum=1, maximum=25, value=7.5, step=1)
                    seed = gr.Number(label="Seed(could be any number; -1 gives you a random number)", value=-1)


            with gr.Column(scale=2):
                image_output = gr.Image(value="https://scontent.cdninstagram.com/v/t51.29350-15/431370105_2616125331898278_6514352555784579245_n.webp?stp=dst-jpg_e35_p720x720&efg=eyJ2ZW5jb2RlX3RhZyI6ImltYWdlX3VybGdlbi4xMDgweDE5MjAuc2RyIn0&_nc_ht=scontent.cdninstagram.com&_nc_cat=106&_nc_ohc=tFxsl3ryJT0AX-p5JYa&edm=APs17CUBAAAA&ccb=7-5&ig_cache_key=MzMxNTQ0NTk4MjQ0Njk0ODA0MA%3D%3D.2-ccb7-5&oh=00_AfDsHKW6Zq51QeW3whGeAR1Mf2EttCojf7lJh_GdPL2uAQ&oe=65F075D9&_nc_sid=10d13b")
        
        text_button.click(flip_text, inputs=[prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed,], outputs=image_output)

demo.queue(concurrency_count=24, max_size=32, api_open=False).launch(max_threads=128)