Spaces:
Runtime error
Runtime error
Singularity666
commited on
Commit
•
3c7ffed
1
Parent(s):
68724c0
Update main.py
Browse files
main.py
CHANGED
@@ -1,41 +1,82 @@
|
|
1 |
import streamlit as st
|
2 |
-
import replicate
|
3 |
-
import os
|
4 |
import requests
|
5 |
from PIL import Image
|
6 |
from io import BytesIO
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
#
|
9 |
-
|
|
|
10 |
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
with open(image_path, "rb") as img_file:
|
14 |
-
# Run the GFPGAN model
|
15 |
output = replicate.run(
|
16 |
"tencentarc/gfpgan:9283608cc6b7be6b65a8e44983db012355fde4132009bf99d976b2f0896856a3",
|
17 |
input={"img": img_file, "version": "v1.4", "scale": 16}
|
18 |
)
|
19 |
-
|
20 |
-
# The output is a URI of the processed image
|
21 |
-
# We will retrieve the image data and save it
|
22 |
response = requests.get(output)
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
|
|
|
|
2 |
import requests
|
3 |
from PIL import Image
|
4 |
from io import BytesIO
|
5 |
+
import getpass, os
|
6 |
+
import warnings
|
7 |
+
from stability_sdk import client
|
8 |
+
import stability_sdk.interfaces.gooseai.generation.generation_pb2 as generation
|
9 |
+
import replicate
|
10 |
+
|
11 |
+
# API keys
|
12 |
+
api_key = '1143a102dbe21628248d4bb992b391a49dc058c584181ea72e17c2ccd49be9ca69ccf4a2b97fc82c89ff1029578abbea'
|
13 |
+
os.environ['STABILITY_KEY'] = 'sk-GBmsWR78MmCSAWGkkC1CFgWgE6GPgV00pNLJlxlyZWyT3QQO'
|
14 |
+
os.environ['REPLICATE_API_TOKEN'] = 'r8_0a77UG8yfrNXOS6xHhUTLh80dJQ5kxO0CTLmq' # Replace with your actual API token
|
15 |
+
|
16 |
+
# Increase the pixel limit
|
17 |
+
Image.MAX_IMAGE_PIXELS = None
|
18 |
+
|
19 |
+
# Establish connection to Stability API
|
20 |
+
stability_api = client.StabilityInference(
|
21 |
+
key=os.environ['STABILITY_KEY'],
|
22 |
+
upscale_engine="esrgan-v1-x2plus",
|
23 |
+
verbose=True,
|
24 |
+
)
|
25 |
+
|
26 |
+
# ClipDrop API function
|
27 |
+
def generate_image(prompt):
|
28 |
+
headers = {'x-api-key': api_key}
|
29 |
+
body_params = {'prompt': (None, prompt, 'text/plain')}
|
30 |
+
response = requests.post('https://clipdrop-api.co/text-to-image/v1', files=body_params, headers=headers)
|
31 |
+
|
32 |
+
if response.status_code == 200:
|
33 |
+
return Image.open(BytesIO(response.content))
|
34 |
+
else:
|
35 |
+
st.write(f"Request failed with status code {response.status_code}")
|
36 |
+
return None
|
37 |
|
38 |
+
# Stability API function
|
39 |
+
def upscale_image_stability(img):
|
40 |
+
answers = stability_api.upscale(init_image=img)
|
41 |
|
42 |
+
for resp in answers:
|
43 |
+
for artifact in resp.artifacts:
|
44 |
+
if artifact.finish_reason == generation.FILTER:
|
45 |
+
warnings.warn(
|
46 |
+
"Your request activated the API's safety filters and could not be processed."
|
47 |
+
"Please submit a different image and try again.")
|
48 |
+
if artifact.type == generation.ARTIFACT_IMAGE:
|
49 |
+
return Image.open(io.BytesIO(artifact.binary))
|
50 |
+
|
51 |
+
# GFPGAN function
|
52 |
+
def upscale_image_gfpgan(image_path):
|
53 |
with open(image_path, "rb") as img_file:
|
|
|
54 |
output = replicate.run(
|
55 |
"tencentarc/gfpgan:9283608cc6b7be6b65a8e44983db012355fde4132009bf99d976b2f0896856a3",
|
56 |
input={"img": img_file, "version": "v1.4", "scale": 16}
|
57 |
)
|
|
|
|
|
|
|
58 |
response = requests.get(output)
|
59 |
+
return Image.open(BytesIO(response.content))
|
60 |
+
|
61 |
+
# Streamlit UI
|
62 |
+
st.title("Image Generator and Upscaler")
|
63 |
+
|
64 |
+
prompt = st.text_input("Enter a prompt for the image generation")
|
65 |
+
|
66 |
+
if st.button("Generate and Upscale"):
|
67 |
+
if prompt:
|
68 |
+
img1 = generate_image(prompt)
|
69 |
+
|
70 |
+
if img1:
|
71 |
+
st.image(img1, caption="Generated Image", use_column_width=True)
|
72 |
+
img1.save('generated_image.png')
|
73 |
+
|
74 |
+
img2 = upscale_image_stability(img1)
|
75 |
+
st.image(img2, caption="Upscaled Image (Stability API)", use_column_width=True)
|
76 |
+
img2.save('upscaled_image_stability.png')
|
77 |
+
|
78 |
+
img3 = upscale_image_gfpgan('upscaled_image_stability.png')
|
79 |
+
st.image(img3, caption="Upscaled Image (GFPGAN)", use_column_width=True)
|
80 |
+
img3.save('upscaled_image_gfpgan.png')
|
81 |
+
else:
|
82 |
+
st.write("Please enter a prompt")
|