Singularity666 commited on
Commit
eded3b6
1 Parent(s): 3c7ffed

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +69 -68
main.py CHANGED
@@ -1,82 +1,83 @@
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")
 
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
+
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
+ # Set up environment variable for Replicate API Token
41
+ os.environ['REPLICATE_API_TOKEN'] = 'r8_3V5WKOBwbbuL0DQGMliP0972IAVIBo62Lmi8I' # Replace with your actual API token
42
+
43
+
44
+ def upscale_image(image_path):
45
+ # Open the image file
46
+
47
+
48
+
49
+
50
+
51
+
52
+
53
+
54
+
55
  with open(image_path, "rb") as img_file:
56
+ # Run the GFPGAN model
57
  output = replicate.run(
58
  "tencentarc/gfpgan:9283608cc6b7be6b65a8e44983db012355fde4132009bf99d976b2f0896856a3",
59
  input={"img": img_file, "version": "v1.4", "scale": 16}
60
  )
61
+
62
+ # The output is a URI of the processed image
63
+ # We will retrieve the image data and save it
64
  response = requests.get(output)
65
+ img = Image.open(BytesIO(response.content))
66
+ img.save("upscaled.png") # Save the upscaled image
67
+ return img
 
 
 
 
 
 
 
68
 
69
+ def main():
70
+ st.title("Image Upscaling")
71
+ st.write("Upload an image and it will be upscaled.")
72
 
73
+ uploaded_file = st.file_uploader("Choose an image...", type="png")
74
+ if uploaded_file is not None:
75
+ with open("temp_img.png", "wb") as f:
76
+ f.write(uploaded_file.getbuffer())
77
+ st.success("Uploaded image successfully!")
78
+ if st.button("Upscale Image"):
79
+ img = upscale_image("temp_img.png")
80
+ st.image(img, caption='Upscaled Image', use_column_width=True)
81
 
82
+ if __name__ == "__main__":
83
+ main()