File size: 8,831 Bytes
6059138
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a23fb2
 
 
6059138
 
 
 
 
 
 
 
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
from __future__ import annotations

import html
import inspect
import os
from functools import lru_cache

import gradio as gr
from langchain_core.messages import AIMessage, BaseMessage, HumanMessage

from memory_agent.agent import UniversalMemoryAgent
from memory_agent.config import AppConfig
from memory_agent.errors import RATE_LIMIT_MESSAGE, is_rate_limit_error

NAMESPACE = "default_user"

HERO_HTML = """
<section class="hero">
  <h1>Universal Memory Agent</h1>
  <p>Learns from interactions, remembers your domain, and answers from stored knowledge.</p>
</section>
"""

CSS = """
:root {
    --bg: #04070d;
    --panel: #0c1324;
    --panel-2: #121b31;
    --text: #e9f2ff;
    --muted: #9aa9c3;
    --yellow: #ffd84d;
    --blue: #2d7cff;
    --blue-soft: #4bc4ff;
}

body, .gradio-container {
    color: var(--text);
    background:
        radial-gradient(1200px 720px at 8% -12%, rgba(36, 128, 255, 0.42), transparent 56%),
        radial-gradient(980px 620px at 96% 3%, rgba(185, 230, 255, 0.24), transparent 58%),
        radial-gradient(920px 580px at 98% 2%, rgba(255, 216, 77, 0.16), transparent 62%),
        linear-gradient(180deg, #050911 0%, var(--bg) 100%) !important;
}

.app-shell {
    max-width: 920px;
    margin: 0 auto;
    padding: 1.6rem 0.4rem 1.2rem 0.4rem;
}

.hero {
    position: relative;
    overflow: hidden;
    border-radius: 22px;
    border: 1px solid rgba(75, 196, 255, 0.26);
    background: linear-gradient(140deg, rgba(12, 19, 36, 0.95), rgba(6, 10, 19, 0.98));
    padding: 1.25rem 1.25rem 1rem 1.25rem;
    box-shadow: 0 18px 45px rgba(0, 0, 0, 0.42);
    margin-bottom: 0.9rem;
}

.hero::after {
    content: "";
    position: absolute;
    inset: 0;
    background:
        radial-gradient(560px 200px at -5% 0%, rgba(255, 216, 77, 0.22), transparent 72%),
        radial-gradient(520px 240px at 105% 0%, rgba(42, 142, 255, 0.5), transparent 76%),
        radial-gradient(430px 190px at 82% 18%, rgba(215, 244, 255, 0.2), transparent 74%);
    pointer-events: none;
}

.hero h1 {
    margin: 0;
    color: #f5fbff;
    font-size: clamp(2rem, 4vw, 3.05rem);
    letter-spacing: 0.25px;
    line-height: 1.1;
}

.hero p {
    margin: 0.52rem 0 0 0;
    color: var(--muted);
    font-size: 1.03rem;
}

#clear-btn {
    border-radius: 999px;
    border: 1px solid rgba(255, 216, 77, 0.55);
    background: linear-gradient(110deg, rgba(255, 216, 77, 0.14), rgba(45, 124, 255, 0.17));
    color: var(--text);
    font-weight: 650;
}

#chatbot {
    min-height: 460px;
    border: 1px solid rgba(45, 124, 255, 0.24);
    border-radius: 18px;
    background: linear-gradient(155deg, rgba(15, 24, 45, 0.82), rgba(9, 14, 26, 0.9));
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.34);
}

#chatbot .message,
#chatbot .message-row .message {
    border-radius: 16px !important;
    border: 1px solid rgba(45, 124, 255, 0.22) !important;
    background: linear-gradient(155deg, rgba(15, 24, 45, 0.92), rgba(9, 14, 26, 0.92)) !important;
    color: #eaf3ff !important;
}

#composer {
    margin-top: 0.7rem;
}

#user-input {
    border-radius: 14px;
    border: 1px solid rgba(255, 216, 77, 0.46);
    background: rgba(6, 11, 21, 0.94);
}

#user-input textarea {
    color: #f3f8ff !important;
    font-size: 1.02rem !important;
}

#user-input textarea::placeholder {
    color: #9fb1cf !important;
}

#send-btn {
    border-radius: 12px;
    border: 1px solid rgba(255, 216, 77, 0.55);
    background: linear-gradient(110deg, rgba(255, 216, 77, 0.14), rgba(45, 124, 255, 0.17));
    color: var(--text);
    font-weight: 700;
    min-width: 56px;
}

#error-box {
    margin-top: 0.9rem;
    border-radius: 12px;
    border: 1px solid rgba(255, 119, 119, 0.35);
    background: rgba(80, 25, 25, 0.35);
    color: #ffdede;
    padding: 0.8rem 0.9rem;
    font-size: 0.96rem;
}

@media (max-width: 768px) {
    .app-shell {
        padding: 1.1rem 0.2rem 1rem 0.2rem;
    }
    .hero {
        padding: 1rem;
    }
}
"""


