Spaces:
Sleeping
Sleeping
File size: 8,563 Bytes
10ba16c a1dbcb7 4816388 8372f4c 074fefa a3f2600 ad396e5 a3f2600 074fefa 6321704 ad396e5 6321704 ad396e5 6321704 ba7b66b a3f2600 ba7b66b a3f2600 074fefa a3f2600 074fefa ba7b66b 6fdac66 ad396e5 931085f 6fdac66 ad396e5 a3f2600 ad396e5 a9d8b21 8372f4c a9d8b21 952d3bf ad396e5 fffcbc3 ad396e5 8372f4c a3f2600 ad396e5 074fefa a3f2600 e0acaeb 8372f4c e0acaeb 8372f4c a3f2600 c31f77a ad396e5 a3f2600 ad396e5 074fefa a3f2600 ad396e5 8372f4c 952d3bf ad396e5 8372f4c 952d3bf 8372f4c 952d3bf a3f2600 074fefa d333813 074fefa a3f2600 074fefa ad396e5 074fefa 952d3bf a3f2600 074fefa d333813 a3f2600 8372f4c 44a22b9 8372f4c 44a22b9 952d3bf a3f2600 952d3bf fc4dde7 952d3bf fc4dde7 a3f2600 8372f4c fc4dde7 952d3bf fc4dde7 a3f2600 d18852a e7d38e7 d18852a 074fefa a3f2600 a1dbcb7 074fefa a3f2600 dd1c945 952d3bf 9e8f5e5 8b0fc18 a3f2600 ad396e5 8372f4c 9e8f5e5 a1dbcb7 bb48252 952d3bf a1dbcb7 a3f2600 da33556 a3f2600 da33556 3ba7b10 a3f2600 da33556 e0acaeb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
from pathlib import Path
from PIL import Image
import streamlit as st
import os, random, numpy as np, yaml, time
from dataclasses import dataclass
from typing import List
from huggingface_hub import InferenceClient
import google.generativeai as genai
st.set_page_config(layout="wide")
HF_TOKEN = os.getenv("HF_TOKEN")
GEMINI_TOKEN = os.getenv("GEMINI_TOKEN")
if not HF_TOKEN:
st.error("Error en el token de Hugging Face! 'HF_TOKEN'.")
st.stop()
if not GEMINI_TOKEN:
st.error("Error en el token de Gemini! 'GEMINI_TOKEN'.")
st.stop()
try:
with open("config.yaml", "r") as file:
credentials = yaml.safe_load(file)
except Exception as e:
st.error(f"Error al cargar el archivo de configuraci贸n: {e}")
credentials = {"username": "", "password": ""}
@dataclass
class AppConfig:
MAX_SEED: int = 1000000
CLEANUP_DAYS: int = 7
MAX_SEED = AppConfig.MAX_SEED
DATA_PATH = Path("./data")
DATA_PATH.mkdir(exist_ok=True)
def get_hf_client():
return InferenceClient(token=HF_TOKEN)
def get_gemini_client():
genai.configure(api_key=GEMINI_TOKEN)
return genai.GenerativeModel('gemini-1.5-flash-8b')
hf_client = get_hf_client()
gemini_client = get_gemini_client()
def authenticate_user(username, password):
return username == credentials["username"] and password == credentials["password"]
def list_saved_images():
return sorted(DATA_PATH.glob("*.jpg"), key=lambda x: x.stat().st_mtime, reverse=True)
def enhance_prompt(text, client=None, use_enhancement=True):
if not use_enhancement:
return text[:200]
if not client:
return text[:200]
try:
enhancement_instruction = "Enhance this text description to be more suitable for text-to-image generation. Focus on vivid, descriptive language that will help an AI generate a photorealistic image. Be specific about colors, composition, lighting, and key details."
response = client.generate_content(f"{enhancement_instruction}\n\nOriginal prompt: {text}")
enhanced_prompt = response.text.strip()
return enhanced_prompt[:200]
except Exception as e:
st.warning(f"Prompt enhancement error: {e}")
return text[:200]
def save_prompt(image_name, enhanced_prompt):
with open(DATA_PATH / "prompts.txt", "a") as f:
f.write(f"{image_name}: {enhanced_prompt}\n")
def generate_variations(prompt, num_variants=8, use_enhanced=True):
instructions = [
"Photorealistic description for txt2img prompt: ",
"Creative, realistic text-to-image prompt: ",
"Descriptive, true-to-life txt2img prompt: ",
"Naturalistic scene with detailed prompt: ",
"Realistic, elegant txt2img prompt: ",
"Visually dynamic, hyperrealistic prompt: ",
"Cinematic txt2img with hyperrealistic elements: ",
"Lifelike txt2img, focusing on photorealistic depth: "
]
if use_enhanced:
prompts = [enhance_prompt(f"{instructions[i % len(instructions)]}{prompt}", client=gemini_client, use_enhancement=True) for i in range(num_variants)]
else:
prompts = [enhance_prompt(prompt, use_enhancement=False) for i in range(num_variants)]
return prompts
def generate_image(prompt, width, height, seed, model_name, client=None):
if not client:
st.error("No Hugging Face client available")
return None, seed, None
try:
with st.spinner("Generando imagen..."):
seed = int(seed) if seed != -1 else random.randint(0, AppConfig.MAX_SEED)
enhanced_prompt = enhance_prompt(prompt, client=gemini_client)
image = client.text_to_image(
prompt=enhanced_prompt,
height=height,
width=width,
model=model_name,
seed=seed
)
return image, seed, enhanced_prompt
except Exception as e:
st.error(f"Image generation error: {e}")
return None, seed, None
def gen(prompts, width, height, model_name, num_variants=8):
images = []
seeds = []
while len(seeds) < num_variants:
seed = random.randint(0, MAX_SEED)
if seed not in seeds:
seeds.append(seed)
for i in range(num_variants):
current_prompt = prompts[i] if len(prompts) > i else prompts[-1]
with st.spinner(f"Generando imagen {i+1}/{num_variants}"):
image, used_seed, enhanced_prompt = generate_image(current_prompt, width, height, seeds[i], model_name, client=hf_client)
if image:
image_path = DATA_PATH / f"generated_image_{used_seed}.jpg"
image.save(image_path)
save_prompt(f"generated_image_{used_seed}.jpg", enhanced_prompt)
images.append((str(image_path), enhanced_prompt))
st.success(f"Imagen {i+1} generada")
return images
def get_prompt_for_image(image_name):
try:
with open(DATA_PATH / "prompts.txt", "r") as f:
for line in f:
if line.startswith(image_name):
original_prompt = line.split(": ", 1)[1].strip()
image_path = DATA_PATH / image_name
if image_path.exists():
return original_prompt
except FileNotFoundError:
return "No hay prompt asociado"
return "No hay prompt asociado"
def display_gallery():
st.header("Galer铆a de Im谩genes Guardadas")
images = list_saved_images()
if images:
cols = st.columns(4)
for i, image_file in enumerate(images):
with cols[i % 4]:
st.image(str(image_file), use_column_width=True)
prompt = get_prompt_for_image(image_file.name)
st.caption(prompt[:250])
if st.button(f"Borrar", key=f"delete_{i}_{image_file}"):
if image_file.exists():
os.remove(image_file)
st.success("Imagen borrada")
st.rerun()
def login_form():
st.title("Iniciar Sesi贸n")
username = st.text_input("Usuario", value="admin")
password = st.text_input("Contrase帽a", value="flux3x", type="password")
if st.button("Iniciar Sesi贸n"):
if authenticate_user(username, password):
st.session_state['authenticated'] = True
st.success("Autenticaci贸n exitosa.")
else:
st.error("Credenciales incorrectas. Intenta de nuevo.")
def upload_image_to_gallery():
uploaded_image = st.sidebar.file_uploader("Sube una imagen a la galer铆a", type=["jpg", "jpeg", "png"])
if uploaded_image:
image = Image.open(uploaded_image)
image_path = DATA_PATH / uploaded_image.name
image.save(image_path)
save_prompt(uploaded_image.name, "uploaded by user")
st.sidebar.success(f"Imagen subida: {image_path}")
def main():
if 'authenticated' not in st.session_state or not st.session_state['authenticated']:
login_form()
return
st.title("Flux +Upscale +Prompt Enhancer")
if not hf_client or not gemini_client:
st.error("No se pudo establecer conexi贸n con los servicios. Verifique sus tokens.")
return
prompt = st.sidebar.text_area("Descripci贸n de la imagen", height=150, max_chars=500)
format_option = st.sidebar.selectbox("Formato", ["9:16", "16:9", "1:1"])
model_option = st.sidebar.selectbox("Modelo", ["salomonsky/flux3"])
prompt_enhance = st.sidebar.checkbox("Mejorar Prompt", True)
num_variants = st.sidebar.slider("N煤mero de im谩genes", 1, 8, 8)
width, height = (720, 1280) if format_option == "9:16" else (1280, 720) if format_option == "16:9" else (1280, 1280)
if st.sidebar.button("Generar Im谩genes"):
if prompt:
prompts = generate_variations(prompt, num_variants=num_variants, use_enhanced=prompt_enhance)
generated_images = gen(prompts, width, height, model_option, num_variants)
st.header("Im谩genes Generadas")
cols = st.columns(4)
for i, (image_path, image_prompt) in enumerate(generated_images):
with cols[i % 4]:
st.image(image_path, use_column_width=True)
st.caption(image_prompt)
else:
st.warning("Por favor, ingrese una descripci贸n de imagen")
upload_image_to_gallery()
display_gallery()
if __name__ == "__main__":
main() |