montyanderson commited on
Commit
5ddef29
0 Parent(s):
Files changed (3) hide show
  1. app.py +152 -0
  2. readme.md +13 -0
  3. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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}/job", params)
20
+ return response.json()
21
+
22
+ def transform(self, params):
23
+ response = self._post(f"{self.base}/transform", params)
24
+ return response.json()
25
+
26
+ def controlnet(self, params):
27
+ response = self._post(f"{self.base}/controlnet", params)
28
+ return response.json()
29
+
30
+ def get_job(self, job_id):
31
+ response = self._get(f"{self.base}/job/{job_id}")
32
+ return response.json()
33
+
34
+ def wait(self, job):
35
+ job_result = job
36
+
37
+ while job_result['status'] not in ['succeeded', 'failed']:
38
+ time.sleep(0.25)
39
+ job_result = self.get_job(job['job'])
40
+
41
+ return job_result
42
+
43
+ def list_models(self):
44
+ response = self._get(f"{self.base}/models/list")
45
+ return response.json()
46
+
47
+ def _post(self, url, params):
48
+ headers = {
49
+ **self.headers,
50
+ "Content-Type": "application/json"
51
+ }
52
+ response = requests.post(url, headers=headers, data=json.dumps(params))
53
+
54
+ if response.status_code != 200:
55
+ raise Exception(f"Bad Prodia Response: {response.status_code}")
56
+
57
+ return response
58
+
59
+ def _get(self, url):
60
+ response = requests.get(url, headers=self.headers)
61
+
62
+ if response.status_code != 200:
63
+ raise Exception(f"Bad Prodia Response: {response.status_code}")
64
+
65
+ return response
66
+
67
+
68
+ def image_to_base64(image_path):
69
+ # Open the image with PIL
70
+ with Image.open(image_path) as image:
71
+ # Convert the image to bytes
72
+ buffered = BytesIO()
73
+ image.save(buffered, format="PNG") # You can change format to PNG if needed
74
+
75
+ # Encode the bytes to base64
76
+ img_str = base64.b64encode(buffered.getvalue())
77
+
78
+ return img_str.decode('utf-8') # Convert bytes to string
79
+
80
+
81
+
82
+ prodia_client = Prodia(api_key=os.getenv("PRODIA_API_KEY"))
83
+
84
+ def flip_text(prompt, negative_prompt, model, steps, sampler, cfg_scale):
85
+ result = prodia_client.generate({
86
+ "prompt": prompt,
87
+ "negative_prompt": negative_prompt,
88
+ "model": model,
89
+ "steps": steps,
90
+ "sampler": sampler,
91
+ "cfg_scale": cfg_scale
92
+ })
93
+
94
+ job = prodia_client.wait(result)
95
+
96
+ return job["imageUrl"]
97
+
98
+ css = """
99
+ #generate {
100
+ height: 100%;
101
+ }
102
+ """
103
+
104
+ with gr.Blocks(css=css) as demo:
105
+ with gr.Tab("txt2img"):
106
+ with gr.Row():
107
+ with gr.Column(scale=6, min_width=600):
108
+ prompt = gr.Textbox(placeholder="Prompt", show_label=False, lines=3)
109
+ negative_prompt = gr.Textbox(placeholder="Negative Prompt", show_label=False, lines=3)
110
+ with gr.Column(equal_height=True):
111
+ text_button = gr.Button("Generate", variant='primary', elem_id="generate")
112
+
113
+ with gr.Row():
114
+ with gr.Column(scale=1):
115
+ with gr.Tab("Generation"):
116
+ with gr.Row():
117
+ with gr.Column(scale=1):
118
+ model = gr.Dropdown(interactive=True,value="v1-5-pruned-emaonly.safetensors [d7049739]", show_label=False, choices=prodia_client.list_models())
119
+
120
+ sampler = gr.Dropdown(value="Euler a", show_label=False, choices=[
121
+ "Euler",
122
+ "Euler a",
123
+ "LMS",
124
+ "Heun",
125
+ "DPM2",
126
+ "DPM2 a",
127
+ "DPM++ 2S a",
128
+ "DPM++ 2M",
129
+ "DPM++ SDE",
130
+ "DPM fast",
131
+ "DPM adaptive",
132
+ "LMS Karras",
133
+ "DPM2 Karras",
134
+ "DPM2 a Karras",
135
+ "DPM++ 2S a Karras",
136
+ "DPM++ 2M Karras",
137
+ "DPM++ SDE Karras",
138
+ "DDIM",
139
+ "PLMS",
140
+ ])
141
+
142
+ with gr.Column(scale=1):
143
+ steps = gr.Slider(label="Steps", miniumum=1, maximum=50, value=25)
144
+ cfg_scale = gr.Slider(label="CFG Scale", miniumum=1, maximum=20, value=7)
145
+
146
+ with gr.Column(scale=1):
147
+ image_output = gr.Image()
148
+
149
+ text_button.click(flip_text, inputs=[prompt, negative_prompt, model, steps, sampler, cfg_scale], outputs=image_output)
150
+
151
+
152
+ demo.launch()
readme.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: stable-diffusion
3
+ emoji: 🔥
4
+ colorFrom: yellow
5
+ colorTo: gray
6
+ sdk: gradio
7
+ sdk_version: 3.11.0
8
+ app_file: app.py
9
+ pinned: false
10
+ license: mit
11
+ ---
12
+
13
+ Prodia's Stable Diffusion Space.
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ numpy
2
+ gradio
3
+ requests