Juned24 commited on
Commit
e7247b2
·
verified ·
1 Parent(s): 06517ea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -29
app.py CHANGED
@@ -1,29 +1,48 @@
1
- import streamlit as st
2
- from PIL import Image
3
- from diffusers import StableDiffusionImg2ImgPipeline
4
- import torch
5
-
6
- # Load Model
7
- @st.cache_resource
8
- def load_model():
9
- model_id = "runwayml/stable-diffusion-v1-5"
10
- pipe = StableDiffusionImg2ImgPipeline.from_pretrained(model_id, torch_dtype=torch.float16).to("cuda")
11
- return pipe
12
-
13
- pipe = load_model()
14
-
15
- # Streamlit UI
16
- st.title("Image-to-Image with Prompt")
17
- uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png"])
18
-
19
- if uploaded_file:
20
- input_image = Image.open(uploaded_file).convert("RGB")
21
- st.image(input_image, caption="Uploaded Image", use_column_width=True)
22
-
23
- prompt = st.text_input("Enter your prompt:")
24
- strength = st.slider("Strength (0.1-1.0)", 0.1, 1.0, 0.5)
25
-
26
- if st.button("Generate"):
27
- with st.spinner("Processing..."):
28
- result = pipe(prompt=prompt, image=input_image, strength=strength, guidance_scale=7.5).images[0]
29
- st.image(result, caption="Generated Image", use_column_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from diffusers import StableDiffusionImg2ImgPipeline
3
+ from PIL import Image
4
+ import torch
5
+
6
+ # Konfigurasi halaman Streamlit
7
+ st.set_page_config(page_title="Image-to-Image Generator", layout="wide")
8
+
9
+ # Load model
10
+ @st.cache_resource
11
+ def load_pipeline():
12
+ model_id = "runwayml/stable-diffusion-v1-5"
13
+ pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
14
+ model_id,
15
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
16
+ )
17
+ pipe.to("cuda" if torch.cuda.is_available() else "cpu")
18
+ return pipe
19
+
20
+ pipe = load_pipeline()
21
+
22
+ # Antarmuka pengguna
23
+ st.title("🖼️ Image-to-Image Generator with Stable Diffusion")
24
+ st.markdown("Upload an image and provide a text prompt to modify the image using Stable Diffusion.")
25
+
26
+ # Upload gambar
27
+ uploaded_image = st.file_uploader("Upload your image", type=["jpg", "jpeg", "png"])
28
+ prompt = st.text_input("Enter your prompt", "a futuristic cityscape at sunset")
29
+
30
+ strength = st.slider("Strength of image transformation", 0.1, 1.0, 0.75, step=0.05)
31
+ guidance_scale = st.slider("Guidance scale", 1.0, 20.0, 7.5, step=0.5)
32
+
33
+ if uploaded_image is not None:
34
+ # Tampilkan gambar yang diunggah
35
+ input_image = Image.open(uploaded_image).convert("RGB")
36
+ st.image(input_image, caption="Uploaded Image", use_column_width=True)
37
+
38
+ # Proses gambar
39
+ if st.button("Generate"):
40
+ with st.spinner("Generating image..."):
41
+ result = pipe(prompt=prompt, image=input_image, strength=strength, guidance_scale=guidance_scale)
42
+ output_image = result.images[0]
43
+ st.image(output_image, caption="Generated Image", use_column_width=True)
44
+ st.success("Image generated successfully!")
45
+
46
+ # Footer
47
+ st.markdown("---")
48
+ st.markdown("Made with ❤️ using [Hugging Face Diffusers](https://huggingface.co/docs/diffusers).")