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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -24
app.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  import os
2
  import gradio as gr
3
  import requests
@@ -129,29 +131,31 @@ Mantén respuestas concisas y enfocadas.
129
 
130
  def chat_voyeur(message, chat_history, api_key):
131
  if not api_key:
132
- # CORRECCIÓN: Retornar el historial actualizado correctamente
133
- chat_history.append((message, "❌ Ingresa tu API Key"))
134
- return "", chat_history
135
 
136
  client = SambaNovaClient(api_key)
137
 
138
- # CORRECCIÓN: Construir mensajes correctamente
139
  messages = [{"role": "system", "content": create_voyeur_specialist_system_prompt()}]
140
 
141
- # CORRECCIÓN: Procesar el historial en el formato correcto
142
- for user_msg, assistant_msg in chat_history:
143
- messages.append({"role": "user", "content": user_msg})
144
- if assistant_msg: # Solo agregar si no está vacío
145
- messages.append({"role": "assistant", "content": assistant_msg})
146
 
147
  # Agregar el mensaje actual
148
  messages.append({"role": "user", "content": message})
149
 
 
150
  response = client.chat_completion(messages)
151
 
152
- # CORRECCIÓN: Agregar al historial en el formato correcto (tupla)
153
- chat_history.append((message, response))
154
- return "", chat_history
 
 
 
 
155
 
156
  # ==========================
157
  # PRUEBA DE API
@@ -193,10 +197,11 @@ def create_interface():
193
  for expertise in VOYEUR_SPECIALIST_CONFIG["technical_expertise"]:
194
  gr.Markdown(f"• {expertise}")
195
  with gr.Column(scale=2):
196
- # CORRECCIÓN: Usar el formato correcto para el chatbot
197
  chatbot = gr.Chatbot(
198
  label="💬 Chat con el Especialista",
199
- height=500
 
200
  )
201
  msg = gr.Textbox(
202
  label="Escribe tu mensaje",
@@ -226,13 +231,9 @@ def create_interface():
226
 
227
  # CORRECCIÓN: Función simplificada para manejar mensajes de usuario
228
  def user_message(user_msg, chat_history):
229
- # Retornar mensaje vacío y el historial actualizado
230
- return "", chat_history + [[user_msg, None]]
231
-
232
- # CORRECCIÓN: Configurar los eventos correctamente
233
- def respond(message, chat_history, api_key):
234
- # Función wrapper para mantener compatibilidad
235
- return chat_voyeur(message, chat_history, api_key)
236
 
237
  # Configurar el envío con Enter
238
  msg.submit(
@@ -240,7 +241,7 @@ def create_interface():
240
  inputs=[msg, chatbot],
241
  outputs=[msg, chatbot],
242
  ).then(
243
- respond,
244
  inputs=[msg, chatbot, api_key_input],
245
  outputs=[msg, chatbot],
246
  )
@@ -251,12 +252,12 @@ def create_interface():
251
  inputs=[msg, chatbot],
252
  outputs=[msg, chatbot],
253
  ).then(
254
- respond,
255
  inputs=[msg, chatbot, api_key_input],
256
  outputs=[msg, chatbot],
257
  )
258
 
259
- # Configurar el botón Limpiar
260
  clear_btn.click(lambda: [], None, chatbot, queue=False)
261
 
262
  return demo
 
1
+ # app.py
2
+
3
  import os
4
  import gradio as gr
5
  import requests
 
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})
148
 
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
  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",
 
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
239
  msg.submit(
 
241
  inputs=[msg, chatbot],
242
  outputs=[msg, chatbot],
243
  ).then(
244
+ chat_voyeur,
245
  inputs=[msg, chatbot, api_key_input],
246
  outputs=[msg, chatbot],
247
  )
 
252
  inputs=[msg, chatbot],
253
  outputs=[msg, chatbot],
254
  ).then(
255
+ chat_voyeur,
256
  inputs=[msg, chatbot, api_key_input],
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