Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from diffusers import StableDiffusionPipeline
|
| 4 |
+
|
| 5 |
+
model_id = "runwayml/stable-diffusion-v1-5"
|
| 6 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 7 |
+
|
| 8 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
| 9 |
+
model_id,
|
| 10 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32
|
| 11 |
+
)
|
| 12 |
+
pipe = pipe.to(device)
|
| 13 |
+
|
| 14 |
+
styles = {
|
| 15 |
+
"Realistic": "highly detailed, realistic, 4k",
|
| 16 |
+
"Cartoon": "cartoon style, colorful",
|
| 17 |
+
"Anime": "anime style",
|
| 18 |
+
"Sketch": "pencil sketch"
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
def generate_image(prompt, style, steps, guidance, seed):
|
| 22 |
+
if not prompt.strip():
|
| 23 |
+
return None, "⚠️ Please enter a prompt."
|
| 24 |
+
|
| 25 |
+
styled_prompt = prompt + ", " + styles.get(style, "")
|
| 26 |
+
generator = torch.manual_seed(int(seed)) if seed != 0 else None
|
| 27 |
+
|
| 28 |
+
image = pipe(
|
| 29 |
+
styled_prompt,
|
| 30 |
+
num_inference_steps=int(steps),
|
| 31 |
+
guidance_scale=float(guidance),
|
| 32 |
+
generator=generator
|
| 33 |
+
).images[0]
|
| 34 |
+
|
| 35 |
+
return image, "✅ Done"
|
| 36 |
+
|
| 37 |
+
with gr.Blocks() as app:
|
| 38 |
+
gr.Markdown("# 🎨 Text-to-Image Generator")
|
| 39 |
+
gr.Markdown("Powered by Stable Diffusion")
|
| 40 |
+
|
| 41 |
+
prompt = gr.Textbox(label="Prompt")
|
| 42 |
+
style = gr.Dropdown(["Realistic", "Cartoon", "Anime", "Sketch"], value="Realistic")
|
| 43 |
+
|
| 44 |
+
steps = gr.Slider(10, 50, value=25)
|
| 45 |
+
guidance = gr.Slider(5.0, 15.0, value=7.5)
|
| 46 |
+
seed = gr.Number(value=0)
|
| 47 |
+
|
| 48 |
+
btn = gr.Button("Generate")
|
| 49 |
+
|
| 50 |
+
image = gr.Image()
|
| 51 |
+
status = gr.Textbox()
|
| 52 |
+
|
| 53 |
+
btn.click(generate_image, [prompt, style, steps, guidance, seed], [image, status])
|
| 54 |
+
|
| 55 |
+
app.launch()
|