prasadpatil25 commited on
Commit
c817c8a
·
verified ·
1 Parent(s): 807633e

Deploy Memory Studio public demo

Browse files
Files changed (5) hide show
  1. README.md +3 -3
  2. app.py +38 -35
  3. local_llm.py +40 -9
  4. memory_studio.py +9 -5
  5. mnemonic_core.py +2 -2
README.md CHANGED
@@ -10,10 +10,10 @@ app_file: app.py
10
  pinned: false
11
  short_description: Editable mnemonic drafts, Major System, ordered stories
12
  models:
13
- - HuggingFaceTB/SmolLM2-360M-Instruct
14
  - segmind/SSD-1B
15
  preload_from_hub:
16
- - HuggingFaceTB/SmolLM2-360M-Instruct
17
  - segmind/SSD-1B unet/diffusion_pytorch_model.fp16.safetensors,text_encoder_2/model.fp16.safetensors,text_encoder/model.fp16.safetensors,vae/diffusion_pytorch_model.fp16.safetensors
18
  ---
19
 
@@ -50,7 +50,7 @@ optional drafts.
50
 
51
  | Model | License | Notes |
52
  |---|---|---|
53
- | `HuggingFaceTB/SmolLM2-360M-Instruct` | Apache-2.0 | Small text model for candidate words, story links, and word cards. Runs on CPU. |
54
  | `segmind/SSD-1B` | Apache-2.0 | SDXL-class text-to-image model for visual drafts, ~4.5 GB of fp16 weights. Runs on ZeroGPU and has no bundled safety checker. |
55
 
56
  Both are public downloads. No paid inference API, token, or account is used by
 
10
  pinned: false
11
  short_description: Editable mnemonic drafts, Major System, ordered stories
12
  models:
13
+ - HuggingFaceTB/SmolLM2-1.7B-Instruct
14
  - segmind/SSD-1B
15
  preload_from_hub:
16
+ - HuggingFaceTB/SmolLM2-1.7B-Instruct
17
  - segmind/SSD-1B unet/diffusion_pytorch_model.fp16.safetensors,text_encoder_2/model.fp16.safetensors,text_encoder/model.fp16.safetensors,vae/diffusion_pytorch_model.fp16.safetensors
18
  ---
19
 
 
50
 
51
  | Model | License | Notes |
52
  |---|---|---|
53
+ | `HuggingFaceTB/SmolLM2-1.7B-Instruct` | Apache-2.0 | Text model for candidate words, story lines, and word cards, about 3.4 GB. Preloaded and run on this Space's free GPU, so nothing is downloaded to your device. |
54
  | `segmind/SSD-1B` | Apache-2.0 | SDXL-class text-to-image model for visual drafts, ~4.5 GB of fp16 weights. Runs on ZeroGPU and has no bundled safety checker. |
55
 
56
  Both are public downloads. No paid inference API, token, or account is used by
