File size: 4,544 Bytes
502f552
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e845dca
502f552
 
 
e845dca
502f552
 
 
 
 
 
 
 
 
 
 
e845dca
502f552
 
 
 
e845dca
 
 
 
 
502f552
e845dca
 
502f552
 
 
 
 
 
 
e845dca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
502f552
 
b7a1aa9
502f552
e845dca
 
 
 
b7a1aa9
 
e845dca
 
b7a1aa9
502f552
 
e845dca
502f552
e845dca
 
 
502f552
 
 
 
 
 
e845dca
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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("""
        <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)