bep40 commited on
Commit
73290fa
·
verified ·
1 Parent(s): ba387fa

Upload patch_frontend_image_gen.py

Browse files
Files changed (1) hide show
  1. patch_frontend_image_gen.py +43 -45
patch_frontend_image_gen.py CHANGED
@@ -8,21 +8,26 @@ composer area instead of starting a chat turn.
8
 
9
  Chat models are untouched: the original onSend() path still runs for them.
10
 
11
- NOTES ON CORRECTNESS (both were real build-breakers found by auditing this
12
- patch against the upstream ChatInput.tsx without executing it):
13
-
14
- 1. `Button` must be added to the MUI import list. The rendered panel uses
15
- <Button size="small">, but upstream ChatInput.tsx imports only
16
- Alert, Box, TextField, IconButton, CircularProgress, Typography, Menu,
17
- MenuItem, ListItemIcon, ListItemText, Chip, LinearProgress, Snackbar and
18
- Tooltip -- and no other frontend patch adds Button. Without this the build
19
- fails with TS2304 "Cannot find name 'Button'".
20
-
21
- 2. The panel is mounted as a PRECEDING SIBLING of the whole
22
- `cond ? (<IconButton/>) : (<IconButton/>)` ternary, not inside either
23
- branch. A ternary branch takes exactly one expression, so putting
24
- {imagePanel} there is a syntax error; and a <Box> panel cannot be a child
25
- of an <IconButton> (that renders a <button>).
 
 
 
 
 
26
  """
27
 
28
  import sys
@@ -36,16 +41,21 @@ if "ming-image-0-1-design" in c:
36
  print("OK: image model already present (idempotent)")
37
  raise SystemExit(0)
38
 
 
 
 
 
 
 
 
39
  # ------------------------------------------------------- 0. MUI Button import
40
- # The panel below renders <Button size="small">. Upstream does not import it,
41
- # so add it next to the other MUI named imports. Idempotent.
42
- if "\n Button,\n" not in c:
43
  import_anchor = " Alert,\n Box,\n"
44
- if import_anchor not in c:
45
- print("FAIL: could not find MUI import anchor (Alert, Box)")
46
- sys.exit(1)
47
  c = c.replace(import_anchor, " Alert,\n Box,\n Button,\n", 1)
48
  print("OK: added Button to MUI imports")
 
 
49
 
50
  # ---------------------------------------------------------------- 1. picker
51
  anchor = (
@@ -62,13 +72,11 @@ image_entry = (
62
  " id: 'ming-image-0-1-design',\n"
63
  " name: 'inclusionai/ming-image-0.1-design (image)',\n"
64
  " modelPath: 'openai/inclusionai/ming-image-0.1-design',\n"
65
- " avatarPath: 'https://huggingface.co/api/avatars/inclusionai',\n"
66
  " recommended: true,\n"
67
  " },\n"
68
- ).replace("avatarPath", "avatarUrl")
69
- if anchor not in c:
70
- print("FAIL: could not find Laguna S 2.1 anchor")
71
- sys.exit(1)
72
  c = c.replace(anchor, anchor + image_entry, 1)
73
  print("OK: added ming-image model to picker")
74
 
@@ -82,9 +90,7 @@ helper = (
82
  "const isImageModelPath = (path: string): boolean => path === IMAGE_MODEL_PATH;\n\n"
83
  )
84
  if "const isImageModelPath" not in c:
85
- if helper_anchor not in c:
86
- print("FAIL: could not find getHfAvatarUrl anchor")
87
- sys.exit(1)
88
  c = c.replace(helper_anchor, helper + helper_anchor, 1)
89
  print("OK: added isImageModelPath helper")
90
 
@@ -97,9 +103,7 @@ state = (
97
  " const [imageError, setImageError] = useState<string | null>(null);\n"
98
  )
99
  if "const [imageBusy" not in c:
100
- if state_anchor not in c:
101
- print("FAIL: could not find datasetUploadProgress state anchor")
102
- sys.exit(1)
103
  c = c.replace(state_anchor, state_anchor + state, 1)
104
  print("OK: added image generation state")
105
 
@@ -155,9 +159,7 @@ new_send = """ // === PATCH: route image models to /api/image/generate ===
155
  setInput('');
156
  }, [input, disabled, isUploadingDataset, imageBusy, onSend, selectedModelPath, generateImage]);"""
157
 
158
- if old_send not in c:
159
- print("FAIL: could not find handleSend block")
160
- sys.exit(1)
161
  c = c.replace(old_send, new_send, 1)
162
  print("OK: patched handleSend to route image models")
163
 
@@ -188,26 +190,22 @@ render = """ // === PATCH: image generation result panel ===
188
 
