Nymbo's picture
Update app.py
5de9add verified
raw
history blame
36.3 kB
import gradio as gr
import requests
import io
import random
import os
import time
import json
from PIL import Image
# Project by Nymbo
# Retrieve the default API token from environment variables
HF_TOKEN = os.getenv("HF_READ_TOKEN")
# Timeout for requests
timeout = 100
# Provider-specific API handlers
def query_hf_inference(prompt, model_path, headers, payload, timeout):
"""Handler for Hugging Face Inference API requests"""
api_url = f"https://api-inference.huggingface.co/models/{model_path}"
print(f"Making request to HF Inference API: {api_url}")
return requests.post(api_url, headers=headers, json=payload, timeout=timeout)
def query_fal_ai(prompt, model_path, headers, payload, timeout):
"""Handler for Fal AI API requests"""
api_url = f"https://live.fal.ai/hugging-face/{model_path}"
print(f"Making request to Fal AI: {api_url}")
return requests.post(api_url, headers=headers, json=payload, timeout=timeout)
def query_nebius(prompt, model_path, headers, payload, timeout):
"""Handler for Nebius API requests"""
api_url = f"https://router.huggingface.co/nebius/v1/text-to-image/{model_path}"
print(f"Making request to Nebius: {api_url}")
return requests.post(api_url, headers=headers, json=payload, timeout=timeout)
def query_replicate(prompt, model_path, headers, payload, timeout):
"""Handler for Replicate API requests"""
api_url = f"https://router.huggingface.co/replicate/v1/text-to-image/{model_path}"
print(f"Making request to Replicate: {api_url}")
return requests.post(api_url, headers=headers, json=payload, timeout=timeout)
def query_together(prompt, model_path, headers, payload, timeout):
"""Handler for Together API requests"""
api_url = f"https://router.huggingface.co/together/v1/text-to-image/{model_path}"
print(f"Making request to Together: {api_url}")
return requests.post(api_url, headers=headers, json=payload, timeout=timeout)
# Main query function
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, custom_api_key="", provider="hf-inference"):
# Debug log to indicate function start
print("Starting query function...")
# Print the parameters for debugging purposes
print(f"Prompt: {prompt}")
print(f"Model: {model}")
print(f"Custom LoRA: {custom_lora}")
print(f"Parameters - Steps: {steps}, CFG Scale: {cfg_scale}, Seed: {seed}, Strength: {strength}, Width: {width}, Height: {height}")
print(f"Provider: {provider}")
print(f"Custom API Key provided: {bool(custom_api_key.strip())}") # Log whether a custom key was provided without printing the key
# Check if the prompt is empty or None
if prompt == "" or prompt is None:
print("Prompt is empty or None. Exiting query function.") # Debug log
return None
# Generate a unique key for tracking the generation process
key = random.randint(0, 999)
print(f"Generated key: {key}") # Debug log
# Determine which token to use - custom API key if provided, otherwise one of the environment variables
if custom_api_key.strip() != "":
print("USING CUSTOM API KEY: BYOK token provided by user is being used for authentication")
API_TOKEN = custom_api_key.strip()
else:
if provider == "hf-inference":
# Randomly select an API token from available options to distribute the load for HF Inference
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")])
print("USING DEFAULT API KEY: Random environment variable token is being used for authentication")
else:
# For other providers, use the default HF token
API_TOKEN = HF_TOKEN
print("USING DEFAULT HF TOKEN: Environment variable HF_TOKEN is being used for authentication")
headers = {"Authorization": f"Bearer {API_TOKEN}"}
# Enhance the prompt with additional details for better quality
prompt = f"{prompt} | ultra detail, ultra elaboration, ultra quality, perfect."
print(f'Generation {key}: {prompt}') # Debug log
# Set the model path based on the selected model or custom LoRA
if custom_lora.strip() != "":
model_path = custom_lora.strip()
else:
# Map to select the appropriate model path
model_mapping = {
'Stable Diffusion XL': "stabilityai/stable-diffusion-xl-base-1.0",
'FLUX.1 [Dev]': "black-forest-labs/FLUX.1-dev",
'FLUX.1 [Schnell]': "black-forest-labs/FLUX.1-schnell",
'HiDream-I1-Full': "HiDream-ai/HiDream-I1-Full",
'HiDream-I1-Dev': "HiDream-ai/HiDream-I1-Dev",
'HiDream-I1-Fast': "HiDream-ai/HiDream-I1-Fast",
'Animagine 4.0': "cagliostrolab/animagine-xl-4.0",
'Flux Icon Kit': "strangerzonehf/Flux-Icon-Kit-LoRA",
'Pixel Background': "strangerzonehf/Flux-Pixel-Background-LoRA",
'Meme XD': "prithivMLmods/Flux-Meme-Xd-LoRA",
'Chill Guy': "prithivMLmods/Flux-Chill-Guy-Zone",
'Pepe': "openfree/pepe",
'NSFWmodel': "lexa862/NSFWmodel",
'Claude Art': "strangerzonehf/Flux-Claude-Art",
'Open Genmoji': "EvanZhouDev/open-genmoji",
'EBook Creative Cover': "prithivMLmods/EBook-Creative-Cover-Flux-LoRA",
'Flux Logo Design 2': "prithivMLmods/Logo-Design-Flux-LoRA",
'Isometric 3D': "strangerzonehf/Flux-Isometric-3D-LoRA",
'Flux Condensation': "fofr/flux-condensation",
'Flux Handwriting': "fofr/flux-handwriting",
'Shou Xin': "Datou1111/shou_xin",
'Sketch Smudge': "strangerzonehf/Flux-Sketch-Smudge-LoRA",
'80s Cyberpunk': "fofr/flux-80s-cyberpunk",
'Coloring Book Flux': "renderartist/coloringbookflux",
'Flux Miniature LoRA': "gokaygokay/Flux-Miniature-LoRA",
'Sketch Paint': "strangerzonehf/Sketch-Paint",
'Flux UltraRealism 2.0': "prithivMLmods/Canopus-LoRA-Flux-UltraRealism-2.0",
'Midjourney Mix': "strangerzonehf/Flux-Midjourney-Mix-LoRA",
'Midjourney Mix 2': "strangerzonehf/Flux-Midjourney-Mix2-LoRA",
'Flux Logo Design': "Shakker-Labs/FLUX.1-dev-LoRA-Logo-Design",
'Flux Uncensored': "enhanceaiteam/Flux-uncensored",
'Flux Uncensored V2': "enhanceaiteam/Flux-Uncensored-V2",
'Flux Tarot Cards': "prithivMLmods/Ton618-Tarot-Cards-Flux-LoRA",
'Pixel Art Sprites': "sWizad/pokemon-trainer-sprites-pixelart-flux",
'3D Sketchfab': "prithivMLmods/Castor-3D-Sketchfab-Flux-LoRA",
'Retro Comic Flux': "renderartist/retrocomicflux",
'Caricature': "TheAwakenOne/caricature",
'Huggieverse': "Chunte/flux-lora-Huggieverse",
'Propaganda Poster': "AlekseyCalvin/Propaganda_Poster_Schnell_by_doctor_diffusion",
'Flux Game Assets V2': "gokaygokay/Flux-Game-Assets-LoRA-v2",
'SDXL HS Card Style': "Norod78/sdxl-hearthstone-card-style-lora",
'SLDR FLUX NSFW v2 Studio': "xey/sldr_flux_nsfw_v2-studio",
'SoftPasty Flux': "alvdansen/softpasty-flux-dev",
'Flux Stickers': "diabolic6045/Flux_Sticker_Lora",
'Flux Animex V2': "strangerzonehf/Flux-Animex-v2-LoRA",
'Flux Animeo V1': "strangerzonehf/Flux-Animeo-v1-LoRA",
'Movie Board': "prithivMLmods/Flux.1-Dev-Movie-Boards-LoRA",
'Purple Dreamy': "prithivMLmods/Purple-Dreamy-Flux-LoRA",
'PS1 Style Flux': "veryVANYA/ps1-style-flux",
'Softserve Anime': "alvdansen/softserve_anime",
'Flux Tarot v1': "multimodalart/flux-tarot-v1",
'Half Illustration': "davisbro/half_illustration",
'OpenDalle v1.1': "dataautogpt3/OpenDalleV1.1",
'Flux Ghibsky Illustration': "aleksa-codes/flux-ghibsky-illustration",
'Flux Koda': "alvdansen/flux-koda",
'Soviet Diffusion XL': "openskyml/soviet-diffusion-xl",
'Flux Realism LoRA': "XLabs-AI/flux-RealismLora",
'Frosting Lane Flux': "alvdansen/frosting_lane_flux",
'Phantasma Anime': "alvdansen/phantasma-anime",
'Boreal': "kudzueye/Boreal",
'How2Draw': "glif/how2draw",
'Flux AestheticAnime': "dataautogpt3/FLUX-AestheticAnime",
'Fashion Hut Modeling LoRA': "prithivMLmods/Fashion-Hut-Modeling-LoRA",
'Flux SyntheticAnime': "dataautogpt3/FLUX-SyntheticAnime",
'Flux Midjourney Anime': "brushpenbob/flux-midjourney-anime",
'Coloring Book Generator': "robert123231/coloringbookgenerator",
'Collage Flux': "prithivMLmods/Castor-Collage-Dim-Flux-LoRA",
'Flux Product Ad Backdrop': "prithivMLmods/Flux-Product-Ad-Backdrop",
'Product Design': "multimodalart/product-design",
'90s Anime Art': "glif/90s-anime-art",
'Brain Melt Acid Art': "glif/Brain-Melt-Acid-Art",
'Lustly Flux Uncensored v1': "lustlyai/Flux_Lustly.ai_Uncensored_nsfw_v1",
'NSFW Master Flux': "Keltezaa/NSFW_MASTER_FLUX",
'Flux Outfit Generator': "tryonlabs/FLUX.1-dev-LoRA-Outfit-Generator",
'Midjourney': "Jovie/Midjourney",
'DreamPhotoGASM': "Yntec/DreamPhotoGASM",
'Flux Super Realism LoRA': "strangerzonehf/Flux-Super-Realism-LoRA",
'Stable Diffusion 2-1': "stabilityai/stable-diffusion-2-1-base",
'Stable Diffusion 3.5 Large': "stabilityai/stable-diffusion-3.5-large",
'Stable Diffusion 3.5 Large Turbo': "stabilityai/stable-diffusion-3.5-large-turbo",
'Stable Diffusion 3 Medium': "stabilityai/stable-diffusion-3-medium-diffusers",
'Duchaiten Real3D NSFW XL': "stablediffusionapi/duchaiten-real3d-nsfw-xl",
'Pixel Art XL': "nerijs/pixel-art-xl",
'Character Design': "KappaNeuro/character-design",
'Sketched Out Manga': "alvdansen/sketchedoutmanga",
'Archfey Anime': "alvdansen/archfey_anime",
'Lofi Cuties': "alvdansen/lofi-cuties",
'YiffyMix': "Yntec/YiffyMix",
'Analog Madness Realistic v7': "digiplay/AnalogMadness-realistic-model-v7",
'Selfie Photography': "artificialguybr/selfiephotographyredmond-selfie-photography-lora-for-sdxl",
'Filmgrain': "artificialguybr/filmgrain-redmond-filmgrain-lora-for-sdxl",
'Leonardo AI Style Illustration': "goofyai/Leonardo_Ai_Style_Illustration",
'Cyborg Style XL': "goofyai/cyborg_style_xl",
'Little Tinies': "alvdansen/littletinies",
'NSFW XL': "Dremmar/nsfw-xl",
'Analog Redmond': "artificialguybr/analogredmond",
'Pixel Art Redmond': "artificialguybr/PixelArtRedmond",
'Ascii Art': "CiroN2022/ascii-art",
'Analog': "Yntec/Analog",
'Maple Syrup': "Yntec/MapleSyrup",
'Perfect Lewd Fantasy': "digiplay/perfectLewdFantasy_v1.01",
'AbsoluteReality 1.8.1': "digiplay/AbsoluteReality_v1.8.1",
'Disney': "goofyai/disney_style_xl",
'Redmond SDXL': "artificialguybr/LogoRedmond-LogoLoraForSDXL-V2",
'epiCPhotoGasm': "Yntec/epiCPhotoGasm"
}
# Add special prompt prefixes for certain models
prompt_prefixes = {
'Animagine 4.0': "masterpiece, high score, great score, absurdres, ",
'Flux Icon Kit': "Icon Kit, ",
'Pixel Background': "Pixel Background, ",
'Meme XD': "meme, ",
'Chill Guy': "chill guy, ",
'Pepe': "pepe, ",
'NSFWmodel': "nude, ",
'Claude Art': "claude art, ",
'Open Genmoji': "emoji, ",
'EBook Creative Cover': "EBook Cover, ",
'Flux Logo Design 2': "Logo Design, ",
'Isometric 3D': "Isometric 3D, ",
'Flux Condensation': "CONDENSATION, ",
'Flux Handwriting': "HWRIT handwriting, ",
'Shou Xin': "shou_xin, pencil sketch, ",
'Sketch Smudge': "Sketch Smudge, ",
'80s Cyberpunk': "80s cyberpunk, ",
'Coloring Book Flux': "c0l0ringb00k, coloring book, coloring book page, ",
'Flux Miniature LoRA': "MNTR, miniature drawing, ",
'Sketch Paint': "Sketch paint, ",
'Flux UltraRealism 2.0': "Ultra realistic, ",
'Midjourney Mix': "midjourney mix, ",
'Midjourney Mix 2': "MJ v6, ",
'Flux Logo Design': "wablogo, logo, Minimalist, ",
'Flux Tarot Cards': "Tarot card, ",
'Pixel Art Sprites': "a pixel image, ",
'3D Sketchfab': "3D Sketchfab, ",
'Retro Comic Flux': "c0m1c, comic book panel, ",
'Caricature': "CCTUR3, ",
'Huggieverse': "HGGRE, ",
'Propaganda Poster': "propaganda poster, ",
'Flux Game Assets V2': "wbgmsst, white background, ",
'SDXL HS Card Style': "Hearthstone Card, ",
'SoftPasty Flux': "araminta_illus illustration style, ",
'Flux Stickers': "5t1cker 5ty1e, ",
'Flux Animex V2': "Animex, ",
'Flux Animeo V1': "Animeo, ",
'Movie Board': "movieboard, ",
'Purple Dreamy': "Purple Dreamy, ",
'PS1 Style Flux': "ps1 game screenshot, ",
'Softserve Anime': "sftsrv style illustration, ",
'Flux Tarot v1': "in the style of TOK a trtcrd tarot style, ",
'Half Illustration': "in the style of TOK, ",
'Flux Ghibsky Illustration': "GHIBSKY style, ",
'Flux Koda': "flmft style, ",
'Soviet Diffusion XL': "soviet poster, ",
'Frosting Lane Flux': "frstingln illustration, ",
'Boreal': "photo, ",
'How2Draw': "How2Draw, ",
'Fashion Hut Modeling LoRA': "Modeling of, ",
'Flux SyntheticAnime': "1980s anime screengrab, VHS quality, syntheticanime, ",
'Flux Midjourney Anime': "egmid, ",
'Collage Flux': "collage, ",
'Flux Product Ad Backdrop': "Product Ad, ",
'Product Design': "product designed by prdsgn, ",
'Brain Melt Acid Art': "maximalism, in an acid surrealism style, ",
'NSFW Master Flux': "NSFW, ",
'Stable Diffusion 3 Medium': "A, ",
'Pixel Art XL': "pixel art, ",
'Character Design': "Character Design, ",
'Sketched Out Manga': "daiton, ",
'Selfie Photography': "instagram model, discord profile picture, ",
'Filmgrain': "Film Grain, FilmGrainAF, ",
'Leonardo AI Style Illustration': "leonardo style, illustration, vector art, ",
'Cyborg Style XL': "cyborg style, ",
'Analog Redmond': "timeless style, ",
'Pixel Art Redmond': "Pixel Art, ",
'Ascii Art': "ascii art, ",
'Disney': "Disney style, "
}
# Get the model path from the mapping
model_path = model_mapping.get(model, "black-forest-labs/FLUX.1-schnell") # Default to FLUX.1 Schnell if not found
# Add model-specific prefixes to prompt if needed
if model in prompt_prefixes:
prompt = f"{prompt_prefixes[model]}{prompt}"
print(f"Model path: {model_path}") # Debug log
# Define the payload for the request
payload = {
"inputs": prompt,
"is_negative": is_negative, # Whether to use a negative prompt
"steps": steps, # Number of sampling steps
"cfg_scale": cfg_scale, # Scale for controlling adherence to prompt
"seed": seed if seed != -1 else random.randint(1, 1000000000), # Random seed for reproducibility
"strength": strength, # How strongly the model should transform the image
"parameters": {
"width": width, # Width of the generated image
"height": height # Height of the generated image
}
}
print(f"Payload: {json.dumps(payload, indent=2)}") # Debug log
# Select the appropriate API handler based on the provider
provider_handlers = {
"hf-inference": query_hf_inference,
"fal-ai": query_fal_ai,
"nebius": query_nebius,
"replicate": query_replicate,
"together": query_together
}
# Get the appropriate handler for the selected provider
handler = provider_handlers.get(provider, query_hf_inference)
# Make a request to the API to generate the image using the selected handler
try:
response = handler(prompt, model_path, headers, payload, timeout)
print(f"Response status code: {response.status_code}") # Debug log
except requests.exceptions.RequestException as e:
# Log any request exceptions and raise an error for the user
print(f"Request failed: {e}") # Debug log
raise gr.Error(f"Request failed: {e}")
# Check if the response status is not successful
if response.status_code != 200:
print(f"Error: Failed to retrieve image. Response status: {response.status_code}") # Debug log
print(f"Response content: {response.text}") # Debug log
if response.status_code == 400:
raise gr.Error(f"{response.status_code}: Bad Request - There might be an issue with the input parameters.")
elif response.status_code == 401:
raise gr.Error(f"{response.status_code}: Unauthorized - Please check your API token.")
elif response.status_code == 403:
raise gr.Error(f"{response.status_code}: Forbidden - You do not have permission to access this model.")
elif response.status_code == 404:
raise gr.Error(f"{response.status_code}: Not Found - The requested model could not be found.")
elif response.status_code == 503:
raise gr.Error(f"{response.status_code}: The model is being loaded. Please try again later.")
else:
raise gr.Error(f"{response.status_code}: An unexpected error occurred.")
try:
# Attempt to read the image from the response content
image_bytes = response.content
image = Image.open(io.BytesIO(image_bytes))
print(f'Generation {key} completed! ({prompt})') # Debug log
return image
except Exception as e:
# Handle any errors that occur when opening the image
print(f"Error while trying to open image: {e}") # Debug log
return None
# Custom CSS to hide the footer in the interface
css = """
* {}
footer {visibility: hidden !important;}
"""
print("Initializing Gradio interface...") # Debug log
# Define the Gradio interface
with gr.Blocks(theme='Nymbo/Nymbo_Theme_5') as dalle:
# Tab for basic settings
with gr.Tab("Basic Settings"):
with gr.Row():
with gr.Column(elem_id="prompt-container"):
with gr.Row():
# Textbox for user to input the prompt
text_prompt = gr.Textbox(label="Prompt", placeholder="Enter a prompt here", lines=3, elem_id="prompt-text-input")
with gr.Row():
# Textbox for custom LoRA input
custom_lora = gr.Textbox(label="Custom LoRA", info="LoRA Hugging Face path (optional)", placeholder="multimodalart/vintage-ads-flux")
with gr.Row():
# Accordion for selecting the model
with gr.Accordion("Featured Models", open=False):
# Textbox for searching models
model_search = gr.Textbox(label="Filter Models", placeholder="Search for a featured model...", lines=1, elem_id="model-search-input")
models_list = (
"3D Sketchfab",
"80s Cyberpunk",
"90s Anime Art",
"AbsoluteReality 1.8.1",
"Analog",
"Analog Madness Realistic v7",
"Analog Redmond",
"Animagine 4.0",
"Archfey Anime",
"Ascii Art",
"Brain Melt Acid Art",
"Boreal",
"Caricature",
"Collage Flux",
"Coloring Book Flux",
"Character Design",
"Chill Guy",
"Claude Art",
"Coloring Book Generator",
"Cyborg Style XL",
"Disney",
"DreamPhotoGASM",
"Duchaiten Real3D NSFW XL",
"EBook Creative Cover",
"EpiCPhotoGasm",
"Fashion Hut Modeling LoRA",
"Filmgrain",
"FLUX.1 [Dev]",
"FLUX.1 [Schnell]",
"FLux Condensation",
"Flux Handwriting",
"Flux Realism LoRA",
"Flux Super Realism LoRA",
"Flux Uncensored",
"Flux Uncensored V2",
"Flux Game Assets V2",
"Flux Icon Kit",
"Flux Ghibsky Illustration",
"Flux Animex V2",
"Flux Animeo V1",
"Flux AestheticAnime",
"Flux SyntheticAnime",
"Flux Stickers",
"Flux Koda",
"Flux Tarot v1",
"Flux Tarot Cards",
"Flux UltraRealism 2.0",
"Flux Midjourney Anime",
"Flux Miniature LoRA",
"Flux Logo Design",
"Flux Logo Design 2",
"Flux Product Ad Backdrop",
"Flux Outfit Generator",
"Frosting Lane Flux",
"Half Illustration",
"HiDream-I1-Full",
"HiDream-I1-Dev",
"HiDream-I1-Fast",
"How2Draw",
"Huggieverse",
"Isometric 3D",
"Leonardo AI Style Illustration",
"Little Tinies",
"Lofi Cuties",
"Lustly Flux Uncensored v1",
"Maple Syrup",
"Meme XD",
"Midjourney",
"Midjourney Mix",
"Midjourney Mix 2",
"Movie Board",
"NSFWmodel",
"NSFW Master Flux",
"NSFW XL",
"OpenDalle v1.1",
"Open Genmoji",
"Pepe",
"Perfect Lewd Fantasy",
"Pixel Art Redmond",
"Pixel Art XL",
"Pixel Art Sprites",
"Pixel Background",
"Product Design",
"Propaganda Poster",
"Purple Dreamy",
"Phantasma Anime",
"PS1 Style Flux",
"Redmond SDXL",
"Retro Comic Flux",
"SDXL HS Card Style",
"Sketch Smudge",
"Shou Xin",
"Softserve Anime",
"SoftPasty Flux",
"Soviet Diffusion XL",
"Sketched Out Manga",
"Sketch Paint",
"SLDR FLUX NSFW v2 Studio",
"Selfie Photography",
"Stable Diffusion 2-1",
"Stable Diffusion XL",
"Stable Diffusion 3 Medium",
"Stable Diffusion 3.5 Large",
"Stable Diffusion 3.5 Large Turbo",
"YiffyMix",
)
# Radio buttons to select the desired model
model = gr.Radio(label="Select a model below", value="FLUX.1 [Schnell]", choices=models_list, interactive=True, elem_id="model-radio")
# Filtering models based on search input
def filter_models(search_term):
filtered_models = [m for m in models_list if search_term.lower() in m.lower()]
return gr.update(choices=filtered_models)
# Update model list when search box is used
model_search.change(filter_models, inputs=model_search, outputs=model)
# Tab for advanced settings
with gr.Tab("Advanced Settings"):
with gr.Row():
# Provider selection
provider_radio = gr.Radio(
choices=["hf-inference", "fal-ai", "nebius", "replicate", "together"],
value="hf-inference",
label="Inference Provider",
info="Select the provider to use for image generation"
)
with gr.Row():
# BYOK (Bring Your Own Key) textbox
byok_textbox = gr.Textbox(
value="",
label="BYOK (Bring Your Own Key)",
info="Enter a custom API key here. When provided, this key will be used instead of the default keys.",
placeholder="Enter your API token",
type="password", # Hide the API key for security
elem_id="byok-input"
)
with gr.Row():
# Textbox for specifying elements to exclude from the image
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")
with gr.Row():
# Slider for selecting the image width
width = gr.Slider(label="Width", value=1024, minimum=64, maximum=1216, step=32)
# Slider for selecting the image height
height = gr.Slider(label="Height", value=1024, minimum=64, maximum=1216, step=32)
with gr.Row():
# Slider for setting the number of sampling steps
steps = gr.Slider(label="Sampling steps", value=35, minimum=1, maximum=100, step=1)
with gr.Row():
# Slider for adjusting the CFG scale (guidance scale)
cfg = gr.Slider(label="CFG Scale", value=7, minimum=1, maximum=20, step=1)
with gr.Row():
# Slider for adjusting the transformation strength
strength = gr.Slider(label="Strength", value=0.7, minimum=0, maximum=1, step=0.001)
with gr.Row():
# Slider for setting the seed for reproducibility
seed = gr.Slider(label="Seed", value=-1, minimum=-1, maximum=1000000000, step=1)
with gr.Row():
# Radio buttons for selecting the sampling method
method = gr.Radio(label="Sampling method", value="DPM++ 2M Karras", choices=["DPM++ 2M Karras", "DPM++ SDE Karras", "Euler", "Euler a", "Heun", "DDIM"])
# Function to validate provider selection
def validate_provider(api_key, provider):
# If custom key is provided, any provider can be used
if api_key.strip():
return gr.update(value=provider, interactive=True)
# If no custom key is provided, only HF Inference is allowed
elif provider != "hf-inference":
return gr.update(value="hf-inference", interactive=True)
return gr.update(value=provider, interactive=True)
# Connect the BYOK textbox to validate provider selection
byok_textbox.change(
fn=validate_provider,
inputs=[byok_textbox, provider_radio],
outputs=provider_radio
)
# Tab for image editing options
with gr.Tab("Image Editor"):
# Function to simulate a delay for processing
def sleep(im):
print("Sleeping for 5 seconds...") # Debug log
time.sleep(5)
return [im["background"], im["layers"][0], im["layers"][1], im["composite"]]
# Function to return the composite image
def predict(im):
print("Predicting composite image...") # Debug log
return im["composite"]
with gr.Blocks() as demo:
with gr.Row():
# Image editor component for user adjustments
im = gr.ImageEditor(
type="numpy",
crop_size="1:1", # Set crop size to a square aspect ratio
)
# Tab to provide information to the user
with gr.Tab("Information"):
with gr.Row():
# Display a sample prompt for guidance
gr.Textbox(label="Sample prompt", value="{prompt} | ultra detail, ultra elaboration, ultra quality, perfect.")
# Accordion displaying featured models
with gr.Accordion("Featured Models (WiP)", open=False):
gr.HTML(
"""
<p><a href="https://huggingface.co/models?inference=warm&pipeline_tag=text-to-image&sort=trending">See all available models</a></p>
<table style="width:100%; text-align:center; margin:auto;">
<tr>
<th>Model Name</th>
<th>Typography</th>
<th>Notes</th>
</tr>
<tr>
<td>FLUX.1 Dev</td>
<td>✅</td>
<td></td>
</tr>
<tr>
<td>FLUX.1 Schnell</td>
<td>✅</td>
<td></td>
</tr>
<tr>
<td>Stable Diffusion 3.5 Large</td>
<td>✅</td>
<td></td>
</tr>
</table>
"""
)
# Accordion providing an overview of providers
with gr.Accordion("Providers Information", open=False):
gr.Markdown(
"""
## Inference Providers
This application supports multiple inference providers for image generation:
### HF Inference
###### The default Hugging Face Inference API. This is a serverless solution that allows you to run model inference on Hugging Face's infrastructure. Free to use with limitations.
### Fal AI
###### Fal AI provides optimized inference for image generation models. May offer better performance for certain model types.
### Nebius
###### Nebius AI is a technology company specializing in AI-centric cloud platforms, offering scalable GPU clusters for inference.
### Replicate
###### Replicate's API makes it easy to run machine learning models in the cloud with a simple API.
### Together
###### Together AI offers high-performance inference for various AI models.
When no custom API key is provided, only HF Inference can be used. For other providers, you must bring your own API key.
"""
)
# Accordion providing an overview of advanced settings
with gr.Accordion("Advanced Settings Overview", open=False):
gr.Markdown(
"""
## Inference Provider
###### Select which provider to use for image generation. Each provider has different performance characteristics and model availability.
## BYOK (Bring Your Own Key)
###### This allows you to use your own API key instead of the default keys. Enter your key here for direct access to models using your account's permissions and rate limits.
## Negative Prompt
###### This box is for telling the AI what you don't want in your images. Think of it as a way to avoid certain elements. For instance, if you don't want blurry images or extra limbs showing up, this is where you'd mention it.
## Width & Height
###### These sliders allow you to specify the resolution of your image. Default value is 1024x1024, and maximum output is 1216x1216.
## Sampling Steps
###### Think of this like the number of brushstrokes in a painting. A higher number can give you a more detailed picture, but it also takes a bit longer. Generally, a middle-ground number like 35 is a good balance between quality and speed.
## CFG Scale
###### CFG stands for "Control Free Guidance." The scale adjusts how closely the AI follows your prompt. A lower number makes the AI more creative and free-flowing, while a higher number makes it stick closely to what you asked for. If you want the AI to take fewer artistic liberties, slide this towards a higher number. Just think "Control Freak Gauge".
## Sampling Method
###### This is the technique the AI uses to create your image. Each option is a different approach, like choosing between pencils, markers, or paint. You don't need to worry too much about this; the default setting is usually the best choice for most users.
## Strength
###### This setting is a bit like the 'intensity' knob. It determines how much the AI modifies the base image it starts with. If you're looking to make subtle changes, keep this low. For more drastic transformations, turn it up.
## Seed
###### You can think of the seed as a 'recipe' for creating an image. If you find a seed that gives you a result you love, you can use it again to create a similar image. If you leave it at -1, the AI will generate a new seed every time.
### Remember, these settings are all about giving you control over the image generation process. Feel free to experiment and see what each one does. And if you're ever in doubt, the default settings are a great place to start. Happy creating!
"""
)
# Row containing the 'Run' button to trigger the image generation
with gr.Row():
text_button = gr.Button("Run", variant='primary', elem_id="gen-button")
# Row for displaying the generated image output
with gr.Row():
image_output = gr.Image(type="pil", label="Image Output", elem_id="gallery")
# Set up button click event to call the query function with the provider and BYOK parameters
text_button.click(query, inputs=[text_prompt, model, custom_lora, negative_prompt, steps, cfg, method, seed, strength, width, height, byok_textbox, provider_radio], outputs=image_output)
print("Launching Gradio interface...") # Debug log
# Launch the Gradio interface without showing the API or sharing externally
dalle.launch(show_api=True, share=False)