snackshell's picture
Update app.py
4103aa4 verified
raw
history blame
3.88 kB
import gradio as gr
import numpy as np
import random
from PIL import Image, ImageDraw, ImageFont
import torch
from diffusers import DiffusionPipeline
import io
# ===== CONFIG =====
device = "cuda" if torch.cuda.is_available() else "cpu"
torch_dtype = torch.float16 if device == "cuda" else torch.float32
model_repo_id = "stabilityai/sdxl-turbo"
pipe = DiffusionPipeline.from_pretrained(
model_repo_id,
torch_dtype=torch_dtype,
variant="fp16" if device == "cuda" else None
)
pipe.to(device)
MAX_SEED = np.iinfo(np.int32).max
IMAGE_WIDTH = 768
IMAGE_HEIGHT = 768
WATERMARK_TEXT = "SelamGPT"
# ===== WATERMARK FUNCTION =====
def add_watermark(image):
draw = ImageDraw.Draw(image)
font_size = int(image.width * 0.03)
try:
font = ImageFont.truetype("Roboto-Bold.ttf", font_size)
except:
font = ImageFont.load_default()
text_width = draw.textlength(WATERMARK_TEXT, font=font)
x = image.width - text_width - 12
y = image.height - font_size - 10
draw.text((x+1, y+1), WATERMARK_TEXT, font=font, fill=(0, 0, 0, 128))
draw.text((x, y), WATERMARK_TEXT, font=font, fill=(255, 255, 255))
return image
# ===== INFERENCE FUNCTION =====
def generate(
prompt,
negative_prompt,
seed,
randomize_seed,
guidance_scale,
num_inference_steps,
progress=gr.Progress(track_tqdm=True),
):
if not prompt.strip():
return None, "⚠️ Please enter a prompt"
if randomize_seed:
seed = random.randint(0, MAX_SEED)
generator = torch.manual_seed(seed)
result = pipe(
prompt=prompt,
negative_prompt=negative_prompt,
width=IMAGE_WIDTH,
height=IMAGE_HEIGHT,
guidance_scale=guidance_scale,
num_inference_steps=num_inference_steps,
generator=generator,
).images[0]
watermarked = add_watermark(result)
buffer = io.BytesIO()
watermarked.convert("RGB").save(buffer, format="JPEG", quality=70)
buffer.seek(0)
return Image.open(buffer), seed
# ===== EXAMPLES =====
examples = [
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
"An astronaut riding a green horse",
"A delicious ceviche cheesecake slice",
]
# ===== INTERFACE =====
css = "#container { max-width: 700px; margin: auto; }"
with gr.Blocks(css=css, title="SelamGPT Turbo Generator") as demo:
with gr.Column(elem_id="container"):
gr.Markdown("# 🖼️ SelamGPT Image Generator")
with gr.Row():
prompt = gr.Textbox(
label="Prompt",
show_label=False,
placeholder="Enter your prompt",
lines=1,
scale=3
)
generate_btn = gr.Button("Generate", variant="primary")
output_image = gr.Image(label="Generated Image", type="pil", format="jpeg")
seed_display = gr.Textbox(label="Seed Used", interactive=False)
with gr.Accordion("⚙️ Advanced Settings", open=False):
negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="What to avoid (optional)", max_lines=1)
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
seed = gr.Slider(0, MAX_SEED, step=1, label="Seed", value=0)
guidance_scale = gr.Slider(0.0, 10.0, step=0.1, label="Guidance Scale", value=0.0)
num_inference_steps = gr.Slider(1, 10, step=1, label="Inference Steps", value=2)
gr.Examples(examples=examples, inputs=[prompt])
generate_btn.click(
fn=generate,
inputs=[
prompt,
negative_prompt,
seed,
randomize_seed,
guidance_scale,
num_inference_steps
],
outputs=[output_image, seed_display]
)
if __name__ == "__main__":
demo.launch()