File size: 1,508 Bytes
88f72c8
 
 
 
 
0287d57
88f72c8
0287d57
 
 
 
 
 
 
88f72c8
0287d57
 
 
 
 
 
 
 
 
88f72c8
0287d57
 
88f72c8
0287d57
88f72c8
0287d57
 
 
 
 
 
 
 
 
 
 
88f72c8
 
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
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, TextGenerationPipeline
from diffusers import StableDiffusionPipeline
from PIL import Image
import io

# Modelo de texto en CPU
text_model = "tiiuae/falcon-rw-1b"
tokenizer = AutoTokenizer.from_pretrained(text_model)
model = AutoModelForCausalLM.from_pretrained(text_model)
text_pipeline = TextGenerationPipeline(model=model, tokenizer=tokenizer, device=-1)

# Modelo de imagen en CPU
image_pipe = StableDiffusionPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5", 
    torch_dtype=torch.float32
).to("cpu")

# Lógica para detectar si el prompt es de texto o imagen
def chatbot(input_text):
    if any(word in input_text.lower() for word in ["imagen", "dibuja", "pinta", "foto", "muestra"]):
        image = image_pipe(input_text).images[0]
        return None, image
    else:
        response = text_pipeline(input_text, max_new_tokens=150, do_sample=True)[0]['generated_text']
        return response, None

# Interfaz Gradio
with gr.Blocks() as demo:
    gr.Markdown("## Bot Generador de Texto e Imágenes (CPU)")

    with gr.Row():
        textbox = gr.Textbox(placeholder="Escribe algo... (ej: Dibuja una chica en la playa)")
        send = gr.Button("Enviar")

    with gr.Row():
        text_output = gr.Textbox(label="Respuesta de texto")
        image_output = gr.Image(label="Imagen generada")

    send.click(fn=chatbot, inputs=textbox, outputs=[text_output, image_output])

demo.launch()