Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,7 @@ import requests
|
|
4 |
from PIL import Image
|
5 |
import io
|
6 |
import logging
|
|
|
7 |
|
8 |
# Set up logging
|
9 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
|
@@ -58,35 +59,46 @@ def enhance_prompt(google_api_key, prompt, style):
|
|
58 |
raise
|
59 |
|
60 |
def generate_image(stability_api_key, enhanced_prompt, style, negative_prompt):
|
61 |
-
url = "https://api.stability.ai/
|
62 |
|
63 |
headers = {
|
64 |
-
"
|
|
|
65 |
"Authorization": f"Bearer {stability_api_key}"
|
66 |
}
|
67 |
|
68 |
-
|
69 |
-
"
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
|
|
|
|
|
|
|
|
|
|
79 |
}
|
80 |
|
81 |
try:
|
82 |
-
response = requests.post(url, headers=headers,
|
83 |
response.raise_for_status()
|
84 |
|
85 |
logging.debug(f"Response headers: {response.headers}")
|
86 |
logging.debug(f"Response content type: {response.headers.get('content-type')}")
|
87 |
|
88 |
-
if response.headers.get('content-type') == '
|
89 |
-
|
|
|
|
|
|
|
|
|
|
|
90 |
else:
|
91 |
error_message = response.text
|
92 |
logging.error(f"Unexpected content type: {response.headers.get('content-type')}. Response: {error_message}")
|
|
|
4 |
from PIL import Image
|
5 |
import io
|
6 |
import logging
|
7 |
+
import base64
|
8 |
|
9 |
# Set up logging
|
10 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
59 |
raise
|
60 |
|
61 |
def generate_image(stability_api_key, enhanced_prompt, style, negative_prompt):
|
62 |
+
url = "https://api.stability.ai/v1/generation/stable-diffusion-v1-5/text-to-image"
|
63 |
|
64 |
headers = {
|
65 |
+
"Content-Type": "application/json",
|
66 |
+
"Accept": "application/json",
|
67 |
"Authorization": f"Bearer {stability_api_key}"
|
68 |
}
|
69 |
|
70 |
+
payload = {
|
71 |
+
"text_prompts": [
|
72 |
+
{
|
73 |
+
"text": f"{enhanced_prompt}, Style: {style}",
|
74 |
+
"weight": 1
|
75 |
+
},
|
76 |
+
{
|
77 |
+
"text": negative_prompt,
|
78 |
+
"weight": -1
|
79 |
+
}
|
80 |
+
],
|
81 |
+
"cfg_scale": 7,
|
82 |
+
"height": 512,
|
83 |
+
"width": 512,
|
84 |
+
"samples": 1,
|
85 |
+
"steps": 30
|
86 |
}
|
87 |
|
88 |
try:
|
89 |
+
response = requests.post(url, headers=headers, json=payload)
|
90 |
response.raise_for_status()
|
91 |
|
92 |
logging.debug(f"Response headers: {response.headers}")
|
93 |
logging.debug(f"Response content type: {response.headers.get('content-type')}")
|
94 |
|
95 |
+
if response.headers.get('content-type') == 'application/json':
|
96 |
+
result = response.json()
|
97 |
+
if 'artifacts' in result and len(result['artifacts']) > 0:
|
98 |
+
image_data = result['artifacts'][0]['base64']
|
99 |
+
return base64.b64decode(image_data)
|
100 |
+
else:
|
101 |
+
raise Exception("No image data found in the response")
|
102 |
else:
|
103 |
error_message = response.text
|
104 |
logging.error(f"Unexpected content type: {response.headers.get('content-type')}. Response: {error_message}")
|