Spaces:
Runtime error
Runtime error
Commit ·
b743891
1
Parent(s): 288487d
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,104 +1,42 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import os
|
| 3 |
import requests
|
| 4 |
from PIL import Image
|
| 5 |
from io import BytesIO
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
os.environ['STABILITY_API_KEY'] = '1143a102dbe21628248d4bb992b391a49dc058c584181ea72e17c2ccd49be9ca69ccf4a2b97fc82c89ff1029578abbea'
|
| 10 |
-
os.environ['REPLICATE_API_TOKEN'] = 'r8_Tm3LQMS81QaGXzzdGVRyUCOQ3cuNd1i1sJlqp'
|
| 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 generate_image(prompt):
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
headers = {'x-api-key': os.environ['CLIPDROP_API_KEY']}
|
| 20 |
-
data = {'prompt': prompt}
|
| 21 |
-
response = requests.post(url, headers=headers, data=data)
|
| 22 |
-
|
| 23 |
-
if response.status_code == 200:
|
| 24 |
-
# Get the generated image from the response
|
| 25 |
-
img = Image.open(BytesIO(response.content))
|
| 26 |
-
return img
|
| 27 |
-
else:
|
| 28 |
-
st.error('Failed to generate image from text prompt.')
|
| 29 |
-
|
| 30 |
-
def upscale_image(img):
|
| 31 |
-
# Set up Stability API connection
|
| 32 |
-
stability_api = replicate.StabilityInference(
|
| 33 |
-
key=os.environ['STABILITY_API_KEY'],
|
| 34 |
-
upscale_engine="stable-diffusion-x4-latent-upscaler"
|
| 35 |
-
)
|
| 36 |
-
|
| 37 |
-
# Upscale the image using the Stability API
|
| 38 |
-
upscale_responses = stability_api.upscale(init_image=img)
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
for resp in upscale_responses:
|
| 44 |
-
for artifact in resp.artifacts:
|
| 45 |
-
if artifact.type == generation.ARTIFACT_IMAGE:
|
| 46 |
-
upscaled_img = Image.open(BytesIO(artifact.binary))
|
| 47 |
-
break
|
| 48 |
-
if upscaled_img:
|
| 49 |
-
break
|
| 50 |
-
return upscaled_img
|
| 51 |
-
else:
|
| 52 |
-
st.error('Failed to upscale the image.')
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
replicate_api = replicate.ReplicateAPI(token=os.environ['REPLICATE_API_TOKEN'])
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
"
|
| 61 |
-
|
| 62 |
-
)
|
| 63 |
-
|
| 64 |
-
if output:
|
| 65 |
-
# Get the upscaled image URI from the output
|
| 66 |
-
response = requests.get(output)
|
| 67 |
-
upscaled_img = Image.open(BytesIO(response.content))
|
| 68 |
-
return upscaled_img
|
| 69 |
-
else:
|
| 70 |
-
st.error('Failed to upscale the image using GFPGAN.')
|
| 71 |
|
| 72 |
def main():
|
| 73 |
-
st.title("Image
|
| 74 |
-
st.write("Enter a text prompt to generate and upscale an image.")
|
| 75 |
-
|
| 76 |
-
# Get user input for the text prompt
|
| 77 |
-
prompt = st.text_input("Enter a text prompt:", max_chars=1000)
|
| 78 |
-
|
| 79 |
-
if st.button("Generate and Upscale"):
|
| 80 |
-
# Generate the image from text prompt using ClipDrop API
|
| 81 |
-
generated_img = generate_image(prompt)
|
| 82 |
-
|
| 83 |
-
if generated_img:
|
| 84 |
-
st.image(generated_img, caption='Generated Image', use_column_width=True)
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
|
|
|
| 94 |
|
| 95 |
-
if gfpgan_upscaled_img:
|
| 96 |
-
st.image(gfpgan_upscaled_img, caption='Upscaled Image (GFPGAN)', use_column_width=True)
|
| 97 |
-
st.success("Image generation and upscaling completed successfully.")
|
| 98 |
-
else:
|
| 99 |
-
st.error("Failed to further upscale the image using GFPGAN.")
|
| 100 |
-
else:
|
| 101 |
-
st.error("Failed to upscale the generated image.")
|
| 102 |
-
|
| 103 |
if __name__ == "__main__":
|
| 104 |
main()
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
import requests
|
| 3 |
from PIL import Image
|
| 4 |
from io import BytesIO
|
| 5 |
|
| 6 |
+
API_KEY = "1143a102dbe21628248d4bb992b391a49dc058c584181ea72e17c2ccd49be9ca69ccf4a2b97fc82c89ff1029578abbea"
|
| 7 |
+
API_URL = "https://clipdrop-api.co/text-to-image/v1"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def generate_image(prompt):
|
| 10 |
+
headers = {"x-api-key": API_KEY}
|
| 11 |
+
files = {"prompt": (None, prompt, "text/plain")}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
try:
|
| 14 |
+
response = requests.post(API_URL, files=files, headers=headers)
|
| 15 |
+
response.raise_for_status()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
# Get the generated image
|
| 18 |
+
image = Image.open(BytesIO(response.content))
|
|
|
|
| 19 |
|
| 20 |
+
return image
|
| 21 |
+
except requests.exceptions.RequestException as e:
|
| 22 |
+
st.error(f"Error occurred during image generation: {str(e)}")
|
| 23 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
def main():
|
| 26 |
+
st.title("Text-to-Image Generator")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
# Text prompt input
|
| 29 |
+
prompt = st.text_input("Enter a text prompt")
|
| 30 |
|
| 31 |
+
if prompt:
|
| 32 |
+
# Generate image when the "Generate Image" button is clicked
|
| 33 |
+
if st.button("Generate Image"):
|
| 34 |
+
st.write("Generating image...")
|
| 35 |
+
image = generate_image(prompt)
|
| 36 |
|
| 37 |
+
if image:
|
| 38 |
+
# Display the generated image
|
| 39 |
+
st.image(image, caption="Generated Image", use_column_width=True)
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
if __name__ == "__main__":
|
| 42 |
main()
|