Update app.py
Browse files
app.py
CHANGED
|
@@ -1,56 +1,57 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from transformers import pipeline
|
| 3 |
-
from diffusers import DiffusionPipeline
|
| 4 |
-
import torch
|
| 5 |
-
import time
|
| 6 |
-
from tqdm import trange
|
| 7 |
-
|
| 8 |
-
# Detectar hardware
|
| 9 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
-
torch_dtype = torch.float16 if device == "cuda" else torch.float32
|
| 11 |
-
|
| 12 |
-
# Modelo de imagen (SSD-1B)
|
| 13 |
-
image_pipe = DiffusionPipeline.from_pretrained(
|
| 14 |
-
"segmind/SSD-1B",
|
| 15 |
-
torch_dtype=torch_dtype
|
| 16 |
-
).to(device)
|
| 17 |
-
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
model
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
# Función principal con progreso
|
| 26 |
-
def crear_microcuento(tema, progress=gr.Progress(track_tqdm=True)):
|
| 27 |
-
prompt_img = f"Ilustración digital detallada sobre: {tema}, arte suave, poético, luces tenues"
|
| 28 |
-
|
| 29 |
-
for _ in trange(50, desc="Generando imagen..."):
|
| 30 |
-
time.sleep(0.05)
|
| 31 |
-
imagen = image_pipe(prompt_img).images[0]
|
| 32 |
-
|
| 33 |
-
for _ in trange(50, desc="Generando microcuento..."):
|
| 34 |
-
time.sleep(0.03)
|
| 35 |
-
|
| 36 |
-
prompt_txt = f"Escribe un microcuento narrativo, creativo y poético en español sobre: '{tema}'. El cuento debe tener inicio, desarrollo y final en no más de 4 líneas."
|
| 37 |
-
cuento = text_pipe(prompt_txt, max_new_tokens=100, do_sample=True, temperature=0.9)[0]["generated_text"]
|
| 38 |
-
|
| 39 |
-
return imagen, cuento.replace(prompt_txt, "").strip()
|
| 40 |
-
|
| 41 |
-
# Interfaz
|
| 42 |
-
gr.Interface(
|
| 43 |
-
fn=crear_microcuento,
|
| 44 |
-
inputs=gr.Textbox(lines=2, placeholder="Ej: La lluvia que susurra secretos", label="Tema del microcuento"),
|
| 45 |
-
outputs=[
|
| 46 |
-
gr.Image(label="Imagen generada"),
|
| 47 |
-
gr.Textbox(label="Microcuento")
|
| 48 |
-
],
|
| 49 |
-
title="✨ Microcuentos Ilustrados por IA",
|
| 50 |
-
description="Ingresa un tema y esta IA creará una imagen artística y un microcuento narrativo inspirado en él.",
|
| 51 |
-
).launch()
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from diffusers import DiffusionPipeline
|
| 4 |
+
import torch
|
| 5 |
+
import time
|
| 6 |
+
from tqdm import trange
|
| 7 |
+
|
| 8 |
+
# Detectar hardware
|
| 9 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
+
torch_dtype = torch.float16 if device == "cuda" else torch.float32
|
| 11 |
+
|
| 12 |
+
# Modelo de imagen (SSD-1B)
|
| 13 |
+
image_pipe = DiffusionPipeline.from_pretrained(
|
| 14 |
+
"segmind/SSD-1B",
|
| 15 |
+
torch_dtype=torch_dtype
|
| 16 |
+
).to(device)
|
| 17 |
+
|
| 18 |
+
# Modelo de texto más liviano y en español
|
| 19 |
+
text_pipe = pipeline(
|
| 20 |
+
"text-generation",
|
| 21 |
+
model="PlanTL-GOB-ES/gpt2-base-bne",
|
| 22 |
+
device=0 if device == "cuda" else -1
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
# Función principal con progreso progresivo
|
| 26 |
+
def crear_microcuento(tema, progress=gr.Progress(track_tqdm=True)):
|
| 27 |
+
prompt_img = f"Ilustración digital detallada sobre: {tema}, arte suave, poético, luces tenues"
|
| 28 |
+
|
| 29 |
+
for _ in trange(50, desc="Generando imagen..."):
|
| 30 |
+
time.sleep(0.05)
|
| 31 |
+
imagen = image_pipe(prompt_img).images[0]
|
| 32 |
+
|
| 33 |
+
for _ in trange(50, desc="Generando microcuento..."):
|
| 34 |
+
time.sleep(0.03)
|
| 35 |
+
|
| 36 |
+
prompt_txt = f"Escribe un microcuento narrativo, creativo y poético en español sobre: '{tema}'. El cuento debe tener inicio, desarrollo y final en no más de 4 líneas."
|
| 37 |
+
cuento = text_pipe(prompt_txt, max_new_tokens=100, do_sample=True, temperature=0.9)[0]["generated_text"]
|
| 38 |
+
|
| 39 |
+
return imagen, cuento.replace(prompt_txt, "").strip()
|
| 40 |
+
|
| 41 |
+
# Interfaz Gradio
|
| 42 |
+
gr.Interface(
|
| 43 |
+
fn=crear_microcuento,
|
| 44 |
+
inputs=gr.Textbox(lines=2, placeholder="Ej: La lluvia que susurra secretos", label="Tema del microcuento"),
|
| 45 |
+
outputs=[
|
| 46 |
+
gr.Image(label="Imagen generada"),
|
| 47 |
+
gr.Textbox(label="Microcuento")
|
| 48 |
+
],
|
| 49 |
+
title="✨ Microcuentos Ilustrados por IA",
|
| 50 |
+
description="Ingresa un tema y esta IA creará una imagen artística y un microcuento narrativo inspirado en él.",
|
| 51 |
+
).launch()
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
|