Update app.py
Browse files
app.py
CHANGED
@@ -4,67 +4,198 @@ import io
|
|
4 |
import random
|
5 |
import os
|
6 |
import time
|
7 |
-
from PIL import Image
|
8 |
from deep_translator import GoogleTranslator
|
9 |
import json
|
|
|
|
|
|
|
10 |
|
11 |
# Project by Nymbo
|
12 |
|
|
|
13 |
API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell"
|
14 |
API_TOKEN = os.getenv("HF_READ_TOKEN")
|
15 |
-
|
16 |
-
|
|
|
|
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
key = random.randint(0, 999)
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
37 |
payload = {
|
38 |
-
"inputs":
|
39 |
-
"
|
40 |
"steps": steps,
|
41 |
-
"
|
42 |
"seed": seed if seed != -1 else random.randint(1, 1000000000),
|
43 |
-
"strength": strength,
|
44 |
"parameters": {
|
45 |
-
"width": width,
|
46 |
-
"height": height
|
|
|
47 |
}
|
|
|
|
|
48 |
}
|
49 |
|
50 |
-
#
|
51 |
-
response = requests.post(API_URL, headers=headers, json=payload, timeout=timeout)
|
52 |
-
if response.status_code != 200:
|
53 |
-
print(f"Error: Failed to get image. Response status: {response.status_code}")
|
54 |
-
print(f"Response content: {response.text}")
|
55 |
-
if response.status_code == 503:
|
56 |
-
raise gr.Error(f"{response.status_code} : The model is being loaded")
|
57 |
-
raise gr.Error(f"{response.status_code}")
|
58 |
-
|
59 |
try:
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
image_bytes = response.content
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
except Exception as e:
|
66 |
-
print(f"
|
67 |
-
|
|
|
|
|
68 |
|
69 |
# CSS to style the app
|
70 |
css = """
|
@@ -76,44 +207,56 @@ css = """
|
|
76 |
textarea:focus {
|
77 |
background: #0d1117 !important;
|
78 |
}
|
|
|
|
|
|
|
|
|
79 |
"""
|
80 |
|
81 |
# Build the Gradio UI with Blocks
|
82 |
with gr.Blocks(theme='Nymbo/Nymbo_Theme', css=css) as app:
|
83 |
-
# Add a title to the app
|
84 |
gr.HTML("<center><h1>FLUX.1-Schnell</h1></center>")
|
85 |
-
|
86 |
-
# Container for all the UI elements
|
87 |
with gr.Column(elem_id="app-container"):
|
88 |
-
# Add a text input for the main prompt
|
89 |
with gr.Row():
|
90 |
with gr.Column(elem_id="prompt-container"):
|
91 |
with gr.Row():
|
92 |
text_prompt = gr.Textbox(label="Prompt", placeholder="Enter a prompt here", lines=2, elem_id="prompt-text-input")
|
93 |
-
|
94 |
-
# Accordion for advanced settings
|
95 |
with gr.Row():
|
96 |
with gr.Accordion("Advanced Settings", open=False):
|
97 |
negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="What should not be in the image", value="(deformed, distorted, disfigured), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation, misspellings, typos", lines=3, elem_id="negative-prompt-text-input")
|
98 |
with gr.Row():
|
99 |
width = gr.Slider(label="Width", value=1024, minimum=64, maximum=1216, step=32)
|
100 |
height = gr.Slider(label="Height", value=1024, minimum=64, maximum=1216, step=32)
|
101 |
-
steps = gr.Slider(label="Sampling steps", value=
|
102 |
-
cfg = gr.Slider(label="CFG Scale", value=7, minimum=1, maximum=20, step=1)
|
103 |
-
strength
|
104 |
-
seed = gr.Slider(label="Seed", value=-1, minimum=-1, maximum=1000000000, step=1
|
105 |
-
method = gr.Radio(label="Sampling method", value="DPM++ 2M Karras", choices=["DPM++ 2M Karras", "DPM++ SDE Karras", "Euler", "Euler a", "Heun", "DDIM"])
|
106 |
|
107 |
-
# Add a button to trigger the image generation
|
108 |
with gr.Row():
|
109 |
text_button = gr.Button("Run", variant='primary', elem_id="gen-button")
|
110 |
-
|
111 |
-
#
|
112 |
with gr.Row():
|
113 |
image_output = gr.Image(type="pil", label="Image Output", elem_id="gallery")
|
114 |
-
|
115 |
-
|
116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
|
118 |
-
# Launch the Gradio app
|
119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
4 |
import random
|
5 |
import os
|
6 |
import time
|
7 |
+
from PIL import Image, UnidentifiedImageError
|
8 |
from deep_translator import GoogleTranslator
|
9 |
import json
|
10 |
+
import uuid
|
11 |
+
from urllib.parse import quote
|
12 |
+
import traceback
|
13 |
|
14 |
# Project by Nymbo
|
15 |
|
16 |
+
# --- Constants ---
|
17 |
API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell"
|
18 |
API_TOKEN = os.getenv("HF_READ_TOKEN")
|
19 |
+
if not API_TOKEN:
|
20 |
+
print("WARNING: HF_READ_TOKEN environment variable not set. API calls may fail.")
|
21 |
+
headers = {"Authorization": f"Bearer {API_TOKEN}"} if API_TOKEN else {}
|
22 |
+
timeout = 100 # seconds for API call timeout
|
23 |
|
24 |
+
IMAGE_DIR = "temp_generated_images" # Directory to store temporary images
|
25 |
+
ARINTELLI_REDIRECT_BASE = "https://arintelli.com/app/" # Your redirector URL
|
26 |
+
|
27 |
+
# --- Ensure temporary directory exists ---
|
28 |
+
try:
|
29 |
+
os.makedirs(IMAGE_DIR, exist_ok=True)
|
30 |
+
print(f"Confirmed temporary image directory exists: {IMAGE_DIR}")
|
31 |
+
except OSError as e:
|
32 |
+
print(f"ERROR: Could not create directory {IMAGE_DIR}: {e}")
|
33 |
+
# This is critical, so raise an error to prevent app start if dir fails
|
34 |
+
raise gr.Error(f"Fatal Error: Cannot create temporary image directory: {e}")
|
35 |
+
|
36 |
+
# --- Get Absolute Path for allowed_paths ---
|
37 |
+
# This needs to be done *before* calling launch()
|
38 |
+
absolute_image_dir = os.path.abspath(IMAGE_DIR)
|
39 |
+
print(f"Absolute path for allowed_paths: {absolute_image_dir}")
|
40 |
+
|
41 |
+
# Function to query the API and return the generated image and download link
|
42 |
+
def query(prompt, negative_prompt="", steps=30, cfg_scale=7, seed=-1, width=1024, height=1024):
|
43 |
+
# Removed sampler and strength as they are not explicitly used in the payload below
|
44 |
+
# Note: If the API endpoint *does* support sampler/strength, add them back to the payload
|
45 |
+
if not prompt or not prompt.strip():
|
46 |
+
print("Empty prompt received.")
|
47 |
+
# Return None for image and an informative message for the HTML component
|
48 |
+
return None, "<p style='color: orange; text-align: center;'>Please enter a prompt.</p>"
|
49 |
|
50 |
key = random.randint(0, 999)
|
51 |
+
print(f"\n--- Generation {key} Started ---")
|
52 |
+
|
53 |
+
# Translation
|
54 |
+
try:
|
55 |
+
translated_prompt = GoogleTranslator(source='auto', target='en').translate(prompt)
|
56 |
+
print(f'Generation {key} translation: {translated_prompt}')
|
57 |
+
except Exception as e:
|
58 |
+
print(f"Translation failed: {e}. Using original prompt.")
|
59 |
+
translated_prompt = prompt # Fallback to original if translation fails
|
60 |
+
|
61 |
+
# Add suffix to prompt
|
62 |
+
final_prompt = f"{translated_prompt} | ultra detail, ultra elaboration, ultra quality, perfect."
|
63 |
+
print(f'Generation {key} final prompt: {final_prompt}')
|
64 |
+
|
65 |
+
# Prepare the payload for the API call
|
66 |
payload = {
|
67 |
+
"inputs": final_prompt,
|
68 |
+
"negative_prompt": negative_prompt, # Assuming API accepts negative_prompt
|
69 |
"steps": steps,
|
70 |
+
"guidance_scale": cfg_scale, # API often uses guidance_scale
|
71 |
"seed": seed if seed != -1 else random.randint(1, 1000000000),
|
|
|
72 |
"parameters": {
|
73 |
+
"width": width,
|
74 |
+
"height": height,
|
75 |
+
# If the API supports other params like 'steps', 'guidance_scale', they might belong here or top-level
|
76 |
}
|
77 |
+
# Removed 'is_negative' as negative_prompt is usually passed directly
|
78 |
+
# Removed 'strength' and 'sampler' as they weren't in the target API structure
|
79 |
}
|
80 |
|
81 |
+
# API Call Section
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
try:
|
83 |
+
print(f"Sending request to API: {API_URL}")
|
84 |
+
if not headers:
|
85 |
+
print("WARNING: Authorization header is missing (HF_READ_TOKEN not set?)")
|
86 |
+
return None, "<p style='color: red; text-align: center;'>Configuration Error: API Token missing.</p>"
|
87 |
+
|
88 |
+
response = requests.post(API_URL, headers=headers, json=payload, timeout=timeout)
|
89 |
+
response.raise_for_status()
|
90 |
+
|
91 |
image_bytes = response.content
|
92 |
+
if not image_bytes or len(image_bytes) < 100:
|
93 |
+
print(f"Error: Received empty or very small response content (length: {len(image_bytes)}). Potential API issue.")
|
94 |
+
return None, "<p style='color: red; text-align: center;'>API returned invalid image data.</p>"
|
95 |
+
|
96 |
+
try:
|
97 |
+
image = Image.open(io.BytesIO(image_bytes))
|
98 |
+
print(f"Image received and opened successfully. Format: {image.format}, Size: {image.size}")
|
99 |
+
except UnidentifiedImageError as img_err:
|
100 |
+
print(f"Error: Could not identify or open image from API response bytes: {img_err}")
|
101 |
+
return None, "<p style='color: red; text-align: center;'>Failed to process image data from API.</p>"
|
102 |
+
|
103 |
+
print(f'Generation {key} API call successful!')
|
104 |
+
|
105 |
+
# --- Save image and create download link ---
|
106 |
+
filename = f"{int(time.time())}_{uuid.uuid4().hex[:8]}.png"
|
107 |
+
save_path = os.path.join(IMAGE_DIR, filename)
|
108 |
+
absolute_save_path = os.path.abspath(save_path)
|
109 |
+
|
110 |
+
try:
|
111 |
+
print(f"Attempting to save image to: {absolute_save_path}")
|
112 |
+
image.save(save_path, "PNG")
|
113 |
+
|
114 |
+
if os.path.exists(save_path):
|
115 |
+
file_size = os.path.getsize(save_path)
|
116 |
+
print(f"SUCCESS: Image confirmed saved to: {save_path} (Absolute: {absolute_save_path})")
|
117 |
+
print(f"Saved file size: {file_size} bytes")
|
118 |
+
if file_size < 100:
|
119 |
+
print(f"WARNING: Saved file {save_path} is very small ({file_size} bytes). May indicate an issue.")
|
120 |
+
else:
|
121 |
+
print(f"CRITICAL ERROR: File NOT found at {save_path} (Absolute: {absolute_save_path}) immediately after saving!")
|
122 |
+
return image, "<p style='color: red; text-align: center;'>Internal Error: Failed to confirm image file save.</p>"
|
123 |
+
|
124 |
+
# Determine space name (adjust logic if API_URL format differs)
|
125 |
+
try:
|
126 |
+
space_name = "greendra-flux-1-schnell-serverless"
|
127 |
+
# A more robust way might involve getting the space ID from env vars if available
|
128 |
+
except IndexError:
|
129 |
+
print("WARNING: Could not reliably determine space name from API_URL. Using a default.")
|
130 |
+
space_name = "unknown-flux-space" # Provide a fallback
|
131 |
+
print(f"Current space name: {space_name}")
|
132 |
+
|
133 |
+
relative_file_url = f"/gradio_api/file={save_path}"
|
134 |
+
print(f"Generated relative file URL for Gradio API: {relative_file_url}")
|
135 |
+
|
136 |
+
encoded_file_url = quote(relative_file_url)
|
137 |
+
arintelli_url = f"{ARINTELLI_REDIRECT_BASE}?download_url={encoded_file_url}&space_name={space_name}"
|
138 |
+
print(f"Generated redirect link: {arintelli_url}")
|
139 |
+
|
140 |
+
# Use Gradio's primary button style for the link
|
141 |
+
download_html = (
|
142 |
+
f'<div style="text-align: center; margin-top: 15px;">' # Added margin-top
|
143 |
+
f'<a href="{arintelli_url}" target="_blank" class="gr-button gr-button-lg gr-button-primary">'
|
144 |
+
f'Download Image'
|
145 |
+
f'</a>'
|
146 |
+
f'</div>'
|
147 |
+
)
|
148 |
+
|
149 |
+
print(f"--- Generation {key} Completed Successfully ---")
|
150 |
+
return image, download_html
|
151 |
+
|
152 |
+
except (OSError, IOError) as save_err:
|
153 |
+
print(f"CRITICAL ERROR: Failed to save image to {save_path} (Absolute: {absolute_save_path}): {save_err}")
|
154 |
+
traceback.print_exc()
|
155 |
+
return image, f"<p style='color: red; text-align: center;'>Internal Error: Failed to save image file. Details: {save_err}</p>"
|
156 |
+
except Exception as e:
|
157 |
+
print(f"Error during link creation or unexpected save issue: {e}")
|
158 |
+
traceback.print_exc()
|
159 |
+
return image, "<p style='color: red; text-align: center;'>Internal Error creating download link.</p>"
|
160 |
+
|
161 |
+
# --- Exception Handling for API Call ---
|
162 |
+
except requests.exceptions.Timeout:
|
163 |
+
print(f"Error: Request timed out after {timeout} seconds.")
|
164 |
+
return None, "<p style='color: red; text-align: center;'>Request timed out. The model is taking too long.</p>"
|
165 |
+
except requests.exceptions.HTTPError as e:
|
166 |
+
status_code = e.response.status_code
|
167 |
+
error_text = e.response.text
|
168 |
+
try:
|
169 |
+
error_data = e.response.json()
|
170 |
+
error_text = error_data.get('error', error_text)
|
171 |
+
if isinstance(error_text, dict) and 'message' in error_text:
|
172 |
+
error_text = error_text['message']
|
173 |
+
except json.JSONDecodeError:
|
174 |
+
pass
|
175 |
+
|
176 |
+
print(f"Error: Failed API call. Status: {status_code}, Response: {error_text}")
|
177 |
+
|
178 |
+
if status_code == 503:
|
179 |
+
estimated_time = error_data.get("estimated_time") if 'error_data' in locals() and isinstance(error_data, dict) else None
|
180 |
+
if estimated_time:
|
181 |
+
error_message = f"Model is loading (503), please wait. Est. time: {estimated_time:.1f}s. Try again."
|
182 |
+
else:
|
183 |
+
error_message = f"Service unavailable (503). Model might be loading or down. Try again later."
|
184 |
+
elif status_code == 400:
|
185 |
+
error_message = f"Bad Request (400): Check parameters. API Error: {error_text}"
|
186 |
+
elif status_code == 422:
|
187 |
+
error_message = f"Validation Error (422): Input invalid. API Error: {error_text}"
|
188 |
+
elif status_code == 401 or status_code == 403:
|
189 |
+
error_message = f"Authorization Error ({status_code}): Check your API Token (HF_READ_TOKEN)."
|
190 |
+
else:
|
191 |
+
error_message = f"API Error: {status_code}. Details: {error_text}"
|
192 |
+
|
193 |
+
return None, f"<p style='color: red; text-align: center;'>{error_message}</p>"
|
194 |
except Exception as e:
|
195 |
+
print(f"An unexpected error occurred: {e}")
|
196 |
+
traceback.print_exc()
|
197 |
+
return None, f"<p style='color: red; text-align: center;'>An unexpected error occurred: {e}</p>"
|
198 |
+
|
199 |
|
200 |
# CSS to style the app
|
201 |
css = """
|
|
|
207 |
textarea:focus {
|
208 |
background: #0d1117 !important;
|
209 |
}
|
210 |
+
#download-link-container p { /* Style error/status messages in the HTML component */
|
211 |
+
margin-top: 10px;
|
212 |
+
font-size: 0.9em;
|
213 |
+
}
|
214 |
"""
|
215 |
|
216 |
# Build the Gradio UI with Blocks
|
217 |
with gr.Blocks(theme='Nymbo/Nymbo_Theme', css=css) as app:
|
|
|
218 |
gr.HTML("<center><h1>FLUX.1-Schnell</h1></center>")
|
219 |
+
|
|
|
220 |
with gr.Column(elem_id="app-container"):
|
|
|
221 |
with gr.Row():
|
222 |
with gr.Column(elem_id="prompt-container"):
|
223 |
with gr.Row():
|
224 |
text_prompt = gr.Textbox(label="Prompt", placeholder="Enter a prompt here", lines=2, elem_id="prompt-text-input")
|
225 |
+
|
|
|
226 |
with gr.Row():
|
227 |
with gr.Accordion("Advanced Settings", open=False):
|
228 |
negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="What should not be in the image", value="(deformed, distorted, disfigured), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation, misspellings, typos", lines=3, elem_id="negative-prompt-text-input")
|
229 |
with gr.Row():
|
230 |
width = gr.Slider(label="Width", value=1024, minimum=64, maximum=1216, step=32)
|
231 |
height = gr.Slider(label="Height", value=1024, minimum=64, maximum=1216, step=32)
|
232 |
+
steps = gr.Slider(label="Sampling steps", value=30, minimum=1, maximum=100, step=1) # Default updated based on query function default
|
233 |
+
cfg = gr.Slider(label="CFG Scale (guidance_scale)", value=7, minimum=1, maximum=20, step=1) # Label updated
|
234 |
+
# Removed strength and sampler sliders as they are not passed to query
|
235 |
+
seed = gr.Slider(label="Seed", value=-1, minimum=-1, maximum=1000000000, step=1, info="Set to -1 for random seed")
|
|
|
236 |
|
|
|
237 |
with gr.Row():
|
238 |
text_button = gr.Button("Run", variant='primary', elem_id="gen-button")
|
239 |
+
|
240 |
+
# --- Output Components ---
|
241 |
with gr.Row():
|
242 |
image_output = gr.Image(type="pil", label="Image Output", elem_id="gallery")
|
243 |
+
with gr.Row():
|
244 |
+
# HTML component to display status messages or the download link
|
245 |
+
download_link_display = gr.HTML(elem_id="download-link-container")
|
246 |
+
|
247 |
+
# Bind the button to the query function
|
248 |
+
text_button.click(
|
249 |
+
query,
|
250 |
+
# Ensure inputs match the query function definition
|
251 |
+
inputs=[text_prompt, negative_prompt, steps, cfg, seed, width, height],
|
252 |
+
# Outputs go to the image and HTML components
|
253 |
+
outputs=[image_output, download_link_display]
|
254 |
+
)
|
255 |
|
256 |
+
# Launch the Gradio app with allowed_paths
|
257 |
+
print("Starting Gradio app...")
|
258 |
+
app.launch(
|
259 |
+
show_api=False,
|
260 |
+
share=False,
|
261 |
+
allowed_paths=[absolute_image_dir] # Added allowed_paths
|
262 |
+
)
|