File size: 1,886 Bytes
33c27ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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 main():
    st.title("Image Upscaling")
    st.write("Upload an image and enter a prompt to generate and upscale an 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 and Upscale Image"):
            img = upscale_image("temp_img.png")
            st.image(img, caption='Generated and Upscaled Image', use_column_width=True)

if __name__ == "__main__":
    main()