app.py CHANGED
@@ -1,35 +1,38 @@
1
- """Hugging Face Spaces entry point for the public Memory Studio demo.
2
-
3
- Locally, run ``memory_studio.py`` instead: it binds to ``127.0.0.1``, creates no
4
- sharing link, and keeps inference on your machine. This file exists only so a
5
- Gradio Space can start the same UI on the Space's public port.
6
-
7
- Deploying this way is a **public demo**: prompts and image cues are sent to the
8
- Space and processed there, so the on-device privacy promise does not apply.
9
- """
10
-
11
- from __future__ import annotations
12
-
13
- import os
14
-
15
- # Spaces sets SPACE_ID, but set the flag explicitly so the public wording also
16
- # applies when this entry point is tested in a container.
17
- os.environ.setdefault("MEMORY_PUBLIC_DEMO", "1")
18
-
19
- # The Space has a free GPU, so it uses a larger image model than the local
20
- # default. Both are public downloads and need no token.
21
- os.environ.setdefault("MEMORY_IMAGE_MODEL", "segmind/SSD-1B")
22
- os.environ.setdefault("MEMORY_IMAGE_DEFAULT_SIZE", "768")
23
- os.environ.setdefault("MEMORY_IMAGE_DEFAULT_STEPS", "8")
24
-
25
- from memory_studio import APP_CSS, APP_THEME, demo # noqa: E402 (env first)
26
-
27
- if __name__ == "__main__":
28
- demo.queue(default_concurrency_limit=1).launch(
29
- server_name=os.getenv("MEMORY_HOST", "0.0.0.0"),
30
- server_port=int(os.getenv("PORT", "7860")),
31
- share=False,
32
- show_error=False,
33
- theme=APP_THEME,
34
- css=APP_CSS,
35
- )
 
 
 
 
1
+ """Hugging Face Spaces entry point for the public Memory Studio demo.
2
+
3
+ Locally, run ``memory_studio.py`` instead: it binds to ``127.0.0.1``, creates no
4
+ sharing link, and keeps inference on your machine. This file exists only so a
5
+ Gradio Space can start the same UI on the Space's public port.
6
+
7
+ Deploying this way is a **public demo**: prompts and image cues are sent to the
8
+ Space and processed there, so the on-device privacy promise does not apply.
9
+ """
10
+
11
+ from __future__ import annotations
12
+
13
+ import os
14
+
15
+ # Spaces sets SPACE_ID, but set the flag explicitly so the public wording also
16
+ # applies when this entry point is tested in a container.
17
+ os.environ.setdefault("MEMORY_PUBLIC_DEMO", "1")
18
+
19
+ # The Space has a free GPU, so it uses a larger image model than the local
20
+ # default. Both are public downloads and need no token.
21
+ os.environ.setdefault("MEMORY_IMAGE_MODEL", "segmind/SSD-1B")
22
+ # The larger text model follows the story-line format far better than the
23
+ # 360M local default, and the Space's free GPU runs it in seconds.
24
+ os.environ.setdefault("MEMORY_MODEL", "HuggingFaceTB/SmolLM2-1.7B-Instruct")
25
+ os.environ.setdefault("MEMORY_IMAGE_DEFAULT_SIZE", "768")
26
+ os.environ.setdefault("MEMORY_IMAGE_DEFAULT_STEPS", "8")
27
+
28
+ from memory_studio import APP_CSS, APP_THEME, demo # noqa: E402 (env first)
29
+
30
+ if __name__ == "__main__":
31
+ demo.queue(default_concurrency_limit=1).launch(
32
+ server_name=os.getenv("MEMORY_HOST", "0.0.0.0"),
33
+ server_port=int(os.getenv("PORT", "7860")),
34
+ share=False,
35
+ show_error=False,
36
+ theme=APP_THEME,
37
+ css=APP_CSS,
38
+ )
local_llm.py CHANGED
@@ -63,20 +63,49 @@ def model_status_markdown(hosted: bool = False) -> str:
63
  "curated words, editable tables, storyboards, and recall prompts all "
64
  "still work. Install the model dependencies only if you want AI drafts."
65
  )
 
 
 
 
 
 
 
66
  return (
67
  f"**Optional AI is available:** `{MODEL_ID}`. Weights are loaded only "
68
  "when you tick an AI-draft checkbox; the first use downloads public weights "
69
- f"and later runs use the cache on {where}. "
70
- + (
71
- "Drafts are processed on this Space's shared servers, not on your "
72
- "device. No API key, account, or paid endpoint is used."
73
- if hosted
74
- else "Inference stays on this computer; no API key, account, or paid "
75
- "endpoint is used."
76
- )
77
  )
78
 
79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  # Gradio's concurrency limit is per event, so two tabs can ask for a draft at
81
  # once. One lock keeps the model from loading twice and serializes generation.
82
  _MODEL_LOCK = threading.RLock()
@@ -161,7 +190,9 @@ def _generate_locked(
161
  max_new_tokens: int,
162
  temperature: float,
163
  ) -> str:
164
- tokenizer, model, device, torch = _load_bundle()
 
 
165
  try:
166
  # Transformers 5 returns a BatchEncoding here; older releases return a
167
  # tensor. Support both and always pass named model inputs.
 
63
  "curated words, editable tables, storyboards, and recall prompts all "
64
  "still work. Install the model dependencies only if you want AI drafts."
65
  )
66
+ if hosted:
67
+ return (
68
+ f"**Optional AI is available:** `{MODEL_ID}` is preloaded on this "
69
+ "Space and runs on its shared free GPU when you tick an AI-draft box. "
70
+ "Nothing is downloaded to your device. Drafts are processed on this "
71
+ "Space's servers; no API key, account, or paid endpoint is used."
72
+ )
73
  return (
74
  f"**Optional AI is available:** `{MODEL_ID}`. Weights are loaded only "
75
  "when you tick an AI-draft checkbox; the first use downloads public weights "
76
+ f"and later runs use the cache on {where}. Inference stays on this "
77
+ "computer; no API key, account, or paid endpoint is used."
 
 
 
 
 
 
78
  )
