from smolagents import CodeAgent, DuckDuckGoSearchTool, LiteLLMModel, tool, FinalAnswerTool, stream_to_gradio import datetime import pytz import gradio as gr @tool def check_brand_voice_rule(channel: str) -> str: """Devuelve las reglas de voz y criterio de marca para un canal específico. Args: channel: El canal de comunicación (ej. 'linkedin', 'web', 'whatsapp'). """ rules = { "linkedin": "Tono directo y estratégico. Priorizar autonomía con criterio. Sin lenguaje corporativo vacío.", "web": "Español neutro con posesivos ('tu marca'). Sin rayas largas. Estrategia antes que herramienta.", "whatsapp": "Respuestas breves, claras y resolutivas orientadas a reservas o consultas concretas." } return rules.get(channel.lower(), "Canal no registrado: aplicar criterio general de claridad y concisión.") @tool def get_current_time_in_timezone(timezone: str) -> str: """Fetches the current local time in a specified timezone. Args: timezone: A string representing a valid timezone (e.g., 'America/New_York', 'Europe/Madrid'). """ try: tz = pytz.timezone(timezone) return f"The current local time in {timezone} is: {datetime.datetime.now(tz).strftime('%Y-%m-%d %H:%M:%S')}" except Exception as e: return f"Error fetching time for timezone '{timezone}': {str(e)}" model = LiteLLMModel( model_id="ollama_chat/qwen2:7b", api_base="http://127.0.0.1:11434", num_ctx=8192, ) agent = CodeAgent( model=model, tools=[FinalAnswerTool(), get_current_time_in_timezone, check_brand_voice_rule, DuckDuckGoSearchTool()], max_steps=6, verbosity_level=1, ) def interact_with_agent(prompt, history): if not prompt or not prompt.strip(): yield "", history return history.append(gr.ChatMessage(role="user", content=prompt)) yield "", history for msg in stream_to_gradio(agent, task=prompt, reset_agent_memory=False): history.append(msg) yield "", history def reset_chat(): agent.memory.reset() return [] custom_css = """ @import url('https://fonts.googleapis.com/css2?family=Space+Grotesk:wght@400;500;600;700&display=swap'); :root { --bbf-ink-900: oklch(0 0 0); --bbf-mint-100: oklch(0.9641 0.0377 186.4); --bbf-accent-400: oklch(0.6936 0.0666 277.6); --bbf-sea-700: oklch(0.421 0.053 188.1); --bbf-deep-800: oklch(0.324 0.070 252.9); } body, .gradio-container { font-family: 'Space Grotesk', system-ui, sans-serif !important; background-color: var(--bbf-ink-900) !important; background-image: radial-gradient(circle at 15% 15%, var(--bbf-deep-800) 0%, transparent 55%), radial-gradient(circle at 85% 85%, var(--bbf-sea-700) 0%, transparent 55%) !important; color: var(--bbf-mint-100) !important; } .zc-header { background: oklch(0 0 0 / 0.85); border: 1px solid var(--bbf-sea-700); border-radius: 16px; padding: 18px 22px; margin-bottom: 14px; display: flex; justify-content: space-between; align-items: center; } """ with gr.Blocks(title="Zavala Cubas · CodeAgent") as demo: gr.HTML("""