Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,47 +1,54 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
3 |
|
4 |
# Cargar el modelo y el tokenizador
|
5 |
-
model_name = "
|
6 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
7 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
8 |
|
9 |
def chatbot(input, history):
|
10 |
-
# Aseg煤rate de que history sea una lista de listas
|
11 |
history = history or []
|
|
|
12 |
|
13 |
-
|
14 |
-
history
|
|
|
|
|
|
|
|
|
15 |
|
16 |
-
|
17 |
-
chat_history = []
|
18 |
-
for human, ai in history:
|
19 |
-
chat_history.append(human)
|
20 |
-
if ai:
|
21 |
-
chat_history.append(ai)
|
22 |
|
23 |
-
|
24 |
-
|
25 |
|
26 |
-
|
27 |
-
attention_mask = input_ids.new_ones(input_ids.shape)
|
28 |
-
output = model.generate(input_ids, attention_mask=attention_mask, max_length=1000, pad_token_id=tokenizer.eos_token_id)
|
29 |
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
-
|
34 |
-
history[-1][1] = response
|
35 |
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
-
# Crear la interfaz de Gradio
|
39 |
iface = gr.Interface(
|
40 |
fn=chatbot,
|
41 |
inputs=["text", "state"],
|
42 |
outputs=["chatbot", "state"],
|
43 |
-
title="Tu Compa帽ero AI",
|
44 |
-
description="Un chatbot de IA dise帽ado para simular conversaciones personales.",
|
45 |
)
|
46 |
|
47 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
import torch
|
4 |
|
5 |
# Cargar el modelo y el tokenizador
|
6 |
+
model_name = "bigscience/bloom-560m"
|
7 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
9 |
|
10 |
def chatbot(input, history):
|
|
|
11 |
history = history or []
|
12 |
+
history.append({"role": "user", "content": input})
|
13 |
|
14 |
+
chat_history = ""
|
15 |
+
for message in history:
|
16 |
+
if message["role"] == "user":
|
17 |
+
chat_history += f"Human: {message['content']}\n"
|
18 |
+
else:
|
19 |
+
chat_history += f"AI: {message['content']}\n"
|
20 |
|
21 |
+
chat_history += "AI:"
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
+
input_ids = tokenizer.encode(chat_history, return_tensors="pt")
|
24 |
+
attention_mask = torch.ones(input_ids.shape, dtype=torch.long, device=input_ids.device)
|
25 |
|
26 |
+
max_length = input_ids.shape[1] + 50
|
|
|
|
|
27 |
|
28 |
+
output = model.generate(
|
29 |
+
input_ids,
|
30 |
+
attention_mask=attention_mask,
|
31 |
+
max_length=max_length,
|
32 |
+
num_return_sequences=1,
|
33 |
+
no_repeat_ngram_size=2,
|
34 |
+
temperature=0.7
|
35 |
+
)
|
36 |
|
37 |
+
response = tokenizer.decode(output[0][input_ids.shape[1]:], skip_special_tokens=True)
|
|
|
38 |
|
39 |
+
history.append({"role": "assistant", "content": response.strip()})
|
40 |
+
|
41 |
+
# Convertir el historial al formato que Gradio espera
|
42 |
+
gradio_history = [[m["content"], h["content"]] for m, h in zip(history[::2], history[1::2])]
|
43 |
+
|
44 |
+
return gradio_history, history
|
45 |
|
|
|
46 |
iface = gr.Interface(
|
47 |
fn=chatbot,
|
48 |
inputs=["text", "state"],
|
49 |
outputs=["chatbot", "state"],
|
50 |
+
title="Tu Compa帽ero AI con BLOOM",
|
51 |
+
description="Un chatbot de IA dise帽ado para simular conversaciones personales, utilizando el modelo BLOOM.",
|
52 |
)
|
53 |
|
54 |
iface.launch()
|