add examples
Browse files- Dockerfile +3 -0
- app.py +0 -470
- examples/chatbot.py +152 -0
- examples/dataset_explorer.py +108 -0
- requirements.txt +3 -0
Dockerfile
CHANGED
|
@@ -30,6 +30,9 @@ RUN --mount=type=secret,id=MARIMO_PASSWORD \
|
|
| 30 |
# Set working directory
|
| 31 |
WORKDIR /data
|
| 32 |
|
|
|
|
|
|
|
|
|
|
| 33 |
# Set user
|
| 34 |
USER user
|
| 35 |
|
|
|
|
| 30 |
# Set working directory
|
| 31 |
WORKDIR /data
|
| 32 |
|
| 33 |
+
# Copy examples
|
| 34 |
+
COPY --chown=user:user ./examples ./examples
|
| 35 |
+
|
| 36 |
# Set user
|
| 37 |
USER user
|
| 38 |
|
app.py
DELETED
|
@@ -1,470 +0,0 @@
|
|
| 1 |
-
import marimo
|
| 2 |
-
|
| 3 |
-
__generated_with = "0.9.2"
|
| 4 |
-
app = marimo.App()
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
@app.cell
|
| 8 |
-
def __():
|
| 9 |
-
import marimo as mo
|
| 10 |
-
|
| 11 |
-
mo.md("# Welcome to marimo! ππ")
|
| 12 |
-
return (mo,)
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
@app.cell
|
| 16 |
-
def __(mo):
|
| 17 |
-
slider = mo.ui.slider(1, 22)
|
| 18 |
-
return (slider,)
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
@app.cell
|
| 22 |
-
def __(mo, slider):
|
| 23 |
-
mo.md(
|
| 24 |
-
f"""
|
| 25 |
-
marimo is a **reactive** Python notebook.
|
| 26 |
-
|
| 27 |
-
This means that unlike traditional notebooks, marimo notebooks **run
|
| 28 |
-
automatically** when you modify them or
|
| 29 |
-
interact with UI elements, like this slider: {slider}.
|
| 30 |
-
|
| 31 |
-
{"##" + "π" * slider.value}
|
| 32 |
-
"""
|
| 33 |
-
)
|
| 34 |
-
return
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
@app.cell(hide_code=True)
|
| 38 |
-
def __(mo):
|
| 39 |
-
mo.accordion(
|
| 40 |
-
{
|
| 41 |
-
"Tip: disabling automatic execution": mo.md(
|
| 42 |
-
rf"""
|
| 43 |
-
marimo lets you disable automatic execution: just go into the
|
| 44 |
-
notebook settings and set
|
| 45 |
-
|
| 46 |
-
"Runtime > On Cell Change" to "lazy".
|
| 47 |
-
|
| 48 |
-
When the runtime is lazy, after running a cell, marimo marks its
|
| 49 |
-
descendants as stale instead of automatically running them. The
|
| 50 |
-
lazy runtime puts you in control over when cells are run, while
|
| 51 |
-
still giving guarantees about the notebook state.
|
| 52 |
-
"""
|
| 53 |
-
)
|
| 54 |
-
}
|
| 55 |
-
)
|
| 56 |
-
return
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
@app.cell(hide_code=True)
|
| 60 |
-
def __(mo):
|
| 61 |
-
mo.md(
|
| 62 |
-
"""
|
| 63 |
-
Tip: This is a tutorial notebook. You can create your own notebooks
|
| 64 |
-
by entering `marimo edit` at the command line.
|
| 65 |
-
"""
|
| 66 |
-
).callout()
|
| 67 |
-
return
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
@app.cell(hide_code=True)
|
| 71 |
-
def __(mo):
|
| 72 |
-
mo.md(
|
| 73 |
-
"""
|
| 74 |
-
## 1. Reactive execution
|
| 75 |
-
|
| 76 |
-
A marimo notebook is made up of small blocks of Python code called
|
| 77 |
-
cells.
|
| 78 |
-
|
| 79 |
-
marimo reads your cells and models the dependencies among them: whenever
|
| 80 |
-
a cell that defines a global variable is run, marimo
|
| 81 |
-
**automatically runs** all cells that reference that variable.
|
| 82 |
-
|
| 83 |
-
Reactivity keeps your program state and outputs in sync with your code,
|
| 84 |
-
making for a dynamic programming environment that prevents bugs before they
|
| 85 |
-
happen.
|
| 86 |
-
"""
|
| 87 |
-
)
|
| 88 |
-
return
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
@app.cell(hide_code=True)
|
| 92 |
-
def __(changed, mo):
|
| 93 |
-
(
|
| 94 |
-
mo.md(
|
| 95 |
-
f"""
|
| 96 |
-
**β¨ Nice!** The value of `changed` is now {changed}.
|
| 97 |
-
|
| 98 |
-
When you updated the value of the variable `changed`, marimo
|
| 99 |
-
**reacted** by running this cell automatically, because this cell
|
| 100 |
-
references the global variable `changed`.
|
| 101 |
-
|
| 102 |
-
Reactivity ensures that your notebook state is always
|
| 103 |
-
consistent, which is crucial for doing good science; it's also what
|
| 104 |
-
enables marimo notebooks to double as tools and apps.
|
| 105 |
-
"""
|
| 106 |
-
)
|
| 107 |
-
if changed
|
| 108 |
-
else mo.md(
|
| 109 |
-
"""
|
| 110 |
-
**π See it in action.** In the next cell, change the value of the
|
| 111 |
-
variable `changed` to `True`, then click the run button.
|
| 112 |
-
"""
|
| 113 |
-
)
|
| 114 |
-
)
|
| 115 |
-
return
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
@app.cell
|
| 119 |
-
def __():
|
| 120 |
-
changed = False
|
| 121 |
-
return (changed,)
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
@app.cell(hide_code=True)
|
| 125 |
-
def __(mo):
|
| 126 |
-
mo.accordion(
|
| 127 |
-
{
|
| 128 |
-
"Tip: execution order": (
|
| 129 |
-
"""
|
| 130 |
-
The order of cells on the page has no bearing on
|
| 131 |
-
the order in which cells are executed: marimo knows that a cell
|
| 132 |
-
reading a variable must run after the cell that defines it. This
|
| 133 |
-
frees you to organize your code in the way that makes the most
|
| 134 |
-
sense for you.
|
| 135 |
-
"""
|
| 136 |
-
)
|
| 137 |
-
}
|
| 138 |
-
)
|
| 139 |
-
return
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
@app.cell(hide_code=True)
|
| 143 |
-
def __(mo):
|
| 144 |
-
mo.md(
|
| 145 |
-
"""
|
| 146 |
-
**Global names must be unique.** To enable reactivity, marimo imposes a
|
| 147 |
-
constraint on how names appear in cells: no two cells may define the same
|
| 148 |
-
variable.
|
| 149 |
-
"""
|
| 150 |
-
)
|
| 151 |
-
return
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
@app.cell(hide_code=True)
|
| 155 |
-
def __(mo):
|
| 156 |
-
mo.accordion(
|
| 157 |
-
{
|
| 158 |
-
"Tip: encapsulation": (
|
| 159 |
-
"""
|
| 160 |
-
By encapsulating logic in functions, classes, or Python modules,
|
| 161 |
-
you can minimize the number of global variables in your notebook.
|
| 162 |
-
"""
|
| 163 |
-
)
|
| 164 |
-
}
|
| 165 |
-
)
|
| 166 |
-
return
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
@app.cell(hide_code=True)
|
| 170 |
-
def __(mo):
|
| 171 |
-
mo.accordion(
|
| 172 |
-
{
|
| 173 |
-
"Tip: private variables": (
|
| 174 |
-
"""
|
| 175 |
-
Variables prefixed with an underscore are "private" to a cell, so
|
| 176 |
-
they can be defined by multiple cells.
|
| 177 |
-
"""
|
| 178 |
-
)
|
| 179 |
-
}
|
| 180 |
-
)
|
| 181 |
-
return
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
@app.cell(hide_code=True)
|
| 185 |
-
def __(mo):
|
| 186 |
-
mo.md(
|
| 187 |
-
"""
|
| 188 |
-
## 2. UI elements
|
| 189 |
-
|
| 190 |
-
Cells can output interactive UI elements. Interacting with a UI
|
| 191 |
-
element **automatically triggers notebook execution**: when
|
| 192 |
-
you interact with a UI element, its value is sent back to Python, and
|
| 193 |
-
every cell that references that element is re-run.
|
| 194 |
-
|
| 195 |
-
marimo provides a library of UI elements to choose from under
|
| 196 |
-
`marimo.ui`.
|
| 197 |
-
"""
|
| 198 |
-
)
|
| 199 |
-
return
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
@app.cell
|
| 203 |
-
def __(mo):
|
| 204 |
-
mo.md("""**π Some UI elements.** Try interacting with the below elements.""")
|
| 205 |
-
return
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
@app.cell
|
| 209 |
-
def __(mo):
|
| 210 |
-
icon = mo.ui.dropdown(["π", "π", "β¨"], value="π")
|
| 211 |
-
return (icon,)
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
@app.cell
|
| 215 |
-
def __(icon, mo):
|
| 216 |
-
repetitions = mo.ui.slider(1, 16, label=f"number of {icon.value}: ")
|
| 217 |
-
return (repetitions,)
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
@app.cell
|
| 221 |
-
def __(icon, repetitions):
|
| 222 |
-
icon, repetitions
|
| 223 |
-
return
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
@app.cell
|
| 227 |
-
def __(icon, mo, repetitions):
|
| 228 |
-
mo.md("# " + icon.value * repetitions.value)
|
| 229 |
-
return
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
@app.cell(hide_code=True)
|
| 233 |
-
def __(mo):
|
| 234 |
-
mo.md(
|
| 235 |
-
"""
|
| 236 |
-
## 3. marimo is just Python
|
| 237 |
-
|
| 238 |
-
marimo cells parse Python (and only Python), and marimo notebooks are
|
| 239 |
-
stored as pure Python files β outputs are _not_ included. There's no
|
| 240 |
-
magical syntax.
|
| 241 |
-
|
| 242 |
-
The Python files generated by marimo are:
|
| 243 |
-
|
| 244 |
-
- easily versioned with git, yielding minimal diffs
|
| 245 |
-
- legible for both humans and machines
|
| 246 |
-
- formattable using your tool of choice,
|
| 247 |
-
- usable as Python scripts, with UI elements taking their default
|
| 248 |
-
values, and
|
| 249 |
-
- importable by other modules (more on that in the future).
|
| 250 |
-
"""
|
| 251 |
-
)
|
| 252 |
-
return
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
@app.cell(hide_code=True)
|
| 256 |
-
def __(mo):
|
| 257 |
-
mo.md(
|
| 258 |
-
"""
|
| 259 |
-
## 4. Running notebooks as apps
|
| 260 |
-
|
| 261 |
-
marimo notebooks can double as apps. Click the app window icon in the
|
| 262 |
-
bottom-right to see this notebook in "app view."
|
| 263 |
-
|
| 264 |
-
Serve a notebook as an app with `marimo run` at the command-line.
|
| 265 |
-
Of course, you can use marimo just to level-up your
|
| 266 |
-
notebooking, without ever making apps.
|
| 267 |
-
"""
|
| 268 |
-
)
|
| 269 |
-
return
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
@app.cell(hide_code=True)
|
| 273 |
-
def __(mo):
|
| 274 |
-
mo.md(
|
| 275 |
-
"""
|
| 276 |
-
## 5. The `marimo` command-line tool
|
| 277 |
-
|
| 278 |
-
**Creating and editing notebooks.** Use
|
| 279 |
-
|
| 280 |
-
```
|
| 281 |
-
marimo edit
|
| 282 |
-
```
|
| 283 |
-
|
| 284 |
-
in a terminal to start the marimo notebook server. From here
|
| 285 |
-
you can create a new notebook or edit existing ones.
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
**Running as apps.** Use
|
| 289 |
-
|
| 290 |
-
```
|
| 291 |
-
marimo run notebook.py
|
| 292 |
-
```
|
| 293 |
-
|
| 294 |
-
to start a webserver that serves your notebook as an app in read-only mode,
|
| 295 |
-
with code cells hidden.
|
| 296 |
-
|
| 297 |
-
**Convert a Jupyter notebook.** Convert a Jupyter notebook to a marimo
|
| 298 |
-
notebook using `marimo convert`:
|
| 299 |
-
|
| 300 |
-
```
|
| 301 |
-
marimo convert your_notebook.ipynb > your_app.py
|
| 302 |
-
```
|
| 303 |
-
|
| 304 |
-
**Tutorials.** marimo comes packaged with tutorials:
|
| 305 |
-
|
| 306 |
-
- `dataflow`: more on marimo's automatic execution
|
| 307 |
-
- `ui`: how to use UI elements
|
| 308 |
-
- `markdown`: how to write markdown, with interpolated values and
|
| 309 |
-
LaTeX
|
| 310 |
-
- `plots`: how plotting works in marimo
|
| 311 |
-
- `sql`: how to use SQL
|
| 312 |
-
- `layout`: layout elements in marimo
|
| 313 |
-
- `fileformat`: how marimo's file format works
|
| 314 |
-
- `markdown-format`: for using `.md` files in marimo
|
| 315 |
-
- `for-jupyter-users`: if you are coming from Jupyter
|
| 316 |
-
|
| 317 |
-
Start a tutorial with `marimo tutorial`; for example,
|
| 318 |
-
|
| 319 |
-
```
|
| 320 |
-
marimo tutorial dataflow
|
| 321 |
-
```
|
| 322 |
-
|
| 323 |
-
In addition to tutorials, we have examples in our
|
| 324 |
-
[our GitHub repo](https://www.github.com/marimo-team/marimo/tree/main/examples).
|
| 325 |
-
"""
|
| 326 |
-
)
|
| 327 |
-
return
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
@app.cell(hide_code=True)
|
| 331 |
-
def __(mo):
|
| 332 |
-
mo.md(
|
| 333 |
-
"""
|
| 334 |
-
## 6. The marimo editor
|
| 335 |
-
|
| 336 |
-
Here are some tips to help you get started with the marimo editor.
|
| 337 |
-
"""
|
| 338 |
-
)
|
| 339 |
-
return
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
@app.cell
|
| 343 |
-
def __(mo, tips):
|
| 344 |
-
mo.accordion(tips)
|
| 345 |
-
return
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
@app.cell(hide_code=True)
|
| 349 |
-
def __(mo):
|
| 350 |
-
mo.md("""## Finally, a fun fact""")
|
| 351 |
-
return
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
@app.cell(hide_code=True)
|
| 355 |
-
def __(mo):
|
| 356 |
-
mo.md(
|
| 357 |
-
"""
|
| 358 |
-
The name "marimo" is a reference to a type of algae that, under
|
| 359 |
-
the right conditions, clumps together to form a small sphere
|
| 360 |
-
called a "marimo moss ball". Made of just strands of algae, these
|
| 361 |
-
beloved assemblages are greater than the sum of their parts.
|
| 362 |
-
"""
|
| 363 |
-
)
|
| 364 |
-
return
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
@app.cell(hide_code=True)
|
| 368 |
-
def __():
|
| 369 |
-
tips = {
|
| 370 |
-
"Saving": (
|
| 371 |
-
"""
|
| 372 |
-
**Saving**
|
| 373 |
-
|
| 374 |
-
- _Name_ your app using the box at the top of the screen, or
|
| 375 |
-
with `Ctrl/Cmd+s`. You can also create a named app at the
|
| 376 |
-
command line, e.g., `marimo edit app_name.py`.
|
| 377 |
-
|
| 378 |
-
- _Save_ by clicking the save icon on the bottom right, or by
|
| 379 |
-
inputting `Ctrl/Cmd+s`. By default marimo is configured
|
| 380 |
-
to autosave.
|
| 381 |
-
"""
|
| 382 |
-
),
|
| 383 |
-
"Running": (
|
| 384 |
-
"""
|
| 385 |
-
1. _Run a cell_ by clicking the play ( β· ) button on the top
|
| 386 |
-
right of a cell, or by inputting `Ctrl/Cmd+Enter`.
|
| 387 |
-
|
| 388 |
-
2. _Run a stale cell_ by clicking the yellow run button on the
|
| 389 |
-
right of the cell, or by inputting `Ctrl/Cmd+Enter`. A cell is
|
| 390 |
-
stale when its code has been modified but not run.
|
| 391 |
-
|
| 392 |
-
3. _Run all stale cells_ by clicking the play ( β· ) button on
|
| 393 |
-
the bottom right of the screen, or input `Ctrl/Cmd+Shift+r`.
|
| 394 |
-
"""
|
| 395 |
-
),
|
| 396 |
-
"Console Output": (
|
| 397 |
-
"""
|
| 398 |
-
Console output (e.g., `print()` statements) is shown below a
|
| 399 |
-
cell.
|
| 400 |
-
"""
|
| 401 |
-
),
|
| 402 |
-
"Creating, Moving, and Deleting Cells": (
|
| 403 |
-
"""
|
| 404 |
-
1. _Create_ a new cell above or below a given one by clicking
|
| 405 |
-
the plus button to the left of the cell, which appears on
|
| 406 |
-
mouse hover.
|
| 407 |
-
|
| 408 |
-
2. _Move_ a cell up or down by dragging on the handle to the
|
| 409 |
-
right of the cell, which appears on mouse hover.
|
| 410 |
-
|
| 411 |
-
3. _Delete_ a cell by clicking the trash bin icon. Bring it
|
| 412 |
-
back by clicking the undo button on the bottom right of the
|
| 413 |
-
screen, or with `Ctrl/Cmd+Shift+z`.
|
| 414 |
-
"""
|
| 415 |
-
),
|
| 416 |
-
"Disabling Automatic Execution": (
|
| 417 |
-
"""
|
| 418 |
-
Via the notebook settings (gear icon) or footer panel, you
|
| 419 |
-
can disable automatic execution. This is helpful when
|
| 420 |
-
working with expensive notebooks or notebooks that have
|
| 421 |
-
side-effects like database transactions.
|
| 422 |
-
"""
|
| 423 |
-
),
|
| 424 |
-
"Disabling Cells": (
|
| 425 |
-
"""
|
| 426 |
-
You can disable a cell via the cell context menu.
|
| 427 |
-
marimo will never run a disabled cell or any cells that depend on it.
|
| 428 |
-
This can help prevent accidental execution of expensive computations
|
| 429 |
-
when editing a notebook.
|
| 430 |
-
"""
|
| 431 |
-
),
|
| 432 |
-
"Code Folding": (
|
| 433 |
-
"""
|
| 434 |
-
You can collapse or fold the code in a cell by clicking the arrow
|
| 435 |
-
icons in the line number column to the left, or by using keyboard
|
| 436 |
-
shortcuts.
|
| 437 |
-
|
| 438 |
-
Use the command palette (`Ctrl/Cmd+k`) or a keyboard shortcut to
|
| 439 |
-
quickly fold or unfold all cells.
|
| 440 |
-
"""
|
| 441 |
-
),
|
| 442 |
-
"Code Formatting": (
|
| 443 |
-
"""
|
| 444 |
-
If you have [ruff](https://github.com/astral-sh/ruff) installed,
|
| 445 |
-
you can format a cell with the keyboard shortcut `Ctrl/Cmd+b`.
|
| 446 |
-
"""
|
| 447 |
-
),
|
| 448 |
-
"Command Palette": (
|
| 449 |
-
"""
|
| 450 |
-
Use `Ctrl/Cmd+k` to open the command palette.
|
| 451 |
-
"""
|
| 452 |
-
),
|
| 453 |
-
"Keyboard Shortcuts": (
|
| 454 |
-
"""
|
| 455 |
-
Open the notebook menu (top-right) or input `Ctrl/Cmd+Shift+h` to
|
| 456 |
-
view a list of all keyboard shortcuts.
|
| 457 |
-
"""
|
| 458 |
-
),
|
| 459 |
-
"Configuration": (
|
| 460 |
-
"""
|
| 461 |
-
Configure the editor by clicking the gears icon near the top-right
|
| 462 |
-
of the screen.
|
| 463 |
-
"""
|
| 464 |
-
),
|
| 465 |
-
}
|
| 466 |
-
return (tips,)
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
if __name__ == "__main__":
|
| 470 |
-
app.run()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import marimo
|
| 2 |
+
|
| 3 |
+
__generated_with = "0.9.14"
|
| 4 |
+
app = marimo.App(width="medium")
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
@app.cell
|
| 8 |
+
def __():
|
| 9 |
+
import marimo as mo
|
| 10 |
+
import os
|
| 11 |
+
from huggingface_hub import InferenceClient
|
| 12 |
+
return InferenceClient, mo, os
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
@app.cell
|
| 16 |
+
def __():
|
| 17 |
+
MODEL_NAME = "HuggingFaceH4/zephyr-7b-beta"
|
| 18 |
+
return (MODEL_NAME,)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
@app.cell(hide_code=True)
|
| 22 |
+
def __(MODEL_NAME, mo):
|
| 23 |
+
mo.md(f"""
|
| 24 |
+
# Chat with **{MODEL_NAME}**
|
| 25 |
+
""")
|
| 26 |
+
return
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
@app.cell
|
| 30 |
+
def __(max_tokens, mo, system_message, temperature, top_p):
|
| 31 |
+
mo.hstack(
|
| 32 |
+
[
|
| 33 |
+
system_message,
|
| 34 |
+
mo.vstack([temperature, top_p, max_tokens], align="end"),
|
| 35 |
+
],
|
| 36 |
+
)
|
| 37 |
+
return
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
@app.cell
|
| 41 |
+
def __(mo, respond):
|
| 42 |
+
chat = mo.ui.chat(
|
| 43 |
+
model=respond,
|
| 44 |
+
prompts=["Tell me a joke.", "What is the square root of {{number}}?"],
|
| 45 |
+
)
|
| 46 |
+
chat
|
| 47 |
+
return (chat,)
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
@app.cell
|
| 51 |
+
def __(InferenceClient, MODEL_NAME, os):
|
| 52 |
+
"""
|
| 53 |
+
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.26.2/en/guides/inference
|
| 54 |
+
"""
|
| 55 |
+
|
| 56 |
+
hf_token = os.environ.get("HF_TOKEN")
|
| 57 |
+
if not hf_token:
|
| 58 |
+
print("HF_TOKEN not set, may have limited access.")
|
| 59 |
+
|
| 60 |
+
client = InferenceClient(
|
| 61 |
+
MODEL_NAME,
|
| 62 |
+
token=hf_token,
|
| 63 |
+
)
|
| 64 |
+
return client, hf_token
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
@app.cell
|
| 68 |
+
def __(client, mo):
|
| 69 |
+
# Create UI controls
|
| 70 |
+
system_message = mo.ui.text_area(
|
| 71 |
+
value="You are a friendly Chatbot.",
|
| 72 |
+
label="System message",
|
| 73 |
+
)
|
| 74 |
+
max_tokens = mo.ui.slider(
|
| 75 |
+
start=1,
|
| 76 |
+
stop=2048,
|
| 77 |
+
value=512,
|
| 78 |
+
step=1,
|
| 79 |
+
label="Max new tokens",
|
| 80 |
+
show_value=True,
|
| 81 |
+
)
|
| 82 |
+
temperature = mo.ui.slider(
|
| 83 |
+
start=0.1,
|
| 84 |
+
stop=4.0,
|
| 85 |
+
value=0.7,
|
| 86 |
+
step=0.1,
|
| 87 |
+
label="Temperature",
|
| 88 |
+
show_value=True,
|
| 89 |
+
)
|
| 90 |
+
top_p = mo.ui.slider(
|
| 91 |
+
start=0.1,
|
| 92 |
+
stop=1.0,
|
| 93 |
+
value=0.95,
|
| 94 |
+
step=0.05,
|
| 95 |
+
label="Top-p (nucleus sampling)",
|
| 96 |
+
show_value=True,
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
# Add more configuration options if needed.
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
# Create chat callback
|
| 103 |
+
def respond(messages: list[mo.ai.ChatMessage], config):
|
| 104 |
+
chat_messages = [{"role": "system", "content": system_message.value}]
|
| 105 |
+
|
| 106 |
+
for message in messages:
|
| 107 |
+
parts = []
|
| 108 |
+
# Add text
|
| 109 |
+
parts.append({"type": "text", "text": message.content})
|
| 110 |
+
|
| 111 |
+
# Add attachments
|
| 112 |
+
if message.attachments:
|
| 113 |
+
for attachment in message.attachments:
|
| 114 |
+
content_type = attachment.content_type or ""
|
| 115 |
+
# This example only supports image attachments
|
| 116 |
+
if content_type.startswith("image"):
|
| 117 |
+
parts.append(
|
| 118 |
+
{
|
| 119 |
+
"type": "image_url",
|
| 120 |
+
"image_url": {"url": attachment.url},
|
| 121 |
+
}
|
| 122 |
+
)
|
| 123 |
+
else:
|
| 124 |
+
raise ValueError(
|
| 125 |
+
f"Unsupported content type {content_type}"
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
chat_messages.append({"role": message.role, "content": parts})
|
| 129 |
+
|
| 130 |
+
response = client.chat_completion(
|
| 131 |
+
chat_messages,
|
| 132 |
+
max_tokens=max_tokens.value,
|
| 133 |
+
temperature=temperature.value,
|
| 134 |
+
top_p=top_p.value,
|
| 135 |
+
stream=False,
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
# You can return strings, markdown, charts, tables, dataframes, and more.
|
| 139 |
+
return response.choices[0].message.content
|
| 140 |
+
return max_tokens, respond, system_message, temperature, top_p
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
@app.cell
|
| 144 |
+
def __():
|
| 145 |
+
# If you need to do anything _reactively_ to the chat messages,
|
| 146 |
+
# you can access the chat messages using the `chat.value` attribute.
|
| 147 |
+
# chat.value
|
| 148 |
+
return
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
if __name__ == "__main__":
|
| 152 |
+
app.run()
|
examples/dataset_explorer.py
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import marimo
|
| 2 |
+
|
| 3 |
+
__generated_with = "0.9.18"
|
| 4 |
+
app = marimo.App(width="full")
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
@app.cell
|
| 8 |
+
def __():
|
| 9 |
+
datasets = [
|
| 10 |
+
# Add your own HF datasets
|
| 11 |
+
"scikit-learn/iris/Iris.csv",
|
| 12 |
+
"scikit-learn/adult-census-income/adult.csv",
|
| 13 |
+
"scikit-learn/auto-mpg/auto-mpg.csv",
|
| 14 |
+
"scikit-learn/credit-card-clients/UCI_Credit_Card.csv",
|
| 15 |
+
"scikit-learn/Fish/Fish.csv",
|
| 16 |
+
"scikit-learn/tips/tips.csv",
|
| 17 |
+
]
|
| 18 |
+
return (datasets,)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
@app.cell(hide_code=True)
|
| 22 |
+
def __(mo):
|
| 23 |
+
mo.md(r"""## Select a dataset""")
|
| 24 |
+
return
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
@app.cell(hide_code=True)
|
| 28 |
+
def __(datasets, mo):
|
| 29 |
+
dataset = mo.ui.dropdown(datasets, value=datasets[0], label="Select a dataset")
|
| 30 |
+
no_limit = mo.ui.switch(label="Limit 1000", value=True)
|
| 31 |
+
mo.hstack([dataset, no_limit])
|
| 32 |
+
return dataset, no_limit
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
@app.cell
|
| 36 |
+
def __(dataset, mo, no_limit):
|
| 37 |
+
explore = mo.sql(
|
| 38 |
+
f"""
|
| 39 |
+
CREATE OR REPLACE TEMP TABLE explore
|
| 40 |
+
AS (FROM 'hf://datasets/{dataset.value}')
|
| 41 |
+
{'LIMIT 1000' if no_limit.value else ''};
|
| 42 |
+
|
| 43 |
+
FROM explore;
|
| 44 |
+
"""
|
| 45 |
+
)
|
| 46 |
+
return (explore,)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
@app.cell(hide_code=True)
|
| 50 |
+
def __(mo):
|
| 51 |
+
mo.md(r"""## Summary""")
|
| 52 |
+
return
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
@app.cell(hide_code=True)
|
| 56 |
+
def __(explore, mo):
|
| 57 |
+
_schema = mo.accordion({"Schema": explore.schema})
|
| 58 |
+
|
| 59 |
+
mo.md(f"""
|
| 60 |
+
* Total rows: **{len(explore):,}**
|
| 61 |
+
* Total columns: **{len(explore.columns)}**
|
| 62 |
+
|
| 63 |
+
{_schema}
|
| 64 |
+
""")
|
| 65 |
+
return
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
@app.cell
|
| 69 |
+
def __(explore):
|
| 70 |
+
explore.describe()
|
| 71 |
+
return
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
@app.cell(hide_code=True)
|
| 75 |
+
def __(mo):
|
| 76 |
+
mo.md("""## Manipulate the data""")
|
| 77 |
+
return
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
@app.cell
|
| 81 |
+
def __(explore, mo):
|
| 82 |
+
transformed = mo.ui.dataframe(explore)
|
| 83 |
+
transformed
|
| 84 |
+
return (transformed,)
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
@app.cell(hide_code=True)
|
| 88 |
+
def __(mo):
|
| 89 |
+
mo.md(r"""## Explore the data""")
|
| 90 |
+
return
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
@app.cell
|
| 94 |
+
def __(mo, transformed):
|
| 95 |
+
mo.ui.data_explorer(transformed.value)
|
| 96 |
+
return
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
@app.cell(hide_code=True)
|
| 100 |
+
def __():
|
| 101 |
+
# Imports
|
| 102 |
+
import marimo as mo
|
| 103 |
+
import polars
|
| 104 |
+
return mo, polars
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
if __name__ == "__main__":
|
| 108 |
+
app.run()
|
requirements.txt
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
|
|
| 1 |
marimo[sql]
|
| 2 |
polars
|
| 3 |
altair
|
|
|
|
|
|
|
| 4 |
# Or a specific version
|
| 5 |
# marimo>=0.9.0
|
| 6 |
|
|
|
|
| 1 |
+
huggingface-hub==0.26.2
|
| 2 |
marimo[sql]
|
| 3 |
polars
|
| 4 |
altair
|
| 5 |
+
openai
|
| 6 |
+
pyarrow
|
| 7 |
# Or a specific version
|
| 8 |
# marimo>=0.9.0
|
| 9 |
|