Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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:
|
| 133 |
-
chat_history
|
| 134 |
-
return "", chat_history
|
| 135 |
|
| 136 |
client = SambaNovaClient(api_key)
|
| 137 |
|
| 138 |
-
#
|
| 139 |
messages = [{"role": "system", "content": create_voyeur_specialist_system_prompt()}]
|
| 140 |
|
| 141 |
-
#
|
| 142 |
-
for
|
| 143 |
-
messages.append({"role": "
|
| 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:
|
| 153 |
-
chat_history
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 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 |
-
#
|
| 230 |
-
|
| 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 |
-
|
| 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 |
-
|
| 255 |
inputs=[msg, chatbot, api_key_input],
|
| 256 |
outputs=[msg, chatbot],
|
| 257 |
)
|
| 258 |
|
| 259 |
-
#
|
| 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
|