File size: 1,803 Bytes
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
44
45
46
47
48
49
50
51
52
53
54
import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, TextGenerationPipeline
from diffusers import StableDiffusionPipeline
from PIL import Image

# Modelo de texto
text_model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
tokenizer = AutoTokenizer.from_pretrained(text_model_name)
text_model = AutoModelForCausalLM.from_pretrained(
    text_model_name,
    torch_dtype=torch.float16,
    device_map="auto"
)
text_pipe = TextGenerationPipeline(
    model=text_model,
    tokenizer=tokenizer,
    max_new_tokens=200,
    do_sample=True,
    temperature=0.8,
    top_p=0.95
)

# Modelo de imagen
image_pipe = StableDiffusionPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5",
    torch_dtype=torch.float16
).to("cuda")

# L贸gica para decidir si es imagen o texto
def is_image_prompt(prompt):
    keywords = ["dibuja", "genera una imagen", "imagen de", "p铆ntame", "crea una ilustraci贸n"]
    return any(kw in prompt.lower() for kw in keywords)

# Funci贸n del bot
def bot_response(message):
    if is_image_prompt(message):
        image = image_pipe(message).images[0]
        return "", image
    else:
        prompt = "Eres una asistente coqueta, creativa y dulce.\nUsuario: " + message + "\nAsistente:"
        result = text_pipe(prompt)[0]['generated_text']
        reply = result.split("Asistente:")[-1].strip()
        return reply, None

# Interfaz
with gr.Blocks() as demo:
    gr.Markdown("## Asistente inteligente de texto e im谩genes")
    input_box = gr.Textbox(label="Tu mensaje", placeholder="Escribe lo que quieras...")
    text_output = gr.Textbox(label="Respuesta de texto")
    image_output = gr.Image(label="Imagen generada")
    input_box.submit(fn=bot_response, inputs=input_box, outputs=[text_output, image_output])

demo.launch()