Upload 3 files
Browse files- ProyectoGradio.py +56 -0
- requirements.txt +7 -7
ProyectoGradio.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
| 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 |
+
# Mejor modelo de texto (Falcon 7B Instruct)
|
| 19 |
+
model_id = "tiiuae/falcon-7b-instruct"
|
| 20 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 21 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch_dtype).to(device)
|
| 22 |
+
|
| 23 |
+
text_pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, device=0 if device == "cuda" else -1)
|
| 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 |
+
|
requirements.txt
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
-
transformers>=4.37.2
|
| 2 |
-
diffusers>=0.24.0
|
| 3 |
-
torch>=2.1.0
|
| 4 |
-
gradio>=4.16.0
|
| 5 |
-
accelerate>=0.25.0
|
| 6 |
-
safetensors>=0.4.0
|
| 7 |
-
|
|
|
|
| 1 |
+
transformers>=4.37.2
|
| 2 |
+
diffusers>=0.24.0
|
| 3 |
+
torch>=2.1.0
|
| 4 |
+
gradio>=4.16.0
|
| 5 |
+
accelerate>=0.25.0
|
| 6 |
+
safetensors>=0.4.0
|
| 7 |
+
|