Update app.py
Browse files
app.py
CHANGED
@@ -63,13 +63,14 @@ def query(prompt, negative_prompt="", steps=4, cfg_scale=0, seed=-1, width=1024,
|
|
63 |
original_prompt = prompt
|
64 |
|
65 |
# Translation
|
|
|
66 |
try:
|
67 |
translated_prompt = GoogleTranslator(source='auto', target='en').translate(prompt)
|
68 |
except Exception as e:
|
69 |
print(f"WARNING: Translation failed. Using original prompt.")
|
70 |
print(f" Error: {e}")
|
71 |
print(f" Prompt: '{original_prompt}'\n") # Add newline
|
72 |
-
translated_prompt = prompt
|
73 |
|
74 |
# --- OpenAI Moderation Check ---
|
75 |
if openai_client:
|
@@ -84,7 +85,9 @@ def query(prompt, negative_prompt="", steps=4, cfg_scale=0, seed=-1, width=1024,
|
|
84 |
print("BLOCKED:")
|
85 |
print(f" Reason: sexual/minors")
|
86 |
print(f" Prompt: '{original_prompt}'")
|
87 |
-
|
|
|
|
|
88 |
return None, "<p style='color: red; text-align: center;'>Prompt violates safety guidelines. Generation blocked.</p>"
|
89 |
|
90 |
except AuthenticationError:
|
@@ -101,18 +104,25 @@ def query(prompt, negative_prompt="", steps=4, cfg_scale=0, seed=-1, width=1024,
|
|
101 |
print("BLOCKED:")
|
102 |
print(f" Reason: OpenAI Connection Error")
|
103 |
print(f" Prompt: '{original_prompt}'")
|
|
|
|
|
104 |
print(f" Error: {e}\n") # Add newline
|
105 |
return None, "<p style='color: red; text-align: center;'>Safety check failed. Please try again later.</p>"
|
106 |
except Exception as e:
|
107 |
print("BLOCKED:")
|
108 |
print(f" Reason: OpenAI Unexpected Error")
|
109 |
print(f" Prompt: '{original_prompt}'")
|
|
|
|
|
110 |
print(f" Error: {e}\n") # Add newline
|
111 |
traceback.print_exc()
|
112 |
return None, "<p style='color: red; text-align: center;'>An unexpected error occurred during safety check. Generation blocked.</p>"
|
113 |
else:
|
114 |
print(f"WARNING: OpenAI client not available. Skipping moderation.")
|
115 |
-
print(f" Prompt: '{original_prompt}'
|
|
|
|
|
|
|
116 |
|
117 |
# --- Proceed with Generation ---
|
118 |
# Add suffix (Ensure consistency with gradio1.py if desired, or keep model-specific suffixes)
|
@@ -148,6 +158,8 @@ def query(prompt, negative_prompt="", steps=4, cfg_scale=0, seed=-1, width=1024,
|
|
148 |
print("FAILED:")
|
149 |
print(f" Reason: Invalid Image Data (Empty/Small)")
|
150 |
print(f" Prompt: '{original_prompt}'")
|
|
|
|
|
151 |
print(f" Length: {len(image_bytes)}\n") # Add newline
|
152 |
return None, "<p style='color: red; text-align: center;'>API returned invalid image data.</p>"
|
153 |
|
@@ -157,6 +169,8 @@ def query(prompt, negative_prompt="", steps=4, cfg_scale=0, seed=-1, width=1024,
|
|
157 |
print("FAILED:")
|
158 |
print(f" Reason: Image Processing Error")
|
159 |
print(f" Prompt: '{original_prompt}'")
|
|
|
|
|
160 |
print(f" Error: {img_err}\n") # Add newline
|
161 |
return None, "<p style='color: red; text-align: center;'>Failed to process image data from API.</p>"
|
162 |
|
@@ -172,6 +186,8 @@ def query(prompt, negative_prompt="", steps=4, cfg_scale=0, seed=-1, width=1024,
|
|
172 |
print("FAILED:")
|
173 |
print(f" Reason: Image Save Verification Error")
|
174 |
print(f" Prompt: '{original_prompt}'")
|
|
|
|
|
175 |
print(f" Path: '{save_path}'\n") # Add newline
|
176 |
return image, "<p style='color: red; text-align: center;'>Internal Error: Failed to confirm image file save.</p>"
|
177 |
|
@@ -192,6 +208,8 @@ def query(prompt, negative_prompt="", steps=4, cfg_scale=0, seed=-1, width=1024,
|
|
192 |
# *** SUCCESS LOG *** (Updated format)
|
193 |
print("SUCCESS:")
|
194 |
print(f" Prompt: '{original_prompt}'")
|
|
|
|
|
195 |
print(f" {arinteli_url}\n") # Add newline
|
196 |
return image, download_html
|
197 |
|
@@ -199,6 +217,8 @@ def query(prompt, negative_prompt="", steps=4, cfg_scale=0, seed=-1, width=1024,
|
|
199 |
print("FAILED:")
|
200 |
print(f" Reason: Image Save IO Error")
|
201 |
print(f" Prompt: '{original_prompt}'")
|
|
|
|
|
202 |
print(f" Path: '{save_path}'")
|
203 |
print(f" Error: {save_err}\n") # Add newline
|
204 |
traceback.print_exc()
|
@@ -207,6 +227,8 @@ def query(prompt, negative_prompt="", steps=4, cfg_scale=0, seed=-1, width=1024,
|
|
207 |
print("FAILED:")
|
208 |
print(f" Reason: Link Creation/Save Unexpected Error")
|
209 |
print(f" Prompt: '{original_prompt}'")
|
|
|
|
|
210 |
print(f" Error: {e}\n") # Add newline
|
211 |
traceback.print_exc()
|
212 |
return image, "<p style='color: red; text-align: center;'>Internal Error creating download link.</p>"
|
@@ -233,6 +255,8 @@ def query(prompt, negative_prompt="", steps=4, cfg_scale=0, seed=-1, width=1024,
|
|
233 |
print("FAILED:")
|
234 |
print(f" Reason: HF API HTTP Error {status_code}")
|
235 |
print(f" Prompt: '{original_prompt}'")
|
|
|
|
|
236 |
print(f" Details: '{error_text[:200]}'\n") # Add newline
|
237 |
|
238 |
# User-facing messages (Keep consistent with previous version of this file)
|
@@ -249,6 +273,8 @@ def query(prompt, negative_prompt="", steps=4, cfg_scale=0, seed=-1, width=1024,
|
|
249 |
print("FAILED:")
|
250 |
print(f" Reason: Unexpected Error During Generation")
|
251 |
print(f" Prompt: '{original_prompt}'")
|
|
|
|
|
252 |
print(f" Error: {e}\n") # Add newline
|
253 |
traceback.print_exc()
|
254 |
return None, f"<p style='color: red; text-align: center;'>An unexpected error occurred: {e}</p>" # Keep specific error for user
|
@@ -272,7 +298,6 @@ textarea:focus {
|
|
272 |
|
273 |
# Build the Gradio UI with Blocks
|
274 |
with gr.Blocks(theme='Nymbo/Nymbo_Theme', css=css) as app:
|
275 |
-
gr.HTML("<center><h1>FLUX.1-Schnell</h1></center>")
|
276 |
|
277 |
with gr.Column(elem_id="app-container"):
|
278 |
with gr.Row():
|
|
|
63 |
original_prompt = prompt
|
64 |
|
65 |
# Translation
|
66 |
+
translated_prompt = prompt # Start with original
|
67 |
try:
|
68 |
translated_prompt = GoogleTranslator(source='auto', target='en').translate(prompt)
|
69 |
except Exception as e:
|
70 |
print(f"WARNING: Translation failed. Using original prompt.")
|
71 |
print(f" Error: {e}")
|
72 |
print(f" Prompt: '{original_prompt}'\n") # Add newline
|
73 |
+
translated_prompt = prompt # Ensure it's the original if translation fails
|
74 |
|
75 |
# --- OpenAI Moderation Check ---
|
76 |
if openai_client:
|
|
|
85 |
print("BLOCKED:")
|
86 |
print(f" Reason: sexual/minors")
|
87 |
print(f" Prompt: '{original_prompt}'")
|
88 |
+
if translated_prompt != original_prompt:
|
89 |
+
print(f" Translated: '{translated_prompt}'")
|
90 |
+
print("") # Add newline
|
91 |
return None, "<p style='color: red; text-align: center;'>Prompt violates safety guidelines. Generation blocked.</p>"
|
92 |
|
93 |
except AuthenticationError:
|
|
|
104 |
print("BLOCKED:")
|
105 |
print(f" Reason: OpenAI Connection Error")
|
106 |
print(f" Prompt: '{original_prompt}'")
|
107 |
+
if translated_prompt != original_prompt:
|
108 |
+
print(f" Translated: '{translated_prompt}'")
|
109 |
print(f" Error: {e}\n") # Add newline
|
110 |
return None, "<p style='color: red; text-align: center;'>Safety check failed. Please try again later.</p>"
|
111 |
except Exception as e:
|
112 |
print("BLOCKED:")
|
113 |
print(f" Reason: OpenAI Unexpected Error")
|
114 |
print(f" Prompt: '{original_prompt}'")
|
115 |
+
if translated_prompt != original_prompt:
|
116 |
+
print(f" Translated: '{translated_prompt}'")
|
117 |
print(f" Error: {e}\n") # Add newline
|
118 |
traceback.print_exc()
|
119 |
return None, "<p style='color: red; text-align: center;'>An unexpected error occurred during safety check. Generation blocked.</p>"
|
120 |
else:
|
121 |
print(f"WARNING: OpenAI client not available. Skipping moderation.")
|
122 |
+
print(f" Prompt: '{original_prompt}'") # Log original prompt even if skipping
|
123 |
+
if translated_prompt != original_prompt:
|
124 |
+
print(f" (Would use translated: '{translated_prompt}')")
|
125 |
+
print("") # Add newline
|
126 |
|
127 |
# --- Proceed with Generation ---
|
128 |
# Add suffix (Ensure consistency with gradio1.py if desired, or keep model-specific suffixes)
|
|
|
158 |
print("FAILED:")
|
159 |
print(f" Reason: Invalid Image Data (Empty/Small)")
|
160 |
print(f" Prompt: '{original_prompt}'")
|
161 |
+
if translated_prompt != original_prompt:
|
162 |
+
print(f" Translated: '{translated_prompt}'")
|
163 |
print(f" Length: {len(image_bytes)}\n") # Add newline
|
164 |
return None, "<p style='color: red; text-align: center;'>API returned invalid image data.</p>"
|
165 |
|
|
|
169 |
print("FAILED:")
|
170 |
print(f" Reason: Image Processing Error")
|
171 |
print(f" Prompt: '{original_prompt}'")
|
172 |
+
if translated_prompt != original_prompt:
|
173 |
+
print(f" Translated: '{translated_prompt}'")
|
174 |
print(f" Error: {img_err}\n") # Add newline
|
175 |
return None, "<p style='color: red; text-align: center;'>Failed to process image data from API.</p>"
|
176 |
|
|
|
186 |
print("FAILED:")
|
187 |
print(f" Reason: Image Save Verification Error")
|
188 |
print(f" Prompt: '{original_prompt}'")
|
189 |
+
if translated_prompt != original_prompt:
|
190 |
+
print(f" Translated: '{translated_prompt}'")
|
191 |
print(f" Path: '{save_path}'\n") # Add newline
|
192 |
return image, "<p style='color: red; text-align: center;'>Internal Error: Failed to confirm image file save.</p>"
|
193 |
|
|
|
208 |
# *** SUCCESS LOG *** (Updated format)
|
209 |
print("SUCCESS:")
|
210 |
print(f" Prompt: '{original_prompt}'")
|
211 |
+
if translated_prompt != original_prompt:
|
212 |
+
print(f" Translated: '{translated_prompt}'")
|
213 |
print(f" {arinteli_url}\n") # Add newline
|
214 |
return image, download_html
|
215 |
|
|
|
217 |
print("FAILED:")
|
218 |
print(f" Reason: Image Save IO Error")
|
219 |
print(f" Prompt: '{original_prompt}'")
|
220 |
+
if translated_prompt != original_prompt:
|
221 |
+
print(f" Translated: '{translated_prompt}'")
|
222 |
print(f" Path: '{save_path}'")
|
223 |
print(f" Error: {save_err}\n") # Add newline
|
224 |
traceback.print_exc()
|
|
|
227 |
print("FAILED:")
|
228 |
print(f" Reason: Link Creation/Save Unexpected Error")
|
229 |
print(f" Prompt: '{original_prompt}'")
|
230 |
+
if translated_prompt != original_prompt:
|
231 |
+
print(f" Translated: '{translated_prompt}'")
|
232 |
print(f" Error: {e}\n") # Add newline
|
233 |
traceback.print_exc()
|
234 |
return image, "<p style='color: red; text-align: center;'>Internal Error creating download link.</p>"
|
|
|
255 |
print("FAILED:")
|
256 |
print(f" Reason: HF API HTTP Error {status_code}")
|
257 |
print(f" Prompt: '{original_prompt}'")
|
258 |
+
if translated_prompt != original_prompt:
|
259 |
+
print(f" Translated: '{translated_prompt}'")
|
260 |
print(f" Details: '{error_text[:200]}'\n") # Add newline
|
261 |
|
262 |
# User-facing messages (Keep consistent with previous version of this file)
|
|
|
273 |
print("FAILED:")
|
274 |
print(f" Reason: Unexpected Error During Generation")
|
275 |
print(f" Prompt: '{original_prompt}'")
|
276 |
+
if translated_prompt != original_prompt:
|
277 |
+
print(f" Translated: '{translated_prompt}'")
|
278 |
print(f" Error: {e}\n") # Add newline
|
279 |
traceback.print_exc()
|
280 |
return None, f"<p style='color: red; text-align: center;'>An unexpected error occurred: {e}</p>" # Keep specific error for user
|
|
|
298 |
|
299 |
# Build the Gradio UI with Blocks
|
300 |
with gr.Blocks(theme='Nymbo/Nymbo_Theme', css=css) as app:
|
|
|
301 |
|
302 |
with gr.Column(elem_id="app-container"):
|
303 |
with gr.Row():
|