|
import gradio as gr |
|
import numpy as np |
|
import random |
|
from PIL import Image, ImageDraw, ImageFont |
|
import torch |
|
from diffusers import DiffusionPipeline |
|
import io |
|
import time |
|
|
|
|
|
|
|
print(f"PyTorch version: {torch.__version__}") |
|
print(f"CUDA available: {torch.cuda.is_available()}") |
|
print(f"CUDA device count: {torch.cuda.device_count()}") |
|
|
|
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) |
|
|
|
|
|
if device == "cuda": |
|
try: |
|
pipe.enable_xformers_memory_efficient_attention() |
|
print("Enabled xformers memory efficient attention") |
|
except Exception as e: |
|
print(f"Could not enable xformers: {str(e)}") |
|
|
|
try: |
|
pipe.unet.to(memory_format=torch.channels_last) |
|
print("Enabled channels last memory format") |
|
except Exception as e: |
|
print(f"Could not enable channels last: {str(e)}") |
|
else: |
|
print("Running on CPU - skipping GPU optimizations") |
|
|
|
MAX_SEED = np.iinfo(np.int32).max |
|
IMAGE_SIZE = 1024 |
|
WATERMARK_TEXT = "SelamGPT" |
|
|
|
|
|
def add_watermark(image): |
|
"""Optimized watermark function matching original style""" |
|
try: |
|
draw = ImageDraw.Draw(image) |
|
font_size = 24 |
|
|
|
try: |
|
font = ImageFont.truetype("Roboto-Bold.ttf", font_size) |
|
except: |
|
font = ImageFont.load_default(font_size) |
|
|
|
text_width = draw.textlength(WATERMARK_TEXT, font=font) |
|
x = image.width - text_width - 10 |
|
y = image.height - 34 |
|
|
|
|
|
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 |
|
except Exception as e: |
|
print(f"Watermark error: {str(e)}") |
|
return image |
|
|
|
|
|
def generate( |
|
prompt, |
|
negative_prompt="", |
|
seed=None, |
|
randomize_seed=True, |
|
guidance_scale=0.0, |
|
num_inference_steps=1, |
|
progress=gr.Progress(track_tqdm=True), |
|
): |
|
if not prompt.strip(): |
|
return None, "⚠️ Please enter a prompt" |
|
|
|
start_time = time.time() |
|
|
|
|
|
if randomize_seed or seed is None: |
|
seed = random.randint(0, MAX_SEED) |
|
|
|
generator = torch.manual_seed(seed) |
|
|
|
try: |
|
|
|
result = pipe( |
|
prompt=prompt, |
|
negative_prompt=negative_prompt, |
|
width=IMAGE_SIZE, |
|
height=IMAGE_SIZE, |
|
guidance_scale=guidance_scale, |
|
num_inference_steps=max(1, num_inference_steps), |
|
generator=generator, |
|
).images[0] |
|
|
|
|
|
watermarked = add_watermark(result) |
|
buffer = io.BytesIO() |
|
watermarked.save(buffer, format="JPEG", quality=85, optimize=True) |
|
buffer.seek(0) |
|
|
|
gen_time = time.time() - start_time |
|
status = f"✔️ Generated in {gen_time:.2f}s | Seed: {seed}" |
|
|
|
return Image.open(buffer), status |
|
|
|
except torch.cuda.OutOfMemoryError: |
|
return None, "⚠️ GPU out of memory - try a simpler prompt" |
|
except Exception as e: |
|
print(f"Generation error: {str(e)}") |
|
return None, f"⚠️ Error: {str(e)[:200]}" |
|
|
|
|
|
examples = [ |
|
["An ancient Aksumite warrior in cyberpunk armor, 4k detailed"], |
|
["Traditional Ethiopian coffee ceremony in zero gravity"], |
|
["Portrait of a Habesha queen with golden jewelry"] |
|
] |
|
|
|
|
|
theme = gr.themes.Default( |
|
primary_hue="emerald", |
|
secondary_hue="amber", |
|
font=[gr.themes.GoogleFont("Poppins"), "Arial", "sans-serif"] |
|
) |
|
|
|
with gr.Blocks(theme=theme, title="SelamGPT Turbo Generator") as demo: |
|
gr.Markdown(""" |
|
# 🎨 SelamGPT Turbo Image Generator |
|
*Ultra-fast 1024x1024 image generation with SDXL-Turbo* |
|
""") |
|
|
|
with gr.Row(): |
|
with gr.Column(scale=3): |
|
prompt = gr.Textbox( |
|
label="Describe your image", |
|
placeholder="A futuristic Ethiopian city with flying cars...", |
|
lines=3, |
|
max_lines=5 |
|
) |
|
with gr.Row(): |
|
generate_btn = gr.Button("Generate Image", variant="primary") |
|
clear_btn = gr.Button("Clear") |
|
|
|
gr.Examples( |
|
examples=examples, |
|
inputs=[prompt] |
|
) |
|
|
|
with gr.Column(scale=2): |
|
output_image = gr.Image( |
|
label="Generated Image", |
|
type="pil", |
|
format="jpeg", |
|
height=512 |
|
) |
|
status_output = gr.Textbox( |
|
label="Status", |
|
interactive=False |
|
) |
|
|
|
with gr.Accordion("⚙️ Advanced Settings", open=False): |
|
negative_prompt = gr.Textbox( |
|
label="Negative Prompt", |
|
placeholder="What to avoid (optional)", |
|
max_lines=1 |
|
) |
|
with gr.Row(): |
|
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True) |
|
seed = gr.Number(label="Seed", value=0, precision=0) |
|
guidance_scale = gr.Slider(0.0, 1.0, value=0.0, step=0.1, label="Guidance Scale") |
|
num_inference_steps = gr.Slider(1, 4, value=1, step=1, label="Inference Steps") |
|
|
|
generate_btn.click( |
|
fn=generate, |
|
inputs=[ |
|
prompt, |
|
negative_prompt, |
|
seed, |
|
randomize_seed, |
|
guidance_scale, |
|
num_inference_steps |
|
], |
|
outputs=[output_image, status_output] |
|
) |
|
|
|
clear_btn.click( |
|
fn=lambda: [None, ""], |
|
outputs=[output_image, status_output] |
|
) |
|
|
|
if __name__ == "__main__": |
|
demo.queue(max_size=4) |
|
demo.launch(server_name="0.0.0.0", server_port=7860) |