Spaces:
Runtime error
Runtime error
Delete chan_finetune_colab.ipynb
Browse files- chan_finetune_colab.ipynb +0 -126
chan_finetune_colab.ipynb
DELETED
|
@@ -1,126 +0,0 @@
|
|
| 1 |
-
# 🎯 Well-Tuned — fine-tune & publish your own model (step by step)
|
| 2 |
-
|
| 3 |
-
Goal: publish a small **fine-tuned** model on Hugging Face and use it inside the
|
| 4 |
-
app. We fine-tune **Qwen3-1.7B** on one focused skill the app already performs —
|
| 5 |
-
turning a Chan-theory *raw read* into a crisp long-hold *AI summary* — then plug
|
| 6 |
-
it back in as the Translator sub-agent. This earns **🎯 Well-Tuned** and
|
| 7 |
-
strengthens **🐜 Tiny Titan** (a 1.7B model doing the job well).
|
| 8 |
-
|
| 9 |
-
Everything you need is already in the app and in this `finetune/` folder. Total
|
| 10 |
-
hands-on time ≈ 1 hour, most of it waiting.
|
| 11 |
-
|
| 12 |
-
---
|
| 13 |
-
|
| 14 |
-
## Why this approach (read once)
|
| 15 |
-
|
| 16 |
-
- **Self-sourcing data.** You don't hunt for a dataset — the app *generates* it.
|
| 17 |
-
Each time you run the Signals **AI summary**, the (raw read → narrative) pair
|
| 18 |
-
is saved to `/data/dataset/pairs.jsonl`. A few hundred pairs is plenty.
|
| 19 |
-
- **Single skill, small model.** Narrowing the task lets 1.7B match or beat a
|
| 20 |
-
generic 4B, which is the whole point of the hackathon's "think small".
|
| 21 |
-
- **Honest fit.** The fine-tune does exactly what the app uses it for — judges
|
| 22 |
-
can see the loop close.
|
| 23 |
-
|
| 24 |
-
---
|
| 25 |
-
|
| 26 |
-
## Step 1 — Capture training data (in the app, ~15 min)
|
| 27 |
-
|
| 28 |
-
1. Open the Space → **Signals** → **Run analysis**.
|
| 29 |
-
2. For each ticker: pick it in the dropdown, click **🤖 AI summary**. Each run
|
| 30 |
-
auto-saves one training pair (you'll see them counted on the Model tab).
|
| 31 |
-
3. Vary it: change the ticker pool (large caps, a few volatile names, an ETF),
|
| 32 |
-
re-run analysis, summarize again. Aim for **150–500 pairs**.
|
| 33 |
-
- Tip: the **Auto Research** reports and the **rotation narrative** also
|
| 34 |
-
exercise the models, but only the Signals AI summary is captured for SFT
|
| 35 |
-
(it's the cleanest single-skill pair).
|
| 36 |
-
4. **Model tab → 🎯 Fine-tuning dataset → ⬇ Export dataset (JSONL)**. It writes
|
| 37 |
-
`/data/dataset/chan_sft_<timestamp>.jsonl` and tells you the pair count.
|
| 38 |
-
|
| 39 |
-
### Get the file onto your computer
|
| 40 |
-
The file lives on the Space's `/data` bucket. Easiest way to grab it:
|
| 41 |
-
```bash
|
| 42 |
-
pip install huggingface_hub
|
| 43 |
-
huggingface-cli login # your token
|
| 44 |
-
# list what's in the Space's persistent storage isn't exposed directly, so
|
| 45 |
-
# instead download via the Space's file browser, OR re-export to the repo:
|
| 46 |
-
```
|
| 47 |
-
Simplest reliable path: in the **Model tab** the export prints the full path;
|
| 48 |
-
open the Space's **Files** isn't enough for /data, so use the Space terminal if
|
| 49 |
-
you enabled "Dev mode", **or** temporarily add a `gr.File` download — if you
|
| 50 |
-
want that button, tell me and I'll wire it in. Otherwise the Colab notebook
|
| 51 |
-
also accepts the JSONL via direct upload (Step 2 there).
|
| 52 |
-
|
| 53 |
-
> If you'd rather not fish the file out of `/data`, I can add a one-click
|
| 54 |
-
> **"Download dataset"** button to the Model tab — say the word.
|
| 55 |
-
|
| 56 |
-
---
|
| 57 |
-
|
| 58 |
-
## Step 2 — Train on a free Colab T4 (~25–40 min)
|
| 59 |
-
|
| 60 |
-
1. Go to <https://colab.research.google.com> → **File → Upload notebook** →
|
| 61 |
-
pick `finetune/chan_finetune_colab.ipynb` from this project.
|
| 62 |
-
2. **Runtime → Change runtime type → T4 GPU → Save.**
|
| 63 |
-
3. Run the cells top to bottom:
|
| 64 |
-
- Cell 1 installs Unsloth (fast, low-memory LoRA).
|
| 65 |
-
- Cell 2 asks you to **upload your `chan_sft_*.jsonl`**.
|
| 66 |
-
- Cells 3–5 load Qwen3-1.7B in 4-bit, attach LoRA adapters, and train
|
| 67 |
-
(3 epochs; 10–20 min for a few hundred rows).
|
| 68 |
-
- Cell 6 prints a sample generation so you can eyeball quality.
|
| 69 |
-
4. If the sample looks off, raise `num_train_epochs` to 4–5 and re-run Cell 5.
|
| 70 |
-
|
| 71 |
-
---
|
| 72 |
-
|
| 73 |
-
## Step 3 — Convert to GGUF + publish (~5 min, same notebook)
|
| 74 |
-
|
| 75 |
-
1. Cell 7: `login()` — paste a **write** token (HF → Settings → Access Tokens).
|
| 76 |
-
2. Set `HF_USER = 'ranranrunforit'`. The cell calls
|
| 77 |
-
`model.push_to_hub_gguf(REPO, tokenizer, quantization_method='q8_0')`, which
|
| 78 |
-
merges the LoRA, converts to GGUF via llama.cpp, and uploads to
|
| 79 |
-
`ranranrunforit/chan-compass-qwen3-1.7b-gguf`.
|
| 80 |
-
3. Open the new repo → **Files** → note the exact `.gguf` filename (e.g.
|
| 81 |
-
`chan-compass-qwen3-1.7b-gguf.Q8_0.gguf` — Unsloth's name may differ).
|
| 82 |
-
|
| 83 |
-
---
|
| 84 |
-
|
| 85 |
-
## Step 4 — Wire it into the app (1 line, ~2 min)
|
| 86 |
-
|
| 87 |
-
In `llm_local.py`, find the commented block in `MODEL_ZOO` and edit it to match
|
| 88 |
-
your repo id and the **exact** filename from Step 3:
|
| 89 |
-
|
| 90 |
-
```python
|
| 91 |
-
"Chan-Tuned Qwen3-1.7B · my fine-tune": (
|
| 92 |
-
"ranranrunforit/chan-compass-qwen3-1.7b-gguf",
|
| 93 |
-
"chan-compass-qwen3-1.7b-gguf.Q8_0.gguf"), # ← exact filename
|
| 94 |
-
```
|
| 95 |
-
|
| 96 |
-
Optionally make it the fast sub-agent's default so the app uses it everywhere:
|
| 97 |
-
|
| 98 |
-
```python
|
| 99 |
-
FAST_MODEL = "Chan-Tuned Qwen3-1.7B · my fine-tune"
|
| 100 |
-
```
|
| 101 |
-
|
| 102 |
-
Commit, reboot the Space. The Model tab now lists your fine-tune, and it loads
|
| 103 |
-
through llama.cpp like any other GGUF. **🎯 Well-Tuned achieved.**
|
| 104 |
-
|
| 105 |
-
---
|
| 106 |
-
|
| 107 |
-
## Checklist for the badge
|
| 108 |
-
|
| 109 |
-
- [ ] Dataset exported from the app (`chan_sft_*.jsonl`).
|
| 110 |
-
- [ ] LoRA trained on Qwen3-1.7B (Colab T4).
|
| 111 |
-
- [ ] Merged + converted to GGUF, **published to a public HF repo**.
|
| 112 |
-
- [ ] App's `MODEL_ZOO` points at that repo; it loads and answers.
|
| 113 |
-
- [ ] (Nice) Mention the fine-tune in your demo + blog (`BLOG_DRAFT.md`).
|
| 114 |
-
|
| 115 |
-
## Troubleshooting
|
| 116 |
-
|
| 117 |
-
- **Colab OOM on T4:** keep `load_in_4bit=True`, batch size 2, max_seq_length
|
| 118 |
-
2048 (defaults here). Don't bump LoRA `r` above 32.
|
| 119 |
-
- **`push_to_hub_gguf` slow/fails:** re-run the cell; conversion downloads
|
| 120 |
-
llama.cpp once. If it still fails, save the merged model with
|
| 121 |
-
`model.save_pretrained_merged(...)` and convert locally with
|
| 122 |
-
`llama.cpp/convert_hf_to_gguf.py` (also documented in Unsloth's README).
|
| 123 |
-
- **Model loads but talks oddly:** too few pairs or too many epochs. 200+ pairs,
|
| 124 |
-
3 epochs is the sweet spot. Keep temperature low (the app uses 0.2 here).
|
| 125 |
-
- **Space can't find the file:** the filename in `MODEL_ZOO` must match the repo
|
| 126 |
-
exactly (case-sensitive), and the repo must be public (or set HF_TOKEN).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|