Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,40 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
|
| 3 |
import torch
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
# Load
|
| 10 |
pipe = StableDiffusionPipeline.from_pretrained(
|
| 11 |
-
|
| 12 |
-
torch_dtype=
|
| 13 |
-
)
|
| 14 |
-
|
| 15 |
-
# ✅ Move to device
|
| 16 |
-
pipe = pipe.to(device)
|
| 17 |
|
| 18 |
-
#
|
| 19 |
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
|
| 20 |
|
| 21 |
-
#
|
| 22 |
def generate_image(prompt):
|
| 23 |
-
enhanced_prompt = f"{prompt},
|
| 24 |
-
|
| 25 |
-
return
|
| 26 |
|
| 27 |
-
# Gradio
|
| 28 |
demo = gr.Interface(
|
| 29 |
fn=generate_image,
|
| 30 |
-
inputs=gr.Textbox(
|
|
|
|
|
|
|
|
|
|
| 31 |
outputs=gr.Image(type="pil"),
|
| 32 |
-
title="Text
|
| 33 |
-
description="
|
| 34 |
)
|
| 35 |
|
|
|
|
| 36 |
if __name__ == "__main__":
|
| 37 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import torch
|
| 3 |
+
from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
|
| 4 |
|
| 5 |
+
# ---- Configuration ---- #
|
| 6 |
+
MODEL_ID = "runwayml/stable-diffusion-v1-5"
|
| 7 |
+
USE_CUDA = torch.cuda.is_available()
|
| 8 |
+
DEVICE = "cuda" if USE_CUDA else "cpu"
|
| 9 |
+
DTYPE = torch.float16 if USE_CUDA else torch.float32
|
| 10 |
|
| 11 |
+
# ---- Load Stable Diffusion Pipeline ---- #
|
| 12 |
pipe = StableDiffusionPipeline.from_pretrained(
|
| 13 |
+
MODEL_ID,
|
| 14 |
+
torch_dtype=DTYPE
|
| 15 |
+
).to(DEVICE)
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
# ---- Swap the Scheduler for Better Quality ---- #
|
| 18 |
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
|
| 19 |
|
| 20 |
+
# ---- Image Generation Function ---- #
|
| 21 |
def generate_image(prompt):
|
| 22 |
+
enhanced_prompt = f"{prompt}, ultra realistic, high detail, 8k resolution, DSLR photography, natural lighting"
|
| 23 |
+
result = pipe(enhanced_prompt)
|
| 24 |
+
return result.images[0]
|
| 25 |
|
| 26 |
+
# ---- Gradio Interface ---- #
|
| 27 |
demo = gr.Interface(
|
| 28 |
fn=generate_image,
|
| 29 |
+
inputs=gr.Textbox(
|
| 30 |
+
label="Enter image description",
|
| 31 |
+
placeholder="e.g. A cozy cabin in a snowy forest"
|
| 32 |
+
),
|
| 33 |
outputs=gr.Image(type="pil"),
|
| 34 |
+
title="🖼️ Realistic Text-to-Image Generator",
|
| 35 |
+
description="Generate high-quality, photorealistic images using Stable Diffusion v1.5 + DPM Scheduler"
|
| 36 |
)
|
| 37 |
|
| 38 |
+
# ---- Launch App ---- #
|
| 39 |
if __name__ == "__main__":
|
| 40 |
demo.launch()
|