fffiloni commited on
Commit
c964b52
·
verified ·
1 Parent(s): 1f95202

no JSON gradio component

Browse files
Files changed (1) hide show
  1. app.py +28 -78
app.py CHANGED
@@ -1,5 +1,3 @@
1
- # app.py
2
- import json
3
  import logging
4
  import os
5
  import sys
@@ -9,9 +7,6 @@ import uuid
9
  import gradio as gr
10
  from daggr import FnNode, Graph
11
 
12
- # -------------------------
13
- # Logging (stable, no decorators)
14
- # -------------------------
15
  logging.basicConfig(
16
  level=logging.INFO,
17
  format="%(asctime)s | %(levelname)s | %(name)s | %(message)s",
@@ -20,15 +15,6 @@ logging.basicConfig(
20
  log = logging.getLogger("daggr.micro")
21
 
22
 
23
- def safe(obj, limit=1500):
24
- """Dump compact et tolérant (évite que les logs explosent)."""
25
- try:
26
- s = json.dumps(obj, default=str, ensure_ascii=False)
27
- except Exception:
28
- s = f"<unserializable {type(obj).__name__}>"
29
- return s if len(s) <= limit else s[:limit] + "…(truncated)"
30
-
31
-
32
  def make_d(seed_d: int, salt_d: int) -> tuple[int, str]:
33
  run_id = uuid.uuid4().hex[:8]
34
  t0 = time.time()
@@ -39,7 +25,7 @@ def make_d(seed_d: int, salt_d: int) -> tuple[int, str]:
39
  out = (val, tag)
40
 
41
  dt = (time.time() - t0) * 1000
42
- log.info("[D] done run=%s dt=%.1fms out=%s", run_id, dt, safe(out))
43
  return out
44
 
45
 
@@ -53,73 +39,56 @@ def make_e(seed_e: int, salt_e: int) -> tuple[int, str]:
53
  out = (val, tag)
54
 
55
  dt = (time.time() - t0) * 1000
56
- log.info("[E] done run=%s dt=%.1fms out=%s", run_id, dt, safe(out))
57
  return out
58
 
59
 
60
- def combine(d_val: int, d_tag: str, e_val: int, e_tag: str, alpha: float) -> tuple[dict, str]:
61
  run_id = uuid.uuid4().hex[:8]
62
  t0 = time.time()
63
- log.info(
64
- "[A] start run=%s d_val=%s d_tag=%s e_val=%s e_tag=%s alpha=%s",
65
- run_id, d_val, d_tag, e_val, e_tag, alpha,
66
- )
67
-
68
- # Rendre alpha robuste (spaces/gradio peut renvoyer float/str)
69
  a = float(alpha)
70
- out = {
71
- "d": {"val": int(d_val), "tag": str(d_tag)},
72
- "e": {"val": int(e_val), "tag": str(e_tag)},
73
- "alpha": a,
74
- "mix": int((int(d_val) * a) + (int(e_val) * (1.0 - a))),
75
- }
76
- summary = f"mix={out['mix']} | {d_tag} + {e_tag} | alpha={a:.2f}"
77
- res = (out, summary)
78
 
79
  dt = (time.time() - t0) * 1000
80
- log.info("[A] done run=%s dt=%.1fms out=%s", run_id, dt, safe(res))
81
- return res
82
 
83
 
84
- def postprocess(a_json: dict, mode: str, bump: int) -> tuple[str, dict]:
85
  run_id = uuid.uuid4().hex[:8]
86
  t0 = time.time()
87
- log.info("[B] start run=%s mode=%s bump=%s a_json=%s", run_id, mode, bump, safe(a_json))
88
-
89
- mix = int((a_json or {}).get("mix", 0))
90
  bump_i = int(bump)
 
91
 
92
  if mode == "add":
93
- mix2 = mix + bump_i
94
  elif mode == "mul":
95
- mix2 = mix * max(1, bump_i)
96
  else:
97
- mix2 = mix
98
 
99
- text = f"B({mode},{bump_i}) => {mix2} (from mix={mix})"
100
- out = {"mode": str(mode), "bump": bump_i, "mix_in": mix, "mix_out": mix2}
101
- res = (text, out)
102
 
103
  dt = (time.time() - t0) * 1000
104
- log.info("[B] done run=%s dt=%.1fms out=%s", run_id, dt, safe(res))
105
- return res
106
 
107
 
108
- def observe(b_json: dict, note: str) -> str:
109
  run_id = uuid.uuid4().hex[:8]
110
  t0 = time.time()
111
- log.info("[C] start run=%s note=%s b_json=%s", run_id, note, safe(b_json))
112
 
113
- out = f"[C] note={note} | b={b_json}"
114
 
115
  dt = (time.time() - t0) * 1000
116
- log.info("[C] done run=%s dt=%.1fms out=%s", run_id, dt, safe(out))
117
  return out
118
 
119
 
120
- # -------------------------
121
- # Node graph (D/E -> A -> B -> C)
122
- # -------------------------
123
  D = FnNode(
124
  fn=make_d,
125
  inputs={
@@ -147,16 +116,14 @@ E = FnNode(
147
  A = FnNode(
148
  fn=combine,
149
  inputs={
150
- # Multi-upstream (port refs)
151
  "d_val": D.d_val,
152
  "d_tag": D.d_tag,
153
  "e_val": E.e_val,
154
  "e_tag": E.e_tag,
155
- # UI input (forces input snapshot values)
156
  "alpha": gr.Slider(label="A alpha", minimum=0.0, maximum=1.0, step=0.05, value=0.5),
157
  },
158
  outputs={
159
- "a_json": gr.JSON(label="A json"),
160
  "a_summary": gr.Textbox(label="A summary"),
161
  },
162
  )
@@ -164,41 +131,24 @@ A = FnNode(
164
  B = FnNode(
165
  fn=postprocess,
166
  inputs={
167
- "a_json": A.a_json,
168
- # UI inputs independent from D/E (good for snapshot stress)
169
  "mode": gr.Radio(label="B mode", choices=["none", "add", "mul"], value="none"),
170
  "bump": gr.Slider(label="B bump", minimum=0, maximum=5, step=1, value=1),
171
  },
172
- outputs={
173
- "b_text": gr.Textbox(label="B text"),
174
- "b_json": gr.JSON(label="B json"),
175
- },
176
  )
177
 
178
  C = FnNode(
179
  fn=observe,
180
  inputs={
181
- "b_json": B.b_json,
182
  "note": gr.Textbox(label="C note", value="hello"),
183
  },
184
  outputs={"c_text": gr.Textbox(label="C text")},
185
  )
186
 
187
- graph = Graph(
188
- name="micro-restore-debug",
189
- nodes=[D, E, A, B, C],
190
- )
191
 
192
- # -------------------------
193
- # Launch (HF Spaces friendly)
194
- # -------------------------
195
- # Spaces fournit souvent PORT
196
  port = int(os.getenv("PORT", "7860"))
197
-
198
- graph.launch(
199
- host="0.0.0.0",
200
- port=port,
201
- open_browser=False,
202
- log_level="debug",
203
- access_log=True,
204
- )
 
 
 
1
  import logging
2
  import os
3
  import sys
 
7
  import gradio as gr
8
  from daggr import FnNode, Graph
9
 
 
 
 
10
  logging.basicConfig(
11
  level=logging.INFO,
12
  format="%(asctime)s | %(levelname)s | %(name)s | %(message)s",
 
15
  log = logging.getLogger("daggr.micro")
16
 
17
 
 
 
 
 
 
 
 
 
 
18
  def make_d(seed_d: int, salt_d: int) -> tuple[int, str]:
19
  run_id = uuid.uuid4().hex[:8]
20
  t0 = time.time()
 
25
  out = (val, tag)
26
 
27
  dt = (time.time() - t0) * 1000
28
+ log.info("[D] done run=%s dt=%.1fms out=%s", run_id, dt, out)
29
  return out
30
 
31
 
 
39
  out = (val, tag)
40
 
41
  dt = (time.time() - t0) * 1000
42
+ log.info("[E] done run=%s dt=%.1fms out=%s", run_id, dt, out)
43
  return out
44
 
45
 
46
+ def combine(d_val: int, d_tag: str, e_val: int, e_tag: str, alpha: float) -> tuple[int, str]:
47
  run_id = uuid.uuid4().hex[:8]
48
  t0 = time.time()
 
 
 
 
 
 
49
  a = float(alpha)
50
+ log.info("[A] start run=%s d=%s e=%s alpha=%s", run_id, (d_val, d_tag), (e_val, e_tag), a)
51
+
52
+ mix = int((int(d_val) * a) + (int(e_val) * (1.0 - a)))
53
+ summary = f"mix={mix} | {d_tag} + {e_tag} | alpha={a:.2f}"
 
 
 
 
54
 
55
  dt = (time.time() - t0) * 1000
56
+ log.info("[A] done run=%s dt=%.1fms mix=%s", run_id, dt, mix)
57
+ return mix, summary
58
 
59
 
60
+ def postprocess(mix: int, a_summary: str, mode: str, bump: int) -> str:
61
  run_id = uuid.uuid4().hex[:8]
62
  t0 = time.time()
 
 
 
63
  bump_i = int(bump)
64
+ log.info("[B] start run=%s mode=%s bump=%s mix=%s", run_id, mode, bump_i, mix)
65
 
66
  if mode == "add":
67
+ mix2 = int(mix) + bump_i
68
  elif mode == "mul":
69
+ mix2 = int(mix) * max(1, bump_i)
70
  else:
71
+ mix2 = int(mix)
72
 
73
+ text = f"{a_summary} || B({mode},{bump_i}) => {mix2}"
 
 
74
 
75
  dt = (time.time() - t0) * 1000
76
+ log.info("[B] done run=%s dt=%.1fms mix_out=%s", run_id, dt, mix2)
77
+ return text
78
 
79
 
80
+ def observe(b_text: str, note: str) -> str:
81
  run_id = uuid.uuid4().hex[:8]
82
  t0 = time.time()
83
+ log.info("[C] start run=%s note=%s", run_id, note)
84
 
85
+ out = f"[C] note={note} | {b_text}"
86
 
87
  dt = (time.time() - t0) * 1000
88
+ log.info("[C] done run=%s dt=%.1fms", run_id, dt)
89
  return out
90
 
91
 
 
 
 
92
  D = FnNode(
93
  fn=make_d,
94
  inputs={
 
116
  A = FnNode(
117
  fn=combine,
118
  inputs={
 
119
  "d_val": D.d_val,
120
  "d_tag": D.d_tag,
121
  "e_val": E.e_val,
122
  "e_tag": E.e_tag,
 
123
  "alpha": gr.Slider(label="A alpha", minimum=0.0, maximum=1.0, step=0.05, value=0.5),
124
  },
125
  outputs={
126
+ "mix": gr.Number(label="A mix"),
127
  "a_summary": gr.Textbox(label="A summary"),
128
  },
129
  )
 
131
  B = FnNode(
132
  fn=postprocess,
133
  inputs={
134
+ "mix": A.mix,
135
+ "a_summary": A.a_summary,
136
  "mode": gr.Radio(label="B mode", choices=["none", "add", "mul"], value="none"),
137
  "bump": gr.Slider(label="B bump", minimum=0, maximum=5, step=1, value=1),
138
  },
139
+ outputs={"b_text": gr.Textbox(label="B text")},
 
 
 
140
  )
141
 
142
  C = FnNode(
143
  fn=observe,
144
  inputs={
145
+ "b_text": B.b_text,
146
  "note": gr.Textbox(label="C note", value="hello"),
147
  },
148
  outputs={"c_text": gr.Textbox(label="C text")},
149
  )
150
 
151
+ graph = Graph(name="micro-restore-debug-nojson", nodes=[D, E, A, B, C])
 
 
 
152
 
 
 
 
 
153
  port = int(os.getenv("PORT", "7860"))
154
+ graph.launch(host="0.0.0.0", port=port, open_browser=False, log_level="debug", access_log=True)