Spaces:
Sleeping
Sleeping
| import os | |
| import gradio as gr | |
| from sambanova import SambaNova | |
| # Configuración | |
| API_KEY = os.getenv("SAMBANOVA_API_KEY") | |
| def chat_with_sambanova(message, history): | |
| if not API_KEY: | |
| return "❌ Error: Configura SAMBANOVA_API_KEY en los Secrets de Hugging Face" | |
| try: | |
| # Inicializar cliente SambaNova | |
| client = SambaNova( | |
| api_key=API_KEY, | |
| base_url="https://api.sambanova.ai/v1", | |
| ) | |
| # Construir mensajes | |
| messages = [{"role": "system", "content": "Eres un asistente útil que responde en español de forma clara y concisa."}] | |
| # Agregar historial | |
| for user_msg, bot_msg in history: | |
| messages.append({"role": "user", "content": user_msg}) | |
| if bot_msg: # Solo agregar si no está vacío | |
| messages.append({"role": "assistant", "content": bot_msg}) | |
| # Agregar mensaje actual | |
| messages.append({"role": "user", "content": message}) | |
| # Llamar a la API | |
| response = client.chat.completions.create( | |
| model="DeepSeek-V3.1", | |
| messages=messages, | |
| temperature=0.7, | |
| top_p=0.9, | |
| max_tokens=1024 | |
| ) | |
| return response.choices[0].message.content | |
| except Exception as e: | |
| return f"❌ Error: {str(e)}" | |
| # Crear interfaz Gradio | |
| with gr.Blocks(title="🚀 Batuto Deep - SambaNova", theme=gr.themes.Soft()) as demo: | |
| gr.Markdown(""" | |
| # 🚀 Batuto Deep | |
| ### Chat con DeepSeek-V3.1 via SambaNova API | |
| """) | |
| chatbot = gr.Chatbot( | |
| label="💬 Conversación", | |
| height=500, | |
| show_copy_button=True | |
| ) | |
| with gr.Row(): | |
| msg = gr.Textbox( | |
| label="Tu mensaje", | |
| placeholder="Escribe tu mensaje aquí...", | |
| scale=4, | |
| container=False | |
| ) | |
| with gr.Row(): | |
| submit_btn = gr.Button("📤 Enviar", variant="primary") | |
| clear_btn = gr.Button("🗑️ Limpiar") | |
| # Event handlers | |
| def respond(message, chat_history): | |
| if not message.strip(): | |
| return "", chat_history | |
| bot_message = chat_with_sambanova(message, chat_history) | |
| chat_history.append((message, bot_message)) | |
| return "", chat_history | |
| submit_btn.click( | |
| respond, | |
| [msg, chatbot], | |
| [msg, chatbot] | |
| ) | |
| msg.submit( | |
| respond, | |
| [msg, chatbot], | |
| [msg, chatbot] | |
| ) | |
| clear_btn.click( | |
| lambda: ([], ""), | |
| None, | |
| [chatbot, msg] | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch(share=False) |