geoffmunn commited on
Commit
c21d3dd
·
verified ·
1 Parent(s): cf9151a

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +161 -112
README.md CHANGED
@@ -2,205 +2,254 @@
2
  base_model: Qwen/Qwen3-4B
3
  library_name: peft
4
  tags:
5
- - base_model:adapter:Qwen/Qwen3-4B
6
- - lora
7
- - transformers
 
 
 
8
  ---
9
 
10
- # Model Card for Model ID
11
-
12
- <!-- Provide a quick summary of what the model is/does. -->
13
-
14
 
 
15
 
16
  ## Model Details
17
 
18
  ### Model Description
19
 
20
- <!-- Provide a longer summary of what this model is. -->
21
-
22
-
23
-
24
- - **Developed by:** [More Information Needed]
25
- - **Funded by [optional]:** [More Information Needed]
26
- - **Shared by [optional]:** [More Information Needed]
27
- - **Model type:** [More Information Needed]
28
- - **Language(s) (NLP):** [More Information Needed]
29
- - **License:** [More Information Needed]
30
- - **Finetuned from model [optional]:** [More Information Needed]
31
 
32
- ### Model Sources [optional]
 
 
 
 
 
33
 
34
- <!-- Provide the basic links for the model. -->
35
 
36
- - **Repository:** [More Information Needed]
37
- - **Paper [optional]:** [More Information Needed]
38
- - **Demo [optional]:** [More Information Needed]
39
 
40
  ## Uses
41
 
42
- <!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
43
-
44
  ### Direct Use
45
 
46
- <!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
47
-
48
- [More Information Needed]
 
 
49
 
50
- ### Downstream Use [optional]
 
51
 
52
- <!-- This section is for the model use when fine-tuned for a task, or when plugged into a larger ecosystem/app -->
53
 
54
- [More Information Needed]
 
 
 
55
 
56
  ### Out-of-Scope Use
57
 
58
- <!-- This section addresses misuse, malicious use, and uses that the model will not work well for. -->
 
 
 
 
 
59
 
60
- [More Information Needed]
61
 
62
  ## Bias, Risks, and Limitations
63
 
64
- <!-- This section is meant to convey both technical and sociotechnical limitations. -->
 
 
 
 
65
 
66
- [More Information Needed]
67
 
68
  ### Recommendations
69
 
70
- <!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->
71
-
72
- Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
73
 
74
  ## How to Get Started with the Model
75
 
76
- Use the code below to get started with the model.
77
-
78
- [More Information Needed]
79
-
80
- ## Training Details
81
-
82
- ### Training Data
83
-
84
- <!-- This should link to a Dataset Card, perhaps with a short stub of information on what the training data is all about as well as documentation related to data pre-processing or additional filtering. -->
85
-
86
- [More Information Needed]
87
-
88
- ### Training Procedure
89
-
90
- <!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
91
 
92
- #### Preprocessing [optional]
 
93
 
94
- [More Information Needed]
95
 
 
 
96
 
97
- #### Training Hyperparameters
 
98
 
99
- - **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
 
 
100
 
101
- #### Speeds, Sizes, Times [optional]
 
102
 
103
- <!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
104
 
105
- [More Information Needed]
 
 
106
 
107
- ## Evaluation
108
 
109
- <!-- This section describes the evaluation protocols and provides the results. -->
 
110
 
111
- ### Testing Data, Factors & Metrics
112
 
113
- #### Testing Data
 
 
 
114
 
115
- <!-- This should link to a Dataset Card if possible. -->
116
 
117
- [More Information Needed]
118
-
119
- #### Factors
120
 
121
- <!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
 
122
 
123
- [More Information Needed]
 
 
 
124
 
125
- #### Metrics
126
 
127
- <!-- These are the evaluation metrics being used, ideally with a description of why. -->
 
 
 
 
 
 
 
 
 
 
 
128
 
129
- [More Information Needed]
130
 
131
- ### Results
 
 
 
132
 
133
- [More Information Needed]
134
 
135
- #### Summary
136
 
 
137
 
 
138
 
139
- ## Model Examination [optional]
140
 
141
- <!-- Relevant interpretability work for the model goes here -->
142
 
143
- [More Information Needed]
 
 
 
 
144
 
145
- ## Environmental Impact
 
 
146
 
147
- <!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
148
 
149
- Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
150
 
