Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files- app.py +129 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from utils import generate_images_simulated
|
| 3 |
+
|
| 4 |
+
# --- Constants ---
|
| 5 |
+
ANYCODER_LINK = "https://huggingface.co/spaces/akhaliq/anycoder"
|
| 6 |
+
MODEL_CHOICES = {
|
| 7 |
+
"Stable Diffusion v1.4 (CompVis)": "CompVis/stable-diffusion-v1-4",
|
| 8 |
+
"Stable Diffusion 2.1 Base (Manojb)": "Manojb/stable-diffusion-2-1-base"
|
| 9 |
+
}
|
| 10 |
+
DEFAULT_MODEL_KEY = list(MODEL_CHOICES.keys())[0]
|
| 11 |
+
|
| 12 |
+
# --- Gradio Logic Wrapper ---
|
| 13 |
+
def process_generation(prompt: str, negative_prompt: str, model_key: str, steps: float, guidance_scale: float, num_images: float):
|
| 14 |
+
"""
|
| 15 |
+
Wrapper function to call the simulated generation logic, translating model key.
|
| 16 |
+
|
| 17 |
+
Args:
|
| 18 |
+
prompt: The text prompt for generation.
|
| 19 |
+
negative_prompt: Text prompt to guide the model away from.
|
| 20 |
+
model_key: The user-friendly name of the selected model.
|
| 21 |
+
steps: Inference steps.
|
| 22 |
+
guidance_scale: CFG scale.
|
| 23 |
+
num_images: Number of images to generate (1-4).
|
| 24 |
+
|
| 25 |
+
Returns:
|
| 26 |
+
A list of PIL Images for the Gradio Gallery.
|
| 27 |
+
"""
|
| 28 |
+
model_name_id = MODEL_CHOICES[model_key]
|
| 29 |
+
|
| 30 |
+
# Note: steps and guidance_scale are floats from the slider, cast to appropriate types
|
| 31 |
+
results = generate_images_simulated(
|
| 32 |
+
prompt=prompt,
|
| 33 |
+
negative_prompt=negative_prompt,
|
| 34 |
+
model_name=model_name_id,
|
| 35 |
+
steps=int(steps),
|
| 36 |
+
guidance_scale=guidance_scale,
|
| 37 |
+
num_images=int(num_images)
|
| 38 |
+
)
|
| 39 |
+
return results
|
| 40 |
+
|
| 41 |
+
# --- Gradio UI Definition ---
|
| 42 |
+
with gr.Blocks(title="Stable Diffusion Model Comparison Demo") as demo:
|
| 43 |
+
|
| 44 |
+
# Custom Header
|
| 45 |
+
gr.HTML(
|
| 46 |
+
f"""
|
| 47 |
+
<div style="text-align: center; max-width: 800px; margin: 0 auto;">
|
| 48 |
+
<h1>Stable Diffusion Model Comparison Demo</h1>
|
| 49 |
+
<p>Compare outputs from Stable Diffusion v1.4 and 2.1 Base. The application handles empty prompts by using a default prompt internally.</p>
|
| 50 |
+
<p>Built with <a href="{ANYCODER_LINK}" target="_blank" style="color: blue; text-decoration: underline;">anycoder</a></p>
|
| 51 |
+
</div>
|
| 52 |
+
"""
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
with gr.Row():
|
| 56 |
+
with gr.Column(scale=2):
|
| 57 |
+
# --- Inputs ---
|
| 58 |
+
prompt = gr.Textbox(
|
| 59 |
+
label="Prompt",
|
| 60 |
+
placeholder="e.g., A photorealistic image of a vintage robot reading a book in a cozy library, cinematic lighting",
|
| 61 |
+
value="A majestic portrait of a futuristic cyborg cat, digital art, highly detailed",
|
| 62 |
+
lines=3
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
negative_prompt = gr.Textbox(
|
| 66 |
+
label="Negative Prompt (Optional)",
|
| 67 |
+
placeholder="e.g., blurry, low quality, distorted, bad anatomy, text",
|
| 68 |
+
lines=2
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
model_selector = gr.Radio(
|
| 72 |
+
label="Select Model",
|
| 73 |
+
choices=list(MODEL_CHOICES.keys()),
|
| 74 |
+
value=DEFAULT_MODEL_KEY,
|
| 75 |
+
interactive=True
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
with gr.Accordion("Generation Parameters", open=True):
|
| 79 |
+
steps = gr.Slider(
|
| 80 |
+
minimum=10, maximum=100, step=5, value=50, label="Inference Steps"
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
guidance_scale = gr.Slider(
|
| 84 |
+
minimum=1.0, maximum=15.0, step=0.5, value=7.5, label="Guidance Scale (CFG)"
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
num_images = gr.Slider(
|
| 88 |
+
minimum=1, maximum=4, step=1, value=4, label="Number of Images to Generate",
|
| 89 |
+
info="Uploads up to 4 images."
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
generate_btn = gr.Button("Generate Images", variant="primary")
|
| 93 |
+
|
| 94 |
+
with gr.Column(scale=3):
|
| 95 |
+
# --- Outputs ---
|
| 96 |
+
output_gallery = gr.Gallery(
|
| 97 |
+
label="Generated Images (512x512)",
|
| 98 |
+
columns=2,
|
| 99 |
+
rows=2,
|
| 100 |
+
height="auto",
|
| 101 |
+
preview=True
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
gr.Examples(
|
| 105 |
+
examples=[
|
| 106 |
+
["A hyper-detailed oil painting of a lighthouse during a storm", "ugly, blurry, low resolution", DEFAULT_MODEL_KEY],
|
| 107 |
+
["", "text, watermark, poor rendering", "Stable Diffusion 2.1 Base (Manojb)"], # Empty prompt example
|
| 108 |
+
["A 1950s sci-fi movie poster featuring a giant insect attacking Paris", "modern, photorealistic, color photo", DEFAULT_MODEL_KEY]
|
| 109 |
+
],
|
| 110 |
+
inputs=[prompt, negative_prompt, model_selector],
|
| 111 |
+
label="Examples (Note the second example uses an empty prompt)"
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
# --- Logic Binding ---
|
| 115 |
+
generate_btn.click(
|
| 116 |
+
fn=process_generation,
|
| 117 |
+
inputs=[prompt, negative_prompt, model_selector, steps, guidance_scale, num_images],
|
| 118 |
+
outputs=[output_gallery]
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
# Run initial generation on load to populate the gallery immediately
|
| 122 |
+
demo.load(
|
| 123 |
+
fn=process_generation,
|
| 124 |
+
inputs=[prompt, negative_prompt, model_selector, steps, guidance_scale, num_images],
|
| 125 |
+
outputs=[output_gallery]
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
if __name__ == "__main__":
|
| 129 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
requests
|
| 3 |
+
Pillow
|
| 4 |
+
numpy
|