AnimeshK509 commited on
Commit
ee2a5a4
Β·
1 Parent(s): 01012b2

Add full dream view with choice blockquotes and txt download

Browse files
Files changed (1) hide show
  1. app.py +80 -0
app.py CHANGED
@@ -199,6 +199,19 @@ hr {
199
  }
200
 
201
  footer { display: none !important; }
 
 
 
 
 
 
 
 
 
 
 
 
 
202
  """
203
 
204
  print("[oneironauts] loading Whisper base...")
@@ -259,9 +272,16 @@ def pick_choice(choice_text, state):
259
  choice_text,
260
  )
261
  print(f"[oneironauts] closing done β€” scene len: {len(closing.get('scene', ''))}, interp len: {len(closing.get('interpretation', ''))}")
 
 
 
 
262
  final_text = (
263
  f"{closing['scene']}\n\n"
264
  f"---\n\n"
 
 
 
265
  f"### Dream journal\n\n"
266
  f"{closing['interpretation']}"
267
  )
@@ -270,6 +290,7 @@ def pick_choice(choice_text, state):
270
  gr.update(value=final_text, visible=True),
271
  "β€”", "β€”", "β€”",
272
  gr.update(visible=False),
 
273
  )
274
 
275
  print(f"[oneironauts] continuing at depth {state['depth']}...")
@@ -286,8 +307,56 @@ def pick_choice(choice_text, state):
286
  gr.update(value=result["scene"], visible=True),
287
  choices[0], choices[1], choices[2],
288
  gr.update(visible=True),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
289
  )
290
 
 
 
 
 
 
 
291
 
292
  def reset_dream():
293
  return (
@@ -296,6 +365,7 @@ def reset_dream():
296
  gr.update(visible=False),
297
  gr.update(value="", visible=False),
298
  "",
 
299
  )
300
 
301
 
@@ -370,6 +440,12 @@ with gr.Blocks(title="Oneironauts") as demo:
370
  outputs=[fragment_box],
371
  )
372
 
 
 
 
 
 
 
373
  for btn in (btn_a, btn_b, btn_c):
374
  btn.click(
375
  pick_choice,
@@ -378,9 +454,12 @@ with gr.Blocks(title="Oneironauts") as demo:
378
  state, scene,
379
  btn_a, btn_b, btn_c,
380
  choices_panel,
 
381
  ],
382
  )
383
 
 
 
384
  reset_btn.click(
385
  reset_dream,
386
  inputs=[],
@@ -388,6 +467,7 @@ with gr.Blocks(title="Oneironauts") as demo:
388
  state,
389
  input_panel, choices_panel,
390
  scene, fragment_box,
 
391
  ],
392
  )
393
 
 
199
  }
200
 
201
  footer { display: none !important; }
202
+ /* choice blockquotes inside the full dream */
203
+ blockquote {
204
+ border-left: 2px solid rgba(190, 180, 210, 0.3) !important;
205
+ margin: 1.5rem 0 !important;
206
+ padding: 0.5rem 0 0.5rem 1.5rem !important;
207
+ color: rgba(180, 170, 195, 0.85) !important;
208
+ font-style: italic !important;
209
+ font-size: 0.98rem !important;
210
+ }
211
+
212
+ blockquote em {
213
+ font-style: italic !important;
214
+ }
215
  """
216
 
217
  print("[oneironauts] loading Whisper base...")
 
272
  choice_text,
273
  )
274
  print(f"[oneironauts] closing done β€” scene len: {len(closing.get('scene', ''))}, interp len: {len(closing.get('interpretation', ''))}")
275
+
276
+ full_dream = build_full_dream(state, closing["scene"])
277
+ dream_file = write_dream_file(state, full_dream, closing["interpretation"])
278
+
279
  final_text = (
280
  f"{closing['scene']}\n\n"
281
  f"---\n\n"
282
+ f"### Your dream\n\n"
283
+ f"{full_dream}\n\n"
284
+ f"---\n\n"
285
  f"### Dream journal\n\n"
286
  f"{closing['interpretation']}"
287
  )
 
290
  gr.update(value=final_text, visible=True),
291
  "β€”", "β€”", "β€”",
292
  gr.update(visible=False),
293
+ gr.update(value=dream_file, visible=True),
294
  )
295
 
296
  print(f"[oneironauts] continuing at depth {state['depth']}...")
 
307
  gr.update(value=result["scene"], visible=True),
308
  choices[0], choices[1], choices[2],
309
  gr.update(visible=True),
310
+ gr.update(),
311
+ )
312
+
313
+ def build_full_dream(state, closing_scene):
314
+ """Reconstruct the whole dream as readable text from history + closing."""
315
+ history = state.get("history", [])
316
+ parts = []
317
+
318
+ # state["history"] alternates: scene, "Chose: X", scene, "Chose: X", ...
319
+ for entry in history:
320
+ if entry.startswith("Chose: "):
321
+ choice = entry[len("Chose: "):]
322
+ parts.append(f"\n> *{choice}*\n")
323
+ else:
324
+ parts.append(entry)
325
+
326
+ parts.append(closing_scene)
327
+ return "\n\n".join(parts)
328
+
329
+
330
+ def write_dream_file(state, full_dream_md, interpretation):
331
+ """Write the dream as a plain-text file the user can download."""
332
+ import tempfile
333
+ from datetime import datetime
334
+
335
+ fragment = state.get("fragment", "")
336
+ timestamp = datetime.now().strftime("%Y-%m-%d")
337
+
338
+ # Reformat markdown blockquotes into plain text
339
+ plain = full_dream_md.replace("> *", "[you chose: ").replace("*\n", "]\n")
340
+
341
+ text = (
342
+ f"ONEIRONAUTS β€” DREAM JOURNAL\n"
343
+ f"{timestamp}\n\n"
344
+ f"FRAGMENT\n"
345
+ f"{fragment}\n\n"
346
+ f"{'-' * 60}\n\n"
347
+ f"THE DREAM\n\n"
348
+ f"{plain}\n\n"
349
+ f"{'-' * 60}\n\n"
350
+ f"REFLECTION\n\n"
351
+ f"{interpretation}\n"
352
  )
353
 
354
+ f = tempfile.NamedTemporaryFile(
355
+ mode="w", suffix=".txt", delete=False, encoding="utf-8"
356
+ )
357
+ f.write(text)
358
+ f.close()
359
+ return f.name
360
 
361
  def reset_dream():
362
  return (
 
365
  gr.update(visible=False),
366
  gr.update(value="", visible=False),
367
  "",
368
+ gr.update(value=None, visible=False),
369
  )
370
 
371
 
 
440
  outputs=[fragment_box],
441
  )
442
 
443
+ download_file = gr.File(
444
+ label="Download your dream",
445
+ visible=False,
446
+ interactive=False,
447
+ )
448
+
449
  for btn in (btn_a, btn_b, btn_c):
450
  btn.click(
451
  pick_choice,
 
454
  state, scene,
455
  btn_a, btn_b, btn_c,
456
  choices_panel,
457
+ download_file,
458
  ],
459
  )
460
 
461
+
462
+
463
  reset_btn.click(
464
  reset_dream,
465
  inputs=[],
 
467
  state,
468
  input_panel, choices_panel,
469
  scene, fragment_box,
470
+ download_file,
471
  ],
472
  )
473