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()