File size: 2,825 Bytes
2e257ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import streamlit as st
from diffusers import StableDiffusionImg2ImgPipeline
import torch
from PIL import Image
import os

# Inisialisasi pipeline model
@st.cache_resource
def load_model():
    model = StableDiffusionImg2ImgPipeline.from_pretrained(
        "runwayml/stable-diffusion-v1-5", 
        torch_dtype=torch.float16
    )
    model.to("cuda")  # Gunakan GPU untuk performa optimal
    return model

# Header aplikasi
st.title("Stable Diffusion Image-to-Image Generator")
st.write("🎨 Unggah gambar dan tambahkan deskripsi untuk menghasilkan gambar baru!")

# Input teks dari pengguna
prompt = st.text_input("Masukkan teks deskripsi gambar:", "")
negative_prompt = st.text_input("Masukkan teks untuk menghindari elemen tertentu (Negative Prompt):", "")

# Opsi parameter tambahan
guidance_scale = st.slider("Guidance Scale (Semakin tinggi, semakin spesifik gambar)", 1.0, 20.0, 7.5)
strength = st.slider("Strength (Pengaruh gambar asli, 0=mirip, 1=berbeda)", 0.1, 1.0, 0.75)
steps = st.slider("Jumlah Langkah Inferensi", 10, 50, 30)

# Input gambar dari pengguna
uploaded_file = st.file_uploader("Unggah gambar awal (Input Image)", type=["jpg", "jpeg", "png"])

# Tombol untuk menghasilkan gambar
if st.button("Generate") and prompt and uploaded_file:
    with st.spinner("Sedang menghasilkan gambar..."):
        # Load model
        model = load_model()
        
        # Buka dan proses gambar
        init_image = Image.open(uploaded_file).convert("RGB")
        init_image = init_image.resize((512, 512))  # Sesuaikan ukuran gambar ke 512x512
        
        # Proses image-to-image
        image = model(
            prompt=prompt,
            negative_prompt=negative_prompt if negative_prompt else None,
            image=init_image,
            strength=strength,
            guidance_scale=guidance_scale,
            num_inference_steps=steps
        ).images[0]  # Ambil gambar pertama dari hasil

        # Tampilkan hasil gambar
        st.image(image, caption="Gambar yang dihasilkan", use_column_width=True)

        # Simpan gambar ke folder assets
        if not os.path.exists("assets/"):
            os.makedirs("assets/")
        save_path = f"assets/{prompt.replace(' ', '_')}_neg_{negative_prompt.replace(' ', '_') if negative_prompt else 'none'}.png"
        image.save(save_path)
        st.success(f"Gambar telah disimpan di {save_path}")
else:
    if not prompt:
        st.warning("Masukkan deskripsi untuk memulai.")
    if not uploaded_file:
        st.warning("Unggah gambar untuk memulai.")

# Tampilan gambar sebelumnya
st.subheader("Gambar Sebelumnya")
if os.path.exists("assets/"):
    for img_file in os.listdir("assets/"):
        st.image(f"assets/{img_file}", caption=img_file, use_column_width=True)