Spaces:
Sleeping
Sleeping
Deploy TinyModel1Space from GitHub Actions
Browse files- scripts/universal_brain_chat.py +93 -28
scripts/universal_brain_chat.py
CHANGED
|
@@ -137,6 +137,92 @@ HELP_TEXT = """**How to use**
|
|
| 137 |
|
| 138 |
If routing misfires, try rephrasing or use a slash command; **`--no-smart-route`** disables inference (chat only, plus `/…`)."""
|
| 139 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
ROUTER_SYSTEM = """You are an intent router for a desktop AI assistant. The user speaks naturally (any language). Output EXACTLY one JSON object, one line, no markdown fences, no explanation.
|
| 141 |
|
| 142 |
Schema:
|
|
@@ -1877,34 +1963,7 @@ def main() -> None:
|
|
| 1877 |
#ub_input textarea { height: 120px !important; }
|
| 1878 |
"""
|
| 1879 |
with gr.Blocks(title="Universal Brain (chat prototype)", css=_css) as demo:
|
| 1880 |
-
gr.
|
| 1881 |
-
"### Universal Brain — chat prototype\n"
|
| 1882 |
-
f"**Generative:** `{mid}` ({lm.device}) · **Brain layers:** {brain_label}\n\n"
|
| 1883 |
-
"**NL routing:** the model infers what you want (summarize, FAQ search, save note, …). "
|
| 1884 |
-
"Use **`--no-smart-route`** for plain chat-only + slash shortcuts. "
|
| 1885 |
-
"`/help` lists slash commands.\n\n"
|
| 1886 |
-
"**NL session controls:** say things like "
|
| 1887 |
-
"**`What is my current scope?`**, **`Start a new private session`**, **`Switch to scope my-key`**, "
|
| 1888 |
-
"**`Be brief`**, **`More detail please`**, **`Use bullet points`**, **`Reset reply style`**, "
|
| 1889 |
-
"**`Strict FAQ`** / **`Relaxed FAQ`** / **`Balanced FAQ`**, "
|
| 1890 |
-
"**`ELI5`** / **`Expert mode`**, **`TLDR first`** / **`Answer directly`**, "
|
| 1891 |
-
"**`Step by step`** / **`No numbered steps`**, **`Flag your assumptions`** / **`Be decisive`**, "
|
| 1892 |
-
"**`Suggest next steps`** / **`No follow-up questions`**, **`Definitions first`** / **`Intuition first`**, "
|
| 1893 |
-
"**`Include examples`** / **`Skip examples`**, **`Use pros and cons`** / **`Compare in flowing prose`**, **`Formal tone`** / **`Casual tone`**, **`Use code fences`** / **`Inline code only`**, "
|
| 1894 |
-
"**`Use analogies`** / **`No analogies`**, **`Spell out acronyms`** / **`Don't expand acronyms`**, "
|
| 1895 |
-
"**`Clarify first`** / **`No clarifying questions`**, **`No speculation`** / **`Brainstorm freely`**, "
|
| 1896 |
-
"**`Show your work`** / **`Final answer only`**, **`Answer in JSON`** / **`Plain text only`**, "
|
| 1897 |
-
"**`Be risk averse`** / **`Be pragmatic`**, **`Give me runnable commands`** / **`No commands`**, "
|
| 1898 |
-
"**`Quote the FAQ excerpts`** / **`Paraphrase only`**, **`Use tables`** / **`No tables`**, "
|
| 1899 |
-
"**`Use emoji`** / **`No emoji`**, **`Use section headings`** / **`Flat answer`**, "
|
| 1900 |
-
"**`Bold key terms`** / **`Minimal bold`**, **`Challenge my assumptions`** / **`Be supportive`**, "
|
| 1901 |
-
"**`Export my memories`**, **`Delete all my memories for this chat`**, **`Clear my session notes`**, "
|
| 1902 |
-
"**`Turn off FAQ context`**, **`Turn off smart routing`**, **`Show the brain trace`** "
|
| 1903 |
-
"(no slash command required). See the repo `README` for more example phrases.\n\n"
|
| 1904 |
-
"Encoder topics (Hub TinyModel1 ≈ AG News) still feed context and an optional *Brain trace* line; "
|
| 1905 |
-
"use `/classify` or ask naturally to see the full probability table in chat."
|
| 1906 |
-
)
|
| 1907 |
-
chat = gr.Chatbot(type="messages", height=520, label="Conversation", allow_tags=False)
|
| 1908 |
ub_state = gr.State(initial_ub_session)
|
| 1909 |
with gr.Row():
|
| 1910 |
inp = gr.Textbox(
|
|
@@ -1917,6 +1976,12 @@ def main() -> None:
|
|
| 1917 |
)
|
| 1918 |
go = gr.Button("Send", variant="primary", scale=1)
|
| 1919 |
gr.ClearButton([chat, inp])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1920 |
|
| 1921 |
def _submit(
|
| 1922 |
m: str,
|
|
|
|
| 137 |
|
| 138 |
If routing misfires, try rephrasing or use a slash command; **`--no-smart-route`** disables inference (chat only, plus `/…`)."""
|
| 139 |
|
| 140 |
+
# Shown under the chat + controls in the Gradio UI (Hugging Face Space and local).
|
| 141 |
+
GRADIO_INSTRUCTIONS_MARKDOWN = """### About this Space
|
| 142 |
+
|
| 143 |
+
**Universal Brain** pairs a small **generative** model with the **TinyModel1** encoder (AG News–style topics: World, Business, Sports, Sci/Tech), **FAQ retrieval** over a bundled corpus, **SQLite memory** scoped per session, and **natural-language routing** so many tasks work without slash commands. First CPU startup can take a few minutes while weights download.
|
| 144 |
+
|
| 145 |
+
---
|
| 146 |
+
|
| 147 |
+
### Using the layout
|
| 148 |
+
|
| 149 |
+
1. **Conversation** — scroll the transcript; replies may end with a *Brain trace* line (classify / RAG / memory hints) if that toggle is on.
|
| 150 |
+
2. **Message box** — type a line or paragraph; press **Send** or submit with Enter.
|
| 151 |
+
3. **Clear** — wipes the visible chat and the input (does not delete long-term memory unless you use the forget commands below).
|
| 152 |
+
|
| 153 |
+
---
|
| 154 |
+
|
| 155 |
+
### What to try (step-by-step)
|
| 156 |
+
|
| 157 |
+
| Goal | What to type |
|
| 158 |
+
| --- | --- |
|
| 159 |
+
| See what is loaded | `/status` |
|
| 160 |
+
| Full in-chat manual | `/help` |
|
| 161 |
+
| Normal Q&A | Ask any question in plain language. |
|
| 162 |
+
| **Classifier** (full probability table) | `/classify Stocks rallied after earnings.` or ask naturally to classify a paragraph. |
|
| 163 |
+
| **FAQ search** (scored chunks) | `/retrieve shipping policy` or “search the FAQ for …”. |
|
| 164 |
+
| **Summarize** | `/summarize` + long text, or “summarize this: …”. |
|
| 165 |
+
| **Rephrase** | `/reformulate` + text, or “rewrite this professionally: …”. |
|
| 166 |
+
| **Answer from facts only** | `/grounded Will you refund? ||| Our policy is 14-day returns.` (question and context separated by `|||`). |
|
| 167 |
+
| **Similarity** (encoder cosine) | `/similarity The market rose. ||| Stocks gained today.` |
|
| 168 |
+
| **Embedding** preview | `/embed A short passage` or `/embedding …`. |
|
| 169 |
+
| **Pick nearest option** | `/nearest query ||| option one ||| option two` (add more `|||` segments for more candidates). |
|
| 170 |
+
| **Memory — long-term** | `/remember My project code is alpha-42` or say you want to remember something. |
|
| 171 |
+
| **Memory — this session** | `/session Temporary note for this chat` |
|
| 172 |
+
| **List saved notes** | `/memories` or ask to show stored notes. |
|
| 173 |
+
| **Clear session notes only** | `/clear-session` |
|
| 174 |
+
| **Export notes (JSON)** | Say *Export my memories* / *Download my notes as JSON*. |
|
| 175 |
+
| **Wipe all notes for this scope** | Say *Delete all my memories for this chat* (long-term + session for current scope). |
|
| 176 |
+
| **Isolate your notes (new scope)** | *Start a new private session* / *Begin a fresh scope* — then use `/remember` and `/memories` to confirm only new notes appear. |
|
| 177 |
+
| **Switch scope** | *Switch to scope my-key* (ASCII id) to attach memory to a named scope. |
|
| 178 |
+
| **Brain trace on/off** | *Show the brain trace* / *Hide debug trace* — then ask a normal question and check the footer line. |
|
| 179 |
+
| **FAQ snippets on/off** | *Turn off the FAQ context* / *Turn FAQ back on*. |
|
| 180 |
+
| **Routing on/off** | *Turn off smart routing* returns to plain chat + slash shortcuts; turn back on per `/help` phrasing. |
|
| 181 |
+
| **Reply style** | Phrases like *Be brief*, *Use bullet points*, *Strict FAQ*, *ELI5*, *Formal tone*, *Reset reply style* (see `/help` for the full list). |
|
| 182 |
+
|
| 183 |
+
---
|
| 184 |
+
|
| 185 |
+
### Natural-language routing (no `/` required)
|
| 186 |
+
|
| 187 |
+
The app can infer intents such as **chat**, **summarize**, **reformulate**, **grounded Q&A**, **FAQ retrieve**, **classify**, **similarity**, **embedding**, **nearest candidate**, **remember / list / clear memory**, and **status**. If the wrong tool runs, repeat with a clearer verb or use the matching **slash command** from the table above.
|
| 188 |
+
|
| 189 |
+
---
|
| 190 |
+
|
| 191 |
+
### Session controls (plain English, no `/`)
|
| 192 |
+
|
| 193 |
+
These adjust **scope**, **memory**, **FAQ injection**, **routing**, **brain trace**, and **reply style** (hints fed into the system prompt). Examples (not exact wording required):
|
| 194 |
+
|
| 195 |
+
- **Scope / visibility:** *What is my current scope?* · *Show my session settings* · *Start a new private session* · *Switch to scope my-key*
|
| 196 |
+
- **Reply shape:** *Be brief* · *More detail please* · *Use bullet points* · *Reset reply style*
|
| 197 |
+
- **FAQ grounding:** *Strict FAQ* · *Relaxed FAQ* · *Balanced FAQ*
|
| 198 |
+
- **Audience & structure:** *ELI5* · *Expert mode* · *TLDR first* · *Answer directly* · *Step by step* · *No numbered steps* · *Definitions first* · *Intuition first*
|
| 199 |
+
- **Tone & format:** *Formal tone* · *Casual tone* · *Use code fences* · *Inline code only* · *Use tables* · *No tables* · *Use emoji* · *No emoji* · *Use section headings* · *Flat answer* · *Bold key terms* · *Minimal bold*
|
| 200 |
+
- **Reasoning habits:** *Flag your assumptions* · *Be decisive* · *Suggest next steps* · *No follow-up questions* · *Clarify first* · *No clarifying questions* · *No speculation* · *Brainstorm freely* · *Show your work* · *Final answer only*
|
| 201 |
+
- **Output & safety:** *Answer in JSON* · *Plain text only* · *Be risk averse* · *Be pragmatic* · *Give me runnable commands* · *No commands* · *Quote the FAQ excerpts* · *Paraphrase only*
|
| 202 |
+
- **Style extras:** *Use analogies* · *No analogies* · *Spell out acronyms* · *Don't expand acronyms* · *Include examples* · *Skip examples* · *Use pros and cons* · *Compare in flowing prose* · *Challenge my assumptions* · *Be supportive*
|
| 203 |
+
- **Memory maintenance:** *Clear my session notes* · *Export my memories* · *Delete all my memories for this chat*
|
| 204 |
+
- **Debug / behavior:** *Turn off FAQ context* · *Turn FAQ back on* · *Turn off smart routing* · *Show the brain trace* · *Hide debug trace*
|
| 205 |
+
|
| 206 |
+
---
|
| 207 |
+
|
| 208 |
+
### Encoder + trace
|
| 209 |
+
|
| 210 |
+
The encoder adds a soft **topic hint** to the system context and can show **`classify:…`** in the brain trace. Labels reflect **TinyModel1** training (≈ AG News). Use `/classify` when you want the full markdown probability table in the reply.
|
| 211 |
+
|
| 212 |
+
---
|
| 213 |
+
|
| 214 |
+
### Hugging Face API
|
| 215 |
+
|
| 216 |
+
On the Space page, open **Use via API** to call the **`chat`** endpoint (same pipeline as the Send button) from HTTP or the Gradio client.
|
| 217 |
+
|
| 218 |
+
---
|
| 219 |
+
|
| 220 |
+
### Tips
|
| 221 |
+
|
| 222 |
+
- **Shared demo**: the default scope may be shared with other visitors; use *Start a new private session* for isolated memory.
|
| 223 |
+
- **Optional Space env**: `HORIZON2_MODEL` can override the generative model id; `HF_TOKEN` (secret) helps with Hub downloads.
|
| 224 |
+
- **More phrases**: the repo `README` and `/help` list additional natural phrasings for session controls."""
|
| 225 |
+
|
| 226 |
ROUTER_SYSTEM = """You are an intent router for a desktop AI assistant. The user speaks naturally (any language). Output EXACTLY one JSON object, one line, no markdown fences, no explanation.
|
| 227 |
|
| 228 |
Schema:
|
|
|
|
| 1963 |
#ub_input textarea { height: 120px !important; }
|
| 1964 |
"""
|
| 1965 |
with gr.Blocks(title="Universal Brain (chat prototype)", css=_css) as demo:
|
| 1966 |
+
chat = gr.Chatbot(type="messages", height=260, label="Conversation", allow_tags=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1967 |
ub_state = gr.State(initial_ub_session)
|
| 1968 |
with gr.Row():
|
| 1969 |
inp = gr.Textbox(
|
|
|
|
| 1976 |
)
|
| 1977 |
go = gr.Button("Send", variant="primary", scale=1)
|
| 1978 |
gr.ClearButton([chat, inp])
|
| 1979 |
+
gr.Markdown(
|
| 1980 |
+
f"### Universal Brain — chat prototype\n\n"
|
| 1981 |
+
f"**Generative:** `{mid}` ({lm.device}) · **Brain layers:** {brain_label}\n\n"
|
| 1982 |
+
f"Use **Conversation** above, type a message, then **Send** (or Enter). **Clear** resets the on-screen chat only.\n\n"
|
| 1983 |
+
f"{GRADIO_INSTRUCTIONS_MARKDOWN}"
|
| 1984 |
+
)
|
| 1985 |
|
| 1986 |
def _submit(
|
| 1987 |
m: str,
|