Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from main import StableDiffusionEngine
|
| 4 |
+
|
| 5 |
+
# Load Hugging Face token from environment
|
| 6 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 7 |
+
|
| 8 |
+
# Initialize Stable Diffusion engine
|
| 9 |
+
sd_engine = StableDiffusionEngine(hf_token=HF_TOKEN)
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def generate(prompt, steps, cfg, width, height):
|
| 13 |
+
"""
|
| 14 |
+
Wrapper function for Gradio UI.
|
| 15 |
+
"""
|
| 16 |
+
return sd_engine.generate_image(
|
| 17 |
+
prompt=prompt,
|
| 18 |
+
num_inference_steps=steps,
|
| 19 |
+
guidance_scale=cfg,
|
| 20 |
+
width=width,
|
| 21 |
+
height=height,
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
examples = [
|
| 26 |
+
"A futuristic African city at sunset, ultra realistic, cinematic lighting",
|
| 27 |
+
"A microscopic view of malaria parasites, scientific illustration",
|
| 28 |
+
"A humanoid AI researcher working in a high-tech laboratory",
|
| 29 |
+
]
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="Stable Diffusion Research Lab") as demo:
|
| 33 |
+
gr.Markdown(
|
| 34 |
+
"""
|
| 35 |
+
# 🧠 Stable Diffusion Research Lab
|
| 36 |
+
**Text-to-Image Generation using SDXL**
|
| 37 |
+
|
| 38 |
+
This app is designed for **research and experimentation** with Stable Diffusion models.
|
| 39 |
+
"""
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
with gr.Row():
|
| 43 |
+
with gr.Column(scale=1):
|
| 44 |
+
prompt = gr.Textbox(
|
| 45 |
+
label="Prompt",
|
| 46 |
+
placeholder="Describe the image you want to generate...",
|
| 47 |
+
lines=4,
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
steps = gr.Slider(
|
| 51 |
+
minimum=10,
|
| 52 |
+
maximum=60,
|
| 53 |
+
value=30,
|
| 54 |
+
step=1,
|
| 55 |
+
label="Inference Steps",
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
cfg = gr.Slider(
|
| 59 |
+
minimum=1.0,
|
| 60 |
+
maximum=15.0,
|
| 61 |
+
value=7.5,
|
| 62 |
+
step=0.5,
|
| 63 |
+
label="Guidance Scale (CFG)",
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
width = gr.Dropdown(
|
| 67 |
+
choices=[512, 768, 1024],
|
| 68 |
+
value=1024,
|
| 69 |
+
label="Image Width",
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
height = gr.Dropdown(
|
| 73 |
+
choices=[512, 768, 1024],
|
| 74 |
+
value=1024,
|
| 75 |
+
label="Image Height",
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
generate_btn = gr.Button("🚀 Generate Image", variant="primary")
|
| 79 |
+
|
| 80 |
+
with gr.Column(scale=1):
|
| 81 |
+
output = gr.Image(
|
| 82 |
+
label="Generated Image",
|
| 83 |
+
type="pil",
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
gr.Examples(examples=examples, inputs=prompt)
|
| 87 |
+
|
| 88 |
+
generate_btn.click(
|
| 89 |
+
fn=generate,
|
| 90 |
+
inputs=[prompt, steps, cfg, width, height],
|
| 91 |
+
outputs=output,
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
demo.launch(share=True)
|