Update app.py
Browse files
app.py
CHANGED
|
@@ -67,13 +67,14 @@ def query(prompt, negative_prompt, steps=4, cfg_scale=0, seed=-1, width=1024, he
|
|
| 67 |
original_prompt = prompt
|
| 68 |
|
| 69 |
# Translation
|
|
|
|
| 70 |
try:
|
| 71 |
translated_prompt = GoogleTranslator(source='auto', target='en').translate(prompt)
|
| 72 |
except Exception as e:
|
| 73 |
print(f"WARNING: Translation failed. Using original prompt.")
|
| 74 |
print(f" Error: {e}")
|
| 75 |
print(f" Prompt: '{original_prompt}'\n") # Add newline
|
| 76 |
-
translated_prompt = prompt
|
| 77 |
|
| 78 |
# --- OpenAI Moderation Check ---
|
| 79 |
if openai_client:
|
|
@@ -88,7 +89,9 @@ def query(prompt, negative_prompt, steps=4, cfg_scale=0, seed=-1, width=1024, he
|
|
| 88 |
print("BLOCKED:")
|
| 89 |
print(f" Reason: sexual/minors")
|
| 90 |
print(f" Prompt: '{original_prompt}'")
|
| 91 |
-
|
|
|
|
|
|
|
| 92 |
return None, "<p style='color: red; text-align: center;'>Prompt violates safety guidelines. Generation blocked.</p>"
|
| 93 |
|
| 94 |
except AuthenticationError:
|
|
@@ -105,18 +108,25 @@ def query(prompt, negative_prompt, steps=4, cfg_scale=0, seed=-1, width=1024, he
|
|
| 105 |
print("BLOCKED:")
|
| 106 |
print(f" Reason: OpenAI Connection Error")
|
| 107 |
print(f" Prompt: '{original_prompt}'")
|
|
|
|
|
|
|
| 108 |
print(f" Error: {e}\n") # Add newline
|
| 109 |
return None, "<p style='color: red; text-align: center;'>Safety check failed. Please try again later.</p>"
|
| 110 |
except Exception as e:
|
| 111 |
print("BLOCKED:")
|
| 112 |
print(f" Reason: OpenAI Unexpected Error")
|
| 113 |
print(f" Prompt: '{original_prompt}'")
|
|
|
|
|
|
|
| 114 |
print(f" Error: {e}\n") # Add newline
|
| 115 |
traceback.print_exc() # Keep traceback for unexpected errors
|
| 116 |
return None, "<p style='color: red; text-align: center;'>An unexpected error occurred during safety check. Generation blocked.</p>"
|
| 117 |
else:
|
| 118 |
print(f"WARNING: OpenAI client not available. Skipping moderation.")
|
| 119 |
-
print(f" Prompt: '{original_prompt}'
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
# --- Proceed with Generation ---
|
| 122 |
final_prompt = f"{translated_prompt} | ultra detail, ultra elaboration, ultra quality, perfect."
|
|
@@ -146,6 +156,8 @@ def query(prompt, negative_prompt, steps=4, cfg_scale=0, seed=-1, width=1024, he
|
|
| 146 |
print("FAILED:")
|
| 147 |
print(f" Reason: Invalid Image Data (Empty/Small)")
|
| 148 |
print(f" Prompt: '{original_prompt}'")
|
|
|
|
|
|
|
| 149 |
print(f" Length: {len(image_bytes)}\n") # Add newline
|
| 150 |
return None, "<p style='color: red; text-align: center;'>API returned invalid image data.</p>"
|
| 151 |
|
|
@@ -155,6 +167,8 @@ def query(prompt, negative_prompt, steps=4, cfg_scale=0, seed=-1, width=1024, he
|
|
| 155 |
print("FAILED:")
|
| 156 |
print(f" Reason: Image Processing Error")
|
| 157 |
print(f" Prompt: '{original_prompt}'")
|
|
|
|
|
|
|
| 158 |
print(f" Error: {img_err}\n") # Add newline
|
| 159 |
return None, "<p style='color: red; text-align: center;'>Failed to process image data from API.</p>"
|
| 160 |
|
|
@@ -170,6 +184,8 @@ def query(prompt, negative_prompt, steps=4, cfg_scale=0, seed=-1, width=1024, he
|
|
| 170 |
print("FAILED:")
|
| 171 |
print(f" Reason: Image Save Verification Error")
|
| 172 |
print(f" Prompt: '{original_prompt}'")
|
|
|
|
|
|
|
| 173 |
print(f" Path: '{save_path}'\n") # Add newline
|
| 174 |
return image, "<p style='color: red; text-align: center;'>Internal Error: Failed to confirm image file save.</p>"
|
| 175 |
|
|
@@ -189,6 +205,8 @@ def query(prompt, negative_prompt, steps=4, cfg_scale=0, seed=-1, width=1024, he
|
|
| 189 |
# *** SUCCESS LOG ***
|
| 190 |
print("SUCCESS:")
|
| 191 |
print(f" Prompt: '{original_prompt}'")
|
|
|
|
|
|
|
| 192 |
print(f" {arinteli_url}\n") # Add newline
|
| 193 |
return image, download_html
|
| 194 |
|
|
@@ -196,6 +214,8 @@ def query(prompt, negative_prompt, steps=4, cfg_scale=0, seed=-1, width=1024, he
|
|
| 196 |
print("FAILED:")
|
| 197 |
print(f" Reason: Image Save IO Error")
|
| 198 |
print(f" Prompt: '{original_prompt}'")
|
|
|
|
|
|
|
| 199 |
print(f" Path: '{save_path}'")
|
| 200 |
print(f" Error: {save_err}\n") # Add newline
|
| 201 |
traceback.print_exc()
|
|
@@ -204,6 +224,8 @@ def query(prompt, negative_prompt, steps=4, cfg_scale=0, seed=-1, width=1024, he
|
|
| 204 |
print("FAILED:")
|
| 205 |
print(f" Reason: Link Creation/Save Unexpected Error")
|
| 206 |
print(f" Prompt: '{original_prompt}'")
|
|
|
|
|
|
|
| 207 |
print(f" Error: {e}\n") # Add newline
|
| 208 |
traceback.print_exc()
|
| 209 |
return image, "<p style='color: red; text-align: center;'>Internal Error creating download link.</p>"
|
|
@@ -230,6 +252,8 @@ def query(prompt, negative_prompt, steps=4, cfg_scale=0, seed=-1, width=1024, he
|
|
| 230 |
print("FAILED:")
|
| 231 |
print(f" Reason: HF API HTTP Error {status_code}")
|
| 232 |
print(f" Prompt: '{original_prompt}'")
|
|
|
|
|
|
|
| 233 |
print(f" Details: '{error_text[:200]}'\n") # Add newline
|
| 234 |
|
| 235 |
# User-facing messages remain the same
|
|
@@ -247,6 +271,8 @@ def query(prompt, negative_prompt, steps=4, cfg_scale=0, seed=-1, width=1024, he
|
|
| 247 |
print("FAILED:")
|
| 248 |
print(f" Reason: Unexpected Error During Generation")
|
| 249 |
print(f" Prompt: '{original_prompt}'")
|
|
|
|
|
|
|
| 250 |
print(f" Error: {e}\n") # Add newline
|
| 251 |
traceback.print_exc()
|
| 252 |
return None, f"<p style='color: red; text-align: center;'>An unexpected error occurred. Please check logs.</p>"
|
|
|
|
| 67 |
original_prompt = prompt
|
| 68 |
|
| 69 |
# Translation
|
| 70 |
+
translated_prompt = prompt # Start with original
|
| 71 |
try:
|
| 72 |
translated_prompt = GoogleTranslator(source='auto', target='en').translate(prompt)
|
| 73 |
except Exception as e:
|
| 74 |
print(f"WARNING: Translation failed. Using original prompt.")
|
| 75 |
print(f" Error: {e}")
|
| 76 |
print(f" Prompt: '{original_prompt}'\n") # Add newline
|
| 77 |
+
translated_prompt = prompt # Ensure it's the original if translation fails
|
| 78 |
|
| 79 |
# --- OpenAI Moderation Check ---
|
| 80 |
if openai_client:
|
|
|
|
| 89 |
print("BLOCKED:")
|
| 90 |
print(f" Reason: sexual/minors")
|
| 91 |
print(f" Prompt: '{original_prompt}'")
|
| 92 |
+
if translated_prompt != original_prompt:
|
| 93 |
+
print(f" Translated: '{translated_prompt}'")
|
| 94 |
+
print("") # Add newline
|
| 95 |
return None, "<p style='color: red; text-align: center;'>Prompt violates safety guidelines. Generation blocked.</p>"
|
| 96 |
|
| 97 |
except AuthenticationError:
|
|
|
|
| 108 |
print("BLOCKED:")
|
| 109 |
print(f" Reason: OpenAI Connection Error")
|
| 110 |
print(f" Prompt: '{original_prompt}'")
|
| 111 |
+
if translated_prompt != original_prompt:
|
| 112 |
+
print(f" Translated: '{translated_prompt}'")
|
| 113 |
print(f" Error: {e}\n") # Add newline
|
| 114 |
return None, "<p style='color: red; text-align: center;'>Safety check failed. Please try again later.</p>"
|
| 115 |
except Exception as e:
|
| 116 |
print("BLOCKED:")
|
| 117 |
print(f" Reason: OpenAI Unexpected Error")
|
| 118 |
print(f" Prompt: '{original_prompt}'")
|
| 119 |
+
if translated_prompt != original_prompt:
|
| 120 |
+
print(f" Translated: '{translated_prompt}'")
|
| 121 |
print(f" Error: {e}\n") # Add newline
|
| 122 |
traceback.print_exc() # Keep traceback for unexpected errors
|
| 123 |
return None, "<p style='color: red; text-align: center;'>An unexpected error occurred during safety check. Generation blocked.</p>"
|
| 124 |
else:
|
| 125 |
print(f"WARNING: OpenAI client not available. Skipping moderation.")
|
| 126 |
+
print(f" Prompt: '{original_prompt}'") # Log original prompt even if skipping
|
| 127 |
+
if translated_prompt != original_prompt:
|
| 128 |
+
print(f" (Would use translated: '{translated_prompt}')")
|
| 129 |
+
print("") # Add newline
|
| 130 |
|
| 131 |
# --- Proceed with Generation ---
|
| 132 |
final_prompt = f"{translated_prompt} | ultra detail, ultra elaboration, ultra quality, perfect."
|
|
|
|
| 156 |
print("FAILED:")
|
| 157 |
print(f" Reason: Invalid Image Data (Empty/Small)")
|
| 158 |
print(f" Prompt: '{original_prompt}'")
|
| 159 |
+
if translated_prompt != original_prompt:
|
| 160 |
+
print(f" Translated: '{translated_prompt}'")
|
| 161 |
print(f" Length: {len(image_bytes)}\n") # Add newline
|
| 162 |
return None, "<p style='color: red; text-align: center;'>API returned invalid image data.</p>"
|
| 163 |
|
|
|
|
| 167 |
print("FAILED:")
|
| 168 |
print(f" Reason: Image Processing Error")
|
| 169 |
print(f" Prompt: '{original_prompt}'")
|
| 170 |
+
if translated_prompt != original_prompt:
|
| 171 |
+
print(f" Translated: '{translated_prompt}'")
|
| 172 |
print(f" Error: {img_err}\n") # Add newline
|
| 173 |
return None, "<p style='color: red; text-align: center;'>Failed to process image data from API.</p>"
|
| 174 |
|
|
|
|
| 184 |
print("FAILED:")
|
| 185 |
print(f" Reason: Image Save Verification Error")
|
| 186 |
print(f" Prompt: '{original_prompt}'")
|
| 187 |
+
if translated_prompt != original_prompt:
|
| 188 |
+
print(f" Translated: '{translated_prompt}'")
|
| 189 |
print(f" Path: '{save_path}'\n") # Add newline
|
| 190 |
return image, "<p style='color: red; text-align: center;'>Internal Error: Failed to confirm image file save.</p>"
|
| 191 |
|
|
|
|
| 205 |
# *** SUCCESS LOG ***
|
| 206 |
print("SUCCESS:")
|
| 207 |
print(f" Prompt: '{original_prompt}'")
|
| 208 |
+
if translated_prompt != original_prompt:
|
| 209 |
+
print(f" Translated: '{translated_prompt}'")
|
| 210 |
print(f" {arinteli_url}\n") # Add newline
|
| 211 |
return image, download_html
|
| 212 |
|
|
|
|
| 214 |
print("FAILED:")
|
| 215 |
print(f" Reason: Image Save IO Error")
|
| 216 |
print(f" Prompt: '{original_prompt}'")
|
| 217 |
+
if translated_prompt != original_prompt:
|
| 218 |
+
print(f" Translated: '{translated_prompt}'")
|
| 219 |
print(f" Path: '{save_path}'")
|
| 220 |
print(f" Error: {save_err}\n") # Add newline
|
| 221 |
traceback.print_exc()
|
|
|
|
| 224 |
print("FAILED:")
|
| 225 |
print(f" Reason: Link Creation/Save Unexpected Error")
|
| 226 |
print(f" Prompt: '{original_prompt}'")
|
| 227 |
+
if translated_prompt != original_prompt:
|
| 228 |
+
print(f" Translated: '{translated_prompt}'")
|
| 229 |
print(f" Error: {e}\n") # Add newline
|
| 230 |
traceback.print_exc()
|
| 231 |
return image, "<p style='color: red; text-align: center;'>Internal Error creating download link.</p>"
|
|
|
|
| 252 |
print("FAILED:")
|
| 253 |
print(f" Reason: HF API HTTP Error {status_code}")
|
| 254 |
print(f" Prompt: '{original_prompt}'")
|
| 255 |
+
if translated_prompt != original_prompt:
|
| 256 |
+
print(f" Translated: '{translated_prompt}'")
|
| 257 |
print(f" Details: '{error_text[:200]}'\n") # Add newline
|
| 258 |
|
| 259 |
# User-facing messages remain the same
|
|
|
|
| 271 |
print("FAILED:")
|
| 272 |
print(f" Reason: Unexpected Error During Generation")
|
| 273 |
print(f" Prompt: '{original_prompt}'")
|
| 274 |
+
if translated_prompt != original_prompt:
|
| 275 |
+
print(f" Translated: '{translated_prompt}'")
|
| 276 |
print(f" Error: {e}\n") # Add newline
|
| 277 |
traceback.print_exc()
|
| 278 |
return None, f"<p style='color: red; text-align: center;'>An unexpected error occurred. Please check logs.</p>"
|