import gradio as gr import requests import io import random import os from PIL import Image from deep_translator import GoogleTranslator # Project by Nymbo API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev" API_TOKEN = os.getenv("HF_READ_TOKEN") headers = {"Authorization": f"Bearer {API_TOKEN}"} timeout = 100 def query(prompt, is_negative=False, steps=30, cfg_scale=7, strength=0.7): if not prompt: return None key = random.randint(0, 999) # Detectar el idioma del prompt y traducirlo al inglés translator = GoogleTranslator(target='en') try: prompt = translator.translate(prompt) except Exception as e: print(f"Error during translation: {e}") return None print(f'\033[1mGeneration {key} translation:\033[0m {prompt}') prompt = f"{prompt} | ultra detail, ultra elaboration, ultra quality, perfect." print(f'\033[1mGeneration {key}:\033[0m {prompt}') payload = { "inputs": prompt, "is_negative": is_negative, "steps": steps, "cfg_scale": cfg_scale, "strength": strength } response = requests.post(API_URL, headers=headers, json=payload, timeout=timeout) if response.status_code != 200: print(f"Error: Failed to get image. Response status: {response.status_code}") print(f"Response content: {response.text}") if response.status_code == 503: raise gr.Error(f"{response.status_code} : The model is being loaded") raise gr.Error(f"{response.status_code}") try: image_bytes = response.content image = Image.open(io.BytesIO(image_bytes)) print(f'\033[1mGeneration {key} completed!\033[0m ({prompt})') return image except Exception as e: print(f"Error when trying to open the image: {e}") return None css = """ #app-container { max-width: 100%; /* Ancho máximo del contenedor en 100% del espacio disponible */ margin: 0 auto; /* Centrar el contenedor */ padding: 20px; /* Espacio interno */ } input, textarea, select { background-color: #f5f5f5; /* Fondo gris claro para inputs */ color: #333333; /* Texto gris oscuro en inputs */ border: 1px solid #cccccc; /* Borde gris claro en inputs */ width: calc(100% - 40px); /* Ancho completo menos espacio para el botón */ box-sizing: border-box; /* Incluir el borde en el ancho total */ padding-right: 40px; /* Espacio para el botón de borrar */ } input[type="text"] { position: relative; } input[type="text"]::after { content: "✕"; /* Símbolo de borrar */ position: absolute; right: 10px; top: 50%; transform: translateY(-50%); cursor: pointer; color: #666; } button { background-color: #f5f5f5; /* Fondo gris claro */ color: #333333; /* Texto gris oscuro */ border: 1px solid black; /* Borde negro en botones */ width: 100%; /* Ancho completo del botón */ box-sizing: border-box; /* Incluir el borde en el ancho total */ padding: 10px; /* Espacio interno en el botón */ } button.primary { background-color: green; /* Fondo verde para el botón 'Generate' */ color: black; /* Texto negro en el botón 'Generate' */ } button.secondary { background-color: #f5f5f5; /* Fondo gris claro para el botón 'Clear' */ color: #333333; /* Texto gris oscuro en el botón 'Clear' */ } button:hover { background-color: #e0e0e0; /* Fondo gris más oscuro en hover */ } h1, h2, h3, h4, h5, h6 { color: #333333; /* Texto gris oscuro en encabezados */ } @media (max-width: 768px) { .button-container { display: flex; flex-direction: column; align-items: center; /* Centra el botón en vista móvil */ } .button-container button { width: auto; /* Ajusta el ancho del botón para móviles */ margin-top: 10px; /* Espacio superior para el botón en móviles */ } } @media (min-width: 769px) { .button-container { display: flex; justify-content: flex-end; /* Alinea el botón a la derecha en vista de escritorio */ } .button-container button { width: auto; /* Ajusta el ancho del botón para escritorio */ } } """ examples = [ "A group of Minions, the small, yellow, goggle-wearing characters, trapped inside in the same large square block of transparent ice. Their comical expressions of surprise and confusion are clearly visible through the ice, with their characteristic overalls and goggles still intact. The surrounding ice is frosted and cracked, with hints of blue light reflecting off the surface. The scene is set in a wintry environment, with snowflakes gently falling and an icy landscape stretching into the back.", "A golden brown horse, trotting forward with bright, expressive eyes and a joyful expression, is captured in stunning 8K realism. The vibrant, colorful sky features brilliantly bright, multicolored clouds in deep blue, pink, and orange hues. The horse's rich coat contrasts beautifully with the swirling clouds, creating a striking and dynamic scene full of energy and joy.", "((Helmet of Kylo Ren)) in dominant shape of cat, heavily adapted to the physical characteristics of (((cat))), incorporating rugged texture and robust elements. Ultra-detailed 3D render with exquisite rim lighting, vibrant and hyper-realistic textures. Majestic design, imposing presence, polished tones according to (colors of Kylo Ren). Contrasted and meticulously crafted background, high-quality octane rendering, 8K." ] with gr.Blocks(css=css) as app: gr.HTML("""