Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Update app.py
Browse files
app.py
CHANGED
@@ -3,47 +3,36 @@ import requests
|
|
3 |
import io
|
4 |
import random
|
5 |
import os
|
6 |
-
import time
|
7 |
from PIL import Image
|
8 |
import json
|
|
|
9 |
|
10 |
-
#
|
11 |
-
|
12 |
-
API_TOKEN = os.getenv("HF_READ_TOKEN")
|
13 |
-
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
14 |
-
timeout = 100
|
15 |
-
|
16 |
|
|
|
|
|
|
|
|
|
17 |
|
18 |
def query(prompt, model, custom_lora, is_negative=False, steps=35, cfg_scale=7, sampler="DPM++ 2M Karras", seed=-1, strength=0.7, width=1024, height=1024):
|
19 |
-
# Debug log to indicate function start
|
20 |
print("Starting query function...")
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
print(f"Custom LoRA: {custom_lora}")
|
25 |
-
print(f"Parameters - Steps: {steps}, CFG Scale: {cfg_scale}, Seed: {seed}, Strength: {strength}, Width: {width}, Height: {height}")
|
26 |
-
|
27 |
-
# Check if the prompt is empty or None
|
28 |
-
if prompt == "" or prompt is None:
|
29 |
-
print("Prompt is empty or None. Exiting query function.") # Debug log
|
30 |
-
return None
|
31 |
|
32 |
-
#
|
|
|
|
|
|
|
33 |
key = random.randint(0, 999)
|
34 |
-
|
35 |
-
|
36 |
-
# Randomly select an API token from available options to distribute the load
|
37 |
-
API_TOKEN = random.choice([os.getenv("HF_READ_TOKEN"), os.getenv("HF_READ_TOKEN_2"), os.getenv("HF_READ_TOKEN_3"), os.getenv("HF_READ_TOKEN_4"), os.getenv("HF_READ_TOKEN_5")])
|
38 |
-
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
39 |
-
print(f"Selected API token: {API_TOKEN}") # Debug log
|
40 |
-
|
41 |
-
# Enhance the prompt with additional details for better quality
|
42 |
prompt = f"{prompt} | ultra detail, ultra elaboration, ultra quality, perfect."
|
43 |
-
print(f'Generation {key}: {prompt}')
|
44 |
|
45 |
-
# Set
|
46 |
-
if custom_lora.strip()
|
47 |
API_URL = f"https://api-inference.huggingface.co/models/{custom_lora.strip()}"
|
48 |
else:
|
49 |
if model == 'Stable Diffusion XL':
|
@@ -245,59 +234,38 @@ def query(prompt, model, custom_lora, is_negative=False, steps=35, cfg_scale=7,
|
|
245 |
API_URL = "https://api-inference.huggingface.co/models/artificialguybr/LogoRedmond-LogoLoraForSDXL-V2"
|
246 |
if model == 'epiCPhotoGasm':
|
247 |
API_URL = "https://api-inference.huggingface.co/models/Yntec/epiCPhotoGasm"
|
248 |
-
print(f"API URL set to: {API_URL}") # Debug log
|
249 |
|
250 |
-
#
|
251 |
payload = {
|
252 |
"inputs": prompt,
|
253 |
-
"is_negative": is_negative,
|
254 |
-
"steps": steps,
|
255 |
-
"cfg_scale": cfg_scale,
|
256 |
-
"seed": seed if seed != -1 else random.randint(1, 1000000000),
|
257 |
-
"strength": strength,
|
258 |
"parameters": {
|
259 |
-
"width": width,
|
260 |
-
"height": height
|
261 |
}
|
262 |
}
|
263 |
-
print(f"Payload: {json.dumps(payload, indent=2)}") # Debug log
|
264 |
|
265 |
-
# Make a request to the API to generate the image
|
266 |
try:
|
267 |
-
response = requests.post(API_URL, headers=headers, json=payload, timeout=
|
268 |
-
|
|
|
|
|
|
|
|
|
|
|
269 |
except requests.exceptions.RequestException as e:
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
# Check if the response status is not successful
|
275 |
-
if response.status_code != 200:
|
276 |
-
print(f"Error: Failed to retrieve image. Response status: {response.status_code}") # Debug log
|
277 |
-
print(f"Response content: {response.text}") # Debug log
|
278 |
-
if response.status_code == 400:
|
279 |
-
raise gr.Error(f"{response.status_code}: Bad Request - There might be an issue with the input parameters.")
|
280 |
-
elif response.status_code == 401:
|
281 |
-
raise gr.Error(f"{response.status_code}: Unauthorized - Please check your API token.")
|
282 |
elif response.status_code == 403:
|
283 |
-
|
284 |
-
elif response.status_code == 404:
|
285 |
-
raise gr.Error(f"{response.status_code}: Not Found - The requested model could not be found.")
|
286 |
elif response.status_code == 503:
|
287 |
-
|
288 |
-
|
289 |
-
raise gr.Error(f"{response.status_code}: An unexpected error occurred.")
|
290 |
-
|
291 |
-
try:
|
292 |
-
# Attempt to read the image from the response content
|
293 |
-
image_bytes = response.content
|
294 |
-
image = Image.open(io.BytesIO(image_bytes))
|
295 |
-
print(f'Generation {key} completed! ({prompt})') # Debug log
|
296 |
-
return image
|
297 |
-
except Exception as e:
|
298 |
-
# Handle any errors that occur when opening the image
|
299 |
-
print(f"Error while trying to open image: {e}") # Debug log
|
300 |
-
return None
|
301 |
|
302 |
css = """
|
303 |
footer {
|
@@ -307,20 +275,17 @@ footer {
|
|
307 |
|
308 |
print("Initializing Gradio interface...")
|
309 |
|
310 |
-
# Define the Gradio interface
|
311 |
with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as dalle:
|
312 |
gr.Markdown("# AI Image Generator")
|
313 |
|
314 |
with gr.Row():
|
315 |
with gr.Column(scale=2):
|
316 |
-
# Main prompt input
|
317 |
text_prompt = gr.Textbox(
|
318 |
label="Prompt",
|
319 |
placeholder="Describe what you want to create...",
|
320 |
lines=3
|
321 |
)
|
322 |
|
323 |
-
# Negative prompt
|
324 |
negative_prompt = gr.Textbox(
|
325 |
label="Negative Prompt",
|
326 |
placeholder="What should not be in the image",
|
@@ -328,7 +293,6 @@ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as dalle:
|
|
328 |
lines=2
|
329 |
)
|
330 |
|
331 |
-
# Custom LoRA input
|
332 |
custom_lora = gr.Textbox(
|
333 |
label="Custom LoRA Path (Optional)",
|
334 |
placeholder="e.g., multimodalart/vintage-ads-flux",
|
@@ -336,13 +300,11 @@ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as dalle:
|
|
336 |
)
|
337 |
|
338 |
with gr.Column(scale=1):
|
339 |
-
# Image dimensions
|
340 |
with gr.Group():
|
341 |
gr.Markdown("### Image Settings")
|
342 |
width = gr.Slider(label="Width", value=1024, minimum=512, maximum=1216, step=64)
|
343 |
height = gr.Slider(label="Height", value=1024, minimum=512, maximum=1216, step=64)
|
344 |
|
345 |
-
# Generation parameters
|
346 |
with gr.Group():
|
347 |
gr.Markdown("### Generation Parameters")
|
348 |
steps = gr.Slider(label="Steps", value=35, minimum=1, maximum=100, step=1)
|
@@ -350,7 +312,6 @@ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as dalle:
|
|
350 |
strength = gr.Slider(label="Strength", value=0.7, minimum=0, maximum=1, step=0.1)
|
351 |
seed = gr.Slider(label="Seed (-1 for random)", value=-1, minimum=-1, maximum=1000000000, step=1)
|
352 |
|
353 |
-
# Model selection
|
354 |
with gr.Group():
|
355 |
gr.Markdown("### Model Selection")
|
356 |
model_search = gr.Textbox(
|
@@ -359,7 +320,6 @@ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as dalle:
|
|
359 |
lines=1
|
360 |
)
|
361 |
|
362 |
-
# Updated model list (reordered by popularity/recency)
|
363 |
models_list = [
|
364 |
"Stable Diffusion 3.5 Large",
|
365 |
"Stable Diffusion 3.5 Large Turbo",
|
@@ -369,7 +329,7 @@ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as dalle:
|
|
369 |
"Midjourney",
|
370 |
"DreamPhotoGASM",
|
371 |
"Disney",
|
372 |
-
"Leonardo AI Style Illustration",
|
373 |
"AbsoluteReality 1.8.1",
|
374 |
"Analog Redmond",
|
375 |
"Stable Diffusion 3 Medium",
|
@@ -379,24 +339,20 @@ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as dalle:
|
|
379 |
"Character Design",
|
380 |
"Pixel Art XL",
|
381 |
"3D Sketchfab",
|
382 |
-
"Anime Collection", # Group of anime-related models
|
383 |
"Flux Animex V2",
|
384 |
"Flux Animeo V1",
|
385 |
"Flux AestheticAnime",
|
386 |
"90s Anime Art",
|
387 |
"Softserve Anime",
|
388 |
-
"Artistic Styles", # Group of artistic style models
|
389 |
"Brain Melt Acid Art",
|
390 |
"Retro Comic Flux",
|
391 |
"Purple Dreamy",
|
392 |
"SoftPasty Flux",
|
393 |
-
"Specialized", # Group of specialized models
|
394 |
"Flux Logo Design",
|
395 |
"Product Design",
|
396 |
"Propaganda Poster",
|
397 |
"Movie Board",
|
398 |
-
"Collage Flux"
|
399 |
-
# Additional models...
|
400 |
]
|
401 |
|
402 |
model = gr.Radio(
|
@@ -406,13 +362,6 @@ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as dalle:
|
|
406 |
interactive=True
|
407 |
)
|
408 |
|
409 |
-
def filter_models(search_term):
|
410 |
-
filtered_models = [m for m in models_list if search_term.lower() in m.lower()]
|
411 |
-
return gr.update(choices=filtered_models)
|
412 |
-
|
413 |
-
model_search.change(filter_models, inputs=model_search, outputs=model)
|
414 |
-
|
415 |
-
# Generate button and output
|
416 |
with gr.Row():
|
417 |
generate_btn = gr.Button("Generate Image", variant="primary", size="lg")
|
418 |
|
@@ -423,7 +372,6 @@ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as dalle:
|
|
423 |
show_label=True
|
424 |
)
|
425 |
|
426 |
-
# Set up the generation event
|
427 |
generate_btn.click(
|
428 |
fn=query,
|
429 |
inputs=[
|
@@ -441,6 +389,11 @@ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as dalle:
|
|
441 |
outputs=image_output
|
442 |
)
|
443 |
|
|
|
|
|
|
|
|
|
|
|
444 |
|
445 |
-
|
446 |
-
dalle.launch(show_api=False, share=False)
|
|
|
3 |
import io
|
4 |
import random
|
5 |
import os
|
|
|
6 |
from PIL import Image
|
7 |
import json
|
8 |
+
from dotenv import load_dotenv
|
9 |
|
10 |
+
# Load environment variables
|
11 |
+
load_dotenv()
|
|
|
|
|
|
|
|
|
12 |
|
13 |
+
# Get API token from environment variable
|
14 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
15 |
+
if not HF_TOKEN:
|
16 |
+
raise ValueError("HF_TOKEN environment variable is not set")
|
17 |
|
18 |
def query(prompt, model, custom_lora, is_negative=False, steps=35, cfg_scale=7, sampler="DPM++ 2M Karras", seed=-1, strength=0.7, width=1024, height=1024):
|
|
|
19 |
print("Starting query function...")
|
20 |
+
|
21 |
+
if not prompt:
|
22 |
+
raise gr.Error("Prompt cannot be empty")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
+
# Set headers with API token
|
25 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
26 |
+
|
27 |
+
# Generate a unique key for tracking
|
28 |
key = random.randint(0, 999)
|
29 |
+
|
30 |
+
# Enhance prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
prompt = f"{prompt} | ultra detail, ultra elaboration, ultra quality, perfect."
|
32 |
+
print(f'Generation {key}: {prompt}')
|
33 |
|
34 |
+
# Set API URL based on model selection
|
35 |
+
if custom_lora.strip():
|
36 |
API_URL = f"https://api-inference.huggingface.co/models/{custom_lora.strip()}"
|
37 |
else:
|
38 |
if model == 'Stable Diffusion XL':
|
|
|
234 |
API_URL = "https://api-inference.huggingface.co/models/artificialguybr/LogoRedmond-LogoLoraForSDXL-V2"
|
235 |
if model == 'epiCPhotoGasm':
|
236 |
API_URL = "https://api-inference.huggingface.co/models/Yntec/epiCPhotoGasm"
|
|
|
237 |
|
238 |
+
# Prepare payload
|
239 |
payload = {
|
240 |
"inputs": prompt,
|
241 |
+
"is_negative": is_negative,
|
242 |
+
"steps": steps,
|
243 |
+
"cfg_scale": cfg_scale,
|
244 |
+
"seed": seed if seed != -1 else random.randint(1, 1000000000),
|
245 |
+
"strength": strength,
|
246 |
"parameters": {
|
247 |
+
"width": width,
|
248 |
+
"height": height
|
249 |
}
|
250 |
}
|
|
|
251 |
|
|
|
252 |
try:
|
253 |
+
response = requests.post(API_URL, headers=headers, json=payload, timeout=100)
|
254 |
+
response.raise_for_status()
|
255 |
+
|
256 |
+
image = Image.open(io.BytesIO(response.content))
|
257 |
+
print(f'Generation {key} completed successfully')
|
258 |
+
return image
|
259 |
+
|
260 |
except requests.exceptions.RequestException as e:
|
261 |
+
error_message = f"Request failed: {str(e)}"
|
262 |
+
if response.status_code == 401:
|
263 |
+
error_message = "Invalid API token. Please check your Hugging Face API token."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
264 |
elif response.status_code == 403:
|
265 |
+
error_message = "Access denied. Please check your API token permissions."
|
|
|
|
|
266 |
elif response.status_code == 503:
|
267 |
+
error_message = "Model is currently loading. Please try again in a few moments."
|
268 |
+
raise gr.Error(error_message)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
269 |
|
270 |
css = """
|
271 |
footer {
|
|
|
275 |
|
276 |
print("Initializing Gradio interface...")
|
277 |
|
|
|
278 |
with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as dalle:
|
279 |
gr.Markdown("# AI Image Generator")
|
280 |
|
281 |
with gr.Row():
|
282 |
with gr.Column(scale=2):
|
|
|
283 |
text_prompt = gr.Textbox(
|
284 |
label="Prompt",
|
285 |
placeholder="Describe what you want to create...",
|
286 |
lines=3
|
287 |
)
|
288 |
|
|
|
289 |
negative_prompt = gr.Textbox(
|
290 |
label="Negative Prompt",
|
291 |
placeholder="What should not be in the image",
|
|
|
293 |
lines=2
|
294 |
)
|
295 |
|
|
|
296 |
custom_lora = gr.Textbox(
|
297 |
label="Custom LoRA Path (Optional)",
|
298 |
placeholder="e.g., multimodalart/vintage-ads-flux",
|
|
|
300 |
)
|
301 |
|
302 |
with gr.Column(scale=1):
|
|
|
303 |
with gr.Group():
|
304 |
gr.Markdown("### Image Settings")
|
305 |
width = gr.Slider(label="Width", value=1024, minimum=512, maximum=1216, step=64)
|
306 |
height = gr.Slider(label="Height", value=1024, minimum=512, maximum=1216, step=64)
|
307 |
|
|
|
308 |
with gr.Group():
|
309 |
gr.Markdown("### Generation Parameters")
|
310 |
steps = gr.Slider(label="Steps", value=35, minimum=1, maximum=100, step=1)
|
|
|
312 |
strength = gr.Slider(label="Strength", value=0.7, minimum=0, maximum=1, step=0.1)
|
313 |
seed = gr.Slider(label="Seed (-1 for random)", value=-1, minimum=-1, maximum=1000000000, step=1)
|
314 |
|
|
|
315 |
with gr.Group():
|
316 |
gr.Markdown("### Model Selection")
|
317 |
model_search = gr.Textbox(
|
|
|
320 |
lines=1
|
321 |
)
|
322 |
|
|
|
323 |
models_list = [
|
324 |
"Stable Diffusion 3.5 Large",
|
325 |
"Stable Diffusion 3.5 Large Turbo",
|
|
|
329 |
"Midjourney",
|
330 |
"DreamPhotoGASM",
|
331 |
"Disney",
|
332 |
+
"Leonardo AI Style Illustration",
|
333 |
"AbsoluteReality 1.8.1",
|
334 |
"Analog Redmond",
|
335 |
"Stable Diffusion 3 Medium",
|
|
|
339 |
"Character Design",
|
340 |
"Pixel Art XL",
|
341 |
"3D Sketchfab",
|
|
|
342 |
"Flux Animex V2",
|
343 |
"Flux Animeo V1",
|
344 |
"Flux AestheticAnime",
|
345 |
"90s Anime Art",
|
346 |
"Softserve Anime",
|
|
|
347 |
"Brain Melt Acid Art",
|
348 |
"Retro Comic Flux",
|
349 |
"Purple Dreamy",
|
350 |
"SoftPasty Flux",
|
|
|
351 |
"Flux Logo Design",
|
352 |
"Product Design",
|
353 |
"Propaganda Poster",
|
354 |
"Movie Board",
|
355 |
+
"Collage Flux"
|
|
|
356 |
]
|
357 |
|
358 |
model = gr.Radio(
|
|
|
362 |
interactive=True
|
363 |
)
|
364 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
365 |
with gr.Row():
|
366 |
generate_btn = gr.Button("Generate Image", variant="primary", size="lg")
|
367 |
|
|
|
372 |
show_label=True
|
373 |
)
|
374 |
|
|
|
375 |
generate_btn.click(
|
376 |
fn=query,
|
377 |
inputs=[
|
|
|
389 |
outputs=image_output
|
390 |
)
|
391 |
|
392 |
+
def filter_models(search_term):
|
393 |
+
filtered_models = [m for m in models_list if search_term.lower() in m.lower()]
|
394 |
+
return gr.update(choices=filtered_models)
|
395 |
+
|
396 |
+
model_search.change(filter_models, inputs=model_search, outputs=model)
|
397 |
|
398 |
+
if __name__ == "__main__":
|
399 |
+
dalle.launch(show_api=False, share=False)
|