Juliofc commited on
Commit
0df9de1
verified
1 Parent(s): ebb67d6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -14
app.py CHANGED
@@ -29,23 +29,20 @@ model_with_adapter = PeftModel.from_pretrained(model_base, "Juliofc/chaterapia_m
29
 
30
  # Suponiendo que `tokenizer` y `model_with_adapter` ya est谩n inicializados
31
 
32
- def chat_with_model(user_input, conversation_history):
33
- # Actualiza el historial con la entrada del usuario
 
 
 
34
  conversation_history += f"Usuario: {user_input}\n"
35
 
36
- # Prepara la entrada para el modelo
37
  input_tokens = tokenizer.encode(user_input, return_tensors='pt')
38
-
39
- # Genera la respuesta del modelo
40
  output_tokens = model_with_adapter.generate(input_tokens, max_new_tokens=50, pad_token_id=tokenizer.pad_token_id)
41
-
42
- # Decodifica la respuesta
43
  generated_text = tokenizer.decode(output_tokens[0], skip_special_tokens=True)
44
 
45
- # Actualiza el historial con la respuesta del modelo
46
  conversation_history += f"Modelo: {generated_text}\n"
47
 
48
- return "", conversation_history # Retorna el historial actualizado
49
 
50
  # Define los componentes de la interfaz de Gradio
51
  with gr.Blocks() as demo:
@@ -53,13 +50,14 @@ with gr.Blocks() as demo:
53
  input_text = gr.Textbox(label="Tu mensaje")
54
  submit_button = gr.Button("Enviar")
55
  output_text = gr.Textbox(label="Historial de la conversaci贸n", lines=10, interactive=False)
56
- conversation_history = gr.State() # Inicializa el estado para mantener el historial
 
57
 
58
- # Configura la acci贸n al presionar el bot贸n de env铆o
59
  submit_button.click(
60
- fn=chat_with_model,
61
- inputs=[input_text, conversation_history],
62
  outputs=[input_text, conversation_history]
63
  )
64
 
65
- demo.launch()
 
 
29
 
30
  # Suponiendo que `tokenizer` y `model_with_adapter` ya est谩n inicializados
31
 
32
+ def chat_with_model(user_input, conversation_history=""):
33
+ # Aseg煤rate de que conversation_history tenga un valor inicial adecuado
34
+ if conversation_history is None:
35
+ conversation_history = ""
36
+
37
  conversation_history += f"Usuario: {user_input}\n"
38
 
 
39
  input_tokens = tokenizer.encode(user_input, return_tensors='pt')
 
 
40
  output_tokens = model_with_adapter.generate(input_tokens, max_new_tokens=50, pad_token_id=tokenizer.pad_token_id)
 
 
41
  generated_text = tokenizer.decode(output_tokens[0], skip_special_tokens=True)
42
 
 
43
  conversation_history += f"Modelo: {generated_text}\n"
44
 
45
+ return "", conversation_history
46
 
47
  # Define los componentes de la interfaz de Gradio
48
  with gr.Blocks() as demo:
 
50
  input_text = gr.Textbox(label="Tu mensaje")
51
  submit_button = gr.Button("Enviar")
52
  output_text = gr.Textbox(label="Historial de la conversaci贸n", lines=10, interactive=False)
53
+ # Inicializa el estado conversation_history con una cadena vac铆a
54
+ conversation_history = gr.State(value="")
55
 
 
56
  submit_button.click(
57
+ fn=chat_with_model,
58
+ inputs=[input_text, conversation_history],
59
  outputs=[input_text, conversation_history]
60
  )
61
 
62
+ # Aseg煤rate de lanzar usando un puerto disponible o soluciona el problema del puerto ocupado como se discuti贸 anteriormente.
63
+ demo.launch()