BATUTO-ART commited on
Commit
42a1e8b
·
verified ·
1 Parent(s): 7ebd92a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -22
app.py CHANGED
@@ -1,5 +1,3 @@
1
- # app.py
2
-
3
  import os
4
  import gradio as gr
5
  import requests
@@ -126,22 +124,25 @@ Mantén respuestas concisas y enfocadas.
126
  """
127
 
128
  # ==========================
129
- # FUNCIÓN DE CHAT CORREGIDA
130
  # ==========================
131
 
132
  def chat_voyeur(message, chat_history, api_key):
133
  if not api_key:
134
- # CORRECCIÓN: Usar el formato correcto para Gradio
135
- return "", chat_history + [{"role": "user", "content": message}, {"role": "assistant", "content": "❌ Ingresa tu API Key"}]
 
136
 
137
  client = SambaNovaClient(api_key)
138
 
139
  # Construir mensajes para la API
140
  messages = [{"role": "system", "content": create_voyeur_specialist_system_prompt()}]
141
 
142
- # Agregar historial de conversación
143
- for msg in chat_history:
144
- messages.append({"role": msg["role"], "content": msg["content"]})
 
 
145
 
146
  # Agregar el mensaje actual
147
  messages.append({"role": "user", "content": message})
@@ -149,13 +150,9 @@ def chat_voyeur(message, chat_history, api_key):
149
  # Obtener respuesta
150
  response = client.chat_completion(messages)
151
 
152
- # CORRECCIÓN: Retornar el historial actualizado en formato correcto
153
- updated_history = chat_history + [
154
- {"role": "user", "content": message},
155
- {"role": "assistant", "content": response}
156
- ]
157
-
158
- return "", updated_history
159
 
160
  # ==========================
161
  # PRUEBA DE API
@@ -197,11 +194,10 @@ def create_interface():
197
  for expertise in VOYEUR_SPECIALIST_CONFIG["technical_expertise"]:
198
  gr.Markdown(f"• {expertise}")
199
  with gr.Column(scale=2):
200
- # CORRECCIÓN: Usar el formato de diccionarios para el Chatbot
201
  chatbot = gr.Chatbot(
202
  label="💬 Chat con el Especialista",
203
- height=500,
204
- type="messages" # Especificar que usamos formato de mensajes
205
  )
206
  msg = gr.Textbox(
207
  label="Escribe tu mensaje",
@@ -229,10 +225,10 @@ def create_interface():
229
  outputs=[test_output],
230
  )
231
 
232
- # CORRECCIÓN: Función simplificada para manejar mensajes de usuario
233
  def user_message(user_msg, chat_history):
234
- # Agregar mensaje de usuario al historial
235
- new_history = chat_history + [{"role": "user", "content": user_msg}]
236
  return "", new_history
237
 
238
  # Configurar el envío con Enter
@@ -257,7 +253,7 @@ def create_interface():
257
  outputs=[msg, chatbot],
258
  )
259
 
260
- # CORRECCIÓN: Botón limpiar con formato correcto
261
  clear_btn.click(lambda: [], None, chatbot, queue=False)
262
 
263
  return demo
 
 
 
1
  import os
2
  import gradio as gr
3
  import requests
 
124
  """
125
 
126
  # ==========================
127
+ # FUNCIÓN DE CHAT CORREGIDA (formato tuplas)
128
  # ==========================
129
 
130
  def chat_voyeur(message, chat_history, api_key):
131
  if not api_key:
132
+ # Formato tuplas para versiones antiguas de Gradio
133
+ chat_history.append((message, "❌ Ingresa tu API Key"))
134
+ return "", chat_history
135
 
136
  client = SambaNovaClient(api_key)
137
 
138
  # Construir mensajes para la API
139
  messages = [{"role": "system", "content": create_voyeur_specialist_system_prompt()}]
140
 
141
+ # Procesar historial en formato tuplas
142
+ for user_msg, assistant_msg in chat_history:
143
+ messages.append({"role": "user", "content": user_msg})
144
+ if assistant_msg and assistant_msg != "❌ Ingresa tu API Key": # Evitar incluir mensajes de error
145
+ messages.append({"role": "assistant", "content": assistant_msg})
146
 
147
  # Agregar el mensaje actual
148
  messages.append({"role": "user", "content": message})
 
150
  # Obtener respuesta
151
  response = client.chat_completion(messages)
152
 
153
+ # Agregar al historial en formato tuplas
154
+ chat_history.append((message, response))
155
+ return "", chat_history
 
 
 
 
156
 
157
  # ==========================
158
  # PRUEBA DE API
 
194
  for expertise in VOYEUR_SPECIALIST_CONFIG["technical_expertise"]:
195
  gr.Markdown(f"• {expertise}")
196
  with gr.Column(scale=2):
197
+ # CORRECCIÓN: Sin parámetro 'type' para compatibilidad
198
  chatbot = gr.Chatbot(
199
  label="💬 Chat con el Especialista",
200
+ height=500
 
201
  )
202
  msg = gr.Textbox(
203
  label="Escribe tu mensaje",
 
225
  outputs=[test_output],
226
  )
227
 
228
+ # CORRECCIÓN: Función para manejar mensajes en formato tuplas
229
  def user_message(user_msg, chat_history):
230
+ # Agregar mensaje de usuario con None como respuesta temporal
231
+ new_history = chat_history + [[user_msg, None]]
232
  return "", new_history
233
 
234
  # Configurar el envío con Enter
 
253
  outputs=[msg, chatbot],
254
  )
255
 
256
+ # Configurar el botón Limpiar
257
  clear_btn.click(lambda: [], None, chatbot, queue=False)
258
 
259
  return demo