189
  """
190
  if "image generation result panel" not in c:
 
191
  idx = c.rindex(" return (\n")
192
  c = c[:idx] + render + c[idx:]
193
  print("OK: added image result panel before the main return")
194
 
195
- # Mount the panel just BEFORE the send/stop ternary. It cannot go inside the
196
- # ternary branches: `cond ? (<IconButton/>) : ( <IconButton/> )` takes exactly
197
- # one expression per branch, so `{imagePanel}` there is a syntax error.
198
- # Placing it as a preceding sibling is also outside any <IconButton>, which is
199
- # required since a <Box> panel cannot be a child of a <button>.
200
  jsx_anchor = " {isProcessing ? (\n <IconButton\n onClick={onStop}"
201
  jsx_new = (
202
  " {imagePanel}\n"
203
  " {isProcessing ? (\n"
204
- " <IconButton\n"
205
- " onClick={onStop}"
206
  )
207
  if "{imagePanel}" not in c:
208
- if jsx_anchor not in c:
209
- print("FAIL: could not find send/stop ternary anchor")
210
- sys.exit(1)
211
  c = c.replace(jsx_anchor, jsx_new, 1)
212
  print("OK: mounted image panel before the send/stop ternary")
213
 
 
8
 
9
  Chat models are untouched: the original onSend() path still runs for them.
10
 
11
+ NOTES ON CORRECTNESS (found by auditing this patch against the upstream
12
+ ChatInput.tsx; both were real build-breakers):
13
+
14
+ 1. `Button` must be in the MUI import list. The panel renders
15
+ <Button size="small">, but upstream imports only Alert, Box, TextField,
16
+ IconButton, CircularProgress, Typography, Menu, MenuItem, ListItemIcon,
17
+ ListItemText, Chip, LinearProgress, Snackbar and Tooltip. Without this the
18
+ build fails with TS2304 "Cannot find name 'Button'".
19
+ The guard matches both " Button,\n" and " Button\n" because
20
+ patch_auto_continue_v17.py runs earlier and may already have appended
21
+ `Button` after Tooltip, so this patch must not add a duplicate.
22
+
23
+ 2. The panel mounts as a PRECEDING SIBLING of the whole
24
+ `cond ? (<IconButton/>) : (<IconButton/>)` ternary. A ternary branch takes
25
+ exactly one expression, so `{imagePanel}` inside a branch is a syntax error;
26
+ and a <Box> panel cannot be a child of an <IconButton> (that renders a
27
+ <button>). Indentation is preserved verbatim from the anchor.
28
+
29
+ Every step is fail-loud: a missing anchor exits 1 rather than silently writing
30
+ a partially patched file that still compiles but does the wrong thing.
31
  """
32
 
33
  import sys
 
41
  print("OK: image model already present (idempotent)")
42
  raise SystemExit(0)
43
 
44
+
45
+ def need(cond: bool, msg: str) -> None:
46
+ if not cond:
47
+ print(f"FAIL: {msg}")
48
+ sys.exit(1)
49
+
50
+
51
  # ------------------------------------------------------- 0. MUI Button import
52
+ if "\n Button,\n" not in c and "\n Button\n" not in c:
 
 
53
  import_anchor = " Alert,\n Box,\n"
54
+ need(import_anchor in c, "could not find MUI import anchor (Alert, Box)")
 
 
55
  c = c.replace(import_anchor, " Alert,\n Box,\n Button,\n", 1)
56
  print("OK: added Button to MUI imports")
57
+ else:
58
+ print("OK: Button already imported (by an earlier patch)")
59
 
60
  # ---------------------------------------------------------------- 1. picker
61
  anchor = (
 
72
  " id: 'ming-image-0-1-design',\n"
73
  " name: 'inclusionai/ming-image-0.1-design (image)',\n"
74
  " modelPath: 'openai/inclusionai/ming-image-0.1-design',\n"
75
+ " avatarUrl: 'https://huggingface.co/api/avatars/inclusionai',\n"
76
  " recommended: true,\n"
77
  " },\n"
78
+ )
79
+ need(anchor in c, "could not find Laguna S 2.1 anchor in DEFAULT_MODEL_OPTIONS")
 
 
80
  c = c.replace(anchor, anchor + image_entry, 1)
81
  print("OK: added ming-image model to picker")
82
 
 
90
  "const isImageModelPath = (path: string): boolean => path === IMAGE_MODEL_PATH;\n\n"
91
  )
92
  if "const isImageModelPath" not in c:
93
+ need(helper_anchor in c, "could not find getHfAvatarUrl anchor")
 
 
94
  c = c.replace(helper_anchor, helper + helper_anchor, 1)
95
  print("OK: added isImageModelPath helper")
96
 
 
103
  " const [imageError, setImageError] = useState<string | null>(null);\n"
104
  )
105
  if "const [imageBusy" not in c:
106
+ need(state_anchor in c, "could not find datasetUploadProgress state anchor")
 
 
107
  c = c.replace(state_anchor, state_anchor + state, 1)
108
  print("OK: added image generation state")
109
 
 
159
  setInput('');
160
  }, [input, disabled, isUploadingDataset, imageBusy, onSend, selectedModelPath, generateImage]);"""
161
 
162
+ need(old_send in c, "could not find handleSend block")
 
 
163
  c = c.replace(old_send, new_send, 1)
164
  print("OK: patched handleSend to route image models")
165
 
 
190
 
191
  """
192
  if "image generation result panel" not in c:
193
+ need(" return (\n" in c, "could not find component return")
194
  idx = c.rindex(" return (\n")
195
  c = c[:idx] + render + c[idx:]
196
  print("OK: added image result panel before the main return")
197
 
198
+ # Mount the panel as a preceding sibling of the whole ternary, preserving the
199
+ # anchor's original indentation verbatim.
 
 
 
200
  jsx_anchor = " {isProcessing ? (\n <IconButton\n onClick={onStop}"
201
  jsx_new = (
202
  " {imagePanel}\n"
203
  " {isProcessing ? (\n"
204
+ " <IconButton\n"
205
+ " onClick={onStop}"
206
  )
207
  if "{imagePanel}" not in c:
208
+ need(jsx_anchor in c, "could not find send/stop ternary anchor")
 
 
209
  c = c.replace(jsx_anchor, jsx_new, 1)
210
  print("OK: mounted image panel before the send/stop ternary")
211