Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,52 +3,41 @@ import torch
|
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM, TextGenerationPipeline
|
| 4 |
from diffusers import StableDiffusionPipeline
|
| 5 |
from PIL import Image
|
|
|
|
| 6 |
|
| 7 |
-
# Modelo de texto
|
| 8 |
-
|
| 9 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
)
|
| 15 |
-
text_pipe = TextGenerationPipeline(
|
| 16 |
-
model=text_model,
|
| 17 |
-
tokenizer=tokenizer,
|
| 18 |
-
max_new_tokens=200,
|
| 19 |
-
do_sample=True,
|
| 20 |
-
temperature=0.8,
|
| 21 |
-
top_p=0.95
|
| 22 |
-
)
|
| 23 |
-
|
| 24 |
-
# Modelo de imagen
|
| 25 |
image_pipe = StableDiffusionPipeline.from_pretrained(
|
| 26 |
-
"runwayml/stable-diffusion-v1-5",
|
| 27 |
-
torch_dtype=torch.
|
| 28 |
-
).to("
|
| 29 |
-
|
| 30 |
-
# L贸gica para
|
| 31 |
-
def
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
# Funci贸n del bot
|
| 36 |
-
def bot_response(message):
|
| 37 |
-
if is_image_prompt(message):
|
| 38 |
-
image = image_pipe(message).images[0]
|
| 39 |
-
return "", image
|
| 40 |
else:
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
reply = result.split("Asistente:")[-1].strip()
|
| 44 |
-
return reply, None
|
| 45 |
|
| 46 |
-
# Interfaz
|
| 47 |
with gr.Blocks() as demo:
|
| 48 |
-
gr.Markdown("##
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
demo.launch()
|
|
|
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM, TextGenerationPipeline
|
| 4 |
from diffusers import StableDiffusionPipeline
|
| 5 |
from PIL import Image
|
| 6 |
+
import io
|
| 7 |
|
| 8 |
+
# Modelo de texto en CPU
|
| 9 |
+
text_model = "tiiuae/falcon-rw-1b"
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(text_model)
|
| 11 |
+
model = AutoModelForCausalLM.from_pretrained(text_model)
|
| 12 |
+
text_pipeline = TextGenerationPipeline(model=model, tokenizer=tokenizer, device=-1)
|
| 13 |
+
|
| 14 |
+
# Modelo de imagen en CPU
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
image_pipe = StableDiffusionPipeline.from_pretrained(
|
| 16 |
+
"runwayml/stable-diffusion-v1-5",
|
| 17 |
+
torch_dtype=torch.float32
|
| 18 |
+
).to("cpu")
|
| 19 |
+
|
| 20 |
+
# L贸gica para detectar si el prompt es de texto o imagen
|
| 21 |
+
def chatbot(input_text):
|
| 22 |
+
if any(word in input_text.lower() for word in ["imagen", "dibuja", "pinta", "foto", "muestra"]):
|
| 23 |
+
image = image_pipe(input_text).images[0]
|
| 24 |
+
return None, image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
else:
|
| 26 |
+
response = text_pipeline(input_text, max_new_tokens=150, do_sample=True)[0]['generated_text']
|
| 27 |
+
return response, None
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# Interfaz Gradio
|
| 30 |
with gr.Blocks() as demo:
|
| 31 |
+
gr.Markdown("## Bot Generador de Texto e Im谩genes (CPU)")
|
| 32 |
+
|
| 33 |
+
with gr.Row():
|
| 34 |
+
textbox = gr.Textbox(placeholder="Escribe algo... (ej: Dibuja una chica en la playa)")
|
| 35 |
+
send = gr.Button("Enviar")
|
| 36 |
+
|
| 37 |
+
with gr.Row():
|
| 38 |
+
text_output = gr.Textbox(label="Respuesta de texto")
|
| 39 |
+
image_output = gr.Image(label="Imagen generada")
|
| 40 |
+
|
| 41 |
+
send.click(fn=chatbot, inputs=textbox, outputs=[text_output, image_output])
|
| 42 |
|
| 43 |
demo.launch()
|