Spaces:
Runtime error
Runtime error
File size: 6,916 Bytes
0f3978b 5683ad2 0f3978b 5bb086c 6dbc9d1 0f3978b 7a1f2b3 0f3978b 7a1f2b3 0f3978b 7a1f2b3 0f3978b 7a1f2b3 0f3978b 7a1f2b3 0f3978b 7a1f2b3 0f3978b 7a1f2b3 0f3978b 7a1f2b3 0f3978b c93ff24 c44b6b5 0718612 795980c c93ff24 0718612 795980c 0718612 795980c 0718612 c93ff24 c44b6b5 7ccc156 7a1f2b3 7ccc156 7a1f2b3 7ccc156 7a1f2b3 7ccc156 0718612 c44b6b5 |
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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
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 = """
/* Overall Styling */
body {
font-family: 'Arial', sans-serif;
}
.container {
display: flex;
flex-direction: column;
gap: 20px;
}
/* Image Output Area */
#image-output-container {
border: 2px solid #ccc;
border-radius: 8px;
overflow: hidden;
}
#image-output {
max-width: 100%;
height: auto;
}
/* Settings Section */
#settings {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 20px;
}
.setting-group {
border: 1px solid #ccc;
padding: 20px;
border-radius: 8px;
}
/* Button Styling */
#generate {
background-color: #007bff; /* Example - use your preferred color */
color: white;
padding: 15px 25px;
border: none;
border-radius: 5px;
cursor: pointer;
}
#generate:hover {
background-color: #0056b3; /* Darker shade on hover */
}
/* Responsive Design - Adjust breakpoints as needed */
@media screen and (max-width: 768px) {
#settings {
grid-template-columns: 1fr;
}
}
"""
# --- Gradio Interface ---
# Define demo OUTSIDE the blocks
demo = gr.Blocks(css=css)
def start_generation():
return demo
# Initial Welcome Screen
with gr.Blocks() as welcome_screen:
with gr.Row():
logo = gr.Image(
value="http://disneypixaraigenerator.com/wp-content/uploads/2023/12/cropped-android-chrome-512x512-1.png",
elem_id="logo",
height=200,
width=300
)
with gr.Row():
title = gr.Markdown("<h1 style='text-align: center;'>Disney Pixar AI Generator</h1>", elem_id="title")
with gr.Row():
start_button = gr.Button("Get Started", variant='primary', elem_id="start-button")
# Connect Welcome Screen to Generation Screen
start_button.click(fn=start_generation, outputs=demo)
# Main Generation Screen (Now use the defined 'demo')
with demo:
with gr.Row():
gr.Markdown("<h1 style='text-align: center;'>Create Your Disney Pixar AI Poster</h1>", elem_id="title")
with gr.Row(elem_id="image-output-container"):
image_output = gr.Image(
value="https://cdn-uploads.huggingface.co/production/uploads/noauth/XWJyh9DhMGXrzyRJk7SfP.png",
label="Generated Image",
elem_id="image-output"
)
with gr.Row(elem_id="settings"):
with gr.Column(scale=1, min_width=300, elem_classes="setting-group"):
prompt = gr.Textbox(
"space warrior, beautiful, female, ultrarealistic, soft lighting, 8k",
placeholder="Enter your prompt here...",
show_label=False,
lines=3,
elem_id="prompt-input"
)
negative_prompt = gr.Textbox(
placeholder="Enter negative prompts (optional)...",
show_label=False,
lines=3,
value="3d, cartoon, anime, (deformed eyes, nose, ears, nose), bad anatomy, ugly"
)
text_button = gr.Button("Generate", variant='primary', elem_id="generate")
with gr.Column(scale=1, min_width=300, elem_classes="setting-group"):
model = gr.Dropdown(
interactive=True,
value="sd_xl_base_1.0.safetensors [be9edd61]",
show_label=True,
label="Model",
choices=prodia_client.list_models()
)
sampler = gr.Dropdown(
value="DPM++ 2M Karras",
show_label=True,
label="Sampling Method",
choices=prodia_client.list_samplers()
)
steps = gr.Slider(label="Sampling Steps", minimum=1, maximum=25, value=20, step=1)
with gr.Column(scale=1, min_width=300, elem_classes="setting-group"):
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)
cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, value=7, step=1)
seed = gr.Number(label="Seed", value=-1)
text_button.click(flip_text, inputs=[prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed], outputs=image_output)
# Launch the Gradio app
welcome_screen.launch(max_threads=128) |