Download app.py from ZavalaCubas/First_agent_template: direct link, hf CLI and curl.
- Browser
- Download file 4.54 kB
-
https://huggingface.co/spaces/ZavalaCubas/First_agent_template/resolve/main/app.py
- Command line
-
hf download hf://spaces/ZavalaCubas/First_agent_template/app.py
-
curl -L -o app.py https://huggingface.co/spaces/ZavalaCubas/First_agent_template/resolve/main/app.py
4.54 kB
| from smolagents import CodeAgent, DuckDuckGoSearchTool, LiteLLMModel, tool, FinalAnswerTool, stream_to_gradio | |
| import datetime | |
| import pytz | |
| import gradio as gr | |
| 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.") | |
| 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(""" | |
| <div class="zc-header"> | |
| <div style="display:flex;align-items:center;gap:12px;"> | |
| <img src="https://www.google.com/s2/favicons?domain=zavalacubas.com&sz=128" style="width:32px;height:32px;border-radius:8px;border:1px solid oklch(0.6936 0.0666 277.6);padding:3px;" /> | |
| <div> | |
| <div style="color:oklch(0.9641 0.0377 186.4);font-size:15px;font-weight:700;">ZAVALA CUBAS</div> | |
| <div style="color:oklch(0.6936 0.0666 277.6);font-size:12px;">zavalacubas.com</div> | |
| </div> | |
| </div> | |
| <div style="padding:5px 14px;border-radius:999px;border:1px solid oklch(0.6936 0.0666 277.6);color:oklch(0.9641 0.0377 186.4);font-size:11px;">smolagents · Ollama (qwen2:7b)</div> | |
| </div> | |
| """) | |
| chatbot = gr.Chatbot(label="ReAct CodeAgent", height=480) | |
| with gr.Row(): | |
| user_input = gr.Textbox(placeholder="Escribe una tarea para el agente...", show_label=False, scale=5) | |
| submit_btn = gr.Button("Ejecutar →", variant="primary", scale=1) | |
| clear_btn = gr.Button("🧹 Limpiar memoria") | |
| user_input.submit(interact_with_agent, [user_input, chatbot], [user_input, chatbot]) | |
| submit_btn.click(interact_with_agent, [user_input, chatbot], [user_input, chatbot]) | |
| clear_btn.click(reset_chat, None, chatbot, queue=False) | |
| if __name__ == "__main__": | |
| demo.launch(css=custom_css) | |