update app.py
Browse files
app.py
CHANGED
|
@@ -2,19 +2,66 @@ import gradio as gr
|
|
| 2 |
from diffusers import StableDiffusionPipeline
|
| 3 |
import torch
|
| 4 |
|
|
|
|
| 5 |
pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16, safety_checker=None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
pipe = pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
)
|
| 19 |
|
| 20 |
-
demo.launch()
|
|
|
|
| 2 |
from diffusers import StableDiffusionPipeline
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
# Load optimized model (float16 for speed and memory — from your efficiency course)
|
| 6 |
pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16, safety_checker=None)
|
| 7 |
+
|
| 8 |
+
# Optimizations for free GPU (key to avoid long processing)
|
| 9 |
+
pipe.enable_vae_slicing()
|
| 10 |
+
pipe.enable_attention_slicing()
|
| 11 |
+
pipe.enable_sequential_cpu_offload() # Crucial for low memory
|
| 12 |
pipe = pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 13 |
|
| 14 |
+
# Generation function
|
| 15 |
+
def generate_image(prompt, negative_prompt="", num_steps=25, seed=42):
|
| 16 |
+
# Negative prompt for better quality
|
| 17 |
+
negative = negative_prompt or "blurry, low quality, ugly, deformed, bad anatomy"
|
| 18 |
+
|
| 19 |
+
# Seed for reproducibility
|
| 20 |
+
generator = torch.Generator("cuda").manual_seed(seed) if torch.cuda.is_available() else None
|
| 21 |
+
|
| 22 |
+
# Generation (25 steps — fast and good quality)
|
| 23 |
+
result = pipe(
|
| 24 |
+
prompt,
|
| 25 |
+
negative_prompt=negative,
|
| 26 |
+
num_inference_steps=num_steps,
|
| 27 |
+
generator=generator
|
| 28 |
+
).images[0]
|
| 29 |
+
|
| 30 |
+
return result
|
| 31 |
+
|
| 32 |
+
# Gradio interface (beautiful and user-friendly)
|
| 33 |
+
with gr.Blocks(title="Epic AI Art Generator 🔥") as demo:
|
| 34 |
+
gr.Markdown("# Epic AI Art Generator")
|
| 35 |
+
gr.Markdown("Made with ❤️ for my amazing girl. Generate our adventures in any style! Попробуй: 'рыжий кот в космосе' или 'романтическая пара на закате'")
|
| 36 |
+
|
| 37 |
+
with gr.Row():
|
| 38 |
+
with gr.Column(scale=1):
|
| 39 |
+
prompt_input = gr.Textbox(
|
| 40 |
+
label="Описание (prompt на русском или английском)",
|
| 41 |
+
placeholder="e.g. 'милый рыжий кот космонавт' or 'beautiful couple at sunset beach'"
|
| 42 |
+
)
|
| 43 |
+
negative_input = gr.Textbox(
|
| 44 |
+
label="Negative prompt (что избегать)",
|
| 45 |
+
placeholder="blurry, low quality, ugly"
|
| 46 |
+
)
|
| 47 |
+
steps_slider = gr.Slider(15, 50, value=25, step=5, label="Шаги (steps) — больше = лучше качество, но дольше")
|
| 48 |
+
seed_slider = gr.Slider(0, 999999999, value=42, step=1, label="Seed (для повторяемости)")
|
| 49 |
+
|
| 50 |
+
generate_btn = gr.Button("Generate Magic 🔥", variant="primary")
|
| 51 |
+
|
| 52 |
+
with gr.Column(scale=1):
|
| 53 |
+
output_image = gr.Image(label="Твоё искусство")
|
| 54 |
+
|
| 55 |
+
gr.Markdown("### Примеры промптов для нас:")
|
| 56 |
+
gr.Markdown("- 'романтическая пара на пляже на закате, реалистично'")
|
| 57 |
+
gr.Markdown("- 'мы как супергерои в городе'")
|
| 58 |
+
gr.Markdown("- 'рыжий кот в фэнтези замке'")
|
| 59 |
|
| 60 |
+
# Action
|
| 61 |
+
generate_btn.click(
|
| 62 |
+
fn=generate_image,
|
| 63 |
+
inputs=[prompt_input, negative_input, steps_slider, seed_slider],
|
| 64 |
+
outputs=output_image
|
| 65 |
+
)
|
|
|
|
| 66 |
|
| 67 |
+
demo.launch(debug=True) # Debug для логов, если что
|