7
7
8
8
import marimo
9
9
10
- __generated_with = "0.10.12 "
10
+ __generated_with = "0.10.13 "
11
11
app = marimo .App ()
12
12
13
13
@@ -17,6 +17,48 @@ def _(mo):
17
17
return
18
18
19
19
20
+ @app .cell
21
+ def _ (textwrap , urllib ):
22
+ # Modify this function to load your own examples
23
+ def load_examples ():
24
+ hamlet_url = "https://gist.githubusercontent.com/provpup/2fc41686eab7400b796b/raw/b575bd01a58494dfddc1d6429ef0167e709abf9b/hamlet.txt"
25
+
26
+ with urllib .request .urlopen (hamlet_url ) as f :
27
+ HAMLET = f .read ().decode ("utf-8" )
28
+
29
+ return [
30
+ textwrap .dedent (block ).strip ()[:1000 ]
31
+ for block in HAMLET .split ("\n \n " )
32
+ if block
33
+ ]
34
+
35
+ return (load_examples ,)
36
+
37
+
38
+ @app .cell
39
+ def _ (random ):
40
+ # Replace with your predictor for model A
41
+ def model_a_predictor (text : str ) -> tuple [int , int ]:
42
+ random .seed (len (text ))
43
+ start = random .randint (0 , len (text ) - 2 )
44
+ end = random .randint (start + 1 , len (text ) - 1 )
45
+ return start , end
46
+
47
+ return (model_a_predictor ,)
48
+
49
+
50
+ @app .cell
51
+ def _ (random ):
52
+ # Replace with your predictor for model B
53
+ def model_b_predictor (text : str ) -> tuple [int , int ]:
54
+ random .seed (len (text ) / 2 )
55
+ start = random .randint (0 , len (text ) - 2 )
56
+ end = random .randint (start + 1 , len (text ) - 1 )
57
+ return start , end
58
+
59
+ return (model_b_predictor ,)
60
+
61
+
20
62
@app .cell (hide_code = True )
21
63
def _ (mo ):
22
64
mo .md (
@@ -28,6 +70,12 @@ def _(mo):
28
70
return
29
71
30
72
73
+ @app .cell
74
+ def _ (load_examples ):
75
+ EXAMPLES = load_examples ()
76
+ return (EXAMPLES ,)
77
+
78
+
31
79
@app .cell
32
80
def _ (NUMBER_OF_EXAMPLES , mo ):
33
81
index = mo .ui .number (
@@ -73,28 +121,32 @@ def _(index):
73
121
74
122
@app .cell
75
123
def _ (CHOICES_PATH , get_choices , index , mo , write_choices ):
76
- preference = get_choices ()[index .value ]["model" ]
77
- mo .stop (preference is None , mo .md ("**Choose the better model**." ).center ())
78
- write_choices (get_choices (), CHOICES_PATH )
79
- mo .md (f"You prefer **model { preference } **." ).center ()
80
- return (preference ,)
124
+ def _ ():
125
+ preference = get_choices ()[index .value ]["model" ]
126
+ mo .stop (preference is None , mo .md ("**Choose the better model**." ).center ())
127
+
128
+ write_choices (get_choices (), CHOICES_PATH )
129
+ return mo .md (f"You prefer **model { preference } **." ).center ()
130
+
131
+ _ ()
132
+ return
81
133
82
134
83
135
@app .cell
84
136
def _ (annotate , mo ):
85
137
mo .hstack (
86
138
[
87
- mo . md ( annotate ("Model A" , [0 , len ("Model A" )], "yellow" ) ),
88
- mo . md ( annotate ("Model B" , [0 , len ("Model B" )], "lightblue" ) ),
139
+ annotate ("Model A" , [0 , len ("Model A" )], "yellow" ),
140
+ annotate ("Model B" , [0 , len ("Model B" )], "lightblue" ),
89
141
],
90
142
justify = "space-around" ,
91
143
)
92
144
return
93
145
94
146
95
147
@app .cell
96
- def _ (CHOICES_PATH , PARAGRAPHS , load_choices , mo ):
97
- get_choices , set_choices = mo .state (load_choices (CHOICES_PATH , len (PARAGRAPHS )))
148
+ def _ (CHOICES_PATH , EXAMPLES , load_choices , mo ):
149
+ get_choices , set_choices = mo .state (load_choices (CHOICES_PATH , len (EXAMPLES )))
98
150
return get_choices , set_choices
99
151
100
152
@@ -122,13 +174,13 @@ def _(index, mo, set_choices):
122
174
123
175
124
176
@app .cell
125
- def _ (PARAGRAPHS , SPANS , annotate , index , mo ):
126
- model_A_prediction = mo . md (
127
- annotate ( PARAGRAPHS [ index . value ], SPANS [ index . value ][ 0 ], color = "yellow" )
128
- )
177
+ def _ (EXAMPLES , annotate , index , model_a_predictor , model_b_predictor ):
178
+ _example = EXAMPLES [ index . value ]
179
+
180
+ model_A_prediction = annotate ( _example , model_a_predictor ( _example ), color = "yellow" )
129
181
130
- model_B_prediction = mo . md (
131
- annotate ( PARAGRAPHS [ index . value ], SPANS [ index . value ][ 1 ] , color = "lightblue" )
182
+ model_B_prediction = annotate (
183
+ _example , model_b_predictor ( _example ) , color = "lightblue"
132
184
)
133
185
return model_A_prediction , model_B_prediction
134
186
@@ -174,34 +226,10 @@ def write_choices(choices, path):
174
226
175
227
176
228
@app .cell
177
- def _ (PARAGRAPHS , random ):
178
- random .seed (0 )
179
-
180
- def predict_spans (text ):
181
- first = [random .randint (0 , len (text ) - 2 )]
182
- first .append (random .randint (first [0 ] + 1 , len (text ) - 1 ))
183
- second = [random .randint (0 , len (text ) - 2 )]
184
- second .append (random .randint (second [0 ] + 1 , len (text ) - 1 ))
185
-
186
- return first , second
187
-
188
- SPANS = [predict_spans (p ) for p in PARAGRAPHS ]
189
- return SPANS , predict_spans
190
-
191
-
192
- @app .cell
193
- def _ (HAMLET , textwrap ):
194
- PARAGRAPHS = [
195
- textwrap .dedent (block ).strip ()[:1000 ] for block in HAMLET .split ("\n \n " ) if block
196
- ]
197
- return (PARAGRAPHS ,)
198
-
199
-
200
- @app .cell
201
- def _ ():
229
+ def _ (mo ):
202
230
def annotate (text , span , color ):
203
231
mark_start = f"<mark style='background-color:{ color } '>"
204
- return (
232
+ return mo . md (
205
233
text [: span [0 ]]
206
234
+ mark_start
207
235
+ text [span [0 ] : span [1 ]]
@@ -213,20 +241,11 @@ def annotate(text, span, color):
213
241
214
242
215
243
@app .cell
216
- def _ (PARAGRAPHS ):
217
- NUMBER_OF_EXAMPLES = len (PARAGRAPHS )
244
+ def _ (EXAMPLES ):
245
+ NUMBER_OF_EXAMPLES = len (EXAMPLES )
218
246
return (NUMBER_OF_EXAMPLES ,)
219
247
220
248
221
- @app .cell
222
- def _ (urllib ):
223
- _hamlet_url = "https://gist.githubusercontent.com/provpup/2fc41686eab7400b796b/raw/b575bd01a58494dfddc1d6429ef0167e709abf9b/hamlet.txt"
224
-
225
- with urllib .request .urlopen (_hamlet_url ) as f :
226
- HAMLET = f .read ().decode ("utf-8" )
227
- return HAMLET , f
228
-
229
-
230
249
@app .cell
231
250
def _ ():
232
251
import marimo as mo
0 commit comments