Update app.py
Browse files8 unmodified lines910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748812 unmodified lines501502503504505506507508509510511512513514515516517518519520521522523524108 unmodified lines63363463563663763863986 unmodified lines72672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776830 unmodified lines7998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498 unmodified lines# Project by Nymbo# Retrieve the API token from environment variablesAPI_TOKEN = os.getenv("HF_READ_TOKEN")# Create a list of available API tokens for load balancingAPI_TOKENS = [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")]# Timeout for requeststimeout = 100def 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,provider="hf-inference" # Added provider parameter):# Debug log to indicate function startprint("Starting query function...")# Print the parameters for debugging purposesprint(f"Prompt: {prompt}")print(f"Model: {model}")print(f"Custom LoRA: {custom_lora}")print(f"Provider: {provider}") # Added provider debug logprint(f"Parameters - Steps: {steps}, CFG Scale: {cfg_scale}, Seed: {seed}, Strength: {strength}, Width: {width}, Height: {height}")# Check if the prompt is empty or Noneif prompt == "" or prompt is None:print("Prompt is empty or None. Exiting query function.")return None# Generate a unique key for tracking the generation processkey = random.randint(0, 999)print(f"Generated key: {key}")# Randomly select an API token from available options to distribute the loadselected_token = random.choice([token for token in API_TOKENS if token])print(f"Selected an API token") # Modified to not print the actual token# Initialize the default headers with authorizationheaders = {"Authorization": f"Bearer {selected_token}"}# Select provider-specific headers if neededif provider == "fal-ai":headers = {"Authorization": f"Bearer {os.getenv('FAL_API_KEY')}","Content-Type": "application/json"}elif provider == "together":headers = {"Authorization": f"Bearer {os.getenv('TOGETHER_API_KEY')}"}elif provider == "replicate":headers = {"Authorization": f"Token {os.getenv('REPLICATE_API_TOKEN')}","Content-Type": "application/json"}elif provider == "nebius":headers = {"Authorization": f"Api-Key {os.getenv('NEBIUS_API_KEY')}","Content-Type": "application/json"}# Enhance the prompt with additional details for better qualityenhanced_prompt = promptif provider == "hf-inference":enhanced_prompt = f"{prompt} | ultra detail, ultra elaboration, ultra quality, perfect."print(f'Generation {key}: {enhanced_prompt}')# Set the API URL based on the selected provider, model, or custom LoRAAPI_URL = get_model_api_url(model, custom_lora, provider)print(f"API URL set to: {API_URL}")# Define the payload for the request (provider-specific)payload = build_request_payload(prompt, is_negative, steps, cfg_scale, seed, strength, width, height, provider)print(f"Payload created for provider: {provider}")# Make a request to the API to generate the imagetry:response = make_provider_request(API_URL, headers, payload, provider, timeout)print(f"Response status code: {response.status_code}")except requests.exceptions.RequestException as e:print(f"Request failed: {e}")raise gr.Error(f"Request failed: {e}")# Check if the response status is not successfulif response.status_code != 200:print(f"Error: Failed to retrieve image. Response status: {response.status_code}")print(f"Response content: {response.text}")handle_error_response(response)try:# Process the response based on the providerimage = process_provider_response(response, provider)print(f'Generation {key} completed! ({prompt})')return imageexcept Exception as e:print(f"Error while processing the response: {e}")return Nonedef get_model_api_url(model, custom_lora, provider):"""Determine the correct API URL based on model, custom LoRA, and provider"""# If a custom LoRA is specified, use it (only for HF Inference)if custom_lora.strip() != "" and provider == "hf-inference":return f"https://api-inference.huggingface.co/models/{custom_lora.strip()}"# Provider-specific base URLsif provider == "fal-ai":return "https://gateway.fal.ai/inference"elif provider == "replicate":return "https://api.replicate.com/v1/predictions"elif provider == "nebius":return "https://llm.api.cloud.yandex.net/foundationModels/v1/image/generate"elif provider == "together":return "https://api.together.xyz/v1/images/generations"# Default to HuggingFace with the selected modelif provider == "hf-inference":# Map model names to their respective API URLsmodel_urls = {'Stable Diffusion XL': "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0",'FLUX.1 [Dev]': "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev",'FLUX.1 [Schnell]': "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell",'HiDream-I1-Full': "https://api-inference.huggingface.co/models/HiDream-ai/HiDream-I1-Full",'HiDream-I1-Dev': "https://api-inference.huggingface.co/models/HiDream-ai/HiDream-I1-Dev",'HiDream-I1-Fast': "https://api-inference.huggingface.co/models/HiDream-ai/HiDream-I1-Fast",'Animagine 4.0': "https://api-inference.huggingface.co/models/cagliostrolab/animagine-xl-4.0",'Flux Icon Kit': "https://api-inference.huggingface.co/models/strangerzonehf/Flux-Icon-Kit-LoRA",'Pixel Background': "https://api-inference.huggingface.co/models/strangerzonehf/Flux-Pixel-Background-LoRA",'Meme XD': "https://api-inference.huggingface.co/models/prithivMLmods/Flux-Meme-Xd-LoRA",'Chill Guy': "https://api-inference.huggingface.co/models/prithivMLmods/Flux-Chill-Guy-Zone",'Pepe': "https://api-inference.huggingface.co/models/openfree/pepe",'NSFWmodel': "https://api-inference.huggingface.co/models/lexa862/NSFWmodel",'Claude Art': "https://api-inference.huggingface.co/models/strangerzonehf/Flux-Claude-Art",'Open Genmoji': "https://api-inference.huggingface.co/models/EvanZhouDev/open-genmoji",'EBook Creative Cover': "https://api-inference.huggingface.co/models/prithivMLmods/EBook-Creative-Cover-Flux-LoRA",'Flux Logo Design 2': "https://api-inference.huggingface.co/models/prithivMLmods/Logo-Design-Flux-LoRA",'Isometric 3D': "https://api-inference.huggingface.co/models/strangerzonehf/Flux-Isometric-3D-LoRA",'Flux Condensation': "https://api-inference.huggingface.co/models/fofr/flux-condensation",'Flux Handwriting': "https://api-inference.huggingface.co/models/fofr/flux-handwriting",'Shou Xin': "https://api-inference.huggingface.co/models/Datou1111/shou_xin",'Sketch Smudge': "https://api-inference.huggingface.co/models/strangerzonehf/Flux-Sketch-Smudge-LoRA",'80s Cyberpunk': "https://api-inference.huggingface.co/models/fofr/flux-80s-cyberpunk",'Coloring Book Flux': "https://api-inference.huggingface.co/models/renderartist/coloringbookflux",'Flux Miniature LoRA': "https://api-inference.huggingface.co/models/gokaygokay/Flux-Miniature-LoRA",'Sketch Paint': "https://api-inference.huggingface.co/models/strangerzonehf/Sketch-Paint",'Flux UltraRealism 2.0': "https://api-inference.huggingface.co/models/prithivMLmods/Canopus-LoRA-Flux-UltraRealism-2.0",'Midjourney Mix': "https://api-inference.huggingface.co/models/strangerzonehf/Flux-Midjourney-Mix-LoRA",'Midjourney Mix 2': "https://api-inference.huggingface.co/models/strangerzonehf/Flux-Midjourney-Mix2-LoRA",'Flux Logo Design': "https://api-inference.huggingface.co/models/Shakker-Labs/FLUX.1-dev-LoRA-Logo-Design",'Flux Uncensored': "https://api-inference.huggingface.co/models/enhanceaiteam/Flux-uncensored",'Flux Uncensored V2': "https://api-inference.huggingface.co/models/enhanceaiteam/Flux-Uncensored-V2",'Flux Tarot Cards': "https://api-inference.huggingface.co/models/prithivMLmods/Ton618-Tarot-Cards-Flux-LoRA",'Pixel Art Sprites': "https://api-inference.huggingface.co/models/sWizad/pokemon-trainer-sprites-pixelart-flux",'3D Sketchfab': "https://api-inference.huggingface.co/models/prithivMLmods/Castor-3D-Sketchfab-Flux-LoRA",'Retro Comic Flux': "https://api-inference.huggingface.co/models/renderartist/retrocomicflux",'Caricature': "https://api-inference.huggingface.co/models/TheAwakenOne/caricature",'Huggieverse': "https://api-inference.huggingface.co/models/Chunte/flux-lora-Huggieverse",'Propaganda Poster': "https://api-inference.huggingface.co/models/AlekseyCalvin/Propaganda_Poster_Schnell_by_doctor_diffusion",'Flux Game Assets V2': "https://api-inference.huggingface.co/models/gokaygokay/Flux-Game-Assets-LoRA-v2",'SDXL HS Card Style': "https://api-inference.huggingface.co/models/Norod78/sdxl-hearthstone-card-style-lora",'SLDR FLUX NSFW v2 Studio': "https://api-inference.huggingface.co/models/xey/sldr_flux_nsfw_v2-studio",'SoftPasty Flux': "https://api-inference.huggingface.co/models/alvdansen/softpasty-flux-dev",'Flux Stickers': "https://api-inference.huggingface.co/models/diabolic6045/Flux_Sticker_Lora",'Flux Animex V2': "https://api-inference.huggingface.co/models/strangerzonehf/Flux-Animex-v2-LoRA",'Flux Animeo V1': "https://api-inference.huggingface.co/models/strangerzonehf/Flux-Animeo-v1-LoRA",'Movie Board': "https://api-inference.huggingface.co/models/prithivMLmods/Flux.1-Dev-Movie-Boards-LoRA",'Purple Dreamy': "https://api-inference.huggingface.co/models/prithivMLmods/Purple-Dreamy-Flux-LoRA",'PS1 Style Flux': "https://api-inference.huggingface.co/models/veryVANYA/ps1-style-flux",'Softserve Anime': "https://api-inference.huggingface.co/models/alvdansen/softserve_anime",'Flux Tarot v1': "https://api-inference.huggingface.co/models/multimodalart/flux-tarot-v1",'Half Illustration': "https://api-inference.huggingface.co/models/davisbro/half_illustration",'OpenDalle v1.1': "https://api-inference.huggingface.co/models/dataautogpt3/OpenDalleV1.1",'Flux Ghibsky Illustration': "https://api-inference.huggingface.co/models/aleksa-codes/flux-ghibsky-illustration",'Flux Koda': "https://api-inference.huggingface.co/models/alvdansen/flux-koda",'Soviet Diffusion XL': "https://api-inference.huggingface.co/models/openskyml/soviet-diffusion-xl",'Flux Realism LoRA': "https://api-inference.huggingface.co/models/XLabs-AI/flux-RealismLora",'Frosting Lane Flux': "https://api-inference.huggingface.co/models/alvdansen/frosting_lane_flux",'Phantasma Anime': "https://api-inference.huggingface.co/models/alvdansen/phantasma-anime",'Boreal': "https://api-inference.huggingface.co/models/kudzueye/Boreal",'How2Draw': "https://api-inference.huggingface.co/models/glif/how2draw",'Flux AestheticAnime': "https://api-inference.huggingface.co/models/dataautogpt3/FLUX-AestheticAnime",'Fashion Hut Modeling LoRA': "https://api-inference.huggingface.co/models/prithivMLmods/Fashion-Hut-Modeling-LoRA",'Flux SyntheticAnime': "https://api-inference.huggingface.co/models/dataautogpt3/FLUX-SyntheticAnime",'Flux Midjourney Anime': "https://api-inference.huggingface.co/models/brushpenbob/flux-midjourney-anime",'Coloring Book Generator': "https://api-inference.huggingface.co/models/robert123231/coloringbookgenerator",'Collage Flux': "https://api-inference.huggingface.co/models/prithivMLmods/Castor-Collage-Dim-Flux-LoRA",'Flux Product Ad Backdrop': "https://api-inference.huggingface.co/models/prithivMLmods/Flux-Product-Ad-Backdrop",'Product Design': "https://api-inference.huggingface.co/models/multimodalart/product-design",'90s Anime Art': "https://api-inference.huggingface.co/models/glif/90s-anime-art",'Brain Melt Acid Art': "https://api-inference.huggingface.co/models/glif/Brain-Melt-Acid-Art",'Lustly Flux Uncensored v1': "https://api-inference.huggingface.co/models/lustlyai/Flux_Lustly.ai_Uncensored_nsfw_v1",'NSFW Master Flux': "https://api-inference.huggingface.co/models/Keltezaa/NSFW_MASTER_FLUX",'Flux Outfit Generator': "https://api-inference.huggingface.co/models/tryonlabs/FLUX.1-dev-LoRA-Outfit-Generator",'Midjourney': "https://api-inference.huggingface.co/models/Jovie/Midjourney",'DreamPhotoGASM': "https://api-inference.huggingface.co/models/Yntec/DreamPhotoGASM",'Flux Super Realism LoRA': "https://api-inference.huggingface.co/models/strangerzonehf/Flux-Super-Realism-LoRA",'Stable Diffusion 2-1': "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2-1-base",'Stable Diffusion 3.5 Large': "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-3.5-large",'Stable Diffusion 3.5 Large Turbo': "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-3.5-large-turbo",'Stable Diffusion 3 Medium': "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-3-medium-diffusers",'Duchaiten Real3D NSFW XL': "https://api-inference.huggingface.co/models/stablediffusionapi/duchaiten-real3d-nsfw-xl",'Pixel Art XL': "https://api-inference.huggingface.co/models/nerijs/pixel-art-xl",'Character Design': "https://api-inference.huggingface.co/models/KappaNeuro/character-design",'Sketched Out Manga': "https://api-inference.huggingface.co/models/alvdansen/sketchedoutmanga",'Archfey Anime': "https://api-inference.huggingface.co/models/alvdansen/archfey_anime",'Lofi Cuties': "https://api-inference.huggingface.co/models/alvdansen/lofi-cuties",'YiffyMix': "https://api-inference.huggingface.co/models/Yntec/YiffyMix",'Analog Madness Realistic v7': "https://api-inference.huggingface.co/models/digiplay/AnalogMadness-realistic-model-v7",'Selfie Photography': "https://api-inference.huggingface.co/models/artificialguybr/selfiephotographyredmond-selfie-photography-lora-for-sdxl",'Filmgrain': "https://api-inference.huggingface.co/models/artificialguybr/filmgrain-redmond-filmgrain-lora-for-sdxl",'Leonardo AI Style Illustration': "https://api-inference.huggingface.co/models/goofyai/Leonardo_Ai_Style_Illustration",'Cyborg Style XL': "https://api-inference.huggingface.co/models/goofyai/cyborg_style_xl",'Little Tinies': "https://api-inference.huggingface.co/models/alvdansen/littletinies",'NSFW XL': "https://api-inference.huggingface.co/models/Dremmar/nsfw-xl",'Analog Redmond': "https://api-inference.huggingface.co/models/artificialguybr/analogredmond",'Pixel Art Redmond': "https://api-inference.huggingface.co/models/artificialguybr/PixelArtRedmond",'Ascii Art': "https://api-inference.huggingface.co/models/CiroN2022/ascii-art",'Analog': "https://api-inference.huggingface.co/models/Yntec/Analog",'Maple Syrup': "https://api-inference.huggingface.co/models/Yntec/MapleSyrup",'Perfect Lewd Fantasy': "https://api-inference.huggingface.co/models/digiplay/perfectLewdFantasy_v1.01",'AbsoluteReality 1.8.1': "https://api-inference.huggingface.co/models/digiplay/AbsoluteReality_v1.8.1",'Disney': "https://api-inference.huggingface.co/models/goofyai/disney_style_xl",'Redmond SDXL': "https://api-inference.huggingface.co/models/artificialguybr/LogoRedmond-LogoLoraForSDXL-V2",'epiCPhotoGasm': "https://api-inference.huggingface.co/models/Yntec/epiCPhotoGasm",}# Add prompt prefixes based on the modelprompt_prefixes = get_model_prompt_prefixes(model)# Return the corresponding URL or a default onereturn model_urls.get(model, "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell")def get_model_prompt_prefixes(model):"""Returns prompt prefixes for specific models"""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, ",'Disney': "Disney style, ",'Pixel Art XL': "pixel art, ",'Character Design': "Character Design, ",'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, ",'Stable Diffusion 3 Medium': "A, ",}return prefixes.get(model, "")def build_request_payload(prompt, is_negative, steps, cfg_scale, seed, strength, width, height, provider):"""Builds the appropriate payload for the selected provider"""# Set a random seed if -1 is providedactual_seed = seed if seed != -1 else random.randint(1, 1000000000)# Provider-specific payloadsif provider == "fal-ai":return {"model": "fal-stable-diffusion-xl","prompt": prompt,"negative_prompt": is_negative,"num_inference_steps": steps,"guidance_scale": cfg_scale,"seed": actual_seed,"width": width,"height": height}elif provider == "replicate":return {"version": "39ed52f2a78e934b3ba6e2a89f5b1c712de7dfea535525255b1aa35c5565e08b","input": {"prompt": prompt,"negative_prompt": is_negative,"num_inference_steps": steps,"guidance_scale": cfg_scale,"seed": actual_seed,"width": width,"height": height}}elif provider == "nebius":return {"model": "yandex/imagen","messages": [{"role": "user","content": prompt}],"generationOptions": {"size": f"{width}x{height}","negativePrompt": is_negative,"seed": actual_seed,"cfgScale": cfg_scale,"steps": steps}}elif provider == "together":return {"model": "stabilityai/stable-diffusion-xl-base-1.0","prompt": prompt,"negative_prompt": is_negative,"steps": steps,"cfg_scale": cfg_scale,"seed": actual_seed,"width": width,"height": height}else: # Default for HF Inferencereturn {"inputs": prompt,"is_negative": is_negative,"steps": steps,"cfg_scale": cfg_scale,"seed": actual_seed,"strength": strength,"parameters": {"width": width,"height": height}}def make_provider_request(api_url, headers, payload, provider, timeout):"""Makes the appropriate request for the selected provider"""if provider == "replicate":# Replicate uses a two-step process: create prediction and get resultscreate_response = requests.post(api_url, headers=headers, json=payload, timeout=timeout)if create_response.status_code != 201:return create_response# Get the prediction IDprediction = create_response.json()get_url = f"{api_url}/{prediction['id']}"# Poll until the prediction is completewhile True:response = requests.get(get_url, headers=headers, timeout=timeout)if response.json()["status"] == "succeeded":# Create a mock response object to match the expected interfaceclass MockResponse:def __init__(self, content, status_code):self.content = contentself.status_code = status_code# Get the image URL from the predictionimage_url = response.json()["output"][0]image_response = requests.get(image_url, timeout=timeout)# Return a mock response with the image contentreturn MockResponse(image_response.content, 200)# Standard request for other providersreturn requests.post(api_url, headers=headers, json=payload, timeout=timeout)def process_provider_response(response, provider):"""Processes the response based on the provider"""if provider == "fal-ai":# Fal AI returns a JSON with an image URLresult = response.json()image_url = result["images"][0]["url"]image_response = requests.get(image_url, timeout=timeout)image = Image.open(io.BytesIO(image_response.content))return imageelif provider == "nebius":# Nebius returns a JSON with Base64 encoded imageresult = response.json()image_data = result["result"]["images"][0]image = Image.open(io.BytesIO(base64.b64decode(image_data)))return imageelif provider == "together":# Together.ai returns a JSON with Base64 encoded imageresult = response.json()image_data = result["images"][0]["base64"]image = Image.open(io.BytesIO(base64.b64decode(image_data)))return imageelse:# Default for HF Inference and Replicate (which uses a MockResponse)image_bytes = response.contentimage = Image.open(io.BytesIO(image_bytes))return imagedef handle_error_response(response):"""Handles error responses from the API"""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.")# Custom CSS to hide the footer in the interfacecss = """12 unmodified lineswith gr.Row():# Textbox for user to input the prompttext_prompt = gr.Textbox(label="Prompt", placeholder="Enter a prompt here", lines=3, elem_id="prompt-text-input")with gr.Row():# Provider selection (new component)providers_list = ["hf-inference", # Default Hugging Face Inference"fal-ai", # Fal AI"nebius", # Nebius"replicate", # Replicate"together", # Together AI]provider_radio = gr.Radio(choices=providers_list,value="hf-inference",label="Inference Provider",info="Select the image generation provider")with gr.Row():# Textbox for custom LoRA inputcustom_lora = gr.Textbox(label="Custom LoRA", info="LoRA Hugging Face path (optional, works with HF Inference only)", placeholder="multimodalart/vintage-ads-flux")with gr.Row():# Accordion for selecting the modelwith gr.Accordion("Featured Models", open=False):108 unmodified lines)# Radio buttons to select the desired modelmodel = gr.Radio(label="Select a model below", value="FLUX.1 [Schnell]", choices=models_list, interactive=True, elem_id="model-radio", info="Note: Some models may only be available with specific providers")# Filtering models based on search inputdef filter_models(search_term):86 unmodified lines""")# Accordion providing information about providerswith gr.Accordion("Provider Information", open=False):gr.Markdown("""## Inference Providers### HF InferenceHugging Face's Inference API provides access to a wide range of models hosted on the Hugging Face Hub.- Supports all models listed in "Featured Models"- Custom LoRA support- Free tier available with API key### Fal AIFal AI offers high-speed inference for image generation models.- Optimized for speed- Limited model selection compared to HF- Requires Fal AI API key### NebiusNebius Cloud provides image generation capabilities.- Good performance for certain model types- Requires Nebius API key### ReplicateReplicate hosts many popular image generation models.- Wide variety of fine-tuned models- Simple API- Requires Replicate API token### Together AITogether AI offers high-performance model hosting.- Optimized for speed and quality- Good selection of models- Requires Together API key""")# Accordion providing an overview of advanced settingswith gr.Accordion("Advanced Settings Overview", open=False):gr.Markdown(30 unmodified lineswith 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 parametertext_button.click(query,inputs=[text_prompt,model,custom_lora,negative_prompt,steps,cfg,method,seed,strength,width,height,provider_radio # Added provider parameter],outputs=image_output)# Function to update UI based on provider selectiondef update_provider_ui(provider):if provider == "hf-inference":return [gr.update(visible=True), # custom_loragr.update(visible=True), # models accordion"Select a model or provide a custom LoRA"]else:return [gr.update(visible=False), # custom_loragr.update(visible=False), # models accordionf"Using {provider} provider - model selection handled by the provider"]# Update UI when provider changesprovider_radio.change(update_provider_ui,inputs=[provider_radio],outputs=[custom_lora,gr.Accordion("Featured Models"),gr.Textbox(label="Provider Status")])print("Launching Gradio interface...") # Debug log# Launch the Gradio interface without showing the API or sharing externally8 unmodified lines910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438512 unmodified lines398399400401402403404405406108 unmodified lines51551651751851952052186 unmodified lines60860961061161261330 unmodified lines6446456466476486496506518 unmodified lines# Project by Nymbo# Base API URL for Hugging Face inferenceAPI_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"# Retrieve the API token from environment variablesAPI_TOKEN = os.getenv("HF_READ_TOKEN")headers = {"Authorization": f"Bearer {API_TOKEN}"}# Timeout for requeststimeout = 100def 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):# Debug log to indicate function startprint("Starting query function...")# Print the parameters for debugging purposesprint(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}")# Check if the prompt is empty or Noneif prompt == "" or prompt is None:print("Prompt is empty or None. Exiting query function.") # Debug logreturn None# Generate a unique key for tracking the generation processkey = random.randint(0, 999)print(f"Generated key: {key}") # Debug log# Randomly select an API token from available options to distribute the loadAPI_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")])headers = {"Authorization": f"Bearer {API_TOKEN}"}print(f"Selected API token: {API_TOKEN}") # Debug log# Enhance the prompt with additional details for better qualityprompt = f"{prompt} | ultra detail, ultra elaboration, ultra quality, perfect."print(f'Generation {key}: {prompt}') # Debug log# Set the API URL based on the selected model or custom LoRAif custom_lora.strip() != "":API_URL = f"https://api-inference.huggingface.co/models/{custom_lora.strip()}"else:if model == 'Stable Diffusion XL':API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"if model == 'FLUX.1 [Dev]':API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"if model == 'FLUX.1 [Schnell]':API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell"if model == 'HiDream-I1-Full':API_URL = "https://api-inference.huggingface.co/models/HiDream-ai/HiDream-I1-Full"if model == 'HiDream-I1-Dev':API_URL = "https://api-inference.huggingface.co/models/HiDream-ai/HiDream-I1-Dev"if model == 'HiDream-I1-Fast':API_URL = "https://api-inference.huggingface.co/models/HiDream-ai/HiDream-I1-Fast"if model == 'Animagine 4.0':API_URL = "https://api-inference.huggingface.co/models/cagliostrolab/animagine-xl-4.0"prompt = f"masterpiece, high score, great score, absurdres, {prompt}"if model == 'Flux Icon Kit':API_URL = "https://api-inference.huggingface.co/models/strangerzonehf/Flux-Icon-Kit-LoRA"prompt = f"Icon Kit, {prompt}"if model == 'Pixel Background':API_URL = "https://api-inference.huggingface.co/models/strangerzonehf/Flux-Pixel-Background-LoRA"prompt = f"Pixel Background, {prompt}"if model == 'Meme XD':API_URL = "https://api-inference.huggingface.co/models/prithivMLmods/Flux-Meme-Xd-LoRA"prompt = f"meme, {prompt}"if model == 'Chill Guy':API_URL = "https://api-inference.huggingface.co/models/prithivMLmods/Flux-Chill-Guy-Zone"prompt = f"chill guy, {prompt}"if model == 'Pepe':API_URL = "https://api-inference.huggingface.co/models/openfree/pepe"prompt = f"pepe, {prompt}"if model == 'NSFWmodel':API_URL = "https://api-inference.huggingface.co/models/lexa862/NSFWmodel"prompt = f"nude, {prompt}"if model == 'Claude Art':API_URL = "https://api-inference.huggingface.co/models/strangerzonehf/Flux-Claude-Art"prompt = f"claude art, {prompt}"if model == 'Open Genmoji':API_URL = "https://api-inference.huggingface.co/models/EvanZhouDev/open-genmoji"prompt = f"emoji, {prompt}"if model == 'EBook Creative Cover':API_URL = "https://api-inference.huggingface.co/models/prithivMLmods/EBook-Creative-Cover-Flux-LoRA"prompt = f"EBook Cover, {prompt}"if model == 'Flux Logo Design 2':API_URL = "https://api-inference.huggingface.co/models/prithivMLmods/Logo-Design-Flux-LoRA"prompt = f"Logo Design, {prompt}"if model == 'Isometric 3D':API_URL = "https://api-inference.huggingface.co/models/strangerzonehf/Flux-Isometric-3D-LoRA"prompt = f"Isometric 3D, {prompt}"if model == 'Flux Condensation':API_URL = "https://api-inference.huggingface.co/models/fofr/flux-condensation"prompt = f"CONDENSATION, {prompt}"if model == 'Flux Handwriting':API_URL = "https://api-inference.huggingface.co/models/fofr/flux-handwriting"prompt = f"HWRIT handwriting, {prompt}"if model == 'Shou Xin':API_URL = "https://api-inference.huggingface.co/models/Datou1111/shou_xin"prompt = f"shou_xin, pencil sketch, {prompt}"if model == 'Sketch Smudge':API_URL = "https://api-inference.huggingface.co/models/strangerzonehf/Flux-Sketch-Smudge-LoRA"prompt = f"Sketch Smudge, {prompt}"if model == '80s Cyberpunk':API_URL = "https://api-inference.huggingface.co/models/fofr/flux-80s-cyberpunk"prompt = f"80s cyberpunk, {prompt}"if model == 'Coloring Book Flux':API_URL = "https://api-inference.huggingface.co/models/renderartist/coloringbookflux"prompt = f"c0l0ringb00k, coloring book, coloring book page, {prompt}"if model == 'Flux Miniature LoRA':API_URL = "https://api-inference.huggingface.co/models/gokaygokay/Flux-Miniature-LoRA"prompt = f"MNTR, miniature drawing, {prompt}"if model == 'Sketch Paint':API_URL = "https://api-inference.huggingface.co/models/strangerzonehf/Sketch-Paint"prompt = f"Sketch paint, {prompt}"if model == 'Flux UltraRealism 2.0':API_URL = "https://api-inference.huggingface.co/models/prithivMLmods/Canopus-LoRA-Flux-UltraRealism-2.0"prompt = f"Ultra realistic, {prompt}"if model == 'Midjourney Mix':API_URL = "https://api-inference.huggingface.co/models/strangerzonehf/Flux-Midjourney-Mix-LoRA"prompt = f"midjourney mix, {prompt}"if model == 'Midjourney Mix 2':API_URL = "https://api-inference.huggingface.co/models/strangerzonehf/Flux-Midjourney-Mix2-LoRA"prompt = f"MJ v6, {prompt}"if model == 'Flux Logo Design':API_URL = "https://api-inference.huggingface.co/models/Shakker-Labs/FLUX.1-dev-LoRA-Logo-Design"prompt = f"wablogo, logo, Minimalist, {prompt}"if model == 'Flux Uncensored':API_URL = "https://api-inference.huggingface.co/models/enhanceaiteam/Flux-uncensored"if model == 'Flux Uncensored V2':API_URL = "https://api-inference.huggingface.co/models/enhanceaiteam/Flux-Uncensored-V2"if model == 'Flux Tarot Cards':API_URL = "https://api-inference.huggingface.co/models/prithivMLmods/Ton618-Tarot-Cards-Flux-LoRA"prompt = f"Tarot card, {prompt}"if model == 'Pixel Art Sprites':API_URL = "https://api-inference.huggingface.co/models/sWizad/pokemon-trainer-sprites-pixelart-flux"prompt = f"a pixel image, {prompt}"if model == '3D Sketchfab':API_URL = "https://api-inference.huggingface.co/models/prithivMLmods/Castor-3D-Sketchfab-Flux-LoRA"prompt = f"3D Sketchfab, {prompt}"if model == 'Retro Comic Flux':API_URL = "https://api-inference.huggingface.co/models/renderartist/retrocomicflux"prompt = f"c0m1c, comic book panel, {prompt}"if model == 'Caricature':API_URL = "https://api-inference.huggingface.co/models/TheAwakenOne/caricature"prompt = f"CCTUR3, {prompt}"if model == 'Huggieverse':API_URL = "https://api-inference.huggingface.co/models/Chunte/flux-lora-Huggieverse"prompt = f"HGGRE, {prompt}"if model == 'Propaganda Poster':API_URL = "https://api-inference.huggingface.co/models/AlekseyCalvin/Propaganda_Poster_Schnell_by_doctor_diffusion"prompt = f"propaganda poster, {prompt}"if model == 'Flux Game Assets V2':API_URL = "https://api-inference.huggingface.co/models/gokaygokay/Flux-Game-Assets-LoRA-v2"prompt = f"wbgmsst, white background, {prompt}"if model == 'SDXL HS Card Style':API_URL = "https://api-inference.huggingface.co/models/Norod78/sdxl-hearthstone-card-style-lora"prompt = f"Hearthstone Card, {prompt}"if model == 'SLDR FLUX NSFW v2 Studio':API_URL = "https://api-inference.huggingface.co/models/xey/sldr_flux_nsfw_v2-studio"if model == 'SoftPasty Flux':API_URL = "https://api-inference.huggingface.co/models/alvdansen/softpasty-flux-dev"prompt = f"araminta_illus illustration style, {prompt}"if model == 'Flux Stickers':API_URL = "https://api-inference.huggingface.co/models/diabolic6045/Flux_Sticker_Lora"prompt = f"5t1cker 5ty1e, {prompt}"if model == 'Flux Animex V2':API_URL = "https://api-inference.huggingface.co/models/strangerzonehf/Flux-Animex-v2-LoRA"prompt = f"Animex, {prompt}"if model == 'Flux Animeo V1':API_URL = "https://api-inference.huggingface.co/models/strangerzonehf/Flux-Animeo-v1-LoRA"prompt = f"Animeo, {prompt}"if model == 'Movie Board':API_URL = "https://api-inference.huggingface.co/models/prithivMLmods/Flux.1-Dev-Movie-Boards-LoRA"prompt = f"movieboard, {prompt}"if model == 'Purple Dreamy':API_URL = "https://api-inference.huggingface.co/models/prithivMLmods/Purple-Dreamy-Flux-LoRA"prompt = f"Purple Dreamy, {prompt}"if model == 'PS1 Style Flux':API_URL = "https://api-inference.huggingface.co/models/veryVANYA/ps1-style-flux"prompt = f"ps1 game screenshot, {prompt}"if model == 'Softserve Anime':API_URL = "https://api-inference.huggingface.co/models/alvdansen/softserve_anime"prompt = f"sftsrv style illustration, {prompt}"if model == 'Flux Tarot v1':API_URL = "https://api-inference.huggingface.co/models/multimodalart/flux-tarot-v1"prompt = f"in the style of TOK a trtcrd tarot style, {prompt}"if model == 'Half Illustration':API_URL = "https://api-inference.huggingface.co/models/davisbro/half_illustration"prompt = f"in the style of TOK, {prompt}"if model == 'OpenDalle v1.1':API_URL = "https://api-inference.huggingface.co/models/dataautogpt3/OpenDalleV1.1"if model == 'Flux Ghibsky Illustration':API_URL = "https://api-inference.huggingface.co/models/aleksa-codes/flux-ghibsky-illustration"prompt = f"GHIBSKY style, {prompt}"if model == 'Flux Koda':API_URL = "https://api-inference.huggingface.co/models/alvdansen/flux-koda"prompt = f"flmft style, {prompt}"if model == 'Soviet Diffusion XL':API_URL = "https://api-inference.huggingface.co/models/openskyml/soviet-diffusion-xl"prompt = f"soviet poster, {prompt}"if model == 'Flux Realism LoRA':API_URL = "https://api-inference.huggingface.co/models/XLabs-AI/flux-RealismLora"if model == 'Frosting Lane Flux':API_URL = "https://api-inference.huggingface.co/models/alvdansen/frosting_lane_flux"prompt = f"frstingln illustration, {prompt}"if model == 'Phantasma Anime':API_URL = "https://api-inference.huggingface.co/models/alvdansen/phantasma-anime"if model == 'Boreal':API_URL = "https://api-inference.huggingface.co/models/kudzueye/Boreal"prompt = f"photo, {prompt}"if model == 'How2Draw':API_URL = "https://api-inference.huggingface.co/models/glif/how2draw"prompt = f"How2Draw, {prompt}"if model == 'Flux AestheticAnime':API_URL = "https://api-inference.huggingface.co/models/dataautogpt3/FLUX-AestheticAnime"if model == 'Fashion Hut Modeling LoRA':API_URL = "https://api-inference.huggingface.co/models/prithivMLmods/Fashion-Hut-Modeling-LoRA"prompt = f"Modeling of, {prompt}"if model == 'Flux SyntheticAnime':API_URL = "https://api-inference.huggingface.co/models/dataautogpt3/FLUX-SyntheticAnime"prompt = f"1980s anime screengrab, VHS quality, syntheticanime, {prompt}"if model == 'Flux Midjourney Anime':API_URL = "https://api-inference.huggingface.co/models/brushpenbob/flux-midjourney-anime"prompt = f"egmid, {prompt}"if model == 'Coloring Book Generator':API_URL = "https://api-inference.huggingface.co/models/robert123231/coloringbookgenerator"if model == 'Collage Flux':API_URL = "https://api-inference.huggingface.co/models/prithivMLmods/Castor-Collage-Dim-Flux-LoRA"prompt = f"collage, {prompt}"if model == 'Flux Product Ad Backdrop':API_URL = "https://api-inference.huggingface.co/models/prithivMLmods/Flux-Product-Ad-Backdrop"prompt = f"Product Ad, {prompt}"if model == 'Product Design':API_URL = "https://api-inference.huggingface.co/models/multimodalart/product-design"prompt = f"product designed by prdsgn, {prompt}"if model == '90s Anime Art':API_URL = "https://api-inference.huggingface.co/models/glif/90s-anime-art"if model == 'Brain Melt Acid Art':API_URL = "https://api-inference.huggingface.co/models/glif/Brain-Melt-Acid-Art"prompt = f"maximalism, in an acid surrealism style, {prompt}"if model == 'Lustly Flux Uncensored v1':API_URL = "https://api-inference.huggingface.co/models/lustlyai/Flux_Lustly.ai_Uncensored_nsfw_v1"if model == 'NSFW Master Flux':API_URL = "https://api-inference.huggingface.co/models/Keltezaa/NSFW_MASTER_FLUX"prompt = f"NSFW, {prompt}"if model == 'Flux Outfit Generator':API_URL = "https://api-inference.huggingface.co/models/tryonlabs/FLUX.1-dev-LoRA-Outfit-Generator"if model == 'Midjourney':API_URL = "https://api-inference.huggingface.co/models/Jovie/Midjourney"if model == 'DreamPhotoGASM':API_URL = "https://api-inference.huggingface.co/models/Yntec/DreamPhotoGASM"if model == 'Flux Super Realism LoRA':API_URL = "https://api-inference.huggingface.co/models/strangerzonehf/Flux-Super-Realism-LoRA"if model == 'Stable Diffusion 2-1':API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2-1-base"if model == 'Stable Diffusion 3.5 Large':API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-3.5-large"if model == 'Stable Diffusion 3.5 Large Turbo':API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-3.5-large-turbo"if model == 'Stable Diffusion 3 Medium':API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-3-medium-diffusers"prompt = f"A, {prompt}"if model == 'Duchaiten Real3D NSFW XL':API_URL = "https://api-inference.huggingface.co/models/stablediffusionapi/duchaiten-real3d-nsfw-xl"if model == 'Pixel Art XL':API_URL = "https://api-inference.huggingface.co/models/nerijs/pixel-art-xl"prompt = f"pixel art, {prompt}"if model == 'Character Design':API_URL = "https://api-inference.huggingface.co/models/KappaNeuro/character-design"prompt = f"Character Design, {prompt}"if model == 'Sketched Out Manga':API_URL = "https://api-inference.huggingface.co/models/alvdansen/sketchedoutmanga"prompt = f"daiton, {prompt}"if model == 'Archfey Anime':API_URL = "https://api-inference.huggingface.co/models/alvdansen/archfey_anime"if model == 'Lofi Cuties':API_URL = "https://api-inference.huggingface.co/models/alvdansen/lofi-cuties"if model == 'YiffyMix':API_URL = "https://api-inference.huggingface.co/models/Yntec/YiffyMix"if model == 'Analog Madness Realistic v7':API_URL = "https://api-inference.huggingface.co/models/digiplay/AnalogMadness-realistic-model-v7"if model == 'Selfie Photography':API_URL = "https://api-inference.huggingface.co/models/artificialguybr/selfiephotographyredmond-selfie-photography-lora-for-sdxl"prompt = f"instagram model, discord profile picture, {prompt}"if model == 'Filmgrain':API_URL = "https://api-inference.huggingface.co/models/artificialguybr/filmgrain-redmond-filmgrain-lora-for-sdxl"prompt = f"Film Grain, FilmGrainAF, {prompt}"if model == 'Leonardo AI Style Illustration':API_URL = "https://api-inference.huggingface.co/models/goofyai/Leonardo_Ai_Style_Illustration"prompt = f"leonardo style, illustration, vector art, {prompt}"if model == 'Cyborg Style XL':API_URL = "https://api-inference.huggingface.co/models/goofyai/cyborg_style_xl"prompt = f"cyborg style, {prompt}"if model == 'Little Tinies':API_URL = "https://api-inference.huggingface.co/models/alvdansen/littletinies"if model == 'NSFW XL':API_URL = "https://api-inference.huggingface.co/models/Dremmar/nsfw-xl"if model == 'Analog Redmond':API_URL = "https://api-inference.huggingface.co/models/artificialguybr/analogredmond"prompt = f"timeless style, {prompt}"if model == 'Pixel Art Redmond':API_URL = "https://api-inference.huggingface.co/models/artificialguybr/PixelArtRedmond"prompt = f"Pixel Art, {prompt}"if model == 'Ascii Art':API_URL = "https://api-inference.huggingface.co/models/CiroN2022/ascii-art"prompt = f"ascii art, {prompt}"if model == 'Analog':API_URL = "https://api-inference.huggingface.co/models/Yntec/Analog"if model == 'Maple Syrup':API_URL = "https://api-inference.huggingface.co/models/Yntec/MapleSyrup"if model == 'Perfect Lewd Fantasy':API_URL = "https://api-inference.huggingface.co/models/digiplay/perfectLewdFantasy_v1.01"if model == 'AbsoluteReality 1.8.1':API_URL = "https://api-inference.huggingface.co/models/digiplay/AbsoluteReality_v1.8.1"if model == 'Disney':API_URL = "https://api-inference.huggingface.co/models/goofyai/disney_style_xl"prompt = f"Disney style, {prompt}"if model == 'Redmond SDXL':API_URL = "https://api-inference.huggingface.co/models/artificialguybr/LogoRedmond-LogoLoraForSDXL-V2"if model == 'epiCPhotoGasm':API_URL = "https://api-inference.huggingface.co/models/Yntec/epiCPhotoGasm"print(f"API URL set to: {API_URL}") # Debug log# Define the payload for the requestpayload = {"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# Make a request to the API to generate the imagetry:response = requests.post(API_URL, headers=headers, json=payload, timeout=timeout)print(f"Response status code: {response.status_code}") # Debug logexcept requests.exceptions.RequestException as e:# Log any request exceptions and raise an error for the userprint(f"Request failed: {e}") # Debug lograise gr.Error(f"Request failed: {e}")# Check if the response status is not successfulif response.status_code != 200:print(f"Error: Failed to retrieve image. Response status: {response.status_code}") # Debug logprint(f"Response content: {response.text}") # Debug logif 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 contentimage_bytes = response.contentimage = Image.open(io.BytesIO(image_bytes))print(f'Generation {key} completed! ({prompt})') # Debug logreturn imageexcept Exception as e:# Handle any errors that occur when opening the imageprint(f"Error while trying to open image: {e}") # Debug logreturn None# Custom CSS to hide the footer in the interfacecss = """12 unmodified lineswith gr.Row():# Textbox for user to input the prompttext_prompt = gr.Textbox(label="Prompt", placeholder="Enter a prompt here", lines=3, elem_id="prompt-text-input")with gr.Row():# Textbox for custom LoRA inputcustom_lora = gr.Textbox(label="Custom LoRA", info="LoRA Hugging Face path (optional)", placeholder="multimodalart/vintage-ads-flux")with gr.Row():# Accordion for selecting the modelwith gr.Accordion("Featured Models", open=False):108 unmodified lines)# Radio buttons to select the desired modelmodel = 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 inputdef filter_models(search_term):86 unmodified lines""")# Accordion providing an overview of advanced settingswith gr.Accordion("Advanced Settings Overview", open=False):gr.Markdown(30 unmodified lineswith gr.Row():image_output = gr.Image(type="pil", label="Image Output", elem_id="gallery")# Set up button click event to call the query functiontext_button.click(query, inputs=[text_prompt, model, custom_lora, negative_prompt, steps, cfg, method, seed, strength, width, height], outputs=image_output)print("Launching Gradio interface...") # Debug log# Launch the Gradio interface without showing the API or sharing externally
app.py+338-536