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
|
|
@@ -123,15 +125,36 @@ Ejemplo de prompt (usa como guía para estructura):
|
|
| 123 |
Mantén respuestas concisas y enfocadas.
|
| 124 |
"""
|
| 125 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
# ==========================
|
| 127 |
# FUNCIÓN DE CHAT COMPATIBLE CON HF SPACES
|
| 128 |
# ==========================
|
| 129 |
|
| 130 |
-
def chat_voyeur(message, chat_history
|
|
|
|
|
|
|
| 131 |
if not api_key:
|
| 132 |
# Formato de diccionario para HF Spaces
|
| 133 |
chat_history.append({"role": "user", "content": message})
|
| 134 |
-
chat_history.append({"role": "assistant", "content": "❌
|
| 135 |
return "", chat_history
|
| 136 |
|
| 137 |
client = SambaNovaClient(api_key)
|
|
@@ -159,9 +182,11 @@ def chat_voyeur(message, chat_history, api_key):
|
|
| 159 |
# PRUEBA DE API
|
| 160 |
# ==========================
|
| 161 |
|
| 162 |
-
def test_sambanova_api(
|
|
|
|
|
|
|
| 163 |
if not api_key:
|
| 164 |
-
return "❌
|
| 165 |
|
| 166 |
client = SambaNovaClient(api_key)
|
| 167 |
test_messages = [
|
|
@@ -181,12 +206,7 @@ def create_interface():
|
|
| 181 |
|
| 182 |
with gr.Row():
|
| 183 |
with gr.Column(scale=1):
|
| 184 |
-
|
| 185 |
-
label="🔑 API Key de SambaNova",
|
| 186 |
-
placeholder="Ingresa tu API key...",
|
| 187 |
-
type="password",
|
| 188 |
-
)
|
| 189 |
-
test_btn = gr.Button("🧪 Probar API")
|
| 190 |
test_output = gr.Textbox(label="Resultado de prueba", interactive=False)
|
| 191 |
gr.Markdown("### 📋 Especialidades")
|
| 192 |
for specialty in VOYEUR_SPECIALIST_CONFIG["specialties"]:
|
|
@@ -194,6 +214,11 @@ def create_interface():
|
|
| 194 |
gr.Markdown("### 🎯 Experiencia Técnica")
|
| 195 |
for expertise in VOYEUR_SPECIALIST_CONFIG["technical_expertise"]:
|
| 196 |
gr.Markdown(f"• {expertise}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
with gr.Column(scale=2):
|
| 198 |
# Chatbot sin parámetros problemáticos
|
| 199 |
chatbot = gr.Chatbot(
|
|
@@ -222,7 +247,7 @@ def create_interface():
|
|
| 222 |
|
| 223 |
test_btn.click(
|
| 224 |
test_sambanova_api,
|
| 225 |
-
inputs=[
|
| 226 |
outputs=[test_output],
|
| 227 |
)
|
| 228 |
|
|
@@ -239,7 +264,7 @@ def create_interface():
|
|
| 239 |
outputs=[msg, chatbot],
|
| 240 |
).then(
|
| 241 |
chat_voyeur,
|
| 242 |
-
inputs=[msg, chatbot
|
| 243 |
outputs=[msg, chatbot],
|
| 244 |
)
|
| 245 |
|
|
@@ -250,7 +275,7 @@ def create_interface():
|
|
| 250 |
outputs=[msg, chatbot],
|
| 251 |
).then(
|
| 252 |
chat_voyeur,
|
| 253 |
-
inputs=[msg, chatbot
|
| 254 |
outputs=[msg, chatbot],
|
| 255 |
)
|
| 256 |
|
|
@@ -261,5 +286,5 @@ def create_interface():
|
|
| 261 |
|
| 262 |
if __name__ == "__main__":
|
| 263 |
demo = create_interface()
|
| 264 |
-
#
|
| 265 |
demo.launch(debug=True)
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
import os
|
| 4 |
import gradio as gr
|
| 5 |
import requests
|
|
|
|
| 125 |
Mantén respuestas concisas y enfocadas.
|
| 126 |
"""
|
| 127 |
|
| 128 |
+
# ==========================
|
| 129 |
+
# FUNCIÓN PARA OBTENER API KEY
|
| 130 |
+
# ==========================
|
| 131 |
+
|
| 132 |
+
def get_api_key():
|
| 133 |
+
"""Obtiene la API key desde variables de entorno/secretos"""
|
| 134 |
+
# Para Hugging Face Spaces
|
| 135 |
+
api_key = os.environ.get("SAMBANOVA_API_KEY")
|
| 136 |
+
|
| 137 |
+
# Si no está en variables de entorno, intenta desde secrets (para Gradio)
|
| 138 |
+
if not api_key:
|
| 139 |
+
try:
|
| 140 |
+
# Esto funciona en Hugging Face Spaces con secretos
|
| 141 |
+
api_key = os.environ.get("SAMBANOVA_API_KEY")
|
| 142 |
+
except:
|
| 143 |
+
pass
|
| 144 |
+
|
| 145 |
+
return api_key
|
| 146 |
+
|
| 147 |
# ==========================
|
| 148 |
# FUNCIÓN DE CHAT COMPATIBLE CON HF SPACES
|
| 149 |
# ==========================
|
| 150 |
|
| 151 |
+
def chat_voyeur(message, chat_history):
|
| 152 |
+
api_key = get_api_key()
|
| 153 |
+
|
| 154 |
if not api_key:
|
| 155 |
# Formato de diccionario para HF Spaces
|
| 156 |
chat_history.append({"role": "user", "content": message})
|
| 157 |
+
chat_history.append({"role": "assistant", "content": "❌ API Key no configurada. Por favor, configura la variable de entorno SAMBANOVA_API_KEY."})
|
| 158 |
return "", chat_history
|
| 159 |
|
| 160 |
client = SambaNovaClient(api_key)
|
|
|
|
| 182 |
# PRUEBA DE API
|
| 183 |
# ==========================
|
| 184 |
|
| 185 |
+
def test_sambanova_api():
|
| 186 |
+
api_key = get_api_key()
|
| 187 |
+
|
| 188 |
if not api_key:
|
| 189 |
+
return "❌ API Key no configurada. Configura la variable de entorno SAMBANOVA_API_KEY."
|
| 190 |
|
| 191 |
client = SambaNovaClient(api_key)
|
| 192 |
test_messages = [
|
|
|
|
| 206 |
|
| 207 |
with gr.Row():
|
| 208 |
with gr.Column(scale=1):
|
| 209 |
+
test_btn = gr.Button("🧪 Probar Conexión API")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
test_output = gr.Textbox(label="Resultado de prueba", interactive=False)
|
| 211 |
gr.Markdown("### 📋 Especialidades")
|
| 212 |
for specialty in VOYEUR_SPECIALIST_CONFIG["specialties"]:
|
|
|
|
| 214 |
gr.Markdown("### 🎯 Experiencia Técnica")
|
| 215 |
for expertise in VOYEUR_SPECIALIST_CONFIG["technical_expertise"]:
|
| 216 |
gr.Markdown(f"• {expertise}")
|
| 217 |
+
|
| 218 |
+
# Información sobre configuración
|
| 219 |
+
gr.Markdown("### ⚙️ Configuración")
|
| 220 |
+
gr.Markdown("API Key configurada desde variables de entorno")
|
| 221 |
+
|
| 222 |
with gr.Column(scale=2):
|
| 223 |
# Chatbot sin parámetros problemáticos
|
| 224 |
chatbot = gr.Chatbot(
|
|
|
|
| 247 |
|
| 248 |
test_btn.click(
|
| 249 |
test_sambanova_api,
|
| 250 |
+
inputs=[],
|
| 251 |
outputs=[test_output],
|
| 252 |
)
|
| 253 |
|
|
|
|
| 264 |
outputs=[msg, chatbot],
|
| 265 |
).then(
|
| 266 |
chat_voyeur,
|
| 267 |
+
inputs=[msg, chatbot],
|
| 268 |
outputs=[msg, chatbot],
|
| 269 |
)
|
| 270 |
|
|
|
|
| 275 |
outputs=[msg, chatbot],
|
| 276 |
).then(
|
| 277 |
chat_voyeur,
|
| 278 |
+
inputs=[msg, chatbot],
|
| 279 |
outputs=[msg, chatbot],
|
| 280 |
)
|
| 281 |
|
|
|
|
| 286 |
|
| 287 |
if __name__ == "__main__":
|
| 288 |
demo = create_interface()
|
| 289 |
+
# Para Hugging Face Spaces, no usar share=True
|
| 290 |
demo.launch(debug=True)
|