Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -8,58 +8,50 @@ print(device)
|
|
8 |
# Asegúrate de que tu token de Hugging Face está cargado como una variable de entorno
|
9 |
hf_token = os.environ.get("token")
|
10 |
|
11 |
-
|
12 |
-
from huggingface_hub import HfFolder
|
13 |
-
HfFolder.save_token(hf_token)
|
14 |
-
else:
|
15 |
-
print("No se encontró el token de Hugging Face. Asegúrate de que la variable de entorno HF_TOKEN esté configurada.")
|
16 |
-
|
17 |
-
|
18 |
-
# Cargar el tokenizador
|
19 |
tokenizer = AutoTokenizer.from_pretrained("Juliofc/chaterapia_model")
|
20 |
-
|
21 |
-
# Añadir el token especial [PAD]
|
22 |
-
#tokenizer.add_special_tokens({'pad_token': '[PAD]'})
|
23 |
-
|
24 |
-
# Cargar el modelo base y ajustar el tamaño de los embeddings de tokens
|
25 |
-
model_base = AutoModelForCausalLM.from_pretrained("google/gemma-2b-it")
|
26 |
model_base.resize_token_embeddings(len(tokenizer))
|
27 |
-
|
28 |
-
# Cargar el modelo con el adaptador
|
29 |
model_with_adapter = PeftModel.from_pretrained(model_base, "Juliofc/chaterapia_model").to(device)
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
-
input_tokens = tokenizer
|
42 |
-
output_tokens = model_with_adapter.generate(input_tokens, max_new_tokens=50, pad_token_id=tokenizer.pad_token_id)
|
43 |
-
generated_text = tokenizer.decode(output_tokens[0], skip_special_tokens=True)
|
44 |
|
45 |
-
|
|
|
|
|
46 |
|
47 |
-
|
|
|
|
|
48 |
|
49 |
-
#
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
submit_button = gr.Button("Enviar")
|
54 |
-
output_text = gr.Textbox(label="Historial de la conversación", lines=10, interactive=False)
|
55 |
-
# Inicializa el estado conversation_history con una cadena vacía
|
56 |
-
conversation_history = gr.State(value="")
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
)
|
63 |
|
64 |
-
|
65 |
-
demo.launch()
|
|
|
8 |
# Asegúrate de que tu token de Hugging Face está cargado como una variable de entorno
|
9 |
hf_token = os.environ.get("token")
|
10 |
|
11 |
+
# Configuración inicial
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
tokenizer = AutoTokenizer.from_pretrained("Juliofc/chaterapia_model")
|
13 |
+
model_base = AutoModelForCausalLM.from_pretrained("google/gemma-2b-it").to(device)
|
|
|
|
|
|
|
|
|
|
|
14 |
model_base.resize_token_embeddings(len(tokenizer))
|
|
|
|
|
15 |
model_with_adapter = PeftModel.from_pretrained(model_base, "Juliofc/chaterapia_model").to(device)
|
16 |
|
17 |
+
CHAT_TEMPLATE= """{% for message in messages %}
|
18 |
+
{% if message['role'] == 'user' %}
|
19 |
+
{{'<user> ' + message['content'].strip() + ' </user>' }}
|
20 |
+
{% elif message['role'] == 'system' %}
|
21 |
+
{{'<system>\\n' + message['content'].strip() + '\\n</system>\\n\\n' }}
|
22 |
+
{% elif message['role'] == 'assistant' %}
|
23 |
+
{{ message['content'].strip() + ' </assistant>' + eos_token }}
|
24 |
+
{% elif message['role'] == 'input' %}
|
25 |
+
{{'<input> ' + message['content'] + ' </input>' }}
|
26 |
+
{% endif %}
|
27 |
+
{% endfor %}""" # Asegúrate de usar tu CHAT_TEMPLATE aquí
|
28 |
+
tokenizer.chat_template = CHAT_TEMPLATE
|
29 |
+
|
30 |
+
# Función para generar respuestas del modelo
|
31 |
+
def generate_response(user_input, chat_history):
|
32 |
+
# Preparar el input agregando el historial de chat
|
33 |
+
mensajes = [{"content": user_input, "role": "user"}]
|
34 |
+
user_input = tokenizer.apply_chat_template(mensajes, tokenize=False)
|
35 |
+
input_with_history = chat_history + user_input
|
36 |
|
37 |
+
input_tokens = tokenizer(input_with_history, return_tensors='pt', padding=True, truncation=True, max_length=1024).to(device)
|
|
|
|
|
38 |
|
39 |
+
# Generar la respuesta
|
40 |
+
output_tokens = model_with_adapter.generate(**input_tokens, max_length=1024, pad_token_id=tokenizer.eos_token_id, top_k=50, top_p=0.95, temperature=0.7)
|
41 |
+
generated_text = tokenizer.decode(output_tokens[0], skip_special_tokens=True)
|
42 |
|
43 |
+
# Actualizar el historial del chat
|
44 |
+
new_chat_history = chat_history + "\n" + generated_text
|
45 |
+
return generated_text, new_chat_history
|
46 |
|
47 |
+
# Gradio interface
|
48 |
+
def chat_interface(user_input, chat_history=""):
|
49 |
+
response, new_chat_history = generate_response(user_input, chat_history)
|
50 |
+
return response, new_chat_history
|
|
|
|
|
|
|
|
|
51 |
|
52 |
+
iface = gr.Interface(fn=chat_interface,
|
53 |
+
inputs=[gr.inputs.Textbox(lines=2, label="Tu Mensaje"), gr.inputs.Textbox(lines=10, label="Historial de Chat", default="")],
|
54 |
+
outputs=[gr.outputs.Textbox(label="Respuesta del Modelo"), gr.outputs.Textbox(label="Nuevo Historial de Chat")],
|
55 |
+
description="Chat con tu Modelo")
|
|
|
56 |
|
57 |
+
iface.launch()
|
|