Spaces:
Running
Running
Update sd.py
Browse files
sd.py
CHANGED
@@ -5,8 +5,10 @@ from datetime import datetime
|
|
5 |
import io
|
6 |
import yaml
|
7 |
|
|
|
8 |
IMAGE_GENERATION_URL = os.getenv("SD_API_URL")
|
9 |
CACHE_DIR = '/tmp/cache' # Use /tmp directory which is writable
|
|
|
10 |
|
11 |
# Ensure the cache directory exists
|
12 |
os.makedirs(CACHE_DIR, exist_ok=True)
|
@@ -15,6 +17,41 @@ def load_config():
|
|
15 |
with open('config.yaml', 'r') as f:
|
16 |
return yaml.safe_load(f)
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
def generate_sd(prompt):
|
19 |
"""
|
20 |
Generate an image using the specified prompt and save it to the cache directory.
|
@@ -22,9 +59,11 @@ def generate_sd(prompt):
|
|
22 |
Returns:
|
23 |
image_data (bytes): Raw image data as bytes.
|
24 |
image_path (str): Absolute path to the saved image file using forward slashes.
|
|
|
25 |
"""
|
26 |
config = load_config()
|
27 |
config = config['config']
|
|
|
28 |
# Define payload for the POST request
|
29 |
payload = {
|
30 |
"prompt": prompt,
|
@@ -58,19 +97,21 @@ def generate_sd(prompt):
|
|
58 |
|
59 |
# Save the image as a JPEG file
|
60 |
with open(image_path, 'wb') as f:
|
61 |
-
|
62 |
-
|
|
|
|
|
63 |
|
64 |
# Convert the path to use forward slashes
|
65 |
image_path = image_path.replace('\\', '/')
|
66 |
|
67 |
-
return image_data, image_path
|
68 |
else:
|
69 |
print("No images found in the response.")
|
70 |
-
return None, None
|
71 |
except requests.RequestException as e:
|
72 |
print(f"HTTP request error: {e}")
|
73 |
-
return None, None
|
74 |
except Exception as e:
|
75 |
print(f"Error generating image: {e}")
|
76 |
-
return None, None
|
|
|
5 |
import io
|
6 |
import yaml
|
7 |
|
8 |
+
# Configuration
|
9 |
IMAGE_GENERATION_URL = os.getenv("SD_API_URL")
|
10 |
CACHE_DIR = '/tmp/cache' # Use /tmp directory which is writable
|
11 |
+
IMGBB_API_KEY = os.getenv("IMGBB_API_KEY") # Replace with your ImgBB API key
|
12 |
|
13 |
# Ensure the cache directory exists
|
14 |
os.makedirs(CACHE_DIR, exist_ok=True)
|
|
|
17 |
with open('config.yaml', 'r') as f:
|
18 |
return yaml.safe_load(f)
|
19 |
|
20 |
+
def upload_to_imgbb(image_data):
|
21 |
+
"""
|
22 |
+
Uploads image data to ImgBB and returns the URL.
|
23 |
+
|
24 |
+
Args:
|
25 |
+
image_data (bytes): Raw image data as bytes.
|
26 |
+
|
27 |
+
Returns:
|
28 |
+
str: URL of the uploaded image on ImgBB.
|
29 |
+
"""
|
30 |
+
try:
|
31 |
+
response = requests.post(
|
32 |
+
"https://api.imgbb.com/1/upload",
|
33 |
+
params={
|
34 |
+
"key": IMGBB_API_KEY,
|
35 |
+
"expiration": 600 # Optional: Set expiration time in seconds
|
36 |
+
},
|
37 |
+
files={
|
38 |
+
"image": ("image.jpg", image_data)
|
39 |
+
}
|
40 |
+
)
|
41 |
+
response.raise_for_status()
|
42 |
+
result = response.json()
|
43 |
+
if result["data"] and "url" in result["data"]:
|
44 |
+
return result["data"]["url"]
|
45 |
+
else:
|
46 |
+
print("Failed to upload image to ImgBB.")
|
47 |
+
return None
|
48 |
+
except requests.RequestException as e:
|
49 |
+
print(f"Error uploading image to ImgBB: {e}")
|
50 |
+
return None
|
51 |
+
except Exception as e:
|
52 |
+
print(f"Unexpected error uploading image to ImgBB: {e}")
|
53 |
+
return None
|
54 |
+
|
55 |
def generate_sd(prompt):
|
56 |
"""
|
57 |
Generate an image using the specified prompt and save it to the cache directory.
|
|
|
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.
|
62 |
+
image_url (str): URL of the uploaded image on ImgBB.
|
63 |
"""
|
64 |
config = load_config()
|
65 |
config = config['config']
|
66 |
+
|
67 |
# Define payload for the POST request
|
68 |
payload = {
|
69 |
"prompt": prompt,
|
|
|
97 |
|
98 |
# Save the image as a JPEG file
|
99 |
with open(image_path, 'wb') as f:
|
100 |
+
f.write(image_data)
|
101 |
+
|
102 |
+
# Upload image to ImgBB
|
103 |
+
image_url = upload_to_imgbb(image_data)
|
104 |
|
105 |
# Convert the path to use forward slashes
|
106 |
image_path = image_path.replace('\\', '/')
|
107 |
|
108 |
+
return image_data, image_path, image_url
|
109 |
else:
|
110 |
print("No images found in the response.")
|
111 |
+
return None, None, None
|
112 |
except requests.RequestException as e:
|
113 |
print(f"HTTP request error: {e}")
|
114 |
+
return None, None, None
|
115 |
except Exception as e:
|
116 |
print(f"Error generating image: {e}")
|
117 |
+
return None, None, None
|