Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,98 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
if __name__ == "__main__":
|
| 17 |
-
demo.launch()
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from sambanova import SambaNova
|
| 4 |
+
|
| 5 |
+
# Configuración
|
| 6 |
+
API_KEY = os.getenv("SAMBANOVA_API_KEY")
|
| 7 |
+
|
| 8 |
+
def chat_with_sambanova(message, history):
|
| 9 |
+
if not API_KEY:
|
| 10 |
+
return "❌ Error: Configura SAMBANOVA_API_KEY en los Secrets de Hugging Face"
|
| 11 |
+
|
| 12 |
+
try:
|
| 13 |
+
# Inicializar cliente SambaNova
|
| 14 |
+
client = SambaNova(
|
| 15 |
+
api_key=API_KEY,
|
| 16 |
+
base_url="https://api.sambanova.ai/v1",
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
# Construir mensajes
|
| 20 |
+
messages = [{"role": "system", "content": "Eres un asistente útil que responde en español de forma clara y concisa."}]
|
| 21 |
+
|
| 22 |
+
# Agregar historial
|
| 23 |
+
for user_msg, bot_msg in history:
|
| 24 |
+
messages.append({"role": "user", "content": user_msg})
|
| 25 |
+
if bot_msg: # Solo agregar si no está vacío
|
| 26 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 27 |
+
|
| 28 |
+
# Agregar mensaje actual
|
| 29 |
+
messages.append({"role": "user", "content": message})
|
| 30 |
+
|
| 31 |
+
# Llamar a la API
|
| 32 |
+
response = client.chat.completions.create(
|
| 33 |
+
model="DeepSeek-V3.1",
|
| 34 |
+
messages=messages,
|
| 35 |
+
temperature=0.7,
|
| 36 |
+
top_p=0.9,
|
| 37 |
+
max_tokens=1024
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
return response.choices[0].message.content
|
| 41 |
+
|
| 42 |
+
except Exception as e:
|
| 43 |
+
return f"❌ Error: {str(e)}"
|
| 44 |
+
|
| 45 |
+
# Crear interfaz Gradio
|
| 46 |
+
with gr.Blocks(title="🚀 Batuto Deep - SambaNova", theme=gr.themes.Soft()) as demo:
|
| 47 |
+
gr.Markdown("""
|
| 48 |
+
# 🚀 Batuto Deep
|
| 49 |
+
### Chat con DeepSeek-V3.1 via SambaNova API
|
| 50 |
+
""")
|
| 51 |
+
|
| 52 |
+
chatbot = gr.Chatbot(
|
| 53 |
+
label="💬 Conversación",
|
| 54 |
+
height=500,
|
| 55 |
+
show_copy_button=True
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
with gr.Row():
|
| 59 |
+
msg = gr.Textbox(
|
| 60 |
+
label="Tu mensaje",
|
| 61 |
+
placeholder="Escribe tu mensaje aquí...",
|
| 62 |
+
scale=4,
|
| 63 |
+
container=False
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
with gr.Row():
|
| 67 |
+
submit_btn = gr.Button("📤 Enviar", variant="primary")
|
| 68 |
+
clear_btn = gr.Button("🗑️ Limpiar")
|
| 69 |
|
| 70 |
+
# Event handlers
|
| 71 |
+
def respond(message, chat_history):
|
| 72 |
+
if not message.strip():
|
| 73 |
+
return "", chat_history
|
| 74 |
+
|
| 75 |
+
bot_message = chat_with_sambanova(message, chat_history)
|
| 76 |
+
chat_history.append((message, bot_message))
|
| 77 |
+
return "", chat_history
|
| 78 |
|
| 79 |
+
submit_btn.click(
|
| 80 |
+
respond,
|
| 81 |
+
[msg, chatbot],
|
| 82 |
+
[msg, chatbot]
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
msg.submit(
|
| 86 |
+
respond,
|
| 87 |
+
[msg, chatbot],
|
| 88 |
+
[msg, chatbot]
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
clear_btn.click(
|
| 92 |
+
lambda: ([], ""),
|
| 93 |
+
None,
|
| 94 |
+
[chatbot, msg]
|
| 95 |
+
)
|
| 96 |
|
| 97 |
if __name__ == "__main__":
|
| 98 |
+
demo.launch(share=False)
|