File size: 3,087 Bytes
27fea1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
74
import streamlit as st
import replicate
import os
import requests
from PIL import Image
from io import BytesIO

# Set up environment variables for Replicate and Stability AI API Tokens
os.environ['REPLICATE_API_TOKEN'] = 'r8_3V5WKOBwbbuL0DQGMliP0972IAVIBo62Lmi8I'  # Replace with your actual Replicate API token
os.environ['STABILITY_KEY'] = 'sk-GBmsWR78MmCSAWGkkC1CFgWgE6GPgV00pNLJlxlyZWyT3QQO'  # Replace with your actual Stability AI API key

def upscale_image(image_path):
    # Open the image file
    with open(image_path, "rb") as img_file:
        # Upscale the image using Stability AI's API and Stable Diffusion 4x Upscaler
        stability_api_url = "https://api.stability.ai/v1/image-upscaling"
        headers = {
            'Authorization': f'Bearer {os.environ["STABILITY_KEY"]}',
        }
        files = {
            'image': img_file,
            'upscaler': 'stable-diffusion-x4-latent-upscaler',
        }
        response = requests.post(stability_api_url, headers=headers, files=files)

        # Retrieve the upscaled image from the response
        img = Image.open(BytesIO(response.content))
        img.save("upscaled.png")  # Save the upscaled image
        return img

def convert_to_webp(img):
    # Convert the image to WebP format
    img_webp = img.convert("RGBA").save("upscaled.webp", format="WEBP")
    return img_webp

def convert_to_jpg(img):
    # Convert the image to JPEG format with quality 95
    img_jpg = img.convert("RGB").save("upscaled.jpg", format="JPEG", quality=95)
    return img_jpg

def main():
    st.title("Image Upscaling")
    st.write("Upload an image and enter a prompt to generate, upscale, and convert the image.")

    uploaded_file = st.file_uploader("Choose an image...", type="png")
    prompt = st.text_input("Enter a prompt for image generation")

    if uploaded_file is not None and prompt:
        with open("temp_img.png", "wb") as f:
            f.write(uploaded_file.getbuffer())
        st.success("Uploaded image successfully!")
        if st.button("Generate, Upscale, and Convert Image"):
            img = upscale_image("temp_img.png")
            st.image(img, caption='Generated and Upscaled Image', use_column_width=True)

            # Convert the upscaled image to WebP and JPEG formats
            img_webp = convert_to_webp(img)
            img_jpg = convert_to_jpg(img)

            # Add download buttons for the converted images
            st.markdown(get_binary_file_downloader_html("upscaled.webp", "Download WebP", "webp"), unsafe_allow_html=True)
            st.markdown(get_binary_file_downloader_html("upscaled.jpg", "Download JPEG", "jpg"))

# Helper function to create download link for binary files
def get_binary_file_downloader_html(file_path, button_text, file_format):
    with open(file_path, "rb") as f:
        file_data = f.read()
    base64_file = base64.b64encode(file_data).decode("utf-8")
    download_link = f'<a href="data:image/{file_format};base64,{base64_file}" download="{file_path}">{button_text}</a>'
    return download_link

if __name__ == "__main__":
    main()