Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
|
4 |
|
5 |
titulo = """# 🤖 Bienvenido al Chatbot con Yi-9B"""
|
6 |
|
@@ -16,51 +17,48 @@ tokenizador = AutoTokenizer.from_pretrained(ruta_modelo)
|
|
16 |
modelo = AutoModelForCausalLM.from_pretrained(ruta_modelo, device_map="auto").eval()
|
17 |
|
18 |
@spaces.GPU(duration=130)
|
19 |
-
def generar_respuesta(
|
20 |
mensajes = [
|
21 |
-
{"role": "system", "content":
|
|
|
22 |
]
|
23 |
-
|
24 |
-
for entrada in historial:
|
25 |
-
mensajes.append({"role": "user", "content": entrada[0]})
|
26 |
-
mensajes.append({"role": "assistant", "content": entrada[1]})
|
27 |
-
|
28 |
-
mensajes.append({"role": "user", "content": usuario_input})
|
29 |
-
|
30 |
texto = tokenizador.apply_chat_template(
|
31 |
mensajes,
|
32 |
tokenize=False,
|
33 |
add_generation_prompt=True
|
34 |
)
|
35 |
-
|
36 |
entradas_modelo = tokenizador([texto], return_tensors="pt").to(dispositivo)
|
37 |
ids_generados = modelo.generate(
|
38 |
entradas_modelo.input_ids,
|
39 |
max_new_tokens=max_longitud,
|
40 |
eos_token_id=tokenizador.eos_token_id
|
41 |
)
|
42 |
-
|
43 |
ids_generados = [
|
44 |
output_ids[len(input_ids):] for input_ids, output_ids in zip(entradas_modelo.input_ids, ids_generados)
|
45 |
]
|
46 |
-
|
47 |
respuesta = tokenizador.batch_decode(ids_generados, skip_special_tokens=True)[0]
|
48 |
-
|
49 |
-
return historial, ""
|
50 |
|
51 |
def interfaz_gradio():
|
52 |
with gr.Blocks() as interfaz:
|
53 |
gr.Markdown(titulo)
|
54 |
gr.Markdown(descripcion)
|
55 |
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
60 |
max_longitud_slider = gr.Slider(minimum=1, maximum=1000, value=500, label="Longitud máxima de la respuesta")
|
61 |
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
|
|
64 |
|
65 |
return interfaz
|
66 |
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
4 |
+
import spaces
|
5 |
|
6 |
titulo = """# 🤖 Bienvenido al Chatbot con Yi-9B"""
|
7 |
|
|
|
17 |
modelo = AutoModelForCausalLM.from_pretrained(ruta_modelo, device_map="auto").eval()
|
18 |
|
19 |
@spaces.GPU(duration=130)
|
20 |
+
def generar_respuesta(prompt_sistema, prompt_usuario, max_longitud):
|
21 |
mensajes = [
|
22 |
+
{"role": "system", "content": prompt_sistema},
|
23 |
+
{"role": "user", "content": prompt_usuario}
|
24 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
texto = tokenizador.apply_chat_template(
|
26 |
mensajes,
|
27 |
tokenize=False,
|
28 |
add_generation_prompt=True
|
29 |
)
|
|
|
30 |
entradas_modelo = tokenizador([texto], return_tensors="pt").to(dispositivo)
|
31 |
ids_generados = modelo.generate(
|
32 |
entradas_modelo.input_ids,
|
33 |
max_new_tokens=max_longitud,
|
34 |
eos_token_id=tokenizador.eos_token_id
|
35 |
)
|
|
|
36 |
ids_generados = [
|
37 |
output_ids[len(input_ids):] for input_ids, output_ids in zip(entradas_modelo.input_ids, ids_generados)
|
38 |
]
|
|
|
39 |
respuesta = tokenizador.batch_decode(ids_generados, skip_special_tokens=True)[0]
|
40 |
+
return respuesta
|
|
|
41 |
|
42 |
def interfaz_gradio():
|
43 |
with gr.Blocks() as interfaz:
|
44 |
gr.Markdown(titulo)
|
45 |
gr.Markdown(descripcion)
|
46 |
|
47 |
+
prompt_sistema = gr.Textbox(
|
48 |
+
label="Instrucción del sistema:",
|
49 |
+
value="Eres un asistente útil y amigable. Proporciona respuestas claras y concisas.",
|
50 |
+
lines=2
|
51 |
+
)
|
52 |
+
prompt_usuario = gr.Textbox(label="Tu mensaje", lines=3)
|
53 |
+
respuesta = gr.Textbox(label="Respuesta del asistente", lines=10)
|
54 |
max_longitud_slider = gr.Slider(minimum=1, maximum=1000, value=500, label="Longitud máxima de la respuesta")
|
55 |
|
56 |
+
boton_generar = gr.Button("Generar respuesta")
|
57 |
+
boton_generar.click(
|
58 |
+
generar_respuesta,
|
59 |
+
inputs=[prompt_sistema, prompt_usuario, max_longitud_slider],
|
60 |
+
outputs=respuesta
|
61 |
+
)
|
62 |
|
63 |
return interfaz
|
64 |
|