Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 🤖✨ CPU Image Generator (Stable Diffusion)
|
| 2 |
+
|
| 3 |
+
import torch
|
| 4 |
+
from diffusers import StableDiffusionPipeline
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
# Load model once at startup (≈3.4 GB; fits HF Space free tier CPU)
|
| 8 |
+
PIPELINE_ID = "runwayml/stable-diffusion-v1-5"
|
| 9 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
| 10 |
+
PIPELINE_ID,
|
| 11 |
+
torch_dtype=torch.float32,
|
| 12 |
+
safety_checker=None, # skip NSFW checker
|
| 13 |
+
)
|
| 14 |
+
pipe = pipe.to("cpu")
|
| 15 |
+
|
| 16 |
+
def generate_image(prompt: str, steps: int):
|
| 17 |
+
if not prompt:
|
| 18 |
+
return None
|
| 19 |
+
image = pipe(prompt, num_inference_steps=steps).images[0]
|
| 20 |
+
return image
|
| 21 |
+
|
| 22 |
+
with gr.Blocks(title="🤖✨ AI Image Generator (CPU)") as demo:
|
| 23 |
+
gr.Markdown(
|
| 24 |
+
"# 🤖✨ AI Image Generator\n"
|
| 25 |
+
"Enter a creative prompt, adjust inference steps, and generate a unique image—**100% CPU**."
|
| 26 |
+
)
|
| 27 |
+
with gr.Row():
|
| 28 |
+
prompt_in = gr.Textbox(
|
| 29 |
+
label="Prompt",
|
| 30 |
+
placeholder="e.g. A photorealistic portrait of a cyberpunk fox"
|
| 31 |
+
)
|
| 32 |
+
steps_in = gr.Slider(
|
| 33 |
+
minimum=1, maximum=50, value=25, step=1, label="Inference Steps"
|
| 34 |
+
)
|
| 35 |
+
run_btn = gr.Button("Generate 🖼️", variant="primary")
|
| 36 |
+
img_out = gr.Image(label="Generated Image")
|
| 37 |
+
|
| 38 |
+
run_btn.click(generate_image, [prompt_in, steps_in], img_out)
|
| 39 |
+
|
| 40 |
+
if __name__ == "__main__":
|
| 41 |
+
demo.launch(server_name="0.0.0.0")
|