Param Dhingana
Param Dhingana

Reputation: 63

How do I resolve Streamlit Image Content changing when download button clicked?

In this code written below, I am running a replicate model to show an image in a streamlit container within a column. And with a download button to download the image. Initially after the condition the container shows the image from Replicate Model, But when the download button is clicked, The image in the streamlit container again changes to the image being previously shown.

with img_col2:
        st.header("AI Generated Output:")
        img_container2 = st.container(border=True)
        if button_pressed is True:
            output_image = replicate.run(
                model,
                input = {
                    "image": uploaded_img,
                    "prompt": f"A detailed multi colored {style} {room_type}, with wall color as {color1}, and other colors inlcuding {color2}, {color3}, {color4}, with {furniture} placed in the room with {walltn} wall texture and with {lighting_type} lighting type.",
                    "a_prompt": "best quality, extremely detailed, interior, cinematic photo, ultra-detailed, ultra-realistic, award-winning, high-definition",
                    "n_prompt": "worst quality, distorted, low quality, low definition, bad qaulity, the absolute worst quality, distorted features, distorted generation"
                }
            )
            new_image = output_image[1]
            data = requests.get(new_image).content 
            f = open('img.jpg','wb')
            f.write(data) 
            f.close()
            img_container2.image(new_image)
            dbc1, dbc2, dbc3, dbc4 = st.columns(4)
            with dbc4:
                with open("img.jpg", "rb") as file:
                    btn = st.download_button(
                            label="Download image",
                            data=file,
                            file_name="img.jpg",
                            mime="image/jpg",
                            use_container_width = True
                        )
        else:
            img_container2.image('images/no_image.jpg')

I want that when the download button is clicked, the image is downloaded and the container still displays the output image of replicate model.

Upvotes: 0

Views: 101

Answers (0)

Related Questions