@lru_cache(maxsize=1)
def build_agent() -> UniversalMemoryAgent:
    config = AppConfig.from_env()
    return UniversalMemoryAgent(config=config)


def _history_to_langchain(history: list[dict[str, str]]) -> list[BaseMessage]:
    messages: list[BaseMessage] = []
    for item in history:
        role = item.get("role", "")
        content = item.get("content", "")
        if role == "user":
            messages.append(HumanMessage(content=content))
        elif role == "assistant":
            messages.append(AIMessage(content=content))
    return messages


def _escape(text: str) -> str:
    return html.escape(text, quote=False)


def _build_error_message(error: Exception) -> str:
    return (
        "Failed to initialize agent. Configure API_KEY (or MISTRAL_API_KEY).\n"
        f"Details: {_escape(str(error))}"
    )


def handle_chat(user_input: str, history: list[dict[str, str]] | None) -> tuple[str, list[dict[str, str]], list[dict[str, str]]]:
    chat_history = list(history or [])
    text = (user_input or "").strip()
    if not text:
        return "", chat_history, chat_history

    try:
        agent = build_agent()
        prior_messages = _history_to_langchain(chat_history)
        assistant_text = agent.run(
            user_input=text,
            chat_history=prior_messages,
            namespace=NAMESPACE,
        )
    except Exception as error:
        if is_rate_limit_error(error):
            assistant_text = RATE_LIMIT_MESSAGE
        else:
            assistant_text = f"Temporary error: {error}"

    chat_history.append({"role": "user", "content": text})
    chat_history.append({"role": "assistant", "content": assistant_text})
    return "", chat_history, chat_history


def clear_chat() -> tuple[list[dict[str, str]], list[dict[str, str]]]:
    return [], []


def build_ui() -> gr.Blocks:
    error_message = ""
    try:
        build_agent()
    except Exception as error:
        error_message = _build_error_message(error)

    with gr.Blocks(title="Universal Memory Agent") as demo:
        with gr.Column(elem_classes=["app-shell"]):
            gr.HTML(HERO_HTML)
            clear_button = gr.Button("Clear chat history", elem_id="clear-btn")
            chatbot = gr.Chatbot(
                [],
                show_label=False,
                elem_id="chatbot",
                height=500,
            )
            with gr.Row(elem_id="composer"):
                user_input = gr.Textbox(
                    placeholder="Share a fact or ask a question...",
                    show_label=False,
                    container=True,
                    lines=1,
                    max_lines=1,
                    elem_id="user-input",
                    scale=20,
                    autofocus=True,
                )
                send_button = gr.Button("↑", elem_id="send-btn", scale=1, min_width=56)

            if error_message:
                gr.HTML(f"<div id='error-box'>{error_message}</div>")

            session_history = gr.State([])

            user_input.submit(
                fn=handle_chat,
                inputs=[user_input, session_history],
                outputs=[user_input, chatbot, session_history],
            )
            send_button.click(
                fn=handle_chat,
                inputs=[user_input, session_history],
                outputs=[user_input, chatbot, session_history],
            )
            clear_button.click(
                fn=clear_chat,
                inputs=None,
                outputs=[chatbot, session_history],
            )

    return demo


def _resolve_host() -> str:
    return os.getenv("APP_HOST", "0.0.0.0")


def _resolve_port() -> int | None:
    # Hugging Face Spaces sets PORT. Local/dev can use APP_PORT.
    env_port = os.getenv("PORT") or os.getenv("APP_PORT")
    if not env_port:
        return None
    try:
        return int(env_port)
    except ValueError:
        return None


demo = build_ui()


if __name__ == "__main__":
    launch_kwargs: dict[str, object] = {
        "server_name": _resolve_host(),
        "css": CSS,
    }
    resolved_port = _resolve_port()
    if resolved_port is not None:
        launch_kwargs["server_port"] = resolved_port
    if "show_api" in inspect.signature(demo.launch).parameters:
        launch_kwargs["show_api"] = False
    # HF Spaces may run Python 3.13 where Gradio SSR can trigger asyncio loop cleanup warnings.
    if "ssr_mode" in inspect.signature(demo.launch).parameters:
        launch_kwargs["ssr_mode"] = False
    try:
        demo.launch(**launch_kwargs)
    except OSError as error:
        # If requested port is busy, retry with Gradio automatic port selection.
        if "Cannot find empty port" not in str(error) or "server_port" not in launch_kwargs:
            raise
        launch_kwargs.pop("server_port", None)
        demo.launch(**launch_kwargs)