Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from diffusers import StableDiffusionImg2ImgPipeline
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
# Load model (CPU mode)
|
| 7 |
+
model_id = "runwayml/stable-diffusion-v1-5"
|
| 8 |
+
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(model_id, torch_dtype=torch.float32)
|
| 9 |
+
pipe = pipe.to("cpu")
|
| 10 |
+
|
| 11 |
+
def generate_image(init_image, prompt, strength, guidance_scale, steps):
|
| 12 |
+
if init_image is None:
|
| 13 |
+
return None
|
| 14 |
+
|
| 15 |
+
init_image = init_image.convert("RGB")
|
| 16 |
+
init_image = init_image.resize((512, 512))
|
| 17 |
+
|
| 18 |
+
images = pipe(
|
| 19 |
+
prompt=prompt,
|
| 20 |
+
image=init_image,
|
| 21 |
+
strength=strength,
|
| 22 |
+
guidance_scale=guidance_scale,
|
| 23 |
+
num_inference_steps=steps
|
| 24 |
+
).images
|
| 25 |
+
|
| 26 |
+
return images[0]
|
| 27 |
+
|
| 28 |
+
# Gradio UI
|
| 29 |
+
with gr.Blocks() as demo:
|
| 30 |
+
gr.Markdown("## 🖼️ CPU Image-to-Image Generator")
|
| 31 |
+
|
| 32 |
+
with gr.Row():
|
| 33 |
+
with gr.Column():
|
| 34 |
+
init_image = gr.Image(label="Upload Gambar", type="pil")
|
| 35 |
+
prompt = gr.Textbox(label="Prompt", placeholder="Deskripsi gambar...")
|
| 36 |
+
strength = gr.Slider(0.1, 1.0, value=0.6, step=0.05, label="Denoising Strength")
|
| 37 |
+
guidance_scale = gr.Slider(1, 20, value=7.5, step=0.5, label="CFG Scale")
|
| 38 |
+
steps = gr.Slider(10, 50, value=25, step=1, label="Steps")
|
| 39 |
+
btn = gr.Button("Generate")
|
| 40 |
+
with gr.Column():
|
| 41 |
+
output_image = gr.Image(label="Hasil", type="pil")
|
| 42 |
+
|
| 43 |
+
btn.click(
|
| 44 |
+
fn=generate_image,
|
| 45 |
+
inputs=[init_image, prompt, strength, guidance_scale, steps],
|
| 46 |
+
outputs=[output_image]
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
demo.launch()
|