File size: 1,523 Bytes
e0247e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import pipeline
from diffusers import StableDiffusionPipeline
import torch

# Clasificador de texto (transformers)
clasificador = pipeline("text-classification", model="TheBritishLibrary/bl-books-genre")

# Generador de imágenes (diffusers)
diff_pipe = StableDiffusionPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16
).to("cuda")

# Lógica
def analizar_y_generar(texto):
    resultado = clasificador(texto)
    genero = resultado[0]["label"]
    confianza = resultado[0]["score"]
    prompt_img = f"A dreamy illustration representing the literary genre: {genero.lower()}"
    
    imagen = diff_pipe(prompt_img).images[0]
    texto_genero = f"Género: {genero} (confianza: {confianza:.2f})"
    
    return texto_genero, imagen

# Interfaz con diseño mejorado
with gr.Blocks(theme=gr.themes.Soft()) as demo:
    gr.Markdown("# 📚 Clasificador de Género + Generador de Imagen")
    gr.Markdown("Introduce un fragmento de texto para clasificar su género y generar una imagen relacionada.")
    
    with gr.Row():
        entrada = gr.Textbox(lines=4, label="Texto del Libro", placeholder="Introduce aquí tu fragmento...")
    
    boton = gr.Button("Analizar y Generar Imagen 🎨")
    
    with gr.Row():
        salida_texto = gr.Textbox(label="Resultado del Modelo")
        salida_img = gr.Image(label="Imagen Generada")
    
    boton.click(fn=analizar_y_generar, inputs=entrada, outputs=[salida_texto, salida_img])

demo.launch()