import gradio as gr from PIL import Image import json import os # Obtener la ruta absoluta para guardar el archivo en el directorio actual def get_file_path(filename): return os.path.join(os.getcwd(), filename) # Cargar puntuaciones Elo desde un archivo JSON def load_elo_scores(): try: with open(get_file_path("elo_scores.json"), "r") as f: return json.load(f) except FileNotFoundError: # Si el archivo no existe, inicializamos las puntuaciones return {"DALL-E 3": 1000, "Midjourney 6.1": 1000, "Flux": 1000} # Guardar puntuaciones Elo en un archivo JSON def save_elo_scores(elo_scores): try: with open(get_file_path("elo_scores.json"), "w") as f: json.dump(elo_scores, f) print("Puntuaciones guardadas correctamente en elo_scores.json") except Exception as e: print(f"Error al guardar el archivo: {e}") # Función para calcular la actualización de Elo def elo_update(winner_elo, loser_elo, k=32): expected_score_winner = 1 / (1 + 10 ** ((loser_elo - winner_elo) / 400)) new_winner_elo = winner_elo + k * (1 - expected_score_winner) new_loser_elo = loser_elo + k * (0 - (1 - expected_score_winner)) return new_winner_elo, new_loser_elo # Cargar imágenes predefinidas def load_images(): dalle3_image = Image.open("images/dalle3_image.png") midjourney_image = Image.open("images/midjourney_image.png") flux_image = Image.open("images/flux_image.png") return dalle3_image, midjourney_image, flux_image # Actualizar el puntaje Elo basado en la votación def update_elo(first, second, third): global elo_scores # Primera elección if first == "DALL-E 3": elo_scores["DALL-E 3"], elo_scores["Midjourney 6.1"] = elo_update(elo_scores["DALL-E 3"], elo_scores["Midjourney 6.1"]) elo_scores["DALL-E 3"], elo_scores["Flux"] = elo_update(elo_scores["DALL-E 3"], elo_scores["Flux"]) elif first == "Midjourney 6.1": elo_scores["Midjourney 6.1"], elo_scores["DALL-E 3"] = elo_update(elo_scores["Midjourney 6.1"], elo_scores["DALL-E 3"]) elo_scores["Midjourney 6.1"], elo_scores["Flux"] = elo_update(elo_scores["Midjourney 6.1"], elo_scores["Flux"]) elif first == "Flux": elo_scores["Flux"], elo_scores["DALL-E 3"] = elo_update(elo_scores["Flux"], elo_scores["DALL-E 3"]) elo_scores["Flux"], elo_scores["Midjourney 6.1"] = elo_update(elo_scores["Flux"], elo_scores["Midjourney 6.1"]) # Segunda elección if second == "DALL-E 3": elo_scores["DALL-E 3"], elo_scores["Midjourney 6.1"] = elo_update(elo_scores["DALL-E 3"], elo_scores["Midjourney 6.1"]) elo_scores["DALL-E 3"], elo_scores["Flux"] = elo_update(elo_scores["DALL-E 3"], elo_scores["Flux"]) elif second == "Midjourney 6.1": elo_scores["Midjourney 6.1"], elo_scores["DALL-E 3"] = elo_update(elo_scores["Midjourney 6.1"], elo_scores["DALL-E 3"]) elo_scores["Midjourney 6.1"], elo_scores["Flux"] = elo_update(elo_scores["Midjourney 6.1"], elo_scores["Flux"]) elif second == "Flux": elo_scores["Flux"], elo_scores["DALL-E 3"] = elo_update(elo_scores["Flux"], elo_scores["DALL-E 3"]) elo_scores["Flux"], elo_scores["Midjourney 6.1"] = elo_update(elo_scores["Flux"], elo_scores["Midjourney 6.1"]) # Guardar las puntuaciones actualizadas en el archivo save_elo_scores(elo_scores) return elo_scores["DALL-E 3"], elo_scores["Midjourney 6.1"], elo_scores["Flux"] # Cargar las puntuaciones Elo elo_scores = load_elo_scores() # Interfaz Gradio def vote(first, second, third): dalle3_score, midjourney_score, flux_score = update_elo(first, second, third) dalle3_image, midjourney_image, flux_image = load_images() return dalle3_image, midjourney_image, flux_image, f"DALL-E 3: {round(dalle3_score)}", f"Midjourney 6.1: {round(midjourney_score)}", f"Flux: {round(flux_score)}" # Cargar imágenes al inicio dalle3_image, midjourney_image, flux_image = load_images() with gr.Blocks() as demo: gr.Markdown("# Akelarre Generativo (DALL-E 3 vs MJ 6.1 vs Flux 1)") gr.Markdown("### Vota por la imagen generada por IA que prefieras. La puntuación Elo se actualizará en tiempo real.") dalle3_img = gr.Image(value=dalle3_image, label="DALL-E 3", interactive=False, height=512, width=512) midjourney_img = gr.Image(value=midjourney_image, label="Midjourney 6.1", interactive=False, height=512, width=512) flux_img = gr.Image(value=flux_image, label="Flux", interactive=False, height=512, width=512) with gr.Row(): first_choice = gr.Dropdown(["DALL-E 3", "Midjourney 6.1", "Flux"], label="Primera Favorita") second_choice = gr.Dropdown(["DALL-E 3", "Midjourney 6.1", "Flux"], label="Segunda Favorita") third_choice = gr.Dropdown(["DALL-E 3", "Midjourney 6.1", "Flux"], label="Tercera Favorita") with gr.Row(): dalle3_score_text = gr.Textbox(value=f"DALL-E 3: {elo_scores['DALL-E 3']}", label="Puntuación DALL-E 3", interactive=False) midjourney_score_text = gr.Textbox(value=f"Midjourney 6.1: {elo_scores['Midjourney 6.1']}", label="Puntuación Midjourney 6.1", interactive=False) flux_score_text = gr.Textbox(value=f"Flux: {elo_scores['Flux']}", label="Puntuación Flux", interactive=False) vote_button = gr.Button("Votar y Actualizar") vote_button.click(vote, inputs=[first_choice, second_choice, third_choice], outputs=[dalle3_img, midjourney_img, flux_img, dalle3_score_text, midjourney_score_text, flux_score_text]) demo.launch()