File size: 2,895 Bytes
3e7d217
33c27ec
 
 
7521b47
 
30b12a2
 
7521b47
9184aae
7521b47
 
 
 
33c27ec
7521b47
 
9184aae
7521b47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9184aae
7521b47
 
3e7d217
7521b47
 
30b12a2
3e7d217
30b12a2
 
7521b47
 
 
 
30b12a2
7521b47
5c84876
7521b47
 
 
5c84876
 
7521b47
5c84876
 
7521b47
5c84876
7521b47
 
5c84876
7521b47
33c27ec
7521b47
 
 
5c84876
7521b47
 
 
3e7d217
7521b47
 
 
33c27ec
7521b47
 
 
 
 
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
75
76
77
78
79
80
81
82
83
import streamlit as st
import requests
from PIL import Image
from io import BytesIO
import getpass, os
import warnings
from stability_sdk import client
import stability_sdk.interfaces.gooseai.generation.generation_pb2 as generation
import replicate

# API keys
api_key = 'YOUR_API_KEY'
os.environ['STABILITY_KEY'] = 'YOUR_API_KEY'
os.environ['REPLICATE_API_TOKEN'] = 'REPLICATE_API_TOKEN'  # Replace with your actual API token

# Increase the pixel limit
Image.MAX_IMAGE_PIXELS = None

# Establish connection to Stability API
stability_api = client.StabilityInference(
    key=os.environ['STABILITY_KEY'], 
    upscale_engine="esrgan-v1-x2plus",
    verbose=True,
)

# ClipDrop API function
def generate_image(prompt):
    headers = {'x-api-key': api_key}
    body_params = {'prompt': (None, prompt, 'text/plain')}
    response = requests.post('https://clipdrop-api.co/text-to-image/v1', files=body_params, headers=headers)

    if response.status_code == 200:
        return Image.open(BytesIO(response.content))
    else:
        st.write(f"Request failed with status code {response.status_code}")
        return None

# Stability API function
def upscale_image_stability(img):
    answers = stability_api.upscale(init_image=img)

    for resp in answers:
        for artifact in resp.artifacts:
            if artifact.finish_reason == generation.FILTER:
                warnings.warn(
                    "Your request activated the API's safety filters and could not be processed."
                    "Please submit a different image and try again.")
            if artifact.type == generation.ARTIFACT_IMAGE:
                return Image.open(io.BytesIO(artifact.binary))

# GFPGAN function
def upscale_image_gfpgan(image_path):
    with open(image_path, "rb") as img_file:
        output = replicate.run(
            "tencentarc/gfpgan:9283608cc6b7be6b65a8e44983db012355fde4132009bf99d976b2f0896856a3",
            input={"img": img_file, "version": "v1.4", "scale": 16}
        )
        response = requests.get(output)
        return Image.open(BytesIO(response.content))

# Streamlit UI
st.title("Image Generator and Upscaler")

prompt = st.text_input("Enter a prompt for the image generation")

if st.button("Generate and Upscale"):
    if prompt:
        img1 = generate_image(prompt)

        if img1:
            st.image(img1, caption="Generated Image", use_column_width=True)
            img1.save('generated_image.png')

            img2 = upscale_image_stability(img1)
            st.image(img2, caption="Upscaled Image (Stability API)", use_column_width=True)
            img2.save('upscaled_image_stability.png')

            img3 = upscale_image_gfpgan('upscaled_image_stability.png')
            st.image(img3, caption="Upscaled Image (GFPGAN)", use_column_width=True)
            img3.save('upscaled_image_gfpgan.png')
    else:
        st.write("Please enter a prompt")