151
- - **Hardware Type:** [More Information Needed]
152
- - **Hours used:** [More Information Needed]
153
- - **Cloud Provider:** [More Information Needed]
154
- - **Compute Region:** [More Information Needed]
155
- - **Carbon Emitted:** [More Information Needed]
 
156
 
157
- ## Technical Specifications [optional]
158
 
159
  ### Model Architecture and Objective
160
 
161
- [More Information Needed]
 
 
 
162
 
163
  ### Compute Infrastructure
164
 
165
- [More Information Needed]
166
-
167
  #### Hardware
168
-
169
- [More Information Needed]
170
 
171
  #### Software
172
 
173
- [More Information Needed]
 
 
 
 
174
 
175
- ## Citation [optional]
176
 
177
- <!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
178
 
179
- **BibTeX:**
180
 
181
- [More Information Needed]
 
 
 
 
 
 
 
 
 
182
 
183
- **APA:**
184
 
185
- [More Information Needed]
186
 
187
- ## Glossary [optional]
188
 
189
- <!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
 
 
 
190
 
191
- [More Information Needed]
192
 
193
- ## More Information [optional]
 
194
 
195
- [More Information Needed]
196
 
197
- ## Model Card Authors [optional]
 
 
 
198
 
199
- [More Information Needed]
 
200
 
201
  ## Model Card Contact
202
-
203
- [More Information Needed]
204
- ### Framework versions
205
-
206
- - PEFT 0.18.0
 
2
  base_model: Qwen/Qwen3-4B
3
  library_name: peft
4
  tags:
5
+ - base_model:adapter:Qwen/Qwen3-4B
6
+ - lora
7
+ - transformers
8
+ - text-classification
9
+ - moderation
10
+ - star-trek
11
  ---
12
 
13
+ # Model Card for geoffmunn/Qwen3Guard-StarTrek-Classification-4B
 
 
 
14
 
15
+ This is a fine-tuned version of **Qwen3-4B** using LoRA (Low-Rank Adaptation) to classify whether user-provided text is related to *Star Trek* or not. The model acts as a domain-specific content classifier, returning one of two labels: `"related"` or `"not_related"`. It was developed as part of the Qwen3Guard demonstration project to showcase how large language models can be adapted for custom classification tasks.
16
 
17
  ## Model Details
18
 
19
  ### Model Description
20
 
21
+ This model is a binary sequence classifier fine-tuned on a synthetic dataset of Star Trek-related questions and general non-Star-Trek text. Built atop the Qwen3-4B foundation model, it uses parameter-efficient fine-tuning via LoRA to adapt the model for topic detection in conversational or input text. It is designed for use in moderation systems where filtering based on pop culture topics like *Star Trek* is desired.
 
 
 
 
 
 
 
 
 
 
22
 
