Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,25 @@
|
|
| 1 |
|
| 2 |
-
import torch
|
| 3 |
-
from diffusers import StableDiffusionPipeline
|
| 4 |
import gradio as gr
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
# Load
|
| 7 |
-
model_id = "
|
| 8 |
-
|
| 9 |
-
pipe =
|
| 10 |
-
model_id,
|
| 11 |
-
torch_dtype=torch.float32
|
| 12 |
-
).to("cpu")
|
| 13 |
-
|
| 14 |
-
# CPU-friendly optimization
|
| 15 |
-
pipe.enable_attention_slicing()
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
num_inference_steps=12, # keep low for speed
|
| 21 |
-
guidance_scale=7, # balanced CFG
|
| 22 |
-
width=384, # smaller resolution
|
| 23 |
-
height=384
|
| 24 |
-
).images[0]
|
| 25 |
return image
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
btn.click(fn=generate, inputs=prompt, outputs=output)
|
| 35 |
|
| 36 |
-
|
|
|
|
|
|
| 1 |
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
+
from diffusers import StableDiffusionPipeline
|
| 4 |
+
import torch
|
| 5 |
|
| 6 |
+
# Load model once
|
| 7 |
+
model_id = "runwayml/stable-diffusion-v1-5"
|
| 8 |
+
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float32)
|
| 9 |
+
pipe = pipe.to("cpu") # force CPU to run free on Spaces
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# Simple function for text-to-image
|
| 12 |
+
def generate_image(prompt: str):
|
| 13 |
+
image = pipe(prompt, num_inference_steps=20, guidance_scale=7.5).images[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
return image
|
| 15 |
|
| 16 |
+
# API-only mode, no full UI
|
| 17 |
+
demo = gr.Interface(
|
| 18 |
+
fn=generate_image,
|
| 19 |
+
inputs=gr.Textbox(label="Enter your prompt"),
|
| 20 |
+
outputs=gr.Image(type="pil"),
|
| 21 |
+
live=False,
|
| 22 |
+
)
|
|
|
|
| 23 |
|
| 24 |
+
# Make it lightweight & responsive (minimal layout)
|
| 25 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, show_api=True)
|