File size: 2,264 Bytes
4598bf1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import cloudinary
import cloudinary.uploader
from cloudinary.utils import cloudinary_url
import time  # Import time for simulating progress

# Cloudinary Configuration
cloudinary.config(
    cloud_name='detwd9o8x',  # Replace with your Cloudinary cloud name
    api_key='439456689456765',  # Replace with your API key
    api_secret='tk4Ho9pJzTaTbtIcJli6JlXQkCA',  # Replace with your API secret
    secure=True
)

# Streamlit App
st.title("Generative Background Replacement with Cloudinary")
st.write("Upload an image to transform the background using Cloudinary's generative AI.")

# Image Upload
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])

# Prompt Input and Automatic Transformation on Enter
if uploaded_file:
    st.success("Image uploaded successfully!")

    # Prompt Input appears only after successful upload
    prompt = st.text_input("Enter a background replacement prompt", value="", key="prompt")

    if prompt:
        # Save uploaded image to Cloudinary
        with st.spinner("Uploading image to Cloudinary..."):
            response = cloudinary.uploader.upload(uploaded_file, folder="streamlit_app")
            public_id = response["public_id"]

        # Apply Generative Background Replace Transformation
        st.spinner("Applying background replacement...")
        progress_bar = st.progress(0)  # Initialize progress bar
        for i in range(100):
            time.sleep(0.05)  # Simulate a long-running process
            progress_bar.progress(i + 1)  # Update progress bar

        # Once the progress is complete, perform the transformation
        transformed_url, options = cloudinary_url(
            public_id,
            effect=f"gen_background_replace:prompt_{prompt}"
        )
        st.success("Transformation complete!")

        # Display Original and Transformed Images Side-by-Side
        col1, col2 = st.columns(2)

        with col1:
            st.image(uploaded_file, caption="Original Image - Before", use_column_width=True)

        with col2:
            st.image(transformed_url, caption="Transformed Image - After", use_column_width=True)

        # Provide a Download Link
        st.write(f"[Download Transformed Image]({transformed_url})")