79
 
80
 
81
+ def warm_up() -> bool:
82
+ """Load the text model before the first request.
83
+
84
+ Only used on a hosted Space, where startup can afford the load and the
85
+ first draft must fit inside one GPU window. Local runs stay lazy.
86
+ """
87
+ try:
88
+ _load_bundle()
89
+ except LocalModelError as exc:
90
+ print(f"Optional text model was not warmed up: {exc.public_message}")
91
+ return False
92
+ return True
93
+
94
+
95
+ def use_zero_gpu(duration: int = 60) -> None:
96
+ """On a ZeroGPU Space, run each generation inside a GPU window.
97
+
98
+ ZeroGPU attaches a GPU only inside functions wrapped by ``spaces.GPU``. This
99
+ does nothing when the ``spaces`` package is not installed (local runs).
100
+ """
101
+ global _generate_locked
102
+ try:
103
+ import spaces
104
+ except Exception:
105
+ return
106
+ _generate_locked = spaces.GPU(duration=duration)(_generate_locked)
107
+
108
+
109
  # Gradio's concurrency limit is per event, so two tabs can ask for a draft at
110
  # once. One lock keeps the model from loading twice and serializes generation.
111
  _MODEL_LOCK = threading.RLock()
 
190
  max_new_tokens: int,
191
  temperature: float,
192
  ) -> str:
193
+ # The caller already holds _MODEL_LOCK. On a ZeroGPU Space this runs in
194
+ # a GPU worker process, so it must not wait on the parent's lock.
195
+ tokenizer, model, device, torch = _load_bundle_once()
196
  try:
197
  # Transformers 5 returns a BatchEncoding here; older releases return a
198
  # tensor. Support both and always pass named model inputs.
memory_studio.py CHANGED
@@ -57,6 +57,7 @@ from local_image import (
57
  image_model_status_markdown,
58
  warm_up as warm_up_image_model,
59
  )
 
