Update app.py
Browse files
app.py
CHANGED
|
@@ -59,14 +59,13 @@ def query(prompt, negative_prompt, steps=35, cfg_scale=7, seed=-1, width=1024, h
|
|
| 59 |
try:
|
| 60 |
# Using 'auto' source detection
|
| 61 |
translated_prompt = GoogleTranslator(source='auto', target='en').translate(prompt)
|
| 62 |
-
print(f'Generation {key} translation: {translated_prompt}')
|
| 63 |
except Exception as e:
|
| 64 |
print(f"Translation failed: {e}. Using original prompt.")
|
| 65 |
translated_prompt = prompt # Fallback to original if translation fails
|
| 66 |
|
| 67 |
# Add suffix to prompt
|
| 68 |
final_prompt = f"{translated_prompt} | ultra detail, ultra elaboration, ultra quality, perfect."
|
| 69 |
-
print(f'Generation {key}
|
| 70 |
|
| 71 |
# Prepare payload for API call
|
| 72 |
payload = {
|
|
@@ -84,7 +83,6 @@ def query(prompt, negative_prompt, steps=35, cfg_scale=7, seed=-1, width=1024, h
|
|
| 84 |
|
| 85 |
# API Call Section
|
| 86 |
try:
|
| 87 |
-
print(f"Sending request to API: {API_URL}")
|
| 88 |
if not headers:
|
| 89 |
print("WARNING: Authorization header is missing (HF_READ_TOKEN not set?)")
|
| 90 |
# Handle error appropriately - maybe return an error message
|
|
@@ -101,7 +99,6 @@ def query(prompt, negative_prompt, steps=35, cfg_scale=7, seed=-1, width=1024, h
|
|
| 101 |
|
| 102 |
try:
|
| 103 |
image = Image.open(io.BytesIO(image_bytes))
|
| 104 |
-
print(f"Image received and opened successfully. Format: {image.format}, Size: {image.size}")
|
| 105 |
except UnidentifiedImageError as img_err:
|
| 106 |
print(f"Error: Could not identify or open image from API response bytes: {img_err}")
|
| 107 |
# Optionally save the invalid bytes for debugging
|
|
@@ -110,8 +107,6 @@ def query(prompt, negative_prompt, steps=35, cfg_scale=7, seed=-1, width=1024, h
|
|
| 110 |
# print(f"Saved problematic bytes to {error_bytes_path}")
|
| 111 |
return None, "<p style='color: red; text-align: center;'>Failed to process image data from API.</p>"
|
| 112 |
|
| 113 |
-
print(f'Generation {key} API call successful!')
|
| 114 |
-
|
| 115 |
# --- Save image and create download link ---
|
| 116 |
filename = f"{int(time.time())}_{uuid.uuid4().hex[:8]}.png"
|
| 117 |
# save_path is relative to the script's execution directory
|
|
@@ -119,15 +114,12 @@ def query(prompt, negative_prompt, steps=35, cfg_scale=7, seed=-1, width=1024, h
|
|
| 119 |
absolute_save_path = os.path.abspath(save_path) # Get absolute path for logging
|
| 120 |
|
| 121 |
try:
|
| 122 |
-
print(f"Attempting to save image to: {absolute_save_path}")
|
| 123 |
# Save image explicitly as PNG
|
| 124 |
image.save(save_path, "PNG")
|
| 125 |
|
| 126 |
# *** Verify file exists after saving ***
|
| 127 |
if os.path.exists(save_path):
|
| 128 |
file_size = os.path.getsize(save_path)
|
| 129 |
-
print(f"SUCCESS: Image confirmed saved to: {save_path} (Absolute: {absolute_save_path})")
|
| 130 |
-
print(f"Saved file size: {file_size} bytes")
|
| 131 |
if file_size < 100: # Warn if the saved file is suspiciously small
|
| 132 |
print(f"WARNING: Saved file {save_path} is very small ({file_size} bytes). May indicate an issue.")
|
| 133 |
# Optionally return a warning message in the UI
|
|
@@ -139,15 +131,13 @@ def query(prompt, negative_prompt, steps=35, cfg_scale=7, seed=-1, width=1024, h
|
|
| 139 |
|
| 140 |
# Get current space name from the API URL
|
| 141 |
space_name = "greendra-stable-diffusion-3-5-large-serverless"
|
| 142 |
-
print(f"Current space name: {space_name}")
|
| 143 |
|
| 144 |
relative_file_url = f"/gradio_api/file={save_path}"
|
| 145 |
-
print(f"Generated relative file URL for Gradio API: {relative_file_url}")
|
| 146 |
|
| 147 |
encoded_file_url = quote(relative_file_url)
|
| 148 |
# Add space_name parameter to the URL
|
| 149 |
arintelli_url = f"{ARINTELLI_REDIRECT_BASE}?download_url={encoded_file_url}&space_name={space_name}"
|
| 150 |
-
print(f"
|
| 151 |
|
| 152 |
# Use simpler button style like the Run button
|
| 153 |
download_html = (
|
|
@@ -158,7 +148,7 @@ def query(prompt, negative_prompt, steps=35, cfg_scale=7, seed=-1, width=1024, h
|
|
| 158 |
f'</div>'
|
| 159 |
)
|
| 160 |
|
| 161 |
-
print(f"--- Generation {key}
|
| 162 |
return image, download_html
|
| 163 |
|
| 164 |
except (OSError, IOError) as save_err:
|
|
|
|
| 59 |
try:
|
| 60 |
# Using 'auto' source detection
|
| 61 |
translated_prompt = GoogleTranslator(source='auto', target='en').translate(prompt)
|
|
|
|
| 62 |
except Exception as e:
|
| 63 |
print(f"Translation failed: {e}. Using original prompt.")
|
| 64 |
translated_prompt = prompt # Fallback to original if translation fails
|
| 65 |
|
| 66 |
# Add suffix to prompt
|
| 67 |
final_prompt = f"{translated_prompt} | ultra detail, ultra elaboration, ultra quality, perfect."
|
| 68 |
+
print(f'Generation {key} prompt: {final_prompt}')
|
| 69 |
|
| 70 |
# Prepare payload for API call
|
| 71 |
payload = {
|
|
|
|
| 83 |
|
| 84 |
# API Call Section
|
| 85 |
try:
|
|
|
|
| 86 |
if not headers:
|
| 87 |
print("WARNING: Authorization header is missing (HF_READ_TOKEN not set?)")
|
| 88 |
# Handle error appropriately - maybe return an error message
|
|
|
|
| 99 |
|
| 100 |
try:
|
| 101 |
image = Image.open(io.BytesIO(image_bytes))
|
|
|
|
| 102 |
except UnidentifiedImageError as img_err:
|
| 103 |
print(f"Error: Could not identify or open image from API response bytes: {img_err}")
|
| 104 |
# Optionally save the invalid bytes for debugging
|
|
|
|
| 107 |
# print(f"Saved problematic bytes to {error_bytes_path}")
|
| 108 |
return None, "<p style='color: red; text-align: center;'>Failed to process image data from API.</p>"
|
| 109 |
|
|
|
|
|
|
|
| 110 |
# --- Save image and create download link ---
|
| 111 |
filename = f"{int(time.time())}_{uuid.uuid4().hex[:8]}.png"
|
| 112 |
# save_path is relative to the script's execution directory
|
|
|
|
| 114 |
absolute_save_path = os.path.abspath(save_path) # Get absolute path for logging
|
| 115 |
|
| 116 |
try:
|
|
|
|
| 117 |
# Save image explicitly as PNG
|
| 118 |
image.save(save_path, "PNG")
|
| 119 |
|
| 120 |
# *** Verify file exists after saving ***
|
| 121 |
if os.path.exists(save_path):
|
| 122 |
file_size = os.path.getsize(save_path)
|
|
|
|
|
|
|
| 123 |
if file_size < 100: # Warn if the saved file is suspiciously small
|
| 124 |
print(f"WARNING: Saved file {save_path} is very small ({file_size} bytes). May indicate an issue.")
|
| 125 |
# Optionally return a warning message in the UI
|
|
|
|
| 131 |
|
| 132 |
# Get current space name from the API URL
|
| 133 |
space_name = "greendra-stable-diffusion-3-5-large-serverless"
|
|
|
|
| 134 |
|
| 135 |
relative_file_url = f"/gradio_api/file={save_path}"
|
|
|
|
| 136 |
|
| 137 |
encoded_file_url = quote(relative_file_url)
|
| 138 |
# Add space_name parameter to the URL
|
| 139 |
arintelli_url = f"{ARINTELLI_REDIRECT_BASE}?download_url={encoded_file_url}&space_name={space_name}"
|
| 140 |
+
print(f"{arintelli_url}")
|
| 141 |
|
| 142 |
# Use simpler button style like the Run button
|
| 143 |
download_html = (
|
|
|
|
| 148 |
f'</div>'
|
| 149 |
)
|
| 150 |
|
| 151 |
+
print(f"--- Generation {key} Done ---")
|
| 152 |
return image, download_html
|
| 153 |
|
| 154 |
except (OSError, IOError) as save_err:
|