Spaces:
Runtime error
Runtime error
Commit ·
87a6c35
1
Parent(s): 23659de
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
import requests
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
|
| 7 |
+
# Set up environment variables for API keys
|
| 8 |
+
os.environ['CLIPDROP_API_KEY'] = '1143a102dbe21628248d4bb992b391a49dc058c584181ea72e17c2ccd49be9ca69ccf4a2b97fc82c89ff1029578abbea'
|
| 9 |
+
os.environ['STABILITY_API_KEY'] = 'sk-GBmsWR78MmCSAWGkkC1CFgWgE6GPgV00pNLJlxlyZWyT3QQO'
|
| 10 |
+
os.environ['REPLICATE_API_TOKEN'] = '1143a102dbe21628248d4bb992b391a49dc058c584181ea72e17c2ccd49be9ca69ccf4a2b97fc82c89ff1029578abbea'
|
| 11 |
+
|
| 12 |
+
# Importing Replicate and Stability SDK libraries
|
| 13 |
+
import replicate
|
| 14 |
+
import stability_sdk.interfaces.gooseai.generation.generation_pb2 as generation
|
| 15 |
+
|
| 16 |
+
def upscale_image(image_path):
|
| 17 |
+
# Open the image file
|
| 18 |
+
with open(image_path, "rb") as img_file:
|
| 19 |
+
# Run the GFPGAN model
|
| 20 |
+
output = replicate.run(
|
| 21 |
+
"tencentarc/gfpgan:9283608cc6b7be6b65a8e44983db012355fde4132009bf99d976b2f0896856a3",
|
| 22 |
+
input={"img": img_file, "version": "v1.4", "scale": 16}
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
# The output is a URI of the processed image
|
| 26 |
+
# We will retrieve the image data and save it
|
| 27 |
+
response = requests.get(output)
|
| 28 |
+
img = Image.open(BytesIO(response.content))
|
| 29 |
+
|
| 30 |
+
return img
|
| 31 |
+
|
| 32 |
+
def generate_and_upscale_image(prompt):
|
| 33 |
+
# Make a POST request to the ClipDrop text-to-image API
|
| 34 |
+
url = 'https://clipdrop-api.co/text-to-image/v1'
|
| 35 |
+
headers = {'x-api-key': os.environ['CLIPDROP_API_KEY']}
|
| 36 |
+
data = {'prompt': prompt}
|
| 37 |
+
response = requests.post(url, headers=headers, data=data)
|
| 38 |
+
|
| 39 |
+
if response.status_code == 200:
|
| 40 |
+
# Get the generated image from the response
|
| 41 |
+
img = Image.open(BytesIO(response.content))
|
| 42 |
+
|
| 43 |
+
# Upscale the generated image using the Stability API
|
| 44 |
+
upscale_api = replicate.StabilityInference(
|
| 45 |
+
key=os.environ['STABILITY_API_KEY'],
|
| 46 |
+
upscale_engine="stable-diffusion-x4-latent-upscaler"
|
| 47 |
+
)
|
| 48 |
+
upscale_responses = upscale_api.upscale(init_image=img)
|
| 49 |
+
|
| 50 |
+
if upscale_responses:
|
| 51 |
+
# Get the upscaled image from the response
|
| 52 |
+
upscaled_img = None
|
| 53 |
+
for resp in upscale_responses:
|
| 54 |
+
for artifact in resp.artifacts:
|
| 55 |
+
if artifact.type == generation.ARTIFACT_IMAGE:
|
| 56 |
+
upscaled_img = Image.open(BytesIO(artifact.binary))
|
| 57 |
+
break
|
| 58 |
+
if upscaled_img:
|
| 59 |
+
break
|
| 60 |
+
return upscaled_img
|
| 61 |
+
else:
|
| 62 |
+
st.error('Failed to upscale the image.')
|
| 63 |
+
else:
|
| 64 |
+
st.error('Failed to generate image from text prompt.')
|
| 65 |
+
|
| 66 |
+
def main():
|
| 67 |
+
st.title("Image Upscaling")
|
| 68 |
+
st.write("Upload an image or enter a text prompt to generate and upscale an image.")
|
| 69 |
+
|
| 70 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["png", "jpg", "jpeg"])
|
| 71 |
+
text_prompt = st.text_input("Enter a text prompt:", max_chars=1000)
|
| 72 |
+
|
| 73 |
+
if uploaded_file is not None:
|
| 74 |
+
with open("temp_img.png", "wb") as f:
|
| 75 |
+
f.write(uploaded_file.getbuffer())
|
| 76 |
+
st.success("Uploaded image successfully!")
|
| 77 |
+
|
| 78 |
+
if st.button("Upscale Image"):
|
| 79 |
+
# Upscale the uploaded image using GFPGAN
|
| 80 |
+
img = upscale_image("temp_img.png")
|
| 81 |
+
st.image(img, caption='Upscaled Image (GFPGAN)', use_column_width=True)
|
| 82 |
+
|
| 83 |
+
elif text_prompt != "":
|
| 84 |
+
if st.button("Generate and Upscale"):
|
| 85 |
+
# Generate and upscale an image from the text prompt
|
| 86 |
+
img = generate_and_upscale_image(text_prompt)
|
| 87 |
+
if img:
|
| 88 |
+
st.image(img, caption='Generated and Upscaled Image', use_column_width=True)
|
| 89 |
+
|
| 90 |
+
if __name__ == "__main__":
|
| 91 |
+
main()
|