Spaces:
Runtime error
Runtime error
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} - {response.text}") | |
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} - {response.text}") | |
return response | |
def image_to_base64(image_path): | |
with Image.open(image_path) as image: | |
buffered = BytesIO() | |
image.save(buffered, format="PNG") | |
img_str = base64.b64encode(buffered.getvalue()) | |
return img_str.decode('utf-8') | |
api_key = os.getenv("PRODIA_API_KEY") | |
if not api_key: | |
raise ValueError("Prodia API key not found in environment variables") | |
prodia_client = Prodia(api_key=api_key) | |
def flip_text(prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed): | |
try: | |
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) | |
if job['status'] == 'succeeded': | |
return job["imageUrl"] | |
else: | |
return "Generation failed, please try again." | |
except Exception as e: | |
return f"An error occurred: {e}" | |
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; | |
color: white; | |
padding: 15px 25px; | |
border: none; | |
border-radius: 5px; | |
cursor: pointer; | |
} | |
#generate:hover { | |
background-color: #0056b3; | |
} | |
/* Responsive Design */ | |
@media screen and (max-width: 768px) { | |
#settings { | |
grid-template-columns: 1fr; | |
} | |
} | |
""" | |
with gr.Blocks(css=css) as demo: | |
state = gr.InterfaceState(value="Welcome Screen") | |
def update_visibility(tab_state): | |
return tab_state == "Main Generation Screen" | |
with gr.Tabs() as tabs: | |
with gr.Tab("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.Textbox("<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") | |
with gr.Tab("Main Generation Screen"): | |
with gr.Row(): | |
gr.Textbox("<h1 style='text-align: center;'>Create Your Disney Pixar AI Poster</h1>", elem_id="title") | |
with gr.Row(): | |
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(): | |
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" | |
) | |