Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,42 +1,51 @@
|
|
| 1 |
# -*- coding: utf-8 -*-
|
| 2 |
"""
|
| 3 |
-
Gradio Space: Text → Image
|
| 4 |
UI designed by Mehak Mazhar
|
| 5 |
"""
|
| 6 |
|
| 7 |
import os
|
| 8 |
-
import
|
| 9 |
-
import
|
| 10 |
-
from PIL import Image
|
| 11 |
import gradio as gr
|
| 12 |
|
| 13 |
-
# ---
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
# ---
|
| 18 |
-
|
| 19 |
-
if not token:
|
| 20 |
-
return None, "❌ Please provide a Hugging Face API token."
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
|
|
|
|
|
|
|
| 34 |
try:
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
image = Image.open(io.BytesIO(response.content))
|
| 39 |
-
return image, "✅ Image generated successfully!"
|
| 40 |
except Exception as e:
|
| 41 |
return None, f"⚠️ Error: {str(e)}"
|
| 42 |
|
|
@@ -52,7 +61,11 @@ with gr.Blocks(css=css, title="Stable Diffusion Text-to-Image") as demo:
|
|
| 52 |
with gr.Row():
|
| 53 |
with gr.Column():
|
| 54 |
prompt = gr.Textbox(label="Prompt", placeholder="A futuristic city at night", lines=3)
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
width = gr.Dropdown([256, 384, 512, 768, 1024], value=512, label="Width")
|
| 57 |
height = gr.Dropdown([256, 384, 512, 768, 1024], value=512, label="Height")
|
| 58 |
guidance = gr.Slider(1.0, 15.0, value=7.5, step=0.1, label="Guidance Scale")
|
|
@@ -60,14 +73,14 @@ with gr.Blocks(css=css, title="Stable Diffusion Text-to-Image") as demo:
|
|
| 60 |
generate_btn = gr.Button("Generate Image", variant="primary")
|
| 61 |
|
| 62 |
with gr.Column():
|
| 63 |
-
output_image = gr.Image(label="Generated Image")
|
| 64 |
status = gr.Textbox(label="Status", interactive=False)
|
| 65 |
|
| 66 |
generate_btn.click(
|
| 67 |
fn=generate_image,
|
| 68 |
-
inputs=[prompt,
|
| 69 |
outputs=[output_image, status]
|
| 70 |
)
|
| 71 |
|
| 72 |
if __name__ == "__main__":
|
| 73 |
-
demo.launch(server_name="0.0.0.0"
|
|
|
|
| 1 |
# -*- coding: utf-8 -*-
|
| 2 |
"""
|
| 3 |
+
Gradio Space: Text → Image (Diffusers Pipeline)
|
| 4 |
UI designed by Mehak Mazhar
|
| 5 |
"""
|
| 6 |
|
| 7 |
import os
|
| 8 |
+
import torch
|
| 9 |
+
from diffusers import StableDiffusionPipeline
|
|
|
|
| 10 |
import gradio as gr
|
| 11 |
|
| 12 |
+
# --- Available models ---
|
| 13 |
+
MODEL_CHOICES = {
|
| 14 |
+
"Dreamlike Diffusion 1.0": "dreamlike-art/dreamlike-diffusion-1.0",
|
| 15 |
+
"Stable Diffusion XL Base": "stabilityai/stable-diffusion-xl-base-1.0"
|
| 16 |
+
}
|
| 17 |
|
| 18 |
+
# --- Cache pipelines to avoid reloading ---
|
| 19 |
+
loaded_pipelines = {}
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
def get_pipeline(model_id):
|
| 22 |
+
"""Load pipeline if not cached"""
|
| 23 |
+
if model_id not in loaded_pipelines:
|
| 24 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
| 25 |
+
model_id,
|
| 26 |
+
torch_dtype=torch.float16,
|
| 27 |
+
use_safetensors=True
|
| 28 |
+
)
|
| 29 |
+
pipe = pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 30 |
+
loaded_pipelines[model_id] = pipe
|
| 31 |
+
return loaded_pipelines[model_id]
|
| 32 |
|
| 33 |
+
# --- Image generation function ---
|
| 34 |
+
def generate_image(prompt, model_choice, width, height, guidance_scale, steps):
|
| 35 |
try:
|
| 36 |
+
model_id = MODEL_CHOICES[model_choice]
|
| 37 |
+
pipe = get_pipeline(model_id)
|
| 38 |
+
|
| 39 |
+
image = pipe(
|
| 40 |
+
prompt,
|
| 41 |
+
width=int(width),
|
| 42 |
+
height=int(height),
|
| 43 |
+
guidance_scale=float(guidance_scale),
|
| 44 |
+
num_inference_steps=int(steps)
|
| 45 |
+
).images[0]
|
| 46 |
+
|
| 47 |
+
return image, f"✅ Generated with {model_choice}"
|
| 48 |
|
|
|
|
|
|
|
| 49 |
except Exception as e:
|
| 50 |
return None, f"⚠️ Error: {str(e)}"
|
| 51 |
|
|
|
|
| 61 |
with gr.Row():
|
| 62 |
with gr.Column():
|
| 63 |
prompt = gr.Textbox(label="Prompt", placeholder="A futuristic city at night", lines=3)
|
| 64 |
+
model_choice = gr.Dropdown(
|
| 65 |
+
list(MODEL_CHOICES.keys()),
|
| 66 |
+
value="Dreamlike Diffusion 1.0",
|
| 67 |
+
label="Choose Model"
|
| 68 |
+
)
|
| 69 |
width = gr.Dropdown([256, 384, 512, 768, 1024], value=512, label="Width")
|
| 70 |
height = gr.Dropdown([256, 384, 512, 768, 1024], value=512, label="Height")
|
| 71 |
guidance = gr.Slider(1.0, 15.0, value=7.5, step=0.1, label="Guidance Scale")
|
|
|
|
| 73 |
generate_btn = gr.Button("Generate Image", variant="primary")
|
| 74 |
|
| 75 |
with gr.Column():
|
| 76 |
+
output_image = gr.Image(label="Generated Image", type="pil")
|
| 77 |
status = gr.Textbox(label="Status", interactive=False)
|
| 78 |
|
| 79 |
generate_btn.click(
|
| 80 |
fn=generate_image,
|
| 81 |
+
inputs=[prompt, model_choice, width, height, guidance, steps],
|
| 82 |
outputs=[output_image, status]
|
| 83 |
)
|
| 84 |
|
| 85 |
if __name__ == "__main__":
|
| 86 |
+
demo.launch(server_name="0.0.0.0")
|