Spaces:
Sleeping
Sleeping
Singularity666
commited on
Commit
·
9184aae
1
Parent(s):
656d47c
Update main.py
Browse files
main.py
CHANGED
@@ -1,47 +1,77 @@
|
|
|
|
|
|
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 |
-
# Set up environment
|
9 |
-
os.environ['REPLICATE_API_TOKEN'] = 'r8_3V5WKOBwbbuL0DQGMliP0972IAVIBo62Lmi8I' # Replace with your actual
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
# Open the image file
|
14 |
-
with open(image_path, "rb") as img_file:
|
15 |
-
# Upscale the image using Stability AI's API and Stable Diffusion 4x Upscaler
|
16 |
-
stability_api_url = "https://api.stability.ai/v1/image-upscaling"
|
17 |
-
headers = {
|
18 |
-
'Authorization': f'Bearer {os.environ["STABILITY_KEY"]}',
|
19 |
-
}
|
20 |
files = {
|
21 |
-
'
|
22 |
-
|
23 |
-
}
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
def main():
|
32 |
-
st.title("Image Upscaling")
|
33 |
-
st.write("
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
st.success("
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
45 |
|
46 |
if __name__ == "__main__":
|
47 |
main()
|
|
|
1 |
+
# main.py
|
2 |
+
|
3 |
import streamlit as st
|
|
|
4 |
import os
|
5 |
import requests
|
6 |
from PIL import Image
|
7 |
from io import BytesIO
|
8 |
+
import replicate
|
9 |
+
|
10 |
+
# Configure your API keys here
|
11 |
+
CLIPDROP_API_KEY = '1143a102dbe21628248d4bb992b391a49dc058c584181ea72e17c2ccd49be9ca69ccf4a2b97fc82c89ff1029578abbea'
|
12 |
+
STABLE_DIFFUSION_API_KEY = 'sk-GBmsWR78MmCSAWGkkC1CFgWgE6GPgV00pNLJlxlyZWyT3QQO'
|
13 |
|
14 |
+
# Set up environment variable for Replicate API Token
|
15 |
+
os.environ['REPLICATE_API_TOKEN'] = 'r8_3V5WKOBwbbuL0DQGMliP0972IAVIBo62Lmi8I' # Replace with your actual API token
|
16 |
+
|
17 |
+
def generate_image_from_text(prompt):
|
18 |
+
r = requests.post('https://clipdrop-api.co/text-to-image/v1',
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
files = {
|
20 |
+
'prompt': (None, prompt, 'text/plain')
|
21 |
+
},
|
22 |
+
headers = { 'x-api-key': CLIPDROP_API_KEY }
|
23 |
+
)
|
24 |
+
|
25 |
+
if r.ok:
|
26 |
+
return r.content
|
27 |
+
else:
|
28 |
+
r.raise_for_status()
|
29 |
+
|
30 |
+
def upscale_image_stable_diffusion(image_bytes):
|
31 |
+
url = 'https://stable-diffusion-x4-latent-upscaler.com/v1/upscaling' # Update this with correct API endpoint
|
32 |
+
headers = { 'x-api-key': STABLE_DIFFUSION_API_KEY }
|
33 |
+
files = {
|
34 |
+
'image': ('image.png', image_bytes, 'image/png')
|
35 |
+
}
|
36 |
+
|
37 |
+
r = requests.post(url, headers=headers, files=files)
|
38 |
+
|
39 |
+
if r.ok:
|
40 |
+
return r.content
|
41 |
+
else:
|
42 |
+
r.raise_for_status()
|
43 |
|
44 |
+
def further_upscale_image(image_bytes):
|
45 |
+
# Run the GFPGAN model
|
46 |
+
output = replicate.run(
|
47 |
+
"tencentarc/gfpgan:9283608cc6b7be6b65a8e44983db012355fde4132009bf99d976b2f0896856a3",
|
48 |
+
input={"img": BytesIO(image_bytes), "version": "v1.4", "scale": 16}
|
49 |
+
)
|
50 |
+
|
51 |
+
# The output is a URI of the processed image
|
52 |
+
# We will retrieve the image data and save it
|
53 |
+
response = requests.get(output)
|
54 |
+
img = Image.open(BytesIO(response.content))
|
55 |
+
img.save("upscaled.png") # Save the upscaled image
|
56 |
+
return img
|
57 |
|
58 |
def main():
|
59 |
+
st.title("Image Generation and Upscaling")
|
60 |
+
st.write("Enter a text prompt and an image will be generated and upscaled.")
|
61 |
+
|
62 |
+
prompt = st.text_input("Enter a textual prompt to generate an image...")
|
63 |
+
|
64 |
+
if prompt:
|
65 |
+
st.success("Generating image from text prompt...")
|
66 |
+
image_bytes = generate_image_from_text(prompt)
|
67 |
+
|
68 |
+
st.success("Upscaling image with stable-diffusion-x4-latent-upscaler...")
|
69 |
+
upscaled_image_bytes = upscale_image_stable_diffusion(image_bytes)
|
70 |
+
|
71 |
+
st.success("Further upscaling image with GFPGAN...")
|
72 |
+
img = further_upscale_image(upscaled_image_bytes)
|
73 |
+
|
74 |
+
st.image(img, caption='Upscaled Image', use_column_width=True)
|
75 |
|
76 |
if __name__ == "__main__":
|
77 |
main()
|