stefanbenten commited on
Commit
0f3978b
1 Parent(s): 4c61483

*: initial release

Browse files
Files changed (3) hide show
  1. README.md +5 -5
  2. app.py +168 -0
  3. requirements.txt +3 -0
README.md CHANGED
@@ -1,8 +1,8 @@
1
  ---
2
- title: Stable Diffusion Xl
3
- emoji: 🏢
4
- colorFrom: red
5
- colorTo: pink
6
  sdk: gradio
7
  sdk_version: 3.42.0
8
  app_file: app.py
@@ -10,4 +10,4 @@ pinned: false
10
  license: mit
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: stable-diffusion-xl
3
+ emoji: 🔥
4
+ colorFrom: yellow
5
+ colorTo: gray
6
  sdk: gradio
7
  sdk_version: 3.42.0
8
  app_file: app.py
 
10
  license: mit
11
  ---
12
 
13
+ Prodia's Stable Diffusion XL Space.
app.py ADDED
@@ -0,0 +1,168 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import gradio as gr
3
+ import requests
4
+ import time
5
+ import json
6
+ import base64
7
+ import os
8
+ from PIL import Image
9
+ from io import BytesIO
10
+
11
+ class Prodia:
12
+ def __init__(self, api_key, base=None):
13
+ self.base = base or "https://api.prodia.com/v1"
14
+ self.headers = {
15
+ "X-Prodia-Key": api_key
16
+ }
17
+
18
+ def generate(self, params):
19
+ response = self._post(f"{self.base}/sdxl/generate", params)
20
+ return response.json()
21
+
22
+ def get_job(self, job_id):
23
+ response = self._get(f"{self.base}/job/{job_id}")
24
+ return response.json()
25
+
26
+ def wait(self, job):
27
+ job_result = job
28
+
29
+ while job_result['status'] not in ['succeeded', 'failed']:
30
+ time.sleep(0.25)
31
+ job_result = self.get_job(job['job'])
32
+
33
+ return job_result
34
+
35
+ def list_models(self):
36
+ response = self._get(f"{self.base}/models/list")
37
+ return response.json()
38
+
39
+ def _post(self, url, params):
40
+ headers = {
41
+ **self.headers,
42
+ "Content-Type": "application/json"
43
+ }
44
+ response = requests.post(url, headers=headers, data=json.dumps(params))
45
+
46
+ if response.status_code != 200:
47
+ raise Exception(f"Bad Prodia Response: {response.status_code}")
48
+
49
+ return response
50
+
51
+ def _get(self, url):
52
+ response = requests.get(url, headers=self.headers)
53
+
54
+ if response.status_code != 200:
55
+ raise Exception(f"Bad Prodia Response: {response.status_code}")
56
+
57
+ return response
58
+
59
+
60
+ def image_to_base64(image_path):
61
+ # Open the image with PIL
62
+ with Image.open(image_path) as image:
63
+ # Convert the image to bytes
64
+ buffered = BytesIO()
65
+ image.save(buffered, format="PNG") # You can change format to PNG if needed
66
+
67
+ # Encode the bytes to base64
68
+ img_str = base64.b64encode(buffered.getvalue())
69
+
70
+ return img_str.decode('utf-8') # Convert bytes to string
71
+
72
+
73
+
74
+ prodia_client = Prodia(api_key=os.getenv("PRODIA_API_KEY"))
75
+
76
+ def flip_text(prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed):
77
+ result = prodia_client.generate({
78
+ "prompt": prompt,
79
+ "negative_prompt": negative_prompt,
80
+ "model": model,
81
+ "steps": steps,
82
+ "sampler": sampler,
83
+ "cfg_scale": cfg_scale,
84
+ "width": width,
85
+ "height": height,
86
+ "seed": seed
87
+ })
88
+
89
+ job = prodia_client.wait(result)
90
+
91
+ return job["imageUrl"]
92
+
93
+ css = """
94
+ #generate {
95
+ height: 100%;
96
+ }
97
+ """
98
+
99
+ with gr.Blocks(css=css) as demo:
100
+
101
+
102
+ with gr.Row():
103
+ with gr.Column(scale=6):
104
+ model = gr.Dropdown(interactive=True,value="sd_xl_base_1.0.safetensors [be9edd61]", show_label=True, label="Stable Diffusion Checkpoint", choices=[
105
+ "sd_xl_base_1.0.safetensors [be9edd61]",
106
+ "dynavisionXL_0411.safetensors [c39cc051]",
107
+ "dreamshaperXL10_alpha2.safetensors [c8afe2ef]",
108
+ ])
109
+
110
+ with gr.Column(scale=1):
111
+ gr.Markdown(elem_id="powered-by-prodia", value="AUTOMATIC1111 Stable Diffusion Web UI for SDXL V1.0.<br>Powered by [Prodia](https://prodia.com).")
112
+
113
+ with gr.Tab("txt2img"):
114
+ with gr.Row():
115
+ with gr.Column(scale=6, min_width=600):
116
+ prompt = gr.Textbox("puppies in a cloud, 4k", placeholder="Prompt", show_label=False, lines=3)
117
+ negative_prompt = gr.Textbox(placeholder="Negative Prompt", show_label=False, lines=3)
118
+ with gr.Column():
119
+ text_button = gr.Button("Generate", variant='primary', elem_id="generate")
120
+
121
+ with gr.Row():
122
+ with gr.Column(scale=3):
123
+ with gr.Tab("Generation"):
124
+ with gr.Row():
125
+ with gr.Column(scale=1):
126
+ sampler = gr.Dropdown(value="Euler a", show_label=True, label="Sampling Method", choices=[
127
+ "Euler",
128
+ "Euler a",
129
+ "LMS",
130
+ "Heun",
131
+ "DPM2",
132
+ "DPM2 a",
133
+ "DPM++ 2S a",
134
+ "DPM++ 2M",
135
+ "DPM++ SDE",
136
+ "DPM fast",
137
+ "DPM adaptive",
138
+ "LMS Karras",
139
+ "DPM2 Karras",
140
+ "DPM2 a Karras",
141
+ "DPM++ 2S a Karras",
142
+ "DPM++ 2M Karras",
143
+ "DPM++ SDE Karras",
144
+ ])
145
+
146
+ with gr.Column(scale=1):
147
+ steps = gr.Slider(label="Sampling Steps", minimum=1, maximum=30, value=25, step=1)
148
+
149
+ with gr.Row():
150
+ with gr.Column(scale=1):
151
+ width = gr.Slider(label="Width", minimum=1024, maximum=1024, value=1024, step=8)
152
+ height = gr.Slider(label="Height", minimum=1024, maximum=1024, value=1024, step=8)
153
+
154
+ with gr.Column(scale=1):
155
+ batch_size = gr.Slider(label="Batch Size", maximum=1, value=1)
156
+ batch_count = gr.Slider(label="Batch Count", maximum=1, value=1)
157
+
158
+ cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, value=7, step=1)
159
+ seed = gr.Number(label="Seed", value=-1)
160
+
161
+
162
+ with gr.Column(scale=2):
163
+ image_output = gr.Image()
164
+
165
+ text_button.click(flip_text, inputs=[prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed], outputs=image_output)
166
+
167
+ demo.queue(concurrency_count=5)
168
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ numpy
2
+ gradio
3
+ requests