Singularity666 commited on
Commit
64030f6
·
1 Parent(s): 88c3a2a

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +20 -45
main.py CHANGED
@@ -1,4 +1,4 @@
1
- import streamlit as st
2
  import os
3
  import requests
4
  from PIL import Image
@@ -12,7 +12,9 @@ CLIPDROP_API_KEY = '1143a102dbe21628248d4bb992b391a49dc058c584181ea72e17c2ccd49b
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',
@@ -27,37 +29,23 @@ def generate_image_from_text(prompt):
27
  else:
28
  r.raise_for_status()
29
 
30
- def resize_image(image_bytes, max_size=(640, 640)):
31
- # Open the image from bytes
32
  img = Image.open(BytesIO(image_bytes))
33
-
34
- # Resize the image
35
  img.thumbnail(max_size)
36
-
37
- # Save it back to bytes
38
  buffer = BytesIO()
39
  img.save(buffer, format="PNG")
40
  return buffer.getvalue()
41
 
42
  def upscale_image_stable_diffusion(image_bytes):
43
- # Set up environment variables
44
  os.environ['STABILITY_HOST'] = 'grpc.stability.ai:443'
45
  os.environ['STABILITY_KEY'] = STABLE_DIFFUSION_API_KEY
46
-
47
- # Set up the connection to the API
48
  stability_api = client.StabilityInference(
49
  key=os.environ['STABILITY_KEY'],
50
  upscale_engine="stable-diffusion-x4-latent-upscaler",
51
  verbose=True,
52
  )
53
-
54
- # Open the image from bytes
55
  img = Image.open(BytesIO(image_bytes))
56
-
57
- # Call the upscale API
58
  answers = stability_api.upscale(init_image=img)
59
-
60
- # Process the response
61
  upscaled_img_bytes = None
62
  for resp in answers:
63
  for artifact in resp.artifacts:
@@ -66,43 +54,30 @@ def upscale_image_stable_diffusion(image_bytes):
66
  upscaled_img_bytes = BytesIO()
67
  upscaled_img.save(upscaled_img_bytes, format='PNG')
68
  upscaled_img_bytes = upscaled_img_bytes.getvalue()
69
-
70
  return upscaled_img_bytes
71
 
72
  def further_upscale_image(image_bytes):
73
- # Run the GFPGAN model
74
  output = replicate.run(
75
- "tencentarc/gfpgan:9283608cc6b7be6b65a8e44983db012355fde4132009bf99d976b2f0896856a3",
76
  input={"img": BytesIO(image_bytes), "version": "v1.4", "scale": 16}
77
  )
78
-
79
- # The output is a URI of the processed image
80
- # We will retrieve the image data and save it
81
  response = requests.get(output)
82
  img = Image.open(BytesIO(response.content))
83
- img.save("upscaled.png") # Save the upscaled image
84
  return img
85
 
86
- def main():
87
- st.title("Image Generation and Upscaling")
88
- st.write("Enter a text prompt and an image will be generated and upscaled.")
89
-
90
- prompt = st.text_input("Enter a textual prompt to generate an image...")
91
-
92
- if prompt:
93
- st.success("Generating image from text prompt...")
94
- image_bytes = generate_image_from_text(prompt)
95
-
96
- st.success("Resizing image...")
97
- resized_image_bytes = resize_image(image_bytes)
98
-
99
- st.success("Upscaling image with stable-diffusion-x4-latent-upscaler...")
100
- upscaled_image_bytes = upscale_image_stable_diffusion(resized_image_bytes)
101
-
102
- st.success("Further upscaling image with GFPGAN...")
103
- img = further_upscale_image(upscaled_image_bytes)
104
-
105
- st.image(img, caption='Upscaled Image', use_column_width=True)
106
 
107
  if __name__ == "__main__":
108
- main()
 
1
+ from flask import Flask, request, render_template
2
  import os
3
  import requests
4
  from PIL import Image
 
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'
16
+
17
+ app = Flask(__name__)
18
 
19
  def generate_image_from_text(prompt):
20
  r = requests.post('https://clipdrop-api.co/text-to-image/v1',
 
29
  else:
30
  r.raise_for_status()
31
 
32
+ def resize_image(image_bytes, max_size=(320, 320)):
 
33
  img = Image.open(BytesIO(image_bytes))
 
 
34
  img.thumbnail(max_size)
 
 
35
  buffer = BytesIO()
36
  img.save(buffer, format="PNG")
37
  return buffer.getvalue()
38
 
39
  def upscale_image_stable_diffusion(image_bytes):
 
40
  os.environ['STABILITY_HOST'] = 'grpc.stability.ai:443'
41
  os.environ['STABILITY_KEY'] = STABLE_DIFFUSION_API_KEY
 
 
42
  stability_api = client.StabilityInference(
43
  key=os.environ['STABILITY_KEY'],
44
  upscale_engine="stable-diffusion-x4-latent-upscaler",
45
  verbose=True,
46
  )
 
 
47
  img = Image.open(BytesIO(image_bytes))
 
 
48
  answers = stability_api.upscale(init_image=img)
 
 
49
  upscaled_img_bytes = None
50
  for resp in answers:
51
  for artifact in resp.artifacts:
 
54
  upscaled_img_bytes = BytesIO()
55
  upscaled_img.save(upscaled_img_bytes, format='PNG')
56
  upscaled_img_bytes = upscaled_img_bytes.getvalue()
 
57
  return upscaled_img_bytes
58
 
59
  def further_upscale_image(image_bytes):
 
60
  output = replicate.run(
61
+ "tencentarc/gfpgan:your_model_version",
62
  input={"img": BytesIO(image_bytes), "version": "v1.4", "scale": 16}
63
  )
 
 
 
64
  response = requests.get(output)
65
  img = Image.open(BytesIO(response.content))
 
66
  return img
67
 
68
+ @app.route('/', methods=['GET', 'POST'])
69
+ def index():
70
+ if request.method == 'POST':
71
+ prompt = request.form.get('prompt')
72
+ if prompt:
73
+ image_bytes = generate_image_from_text(prompt)
74
+ resized_image_bytes = resize_image(image_bytes)
75
+ upscaled_image_bytes = upscale_image_stable_diffusion(resized_image_bytes)
76
+ img = further_upscale_image(upscaled_image_bytes)
77
+ img_path = os.path.join('static', 'upscaled_image.png')
78
+ img.save(img_path)
79
+ return render_template('index.html', image_url=img_path)
80
+ return render_template('index.html')
 
 
 
 
 
 
 
81
 
82
  if __name__ == "__main__":
83
+ app.run(port=8501, debug=True)