Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,36 +1,54 @@
|
|
1 |
-
import gradio as gr
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
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()
|