Spaces:
Runtime error
Runtime error
File size: 5,244 Bytes
de4142b b19e158 de4142b 190c863 b19e158 de4142b 190c863 de4142b 190c863 b19e158 190c863 b19e158 190c863 b19e158 190c863 b19e158 190c863 b19e158 de4142b 97b711a de4142b 190c863 de4142b b19e158 97b711a b19e158 97b711a de4142b 190c863 b19e158 de4142b 190c863 b19e158 97b711a 190c863 de4142b b19e158 190c863 de4142b b19e158 190c863 97b711a 190c863 97b711a 190c863 b19e158 190c863 97b711a 190c863 97b711a de4142b 190c863 97b711a 190c863 b19e158 190c863 97b711a 190c863 b19e158 97b711a 190c863 97b711a 190c863 97b711a de4142b 190c863 de4142b 190c863 97b711a |
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 |
import gradio as gr
import torch
from diffusers import DiffusionPipeline
import random
# --- Model Configuration ---
model_id = "NewBie-AI/NewBie-image-Exp0.1"
device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Loading model on: {device}...")
# Load pipeline with proper error handling
try:
pipe = DiffusionPipeline.from_pretrained(
model_id,
torch_dtype=torch.float16 if device == "cuda" else torch.float32,
use_safetensors=True
)
pipe.to(device)
print("Model loaded successfully.")
except Exception as e:
print(f"Error loading model: {e}")
# --- Generation Function ---
def generate_image(prompt, negative_prompt, steps, guidance_scale, width, height, seed):
if seed == -1:
seed = random.randint(0, 2147483647)
# Configure generator for reproducibility - FIXED SYNTAX ERROR
generator = torch.Generator(device).manual_seed(int(seed))
print(f"Generating with seed: {seed}")
try:
image = pipe(
prompt=prompt,
negative_prompt=negative_prompt,
num_inference_steps=int(steps),
guidance_scale=float(guidance_scale),
width=int(width),
height=int(height),
generator=generator
).images[0]
return image, seed
except Exception as e:
return None, f"Error: {str(e)}"
# --- Gradio Interface ---
css = """
#col-container {max-width: 800px; margin-left: auto; margin-right: auto;}
"""
with gr.Blocks() as demo:
with gr.Column(elem_id="col-container"):
gr.Markdown(f"# 🎨 Image Generator: {model_id}")
gr.Markdown("Write a prompt to generate an image using the NewBie AI model.")
with gr.Group():
prompt = gr.Textbox(
label="Prompt (Positive description)",
placeholder="An astronaut riding a horse on Mars, 4k, realistic...",
lines=2
)
negative_prompt = gr.Textbox(
label="Negative Prompt (What you DON'T want)",
placeholder="blurry, deformed, bad quality, text...",
value="bad quality, worst quality, low resolution, blurry, distorted"
)
with gr.Row():
with gr.Column():
width = gr.Slider(
label="Width",
minimum=256,
maximum=1024,
step=64,
value=512
)
with gr.Column():
height = gr.Slider(
label="Height",
minimum=256,
maximum=1024,
step=64,
value=512
)
with gr.Row():
with gr.Column():
steps = gr.Slider(
label="Inference Steps",
minimum=10,
maximum=100,
step=1,
value=25
)
with gr.Column():
guidance_scale = gr.Slider(
label="Guidance Scale (Prompt fidelity)",
minimum=1.0,
maximum=20.0,
step=0.5,
value=7.5
)
with gr.Row():
seed = gr.Number(
label="Seed (Use -1 for random)",
value=-1,
precision=0
)
with gr.Accordion("Advanced Configuration", open=False):
gr.Markdown("Adjust these parameters for finer control over image generation.")
run_button = gr.Button("Generate Image", variant="primary")
result_image = gr.Image(label="Result", interactive=False)
seed_output = gr.Number(label="Seed used", interactive=False)
clear_button = gr.Button("Clear", variant="secondary")
# Examples
examples = gr.Examples(
examples=[
["A beautiful sunset over mountains, digital art", "blurry, distorted", 25, 7.5, 512, 512, -1],
["A futuristic city with flying cars, cyberpunk style", "low quality, pixelated", 30, 5.0, 768, 768, -1],
["A magical forest with glowing mushrooms, fantasy", "text, watermark", 40, 3.0, 1024, 1024, -1]
],
inputs=[prompt, negative_prompt, steps, guidance_scale, width, height, seed]
)
# Events
run_button.click(
fn=generate_image,
inputs=[prompt, negative_prompt, steps, guidance_scale, width, height, seed],
outputs=[result_image, seed_output],
api_visibility="public"
)
def clear_all():
return None, -1
clear_button.click(
fn=clear_all,
inputs=None,
outputs=[result_image, seed_output],
api_visibility="private"
)
# Launch the app
if __name__ == "__main__":
demo.launch(
theme=gr.themes.Soft(primary_hue="indigo", secondary_hue="purple"),
css=css,
footer_links=[{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"}]
) |