Spaces:
Paused
Paused
ChandimaPrabath
commited on
Commit
•
b6d72ff
1
Parent(s):
4bede7e
Update sd.py
Browse files
sd.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
import requests
|
|
|
2 |
import base64
|
3 |
import os
|
4 |
from datetime import datetime
|
@@ -54,7 +55,7 @@ def upload_to_imgbb(image_data, file_name):
|
|
54 |
def generate_sd(prompt):
|
55 |
"""
|
56 |
Generate an image using the specified prompt and save it to the cache directory.
|
57 |
-
|
58 |
Returns:
|
59 |
image_data (bytes): Raw image data as bytes.
|
60 |
image_path (str): Absolute path to the saved image file using forward slashes.
|
@@ -85,21 +86,28 @@ def generate_sd(prompt):
|
|
85 |
if 'images' in r and r['images']:
|
86 |
image_data = base64.b64decode(r['images'][0])
|
87 |
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
|
88 |
-
image_filename = f"image_{timestamp}.
|
89 |
-
prompt_filename = f"image_{timestamp}.yaml"
|
90 |
image_path = os.path.abspath(os.path.join(CACHE_DIR, image_filename))
|
91 |
-
prompt_path = os.path.abspath(os.path.join(CACHE_DIR, prompt_filename))
|
92 |
-
|
93 |
-
# Save the prompt to a YAML file
|
94 |
-
with open(prompt_path, 'w') as f:
|
95 |
-
yaml.dump(payload, f)
|
96 |
|
97 |
-
# Save the image as a
|
98 |
with open(image_path, 'wb') as f:
|
99 |
f.write(image_data)
|
100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
# Upload image to ImgBB
|
102 |
-
image_url = upload_to_imgbb(
|
103 |
|
104 |
# Convert the path to use forward slashes
|
105 |
image_path = image_path.replace('\\', '/')
|
@@ -107,10 +115,10 @@ def generate_sd(prompt):
|
|
107 |
return image_data, image_path
|
108 |
else:
|
109 |
print("No images found in the response.")
|
110 |
-
return None, None
|
111 |
except requests.RequestException as e:
|
112 |
print(f"HTTP request error: {e}")
|
113 |
-
return None, None
|
114 |
except Exception as e:
|
115 |
print(f"Error generating image: {e}")
|
116 |
-
return None, None
|
|
|
1 |
import requests
|
2 |
+
from PIL import Image, PngImagePlugin
|
3 |
import base64
|
4 |
import os
|
5 |
from datetime import datetime
|
|
|
55 |
def generate_sd(prompt):
|
56 |
"""
|
57 |
Generate an image using the specified prompt and save it to the cache directory.
|
58 |
+
Embeds the YAML payload into the image metadata.
|
59 |
Returns:
|
60 |
image_data (bytes): Raw image data as bytes.
|
61 |
image_path (str): Absolute path to the saved image file using forward slashes.
|
|
|
86 |
if 'images' in r and r['images']:
|
87 |
image_data = base64.b64decode(r['images'][0])
|
88 |
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
|
89 |
+
image_filename = f"image_{timestamp}.png"
|
|
|
90 |
image_path = os.path.abspath(os.path.join(CACHE_DIR, image_filename))
|
|
|
|
|
|
|
|
|
|
|
91 |
|
92 |
+
# Save the image as a PNG file
|
93 |
with open(image_path, 'wb') as f:
|
94 |
f.write(image_data)
|
95 |
|
96 |
+
# Open the image with Pillow
|
97 |
+
img = Image.open(image_path)
|
98 |
+
|
99 |
+
# Convert payload to YAML string
|
100 |
+
yaml_data = yaml.dump(payload)
|
101 |
+
|
102 |
+
# Embed YAML data into image metadata
|
103 |
+
metadata = PngImagePlugin.PngInfo()
|
104 |
+
metadata.add_text("YAML", yaml_data)
|
105 |
+
|
106 |
+
# Save the image with updated metadata
|
107 |
+
img.save(image_path, "PNG", pnginfo=metadata)
|
108 |
+
|
109 |
# Upload image to ImgBB
|
110 |
+
image_url = upload_to_imgbb(image_path, image_filename)
|
111 |
|
112 |
# Convert the path to use forward slashes
|
113 |
image_path = image_path.replace('\\', '/')
|
|
|
115 |
return image_data, image_path
|
116 |
else:
|
117 |
print("No images found in the response.")
|
118 |
+
return None, None
|
119 |
except requests.RequestException as e:
|
120 |
print(f"HTTP request error: {e}")
|
121 |
+
return None, None
|
122 |
except Exception as e:
|
123 |
print(f"Error generating image: {e}")
|
124 |
+
return None, None
|