alonsosilva commited on
Commit
e978111
·
1 Parent(s): 4e465dd

Input Textarea instead of Text

Browse files
Files changed (1) hide show
  1. app.py +72 -1
app.py CHANGED
@@ -4,12 +4,83 @@ import random
4
  import solara
5
  import torch
6
  import torch.nn.functional as F
 
 
 
 
7
 
8
  from transformers import AutoTokenizer, AutoModelForCausalLM
9
 
10
  tokenizer = AutoTokenizer.from_pretrained('gpt2', padding_side='left')
11
  model = AutoModelForCausalLM.from_pretrained('gpt2')
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  text1 = solara.reactive("""One, two, three, four, mango""")
14
  @solara.component
15
  def Page():
@@ -44,7 +115,7 @@ def Page():
44
  white-space-collapse:preserve;
45
  }
46
  """
47
- solara.InputText("Enter text:", value=text1, continuous_update=True)
48
  if text1.value != "":
49
  with solara.Column():
50
  with solara.Row(gap="0px"):
 
4
  import solara
5
  import torch
6
  import torch.nn.functional as F
7
+ import ipyvue
8
+ import reacton
9
+ from solara.alias import rv as v
10
+ from typing import Any, Callable, Optional, TypeVar, Union, cast, overload
11
 
12
  from transformers import AutoTokenizer, AutoModelForCausalLM
13
 
14
  tokenizer = AutoTokenizer.from_pretrained('gpt2', padding_side='left')
15
  model = AutoModelForCausalLM.from_pretrained('gpt2')
16
 
17
+ def use_change(el: reacton.core.Element, on_value: Callable[[Any], Any], enabled=True):
18
+ """Trigger a callback when a blur events occurs or the enter key is pressed."""
19
+ on_value_ref = solara.use_ref(on_value)
20
+ on_value_ref.current = on_value
21
+
22
+ def add_events():
23
+ def on_change(widget, event, data):
24
+ if enabled:
25
+ on_value_ref.current(widget.v_model)
26
+
27
+ widget = cast(ipyvue.VueWidget, solara.get_widget(el))
28
+ if enabled:
29
+ widget.on_event("blur", on_change)
30
+ widget.on_event("keyup.enter", on_change)
31
+
32
+ def cleanup():
33
+ if enabled:
34
+ widget.on_event("blur", on_change, remove=True)
35
+ widget.on_event("keyup.enter", on_change, remove=True)
36
+
37
+ return cleanup
38
+
39
+ solara.use_effect(add_events, [enabled])
40
+
41
+ @solara.component
42
+ def InputTextarea(
43
+ label: str,
44
+ value: Union[str, solara.Reactive[str]] = "",
45
+ on_value: Callable[[str], None] = None,
46
+ disabled: bool = False,
47
+ password: bool = False,
48
+ continuous_update: bool = False,
49
+ error: Union[bool, str] = False,
50
+ message: Optional[str] = None,
51
+ ):
52
+ reactive_value = solara.use_reactive(value, on_value)
53
+ del value, on_value
54
+
55
+ def set_value_cast(value):
56
+ reactive_value.value = str(value)
57
+
58
+ def on_v_model(value):
59
+ if continuous_update:
60
+ set_value_cast(value)
61
+
62
+ messages = []
63
+ if error and isinstance(error, str):
64
+ messages.append(error)
65
+ elif message:
66
+ messages.append(message)
67
+ text_area = v.Textarea(
68
+ v_model=reactive_value.value,
69
+ on_v_model=on_v_model,
70
+ label=label,
71
+ disabled=disabled,
72
+ type="password" if password else None,
73
+ error=bool(error),
74
+ messages=messages,
75
+ solo=True,
76
+ hide_details=True,
77
+ outlined=True,
78
+ rows=1,
79
+ auto_grow=True,
80
+ )
81
+ use_change(text_area, set_value_cast, enabled=not continuous_update)
82
+ return text_area
83
+
84
  text1 = solara.reactive("""One, two, three, four, mango""")
85
  @solara.component
86
  def Page():
 
115
  white-space-collapse:preserve;
116
  }
117
  """
118
+ InputTextarea("Enter text:", value=text1, continuous_update=True)
119
  if text1.value != "":
120
  with solara.Column():
121
  with solara.Row(gap="0px"):