60
  from local_llm import (
61
  MODEL_ID,
62
  MODEL_LICENSE_NOTE,
@@ -481,7 +482,7 @@ def _chain_practice_items(
481
  prompt += " Then decode its digits."
482
  answer = f"**{label}**"
483
  if cue:
484
- answer += f" — {cue}"
485
  if code:
486
  answer += f" → `{code}`"
487
  items.append({"prompt": prompt, "answer": answer})
@@ -839,7 +840,7 @@ def _scene_choice_label(index: int, code: str, subject: str, detail: str) -> str
839
  if len(subject) > 32:
840
  subject = subject[:29].rstrip() + "…"
841
  head = " · ".join(part for part in (str(index), code, subject) if part)
842
- return f"{head} — {detail}"
843
 
844
 
845
  def _scene_choices(table: Any, kind: str) -> list[tuple[str, str]]:
@@ -2925,7 +2926,7 @@ with gr.Blocks(
2925
  <div class="ms-note">
2926
  <strong>Cue-writing tips</strong>
2927
  <ul class="ms-muted" style="margin:.4rem 0 0; padding-left:1.1rem;">
2928
- <li>Name one subject, one action, and one place — extra detail
2929
  often gets dropped.</li>
2930
  <li>Make it exaggerated: huge, glowing, floating, or absurd.</li>
2931
  <li>Keep the digits or item order in the story, not in the
@@ -3149,7 +3150,7 @@ with gr.Blocks(
3149
  visual draft, never evidence.
3150
 
3151
  **Can I use my own associations?**
3152
- Yes — that is the point. Every word, cue, palace stop, and card
3153
  field is editable. Personal images usually work better than
3154
  generic ones.
3155
 
@@ -3347,7 +3348,7 @@ with gr.Blocks(
3347
  **No paid API · editable output.**
3348
  **Text model:** `{MODEL_ID}` (optional; no-model mode always available).
3349
  **Image model:** `{IMAGE_MODEL_ID}` (optional; text-only route always available).
3350
- {"**Privacy:** public hosted demo — submissions are processed on this Space's servers, not on your device." if PUBLIC_DEMO else ("**Privacy:** localhost only; " if LOCAL_ONLY else f"**Privacy:** listening on `{LISTEN_HOST}` (your network); ") + "no sharing link; submitted text and image cues are not intentionally logged or saved."}
3351
  """
3352
  )
3353
 
@@ -3355,6 +3356,9 @@ with gr.Blocks(
3355
  # A hosted Space pays the model download during startup, so the first visitor's
3356
  # Draw is fast and fits inside one ZeroGPU window. Local runs stay lazy.
3357
  if PUBLIC_DEMO:
 
 
 
3358
  warm_up_image_model()
3359
 
3360
 
 
57
  image_model_status_markdown,
58
  warm_up as warm_up_image_model,
59
  )
60
+ from local_llm import use_zero_gpu, warm_up as warm_up_text_model
61
  from local_llm import (
62
  MODEL_ID,
63
  MODEL_LICENSE_NOTE,
 
482
  prompt += " Then decode its digits."
483
  answer = f"**{label}**"
484
  if cue:
485
+ answer += f": {cue}"
486
  if code:
487
  answer += f" → `{code}`"
488
  items.append({"prompt": prompt, "answer": answer})
 
840
  if len(subject) > 32:
841
  subject = subject[:29].rstrip() + "…"
842
  head = " · ".join(part for part in (str(index), code, subject) if part)
843
+ return f"{head}: {detail}"
844
 
845
 
846
  def _scene_choices(table: Any, kind: str) -> list[tuple[str, str]]:
 
2926
  <div class="ms-note">
2927
  <strong>Cue-writing tips</strong>
2928
  <ul class="ms-muted" style="margin:.4rem 0 0; padding-left:1.1rem;">
2929
+ <li>Name one subject, one action, and one place; extra detail
2930
  often gets dropped.</li>
2931
  <li>Make it exaggerated: huge, glowing, floating, or absurd.</li>
2932
  <li>Keep the digits or item order in the story, not in the
 
3150
  visual draft, never evidence.
3151
 
3152
  **Can I use my own associations?**
3153
+ Yes, that is the point. Every word, cue, palace stop, and card
3154
  field is editable. Personal images usually work better than
3155
  generic ones.
3156
 
 
3348
  **No paid API · editable output.**
3349
  **Text model:** `{MODEL_ID}` (optional; no-model mode always available).
3350
  **Image model:** `{IMAGE_MODEL_ID}` (optional; text-only route always available).
3351
+ {"**Privacy:** public hosted demo; submissions are processed on this Space's servers, not on your device." if PUBLIC_DEMO else ("**Privacy:** localhost only; " if LOCAL_ONLY else f"**Privacy:** listening on `{LISTEN_HOST}` (your network); ") + "no sharing link; submitted text and image cues are not intentionally logged or saved."}
3352
  """
3353
  )
3354
 
 
3356
  # A hosted Space pays the model download during startup, so the first visitor's
3357
  # Draw is fast and fits inside one ZeroGPU window. Local runs stay lazy.
3358
  if PUBLIC_DEMO:
3359
+ # Text generation needs a GPU window on ZeroGPU, like image drawing.
3360
+ use_zero_gpu()
3361
+ warm_up_text_model()
3362
  warm_up_image_model()
3363
 
3364
 
mnemonic_core.py CHANGED
@@ -976,7 +976,7 @@ def build_anchor_markdown(
976
  )
977
  place_header = "Place"
978
  action_text = "Put this image here and add one sensory detail."
979
- missing_place = "— (add a stop)"
980
  missing_action = "Unplaced: add a familiar stop to keep the order complete."
981
  else:
982
  title = f"### {mode.split(' (')[0]}"
@@ -987,7 +987,7 @@ def build_anchor_markdown(
987
  )
988
  place_header = "Peg"
989
  action_text = "Make the image and the peg collide in one vivid scene."
990
- missing_place = "— (no peg)"
991
  missing_action = (
992
  "No peg past this point: use Major System pegs or a palace route "
993
  "to anchor every image."
 
976
  )
977
  place_header = "Place"
978
  action_text = "Put this image here and add one sensory detail."
979
+ missing_place = "(add a stop)"
980
  missing_action = "Unplaced: add a familiar stop to keep the order complete."
981
  else:
982
  title = f"### {mode.split(' (')[0]}"
 
987
  )
988
  place_header = "Peg"
989
  action_text = "Make the image and the peg collide in one vivid scene."
990
+ missing_place = "(no peg)"
991
  missing_action = (
992
  "No peg past this point: use Major System pegs or a palace route "
993
  "to anchor every image."