# This space used model: stabilityai/stable-diffusion-xl-base-1.0 # and model: stabilityai/stable-diffusion-xl-refiner-1.0 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 batch_size=1 batch_count=1 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("API_KEY")) def flip_text(prompt, negative_prompt, steps, cfg_scale, width, height, seed): result = prodia_client.generate({ "prompt": prompt, "negative_prompt": negative_prompt, "model": "sd_xl_base_1.0.safetensors [be9edd61]", "steps": steps, "sampler": "DPM++ 2M Karras", "cfg_scale": cfg_scale, "width": width, "height": height, "seed": seed }) job = prodia_client.wait(result) return job["imageUrl"] css = """ #prompt-container .form{ border-top-right-radius: 0; border-bottom-right-radius: 0; } #prompt-text-input, #negative-prompt-text-input{padding: .45rem 0.625rem} #component-16{border-top-width: 1px!important;margin-top: 1em} .image_duplication{position: absolute; width: 100px; left: 50px} .tabitem{border: 0 !important}.style(mobile_collapse=False, equal_height=True).style(mobile_collapse=False, equal_height=True).style(mobile_collapse=False, equal_height=True).style(mobile_collapse=False, equal_height=True #gen-button{ border-top-left-radius:0; border-bottom-left-radius:0; } #gallery { min-height: 22rem; margin-bottom: 15px; margin-left: auto; margin-right: auto; border-bottom-right-radius: .5rem !important; border-bottom-left-radius: .5rem !important; } #gallery>div>.h-full { min-height: 20rem; } """ with gr.Blocks(css=css) as demo: gr.HTML( """