chris0809 commited on
Commit
b3b1dc5
·
verified ·
1 Parent(s): 12cff86

Add tinyLLM demo

Browse files
Files changed (4) hide show
  1. README.md +19 -7
  2. __pycache__/app.cpython-310.pyc +0 -0
  3. app.py +120 -0
  4. requirements.txt +5 -0
README.md CHANGED
@@ -1,13 +1,25 @@
1
  ---
2
- title: TinyLLM Demo
3
- emoji: ⚡
4
- colorFrom: yellow
5
- colorTo: blue
6
  sdk: gradio
7
- sdk_version: 6.26.0
8
- python_version: '3.12'
9
  app_file: app.py
10
  pinned: false
 
 
 
 
 
 
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
1
  ---
2
+ title: tinyLLM 0.51B Demo
3
+ emoji: 🧩
4
+ colorFrom: blue
5
+ colorTo: indigo
6
  sdk: gradio
7
+ sdk_version: 5.49.1
 
8
  app_file: app.py
9
  pinned: false
10
+ license: other
11
+ models:
12
+ - chris0809/tinyLLM-0.51B-SFT
13
+ - chris0809/tinyLLM-0.51B-ARC-GRPO
14
+ - chris0809/tinyLLM-0.51B-IFEval-OPD
15
+ - chris0809/tinyLLM-0.51B-VLM
16
  ---
17
 
18
+ # tinyLLM 0.51B Demo
19
+
20
+ This CPU Space loads the public tinyLLM SFT model and switches the ARC GRPO or
21
+ IFEval OPD LoRA without keeping multiple base models in memory.
22
+
23
+ The first response is slower because the 0.51B checkpoint is downloaded and
24
+ loaded on demand. The full multimodal demo remains available in the GitHub
25
+ repository; the free CPU Space focuses on text and adapter switching.
__pycache__/app.cpython-310.pyc ADDED
Binary file (3.76 kB). View file
 
app.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Small CPU-friendly Hugging Face Space for the public tinyLLM checkpoints."""
2
+ from __future__ import annotations
3
+
4
+ import threading
5
+
6
+ import gradio as gr
7
+ import torch
8
+ from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
9
+
10
+
11
+ BASE_REPO = "chris0809/tinyLLM-0.51B-SFT"
12
+ ADAPTERS = {
13
+ "SFT base": None,
14
+ "ARC-Easy GRPO": "chris0809/tinyLLM-0.51B-ARC-GRPO",
15
+ "IFEval OPD": "chris0809/tinyLLM-0.51B-IFEval-OPD",
16
+ }
17
+
18
+ _model = None
19
+ _tokenizer = None
20
+ _loaded_adapters: set[str] = set()
21
+ _lock = threading.RLock()
22
+
23
+
24
+ def load_runtime():
25
+ global _model, _tokenizer
26
+ with _lock:
27
+ if _model is None:
28
+ _tokenizer = AutoTokenizer.from_pretrained(BASE_REPO)
29
+ _model = AutoModelForCausalLM.from_pretrained(
30
+ BASE_REPO,
31
+ trust_remote_code=True,
32
+ dtype=torch.float32,
33
+ ).eval()
34
+ return _model, _tokenizer
35
+
36
+
37
+ def select_adapter(model, mode: str) -> None:
38
+ adapter_repo = ADAPTERS[mode]
39
+ if adapter_repo is None:
40
+ model.activate_single_lora(None)
41
+ return
42
+ if adapter_repo not in _loaded_adapters:
43
+ model.load_lora_pretrained(adapter_repo)
44
+ _loaded_adapters.add(adapter_repo)
45
+ else:
46
+ name = "arc" if mode == "ARC-Easy GRPO" else "opd_ifeval_rank64"
47
+ model.activate_single_lora(name)
48
+
49
+
50
+ def respond(message: str, history: list[dict], mode: str, max_new_tokens: int):
51
+ if not message.strip():
52
+ yield "请输入一个问题。"
53
+ return
54
+ with _lock:
55
+ model, tokenizer = load_runtime()
56
+ select_adapter(model, mode)
57
+ messages = [
58
+ {"role": row["role"], "content": row["content"]}
59
+ for row in history
60
+ if row.get("role") in {"user", "assistant"} and isinstance(row.get("content"), str)
61
+ ]
62
+ messages.append({"role": "user", "content": message.strip()})
63
+ prompt = tokenizer.apply_chat_template(
64
+ messages,
65
+ tokenize=False,
66
+ add_generation_prompt=True,
67
+ )
68
+ inputs = tokenizer(prompt, return_tensors="pt")
69
+ streamer = TextIteratorStreamer(
70
+ tokenizer,
71
+ skip_prompt=True,
72
+ skip_special_tokens=True,
73
+ )
74
+ kwargs = {
75
+ **inputs,
76
+ "streamer": streamer,
77
+ "max_new_tokens": int(max_new_tokens),
78
+ "do_sample": False,
79
+ "repetition_penalty": 1.10,
80
+ "no_repeat_ngram_size": 12,
81
+ "use_cache": True,
82
+ }
83
+ worker = threading.Thread(target=model.generate, kwargs=kwargs, daemon=True)
84
+ worker.start()
85
+ text = ""
86
+ for piece in streamer:
87
+ text += piece
88
+ yield text
89
+ worker.join()
90
+
91
+
92
+ with gr.Blocks(title="tinyLLM 0.51B") as demo:
93
+ gr.Markdown(
94
+ "# tinyLLM 0.51B\n"
95
+ "从零训练的中英双语小模型。可在同一个基座上切换 SFT、ARC GRPO 和 IFEval OPD LoRA。"
96
+ )
97
+ with gr.Row():
98
+ mode = gr.Dropdown(list(ADAPTERS), value="SFT base", label="Model / LoRA")
99
+ max_tokens = gr.Slider(32, 512, value=160, step=32, label="Maximum new tokens")
100
+ gr.ChatInterface(
101
+ fn=respond,
102
+ additional_inputs=[mode, max_tokens],
103
+ type="messages",
104
+ examples=[
105
+ ["用两句话介绍一下你自己。", "SFT base", 128],
106
+ ["Which material is attracted to a magnet? A. Wood B. Iron C. Plastic D. Glass", "ARC-Easy GRPO", 96],
107
+ ["Write exactly three bullet points about small language models.", "IFEval OPD", 160],
108
+ ],
109
+ cache_examples=False,
110
+ concurrency_limit=1,
111
+ )
112
+ gr.Markdown(
113
+ "多模态权重已发布在 [tinyLLM-0.51B-VLM]"
114
+ "(https://huggingface.co/chris0809/tinyLLM-0.51B-VLM)。"
115
+ "完整图片演示建议使用 GitHub 中的本地 Demo;免费 CPU Space 主要用于快速验证文本模型和 LoRA。"
116
+ )
117
+
118
+
119
+ if __name__ == "__main__":
120
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ transformers==4.57.1
2
+ huggingface-hub>=0.34.4
3
+ safetensors>=0.4.5
4
+ sentencepiece>=0.2.0
5
+ protobuf>=4.25