push new demo
Browse files
app.py
CHANGED
@@ -1,469 +1,236 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import marimo
|
2 |
|
3 |
-
__generated_with = "0.
|
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
|
17 |
-
|
18 |
-
|
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
|
61 |
mo.md(
|
62 |
-
"""
|
63 |
-
|
64 |
-
by entering `marimo edit` at the command line.
|
65 |
-
"""
|
66 |
-
).callout()
|
67 |
-
return
|
68 |
|
|
|
69 |
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
"""
|
74 |
-
## 1. Reactive execution
|
75 |
|
76 |
-
|
77 |
-
|
|
|
78 |
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
|
83 |
-
|
84 |
-
|
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 |
-
|
121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
|
123 |
|
124 |
-
@app.cell
|
125 |
-
def
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
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
|
185 |
-
def
|
186 |
-
|
187 |
-
|
188 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
196 |
-
|
|
|
|
|
|
|
|
|
197 |
"""
|
198 |
)
|
|
|
199 |
return
|
200 |
|
201 |
|
202 |
@app.cell
|
203 |
-
def
|
204 |
-
|
205 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
206 |
|
207 |
|
208 |
@app.cell
|
209 |
-
def
|
210 |
-
|
211 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
212 |
|
213 |
|
214 |
@app.cell
|
215 |
-
def
|
216 |
-
|
217 |
-
return (
|
218 |
|
219 |
|
220 |
@app.cell
|
221 |
-
def
|
222 |
-
|
223 |
-
|
|
|
224 |
|
225 |
|
226 |
@app.cell
|
227 |
-
def
|
228 |
-
|
229 |
-
|
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 |
-
|
245 |
-
|
246 |
-
|
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 |
-
|
285 |
-
|
|
|
286 |
|
287 |
-
|
288 |
-
|
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
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
|
362 |
-
|
363 |
-
|
364 |
-
|
|
|
|
|
|
|
|
|
365 |
|
366 |
|
367 |
-
@app.cell
|
368 |
-
def
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
|
374 |
-
|
375 |
-
|
376 |
-
|
377 |
-
|
378 |
-
|
379 |
-
|
380 |
-
|
381 |
-
|
382 |
-
|
383 |
-
|
384 |
-
|
385 |
-
|
386 |
-
|
387 |
-
|
388 |
-
|
389 |
-
|
390 |
-
|
391 |
-
|
392 |
-
|
393 |
-
|
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__":
|
|
|
1 |
+
# /// script
|
2 |
+
# requires-python = ">=3.12"
|
3 |
+
# dependencies = [
|
4 |
+
# "groq==0.18.0",
|
5 |
+
# "pandas==2.2.3",
|
6 |
+
# "marimo",
|
7 |
+
# ]
|
8 |
+
# ///
|
9 |
+
|
10 |
import marimo
|
11 |
|
12 |
+
__generated_with = "0.11.6"
|
13 |
+
app = marimo.App(width="medium")
|
14 |
|
15 |
|
16 |
@app.cell
|
17 |
+
def _():
|
18 |
import marimo as mo
|
|
|
|
|
19 |
return (mo,)
|
20 |
|
21 |
|
22 |
@app.cell
|
23 |
+
def _(mo):
|
24 |
+
groq_api_key = mo.ui.text(label='Enter your groq api key here', kind='password')
|
25 |
+
groq_api_key
|
26 |
+
return (groq_api_key,)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
|
29 |
@app.cell(hide_code=True)
|
30 |
+
def _(mo):
|
31 |
mo.md(
|
32 |
+
r"""
|
33 |
+
# LLM for NER
|
|
|
|
|
|
|
|
|
34 |
|
35 |
+
- do a [Few-shot prompting](https://huggingface.co/docs/transformers/main/en/tasks/prompting#few-shot-prompting) and repeat multiple times.
|
36 |
|
37 |
+
The default prompt is:
|
38 |
+
```python
|
39 |
+
Return a list of named entities in the text with your confidence score on a scale of 0 to 1 for this tag.
|
40 |
+
The available entities are: "ADE", "Dosage", "Drug", "Duration", "Form", "Frequency", "Reason", "Route" and "Strength".
|
|
|
41 |
|
42 |
+
For example:
|
43 |
+
Text: MEDICATIONS : Lipitor , Tylenol with Codeine , Dilantin , previously on Decadron q.i.d .
|
44 |
+
Named Entities: <start> Lipitor (Drug: 0.87), Tylenol (Drug: 0.59) <end>
|
45 |
|
46 |
+
Here is your task:
|
47 |
+
Text: The patient then developed oral sores and rash in the chest the night before admission which rapidly spread to the face , trunk , and upper extremities within the last 24 hours.
|
48 |
+
Named Entities:
|
49 |
|
50 |
+
Remember to answer in the exact form of the example.
|
51 |
+
```
|
|
|
52 |
"""
|
53 |
)
|
54 |
return
|
55 |
|
56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
@app.cell
|
58 |
+
def _(mo, models):
|
59 |
+
# define some params
|
60 |
+
call_groq_times = mo.ui.slider(10, 100, 10, 30, label='How many times do you want to call groq')
|
61 |
+
model_ner = mo.ui.dropdown(options=models, value="llama3-8b-8192", label="Choose a LLM")
|
62 |
+
ner_text = mo.ui.text_area(value="", label='Type your text here or leave it to default:')
|
63 |
+
ner_tags = mo.ui.text_area(value="", label="Type the ner tags here or leave it to default:", placeholder='e.g. Country, Person')
|
64 |
+
checkbox_ner = mo.ui.checkbox(label=' Whether to call groq api')
|
65 |
+
mo.vstack([mo.md("# Experiment"), mo.hstack([call_groq_times, model_ner]), mo.hstack([ner_text, ner_tags]), checkbox_ner], align='center')
|
66 |
+
return call_groq_times, checkbox_ner, model_ner, ner_tags, ner_text
|
67 |
|
68 |
|
69 |
+
@app.cell
|
70 |
+
def _(
|
71 |
+
call_groq_times,
|
72 |
+
chat_completion,
|
73 |
+
checkbox_ner,
|
74 |
+
client,
|
75 |
+
extract_ner_from_assistant,
|
76 |
+
mo,
|
77 |
+
model_ner,
|
78 |
+
prompt_ner,
|
79 |
+
):
|
80 |
+
# calling groq
|
81 |
+
result = []
|
82 |
+
if checkbox_ner.value:
|
83 |
+
for _ in mo.status.progress_bar(range(call_groq_times.value), title='In Progress β¦', completion_title='Finished.'):
|
84 |
+
try:
|
85 |
+
answer = chat_completion(client, prompt_ner, model_ner.value)
|
86 |
+
result += extract_ner_from_assistant(answer)
|
87 |
+
except Exception:
|
88 |
+
pass
|
89 |
+
return answer, result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
|
91 |
|
92 |
+
@app.cell
|
93 |
+
def _(mo, pd, result):
|
94 |
+
# transform data
|
95 |
+
data = pd.DataFrame.from_dict(result)
|
96 |
+
# data.to_csv('data.csv')
|
97 |
+
# data = pl.from_dicts(result)
|
98 |
+
# data.write_csv('data.csv')
|
99 |
+
try:
|
100 |
+
transformed_df = mo.ui.dataframe(data)
|
101 |
+
except Exception:
|
102 |
+
df = pd.read_csv('data.csv')
|
103 |
+
transformed_df = mo.ui.dataframe(df)
|
104 |
+
return data, df, transformed_df
|
105 |
|
|
|
|
|
|
|
|
|
106 |
|
107 |
+
@app.cell
|
108 |
+
def _(mo, transformed_df):
|
109 |
+
_md = mo.md(
|
110 |
+
r"""
|
111 |
+
The results are shown below, use __+ Add__ to apply different transforms and explore more:
|
112 |
+
------
|
113 |
"""
|
114 |
)
|
115 |
+
mo.vstack([_md, transformed_df])
|
116 |
return
|
117 |
|
118 |
|
119 |
@app.cell
|
120 |
+
async def _():
|
121 |
+
import os
|
122 |
+
import pandas as pd
|
123 |
+
from functools import reduce
|
124 |
+
import micropip
|
125 |
+
await micropip.install("ssl")
|
126 |
+
await micropip.install("groq")
|
127 |
+
from groq import Groq
|
128 |
+
return Groq, micropip, os, pd, reduce
|
129 |
|
130 |
|
131 |
@app.cell
|
132 |
+
def _():
|
133 |
+
# availabel models on groq
|
134 |
+
models = [
|
135 |
+
"llama3-8b-8192",
|
136 |
+
"llama3-70b-8192",
|
137 |
+
"llama2-70b-4096",
|
138 |
+
"mixtral-8x7b-32768",
|
139 |
+
"gemma-7b-it",
|
140 |
+
]
|
141 |
+
return (models,)
|
142 |
|
143 |
|
144 |
@app.cell
|
145 |
+
def _(Groq, groq_api_key):
|
146 |
+
client = Groq(api_key=groq_api_key.value)
|
147 |
+
return (client,)
|
148 |
|
149 |
|
150 |
@app.cell
|
151 |
+
def _():
|
152 |
+
default_sentence = "The patient then developed oral sores and rash in the chest the night before admission which rapidly spread to the face , trunk , and upper extremities within the last 24 hours."
|
153 |
+
default_tags = ["ADE", "Dosage", "Drug", "Duration", "Form", "Frequency", "Reason", "Route" and "Strength"]
|
154 |
+
return default_sentence, default_tags
|
155 |
|
156 |
|
157 |
@app.cell
|
158 |
+
def _(default_sentence, default_tags, ner_tags, ner_text):
|
159 |
+
prompt_ner = fr"""Return a list of named entities in the text with your confidence score on a scale of 0 to 1 for this tag.
|
160 |
+
The available entities are: {ner_tags.value.split(',') if ner_tags.value else default_tags}.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
161 |
|
162 |
+
For example:
|
163 |
+
Text: MEDICATIONS : Lipitor , Tylenol with Codeine , Dilantin , previously on Decadron q.i.d .
|
164 |
+
Named Entities: <start> Lipitor (Drug: 0.87), Tylenol (Drug: 0.59) <end>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
165 |
|
166 |
+
Here is your task:
|
167 |
+
Text: {ner_text.value if ner_text.value else default_sentence}
|
168 |
+
Named Entities:
|
169 |
|
170 |
+
Remember to answer in the exact form of the example.
|
171 |
+
"""
|
172 |
+
prompt_ner
|
173 |
+
return (prompt_ner,)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
174 |
|
175 |
|
176 |
@app.cell
|
177 |
+
def _():
|
178 |
+
def chat_completion(client, prompt, model):
|
179 |
+
completion = client.chat.completions.create(
|
180 |
+
messages=[
|
181 |
+
{
|
182 |
+
"role": "system",
|
183 |
+
"content": "you will help me with some NER tasks."
|
184 |
+
},
|
185 |
+
# set a user message for the assistant to respond to.
|
186 |
+
{
|
187 |
+
"role": "user",
|
188 |
+
"content": prompt,
|
189 |
+
}
|
190 |
+
],
|
191 |
+
# The language model which will generate the completion.
|
192 |
+
model=model,
|
193 |
+
temperature=0.5,
|
194 |
+
max_tokens=100,
|
195 |
+
top_p=1,
|
196 |
+
stop='<end>',
|
197 |
+
# If set, partial message deltas will be sent.
|
198 |
+
stream=False,
|
199 |
+
)
|
200 |
+
answer = completion.choices[0].message.content
|
201 |
+
return answer
|
202 |
+
return (chat_completion,)
|
203 |
|
204 |
|
205 |
+
@app.cell
|
206 |
+
def _(reduce):
|
207 |
+
def extract_ner_from_assistant(answer: str) -> list[dict]:
|
208 |
+
# initialize a generator
|
209 |
+
tokens = (token for token in answer.split())
|
210 |
+
# iterate through tokens until <start>
|
211 |
+
for token in tokens:
|
212 |
+
if token == "<start>":
|
213 |
+
break
|
214 |
+
# e.g. ['oral sores (ADE: 0.98)', 'rash (ADE: 0.98)']
|
215 |
+
records = " ".join(list(tokens)).split(",")
|
216 |
+
# clean data
|
217 |
+
result = map(
|
218 |
+
lambda record: reduce(
|
219 |
+
lambda acc, elem: {**acc, **{elem[0]: elem[1]}},
|
220 |
+
zip(
|
221 |
+
["named entity", "tag", "score"],
|
222 |
+
[
|
223 |
+
" ".join(record.split()[:-2]),
|
224 |
+
str(record.split()[-2])[1:-1],
|
225 |
+
float(str(record.split()[-1])[:-1]),
|
226 |
+
],
|
227 |
+
),
|
228 |
+
{}, ## initial value of accumulator
|
229 |
+
),
|
230 |
+
records,
|
231 |
+
)
|
232 |
+
return list(result)
|
233 |
+
return (extract_ner_from_assistant,)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
234 |
|
235 |
|
236 |
if __name__ == "__main__":
|