Spaces:
Runtime error
Runtime error
File size: 8,937 Bytes
52442e0 |
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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
import gradio as gr
import requests
import base64
from PIL import Image
import io
import json
# Mock function to simulate image generation
def generate_image(prompt, negative_prompt, guidance_scale, num_inference_steps, width, height, seed):
"""
Generate an image using NewBie-AI/NewBie-image-Exp0.1
In a real implementation, this would connect to the actual model API
"""
try:
# This is a placeholder for actual model inference
# In production, you would replace this with actual API calls to NewBie-AI
# For demonstration, we'll create a simple gradient image
# In reality, this would call: https://huggingface.co/NewBie-AI/NewBie-image-Exp0.1
# Create a colorful gradient image
import numpy as np
img = np.zeros((height, width, 3), dtype=np.uint8)
# Create gradient
for i in range(height):
for j in range(width):
img[i, j, 0] = int(255 * i / height)
img[i, j, 1] = int(255 * j / width)
img[i, j, 2] = int(255 * (i + j) / (height + width))
return img
except Exception as e:
raise gr.Error(f"Image generation failed: {str(e)}")
def generate_images_interface(
prompt,
negative_prompt="",
guidance_scale=7.5,
num_inference_steps=20,
width=512,
height=512,
seed=-1,
batch_size=1
):
"""
Interface function for image generation
"""
# Validate inputs
if not prompt or len(prompt.strip()) == 0:
raise gr.Warning("Please enter a prompt to generate an image")
if width < 64 or width > 1024:
raise gr.Warning("Width must be between 64 and 1024")
if height < 64 or height > 1024:
raise gr.Warning("Height must be between 64 and 1024")
generated_images = []
for i in range(batch_size):
image = generate_image(
prompt=prompt,
negative_prompt=negative_prompt,
guidance_scale=guidance_scale,
num_inference_steps=num_inference_steps,
width=width,
height=height,
seed=seed if seed != -1 else None
)
generated_images.append(image)
if batch_size == 1:
return generated_images[0]
else:
return generated_images
def main():
with gr.Blocks() as demo:
gr.Markdown("# π¨ NewBie-AI Image Generator")
gr.Markdown("Create stunning AI-generated images with full customization")
with gr.Row():
with gr.Column(scale=1):
with gr.Group():
gr.Markdown("### π Text Prompts")
prompt_input = gr.Textbox(
label="Prompt",
placeholder="Describe the image you want to create...",
lines=3,
value="A beautiful landscape with mountains and a lake"
)
negative_prompt_input = gr.Textbox(
label="Negative Prompt",
placeholder="What you don't want in the image...",
)
with gr.Row():
width_slider = gr.Slider(
minimum=64,
maximum=1024,
value=512,
step=64,
label="Width"
)
height_slider = gr.Slider(
minimum=64,
maximum=1024,
value=512,
label="Height"
)
with gr.Row():
guidance_scale_slider = gr.Slider(
minimum=1.0,
maximum=20.0,
value=7.5,
step=0.5,
label="Guidance Scale"
)
with gr.Row():
inference_steps_slider = gr.Slider(
minimum=1,
maximum=50,
value=20,
step=1,
label="Inference Steps"
)
with gr.Row():
seed_input = gr.Number(
value=-1,
label="Seed (-1 for random)"
)
with gr.Row():
batch_size_dropdown = gr.Dropdown(
choices=["1", "2", "4"],
value="1",
label="Batch Size"
)
with gr.Group():
gr.Markdown("### βοΈ Advanced Settings")
num_inference_steps = gr.Number(
value=20,
label="Number of Inference Steps"
)
generate_btn = gr.Button(
"Generate Image π¨",
variant="primary",
size="lg"
)
with gr.Column(scale=1):
with gr.Group():
gr.Markdown("### πΌοΈ Generated Images")
output_gallery = gr.Gallery(
label="Generated Images",
columns=2,
height=500
)
# Event handling with Gradio 6 syntax
generate_btn.click(
fn=generate_images_interface,
inputs=[
prompt_input,
negative_prompt_input,
guidance_scale_slider,
inference_steps_slider,
width_slider,
height_slider,
seed_input
],
outputs=[output_gallery],
api_visibility="public"
)
# Examples section
gr.Examples(
examples=[
[
"A majestic dragon flying over a medieval castle at sunset",
"blurry, low quality",
7.5,
20,
512,
512,
-1
],
[
"A futuristic cityscape with flying cars and neon lights",
"watermark, signature",
8.0,
25,
768,
768,
42
],
[
"An astronaut riding a horse on Mars, photorealistic",
"cartoon, animated",
12.0,
30,
1024,
1024,
123
]
],
inputs=[
prompt_input,
negative_prompt_input,
guidance_scale_slider,
inference_steps_slider,
width_slider,
height_slider,
seed_input
],
outputs=[output_gallery],
fn=generate_images_interface,
cache_examples=True
)
gr.Markdown("---")
gr.HTML('<div style="text-align: center; padding: 20px; font-size: 14px; color: #666;">Built with <a href="https://huggingface.co/spaces/akhaliq/anycoder" style="color: #666; text-decoration: none;">anycoder</a></div>')
# Launch with Gradio 6 syntax - ALL parameters go here
demo.launch(
theme=gr.themes.Soft(
primary_hue="indigo",
secondary_hue="blue",
neutral_hue="slate",
font=gr.themes.GoogleFont("Inter"),
text_size="lg",
spacing_size="lg",
radius_size="md"
).set(
button_primary_background_fill="*primary_600",
button_primary_background_fill_hover="*primary_700",
block_title_text_weight="600",
),
css="""
.gradio-container {
max-width: 1200px !important;
}
.darktest {
background-color: #f8fafc;
padding: 20px;
border-radius: 8px;
}
.cool-col {
border: 1px solid #e2e8f0;
border-radius: 8px;
padding: 20px;
}
""",
footer_links=[{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"}
)
if __name__ == "__main__":
main() |