Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -8,6 +8,7 @@ import requests
|
|
8 |
import logging
|
9 |
import threading
|
10 |
import io
|
|
|
11 |
|
12 |
# Set up logging
|
13 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
|
@@ -119,19 +120,22 @@ def generate_image(stability_api_key, enhanced_prompt, style, negative_prompt):
|
|
119 |
"width": 1024,
|
120 |
"height": 1024,
|
121 |
"num_images": 1,
|
122 |
-
"steps": 20
|
123 |
"cfg_scale": 7,
|
124 |
}
|
125 |
|
126 |
try:
|
127 |
-
response = requests.post(url, headers=headers, files={"none": ''}, data=data)
|
128 |
response.raise_for_status()
|
129 |
|
130 |
logging.debug(f"Response headers: {response.headers}")
|
131 |
logging.debug(f"Response content type: {response.headers.get('content-type')}")
|
132 |
|
133 |
if response.headers.get('content-type').startswith('image/'):
|
134 |
-
|
|
|
|
|
|
|
135 |
else:
|
136 |
error_message = response.text
|
137 |
logging.error(f"Unexpected content type: {response.headers.get('content-type')}. Response: {error_message}")
|
@@ -147,10 +151,18 @@ def process_and_generate(google_api_key, stability_api_key, prompt, style, set_s
|
|
147 |
enhanced_prompt = enhance_prompt(google_api_key, prompt, style)
|
148 |
|
149 |
set_status("Generating image...")
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
except Exception as e:
|
155 |
logging.error(f"Error in process_and_generate: {str(e)}")
|
156 |
set_status(f"Error: {str(e)}")
|
@@ -187,12 +199,20 @@ def update_output(n_clicks, google_api_key, stability_api_key, prompt, style):
|
|
187 |
else:
|
188 |
return "", f"Error: {enhanced_prompt}", status["message"], True
|
189 |
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
196 |
|
197 |
@app.callback(
|
198 |
Output("download-image", "data"),
|
|
|
8 |
import logging
|
9 |
import threading
|
10 |
import io
|
11 |
+
import time
|
12 |
|
13 |
# Set up logging
|
14 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
120 |
"width": 1024,
|
121 |
"height": 1024,
|
122 |
"num_images": 1,
|
123 |
+
"steps": 30, # Increased from 20 to 30
|
124 |
"cfg_scale": 7,
|
125 |
}
|
126 |
|
127 |
try:
|
128 |
+
response = requests.post(url, headers=headers, files={"none": ''}, data=data, timeout=60) # Added 60-second timeout
|
129 |
response.raise_for_status()
|
130 |
|
131 |
logging.debug(f"Response headers: {response.headers}")
|
132 |
logging.debug(f"Response content type: {response.headers.get('content-type')}")
|
133 |
|
134 |
if response.headers.get('content-type').startswith('image/'):
|
135 |
+
image_data = response.content
|
136 |
+
if len(image_data) < 1000: # Check if the image data is too small (likely incomplete)
|
137 |
+
raise Exception("Received incomplete image data")
|
138 |
+
return image_data
|
139 |
else:
|
140 |
error_message = response.text
|
141 |
logging.error(f"Unexpected content type: {response.headers.get('content-type')}. Response: {error_message}")
|
|
|
151 |
enhanced_prompt = enhance_prompt(google_api_key, prompt, style)
|
152 |
|
153 |
set_status("Generating image...")
|
154 |
+
max_attempts = 3
|
155 |
+
for attempt in range(max_attempts):
|
156 |
+
try:
|
157 |
+
image_bytes = generate_image(stability_api_key, enhanced_prompt, style, DEFAULT_NEGATIVE_PROMPT)
|
158 |
+
set_status("Image generated successfully!")
|
159 |
+
return image_bytes, enhanced_prompt
|
160 |
+
except Exception as e:
|
161 |
+
if attempt < max_attempts - 1:
|
162 |
+
set_status(f"Attempt {attempt + 1} failed. Retrying...")
|
163 |
+
time.sleep(2) # Wait for 2 seconds before retrying
|
164 |
+
else:
|
165 |
+
raise e
|
166 |
except Exception as e:
|
167 |
logging.error(f"Error in process_and_generate: {str(e)}")
|
168 |
set_status(f"Error: {str(e)}")
|
|
|
199 |
else:
|
200 |
return "", f"Error: {enhanced_prompt}", status["message"], True
|
201 |
|
202 |
+
try:
|
203 |
+
# Run the process in a separate thread
|
204 |
+
thread = threading.Thread(target=run_process)
|
205 |
+
thread.start()
|
206 |
+
thread.join(timeout=90) # Wait for up to 90 seconds
|
207 |
+
|
208 |
+
if thread.is_alive():
|
209 |
+
# If the thread is still running after 90 seconds, we consider it a timeout
|
210 |
+
return "", "Error: Image generation timed out", "Process timed out", True
|
211 |
+
|
212 |
+
return run_process()
|
213 |
+
except Exception as e:
|
214 |
+
logging.error(f"Unexpected error in update_output: {str(e)}")
|
215 |
+
return "", f"Unexpected error: {str(e)}", "An unexpected error occurred", True
|
216 |
|
217 |
@app.callback(
|
218 |
Output("download-image", "data"),
|