23
+ - **Developed by:** Geoff Munn ([@geoffmunn](https://github.com/geoffmunn))
24
+ - **Shared by:** Geoff Munn
25
+ - **Model type:** Causal language model with LoRA adapter for sequence classification
26
+ - **Language(s) (NLP):** English
27
+ - **License:** MIT License (see [GitHub repo](https://github.com/geoffmunn/Qwen3Guard))
28
+ - **Finetuned from model:** Qwen/Qwen3-4B
29
 
30
+ ### Model Sources
31
 
32
+ - **Repository:** [https://github.com/geoffmunn/Qwen3Guard](https://github.com/geoffmunn/Qwen3Guard)
33
+ - **Demo:** Interactive demo available via `star_trek_chat.html` in the repository; requires local API server
 
34
 
35
  ## Uses
36
 
 
 
37
  ### Direct Use
38
 
39
+ The model can directly classify whether a given piece of text is related to *Star Trek*. Example applications include:
40
+ - Filtering fan forum posts
41
+ - Moderating trivia chatbots
42
+ - Enhancing themed AI assistants
43
+ - Educational tools focused on science fiction media
44
 
45
+ Input: A string of text
46
+ Output: One of two labels — `"related"` or `"not_related"`
47
 
48
+ ### Downstream Use
49
 
50
+ This model can be integrated into larger systems such as:
51
+ - Themed conversational agents (e.g., a *Star Trek*-focused chatbot)
52
+ - Content recommendation engines that route queries based on topic relevance
53
+ - Fine-tuning starter for other sci-fi franchises (e.g., *Star Wars*, *Doctor Who*) using similar methodology
54
 
55
  ### Out-of-Scope Use
56
 
57
+ This model should **not** be used for:
58
+ - General content moderation (toxicity, hate speech, etc.)
59
+ - Medical, legal, or safety-critical decision-making
60
+ - Multilingual classification (trained only on English)
61
+ - Detecting nuanced sentiment or emotion
62
+ - Classifying topics outside entertainment/pop culture without retraining
63
 
64
+ It may produce inaccurate classifications when presented with ambiguous references, parody content, or highly technical scientific discussions unrelated to *Star Trek* lore.
65
 
66
  ## Bias, Risks, and Limitations
67
 
68
+ The training data consists entirely of synthetically generated questions about *Star Trek*, which introduces several limitations:
69
+ - Potential overfitting to question formats rather than natural language statements
70
+ - Limited coverage of obscure characters, episodes, or expanded universe material
71
+ - No representation of non-English *Star Trek* content
72
+ - Biases toward canonical series (TOS, TNG, DS9, etc.) over newer entries
73
 
74
+ Additionally, because the dataset was auto-generated using prompts, there may be inconsistencies in labeling or artificial phrasing patterns.
75
 
76
  ### Recommendations
77
 
78
+ Users should validate performance on real-world data before deployment. For production use, consider augmenting the dataset with human-labeled examples and testing across diverse inputs. Always pair this model with broader safeguards if used in public-facing applications.
 
 
79
 
80
  ## How to Get Started with the Model
81
 
82
+ You can load and run inference using Hugging Face Transformers:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
 
84
+ ```python
85
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer
86
 
87
+ model_id = "geoffmunn/Qwen3Guard-StarTrek-Classification-4B"
88
 
89
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
90
+ model = AutoModelForSequenceClassification.from_pretrained(model_id)
91
 
92
+ input_text = "What is the warp core made of in Star Trek?"
93
+ inputs = tokenizer(input_text, return_tensors="pt", truncation=True, max_length=512)
94
 
95
+ outputs = model(**inputs)
96
+ predicted_class_id = outputs.logits.argmax().item()
97
+ label = model.config.id2label[predicted_class_id]
98
 
99
+ print(f"Label: {label}")
100
+ ```
101
 
102
+ Ensure you have the required libraries installed:
103
 
104
+ ```bash
105
+ pip install transformers torch peft
106
+ ```
107
 
108
+ ## Training Details
109
 
110
+ ### Training Data
111
+ The model was trained on a synthetic JSONL dataset containing 2,500 labeled examples of Star Trek-related questions marked as `"related"`, and an equal number of randomly sampled general knowledge questions labeled `"not_related"`. The dataset was generated using the script `generate_star_trek_questions.py` from the repository.
112
 
113
+ Dataset format:
114
 
115
+ ```json
116
+ {"input": "What planet is Spock from?", "label": "related"}
117
+ {"input": "Who wrote 'Pride and Prejudice'?", "label": "not_related"}
118
+ ```
119
 
120
+ Place your dataset at: `finetuning/star_trek/star_trek_guard_dataset.jsonl`
121
 
122
+ ### Training Procedure
 
 
123
 
124
+ #### Preprocessing
125
+ Text inputs were tokenized using the Qwen3 tokenizer with a maximum sequence length of 512 tokens. Inputs longer than this were truncated. Labels were mapped via:
126
 
127
+ ```python
128
+ label2id = {"not_related": 0, "related": 1}
129
+ id2label = {0: "not_related", 1: "related"}
130
+ ```
131
 
132
+ ### Training Hyperparameters
133
 
134
+ - **Training regime:** Mixed precision training (fp16), enabled via Hugging Face Accelerate
135
+ - **Batch size:** 2 (per GPU)
136
+ - **Gradient accumulation steps:** 16 → effective batch size: 32
137
+ - **Number of epochs:** 3
138
+ - **Learning rate:** 2e-4
139
+ - **Optimizer:** AdamW
140
+ - **Max sequence length:** 512
141
+ - **LoRA configuration:**
142
+ - **Rank (r):** 16
143
+ - **Alpha:** 32
144
+ - **Dropout:** 0.05
145
+ - **Target modules:** attention query/value layers and MLP up/down projections
146
 
147
+ ### Speeds, Sizes, Times
148
 
149
+ - **Hardware used:** NVIDIA GPU (assumed: A100 or equivalent)
150
+ - **Training time:** ~2–3 hours depending on hardware
151
+ - **Checkpoint size:** ~3.8 GB (adapter weights only, PEFT format)
152
+ - **Inference memory:** < 10 GB VRAM (with quantization further reduction possible)
153
 
154
+ ## Evaluation
155
 
156
+ ### Testing Data, Factors & Metrics
157
 
158
+ #### Testing Data
159
 
160
+ A 10% holdout test set (~500 samples) was used for evaluation, split from the full dataset during training.
161
 
162
+ #### Factors
163
 
164
+ Evaluation focused on accuracy across:
165
 
166
+ - Canonical vs. obscure Star Trek references
167
+ - Question vs. statement format
168
+ - Length of input text
169
+
170
+ #### Metrics
171
 
172
+ - **Accuracy:** Primary metric
173
+ - **Precision, Recall, F1-score:** Per-class metrics reported during training
174
+ - **Confusion Matrix:** Generated internally during test phase
175
 
176
+ ## Results
177
 
178
+ During final evaluation, the model achieved:
179
 
180
+ - **Accuracy:** ~96–98% (on synthetic test set)
181
+ - Strong precision/recall for "related" class
182
+ - Minor false positives on space/science topics unrelated to Star Trek
183
+
184
+ ## Summary
185
+ The model performs well on its intended task within the scope of the training distribution but may degrade on edge cases or metaphorical references.
186
 
187
+ ## Technical Specifications
188
 
189
  ### Model Architecture and Objective
190
 
191
+ - **Base architecture:** Qwen3-4B (causal decoder-only LLM)
192
+ - **Adaptation method:** LoRA (PEFT)
193
+ - **Task head:** Sequence classification (single-label)
194
+ - **Objective function:** Cross-entropy loss
195
 
196
  ### Compute Infrastructure
197
 
 
 
198
  #### Hardware
199
+ GPU: NVIDIA A100 / RTX 3090 / L40S or equivalent
200
+ RAM: ≥ 32 GB system memory recommended
201
 
202
  #### Software
203
 
204
+ - Python 3.10+
205
+ - PyTorch 2.4+ with CUDA 12.1+
206
+ - Transformers 4.40+
207
+ - PEFT 0.18.0
208
+ - Accelerate, Datasets, Tokenizers
209
 
210
+ ## Citation
211
 
212
+ While no formal paper exists, please cite the GitHub repository if used academically.
213
 
214
+ BibTeX:
215
 
216
+ ```bibtex
217
+ @software{munn_qwen3guard_2025,
218
+ author = {Munn, Geoff},
219
+ title = {Qwen3Guard: Demonstration of Qwen3Guard Models for Content Classification},
220
+ year = {2025},
221
+ publisher = {GitHub},
222
+ journal = {GitHub repository},
223
+ url = {https://github.com/geoffmunn/Qwen3Guard}
224
+ }
225
+ ```
226
 
227
+ ### APA:
228
 
229
+ Munn, G. (2025). Qwen3Guard: Demonstration of Qwen3Guard Models for Content Classification [Software]. GitHub. https://github.com/geoffmunn/Qwen3Guard
230
 
231
+ ## Glossary
232
 
233
+ - **LoRA (Low-Rank Adaptation):** A parameter-efficient fine-tuning technique that adds trainable low-rank matrices to pretrained weights.
234
+ - **PEFT:** Parameter-Efficient Fine-Tuning, a Hugging Face library for lightweight adaptation of large models.
235
+ - **GGUF:** Format used for running models in llama.cpp; not supported for streaming variant here.
236
+ - **JSONL:** JSON Lines format – one JSON object per line.
237
 
238
+ ## More Information
239
 
240
+ For more details, including API server setup and web demos, visit:
241
+ 👉 https://github.com/geoffmunn/Qwen3Guard
242
 
243
+ Includes:
244
 
245
+ - Ollama-compatible scripts
246
+ - Flask-based API server (api_server.py)
247
+ - HTML chat interface (star_trek_chat.html)
248
+ - Dataset generation tools
249
 
250
+ ## Model Card Authors
251
+ Geoff Munn – Developer and maintainer
252
 
253
  ## Model Card Contact
254
+ For questions or feedback, contact the author via GitHub:
255
+ @geoffmunn