gouravbhadraDev commited on
Commit
65cc444
·
verified ·
1 Parent(s): a8d9a73

Upload ghibli2.py

Browse files
Files changed (1) hide show
  1. ghibli2.py +61 -0
ghibli2.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image as PILImage
3
+ import torch
4
+ from io import BytesIO
5
+ from diffusers import StableDiffusionImg2ImgPipeline
6
+
7
+ # Function to load the Stable Diffusion model
8
+ def load_model():
9
+ model_id = "stabilityai/stable-diffusion-2-1"
10
+ device = "mps" if torch.backends.mps.is_available() else "cpu"
11
+ pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
12
+ model_id, torch_dtype=torch.float32, safety_checker=None
13
+ ).to(device)
14
+ return pipe, device
15
+
16
+ # Function to generate the transformed image
17
+ def generate_image(pipe, device, init_image, prompt, strength, guidance_scale):
18
+ init_image = init_image.convert("RGB").resize((512, 512))
19
+ generator = torch.manual_seed(42)
20
+ output_image = pipe(
21
+ prompt=prompt,
22
+ image=init_image,
23
+ strength=strength,
24
+ guidance_scale=guidance_scale,
25
+ generator=generator
26
+ ).images[0]
27
+ return output_image
28
+
29
+ # Streamlit App
30
+ def main():
31
+ st.title("Stable Diffusion Image Transformer")
32
+
33
+ # Load the model
34
+ pipe, device = load_model()
35
+
36
+ # Image Upload
37
+ uploaded_file = st.file_uploader("Choose an image...", type=["png", "jpg", "jpeg"])
38
+ if uploaded_file is not None:
39
+ init_image = PILImage.open(uploaded_file)
40
+ st.image(init_image, caption="Uploaded Image", use_column_width=True)
41
+
42
+ # Prompt Input
43
+ prompt = st.text_input("Enter transformation prompt:")
44
+
45
+ # Sliders for Strength and Guidance Scale
46
+ strength = st.slider("Select strength:", 0.1, 1.0, 0.5, 0.1)
47
+ guidance_scale = st.slider("Select guidance scale:", 1.0, 10.0, 7.5, 0.5)
48
+
49
+ # Generate Image Button
50
+ if st.button("Generate Image"):
51
+ if prompt:
52
+ with st.spinner("Generating image..."):
53
+ transformed_image = generate_image(
54
+ pipe, device, init_image, prompt, strength, guidance_scale
55
+ )
56
+ st.image(transformed_image, caption="Transformed Image", use_column_width=True)
57
+ else:
58
+ st.error("Please enter a transformation prompt.")
59
+
60
+ if __name__ == "__main__":
61
+ main()