Teddy-Project commited on
Commit
0287d57
verified
1 Parent(s): 88f72c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -42
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
- text_model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
9
- tokenizer = AutoTokenizer.from_pretrained(text_model_name)
10
- text_model = AutoModelForCausalLM.from_pretrained(
11
- text_model_name,
12
- torch_dtype=torch.float16,
13
- device_map="auto"
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.float16
28
- ).to("cuda")
29
-
30
- # L贸gica para decidir si es imagen o texto
31
- def is_image_prompt(prompt):
32
- keywords = ["dibuja", "genera una imagen", "imagen de", "p铆ntame", "crea una ilustraci贸n"]
33
- return any(kw in prompt.lower() for kw in keywords)
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
- prompt = "Eres una asistente coqueta, creativa y dulce.\nUsuario: " + message + "\nAsistente:"
42
- result = text_pipe(prompt)[0]['generated_text']
43
- reply = result.split("Asistente:")[-1].strip()
44
- return reply, None
45
 
46
- # Interfaz
47
  with gr.Blocks() as demo:
48
- gr.Markdown("## Asistente inteligente de texto e im谩genes")
49
- input_box = gr.Textbox(label="Tu mensaje", placeholder="Escribe lo que quieras...")
50
- text_output = gr.Textbox(label="Respuesta de texto")
51
- image_output = gr.Image(label="Imagen generada")
52
- input_box.submit(fn=bot_response, inputs=input_box, outputs=[text_output, image_output])
 
 
 
 
 
 
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()