Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import os | |
| import random | |
| from datetime import datetime | |
| from huggingface_hub import InferenceClient | |
| # Configuración | |
| OUTPUT_DIR = "generated_images" | |
| os.makedirs(OUTPUT_DIR, exist_ok=True) | |
| # Usamos el Token de los Secrets del Space | |
| HF_TOKEN = os.getenv("HUGGINGFACE_TOKEN") or os.getenv("HF_TOKEN") | |
| client = InferenceClient(token=HF_TOKEN) | |
| # Identidad de Sofía Rivera (Optimizada para realismo) | |
| SOFIA_CORE = ( | |
| "sofia rivera, a beautiful 25-year-old latina cuban-american woman, " | |
| "long dark wavy hair, mesmerizing hazel eyes, toned athletic fitness body, " | |
| "natural radiant skin, miami influencer aesthetic, hyperrealistic, 8k" | |
| ) | |
| CATEGORIES = { | |
| "Fitness - Gym": { | |
| "prompt": f"full body mirror selfie of {SOFIA_CORE}, wearing black sports bra and leggings, modern miami gym background, cinematic lighting", | |
| "desc": "### 🏋️ Pilar Fitness\nIdeal para Lunes, Miércoles y Viernes." | |
| }, | |
| "Lifestyle - Beach": { | |
| "prompt": f"candid photo of {SOFIA_CORE}, tropical outfit, miami beach sunset, vibrant colors, bokeh background", | |
| "desc": "### 🌴 Pilar Lifestyle\nIdeal para Martes y Jueves." | |
| }, | |
| "Lifestyle - Luxury": { | |
| "prompt": f"portrait of {SOFIA_CORE}, luxury miami apartment balcony, morning light, natural makeup, authentic vibe", | |
| "desc": "### ☕ Pilar Personal\nIdeal para Sábados." | |
| }, | |
| "Premium - Elegant": { | |
| "prompt": f"tasteful photography of {SOFIA_CORE}, elegant black lace outfit, soft moody lighting, professional studio aesthetic", | |
| "desc": "### 💎 Pilar Premium\nIdeal para Domingos." | |
| } | |
| } | |
| def generate(category_name, tweak, model_id, steps, cfg, seed): | |
| try: | |
| if not HF_TOKEN: | |
| return None, "❌ Error: No se encontró el HUGGINGFACE_TOKEN en los Secrets del Space." | |
| cat = CATEGORIES[category_name] | |
| prompt = f"{cat['prompt']}, {tweak}" if tweak else cat['prompt'] | |
| # Gestión de la Seed | |
| final_seed = random.randint(0, 2**32 - 1) if seed == -1 else int(seed) | |
| image = client.text_to_image( | |
| prompt=prompt, | |
| model=model_id, | |
| num_inference_steps=int(steps), | |
| guidance_scale=float(cfg), | |
| seed=final_seed | |
| ) | |
| return image, f"✅ Generada con {model_id} | Seed: {final_seed}" | |
| except Exception as e: | |
| return None, f"❌ Error: {str(e)}" | |
| def create_interface(): | |
| with gr.Blocks(theme=gr.themes.Soft()) as demo: | |
| gr.Markdown("# 🌴 Sofia Rivera AI Workspace") | |
| gr.Markdown("Generador de contenido oficial para la agencia de Sofía.") | |
| with gr.Tab("🎨 Generador de Imágenes"): | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| cat_drop = gr.Dropdown(choices=list(CATEGORIES.keys()), label="Selecciona el Pilar de Contenido", value="Fitness - Gym") | |
| cat_info = gr.Markdown(CATEGORIES["Fitness - Gym"]["desc"]) | |
| tweak = gr.Textbox(label="Detalles extra (opcional)", placeholder="ej: holding a coffee, sunset, smiling...") | |
| with gr.Accordion("Ajustes Técnicos (Avanzado)", open=True): | |
| # CAMBIO CLAVE: 'schnell' es el modelo gratuito y rápido | |
| model = gr.Dropdown( | |
| choices=["black-forest-labs/FLUX.1-schnell", "black-forest-labs/FLUX.1-dev"], | |
| label="Modelo (Schnell = Gratis)", | |
| value="black-forest-labs/FLUX.1-schnell" | |
| ) | |
| steps = gr.Slider(1, 12, 4, label="Pasos (Schnell funciona mejor con 4-8)") | |
| cfg = gr.Slider(1, 20, 3.5, label="Guidance Scale") | |
| seed = gr.Number(-1, label="Seed (-1 para aleatorio)") | |
| btn = gr.Button("🚀 Generar Imagen de Sofía", variant="primary") | |
| with gr.Column(scale=2): | |
| out_img = gr.Image(label="Vista previa de Sofía") | |
| out_log = gr.Textbox(label="Log de Estado", interactive=False) | |
| cat_drop.change(lambda x: CATEGORIES[x]["desc"], inputs=[cat_drop], outputs=[cat_info]) | |
| btn.click(generate, [cat_drop, tweak, model, steps, cfg, seed], [out_img, out_log]) | |
| return demo | |
| if __name__ == "__main__": | |
| create_interface().launch() | |