Upload processor
Browse files- .gitattributes +1 -0
- README.md +199 -0
- chat_template.jinja +237 -0
- midi_tokenizer/tokenization_song2midi.py +200 -0
- midi_tokenizer/tokenizer_config.json +51 -0
- midi_tokenizer/vocab.json +0 -0
- processing_song2midi.py +401 -0
- processor_config.json +23 -0
- tokenizer.json +3 -0
- tokenizer_config.json +41 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
tokenizer.json filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
library_name: transformers
|
| 3 |
+
tags: []
|
| 4 |
+
---
|
| 5 |
+
|
| 6 |
+
# Model Card for Model ID
|
| 7 |
+
|
| 8 |
+
<!-- Provide a quick summary of what the model is/does. -->
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
## Model Details
|
| 13 |
+
|
| 14 |
+
### Model Description
|
| 15 |
+
|
| 16 |
+
<!-- Provide a longer summary of what this model is. -->
|
| 17 |
+
|
| 18 |
+
This is the model card of a 🤗 transformers model that has been pushed on the Hub. This model card has been automatically generated.
|
| 19 |
+
|
| 20 |
+
- **Developed by:** [More Information Needed]
|
| 21 |
+
- **Funded by [optional]:** [More Information Needed]
|
| 22 |
+
- **Shared by [optional]:** [More Information Needed]
|
| 23 |
+
- **Model type:** [More Information Needed]
|
| 24 |
+
- **Language(s) (NLP):** [More Information Needed]
|
| 25 |
+
- **License:** [More Information Needed]
|
| 26 |
+
- **Finetuned from model [optional]:** [More Information Needed]
|
| 27 |
+
|
| 28 |
+
### Model Sources [optional]
|
| 29 |
+
|
| 30 |
+
<!-- Provide the basic links for the model. -->
|
| 31 |
+
|
| 32 |
+
- **Repository:** [More Information Needed]
|
| 33 |
+
- **Paper [optional]:** [More Information Needed]
|
| 34 |
+
- **Demo [optional]:** [More Information Needed]
|
| 35 |
+
|
| 36 |
+
## Uses
|
| 37 |
+
|
| 38 |
+
<!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
|
| 39 |
+
|
| 40 |
+
### Direct Use
|
| 41 |
+
|
| 42 |
+
<!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
|
| 43 |
+
|
| 44 |
+
[More Information Needed]
|
| 45 |
+
|
| 46 |
+
### Downstream Use [optional]
|
| 47 |
+
|
| 48 |
+
<!-- This section is for the model use when fine-tuned for a task, or when plugged into a larger ecosystem/app -->
|
| 49 |
+
|
| 50 |
+
[More Information Needed]
|
| 51 |
+
|
| 52 |
+
### Out-of-Scope Use
|
| 53 |
+
|
| 54 |
+
<!-- This section addresses misuse, malicious use, and uses that the model will not work well for. -->
|
| 55 |
+
|
| 56 |
+
[More Information Needed]
|
| 57 |
+
|
| 58 |
+
## Bias, Risks, and Limitations
|
| 59 |
+
|
| 60 |
+
<!-- This section is meant to convey both technical and sociotechnical limitations. -->
|
| 61 |
+
|
| 62 |
+
[More Information Needed]
|
| 63 |
+
|
| 64 |
+
### Recommendations
|
| 65 |
+
|
| 66 |
+
<!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->
|
| 67 |
+
|
| 68 |
+
Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
|
| 69 |
+
|
| 70 |
+
## How to Get Started with the Model
|
| 71 |
+
|
| 72 |
+
Use the code below to get started with the model.
|
| 73 |
+
|
| 74 |
+
[More Information Needed]
|
| 75 |
+
|
| 76 |
+
## Training Details
|
| 77 |
+
|
| 78 |
+
### Training Data
|
| 79 |
+
|
| 80 |
+
<!-- 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. -->
|
| 81 |
+
|
| 82 |
+
[More Information Needed]
|
| 83 |
+
|
| 84 |
+
### Training Procedure
|
| 85 |
+
|
| 86 |
+
<!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
|
| 87 |
+
|
| 88 |
+
#### Preprocessing [optional]
|
| 89 |
+
|
| 90 |
+
[More Information Needed]
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
#### Training Hyperparameters
|
| 94 |
+
|
| 95 |
+
- **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
|
| 96 |
+
|
| 97 |
+
#### Speeds, Sizes, Times [optional]
|
| 98 |
+
|
| 99 |
+
<!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
|
| 100 |
+
|
| 101 |
+
[More Information Needed]
|
| 102 |
+
|
| 103 |
+
## Evaluation
|
| 104 |
+
|
| 105 |
+
<!-- This section describes the evaluation protocols and provides the results. -->
|
| 106 |
+
|
| 107 |
+
### Testing Data, Factors & Metrics
|
| 108 |
+
|
| 109 |
+
#### Testing Data
|
| 110 |
+
|
| 111 |
+
<!-- This should link to a Dataset Card if possible. -->
|
| 112 |
+
|
| 113 |
+
[More Information Needed]
|
| 114 |
+
|
| 115 |
+
#### Factors
|
| 116 |
+
|
| 117 |
+
<!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
|
| 118 |
+
|
| 119 |
+
[More Information Needed]
|
| 120 |
+
|
| 121 |
+
#### Metrics
|
| 122 |
+
|
| 123 |
+
<!-- These are the evaluation metrics being used, ideally with a description of why. -->
|
| 124 |
+
|
| 125 |
+
[More Information Needed]
|
| 126 |
+
|
| 127 |
+
### Results
|
| 128 |
+
|
| 129 |
+
[More Information Needed]
|
| 130 |
+
|
| 131 |
+
#### Summary
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
## Model Examination [optional]
|
| 136 |
+
|
| 137 |
+
<!-- Relevant interpretability work for the model goes here -->
|
| 138 |
+
|
| 139 |
+
[More Information Needed]
|
| 140 |
+
|
| 141 |
+
## Environmental Impact
|
| 142 |
+
|
| 143 |
+
<!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
|
| 144 |
+
|
| 145 |
+
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).
|
| 146 |
+
|
| 147 |
+
- **Hardware Type:** [More Information Needed]
|
| 148 |
+
- **Hours used:** [More Information Needed]
|
| 149 |
+
- **Cloud Provider:** [More Information Needed]
|
| 150 |
+
- **Compute Region:** [More Information Needed]
|
| 151 |
+
- **Carbon Emitted:** [More Information Needed]
|
| 152 |
+
|
| 153 |
+
## Technical Specifications [optional]
|
| 154 |
+
|
| 155 |
+
### Model Architecture and Objective
|
| 156 |
+
|
| 157 |
+
[More Information Needed]
|
| 158 |
+
|
| 159 |
+
### Compute Infrastructure
|
| 160 |
+
|
| 161 |
+
[More Information Needed]
|
| 162 |
+
|
| 163 |
+
#### Hardware
|
| 164 |
+
|
| 165 |
+
[More Information Needed]
|
| 166 |
+
|
| 167 |
+
#### Software
|
| 168 |
+
|
| 169 |
+
[More Information Needed]
|
| 170 |
+
|
| 171 |
+
## Citation [optional]
|
| 172 |
+
|
| 173 |
+
<!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
|
| 174 |
+
|
| 175 |
+
**BibTeX:**
|
| 176 |
+
|
| 177 |
+
[More Information Needed]
|
| 178 |
+
|
| 179 |
+
**APA:**
|
| 180 |
+
|
| 181 |
+
[More Information Needed]
|
| 182 |
+
|
| 183 |
+
## Glossary [optional]
|
| 184 |
+
|
| 185 |
+
<!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
|
| 186 |
+
|
| 187 |
+
[More Information Needed]
|
| 188 |
+
|
| 189 |
+
## More Information [optional]
|
| 190 |
+
|
| 191 |
+
[More Information Needed]
|
| 192 |
+
|
| 193 |
+
## Model Card Authors [optional]
|
| 194 |
+
|
| 195 |
+
[More Information Needed]
|
| 196 |
+
|
| 197 |
+
## Model Card Contact
|
| 198 |
+
|
| 199 |
+
[More Information Needed]
|
chat_template.jinja
ADDED
|
@@ -0,0 +1,237 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{%- set image_count = namespace(value=0) %}
|
| 2 |
+
{%- set video_count = namespace(value=0) %}
|
| 3 |
+
{%- set midi_count = namespace(value=0) %} {# added midi counter #}
|
| 4 |
+
{%- macro render_content(content, do_vision_count, is_system_content=false) %}
|
| 5 |
+
{%- if content is string %}
|
| 6 |
+
{{- content }}
|
| 7 |
+
{%- elif content is iterable and content is not mapping %}
|
| 8 |
+
{%- for item in content %}
|
| 9 |
+
{%- if 'image' in item or 'image_url' in item or item.type == 'image' %}
|
| 10 |
+
{%- if is_system_content %}
|
| 11 |
+
{{- raise_exception('System message cannot contain images.') }}
|
| 12 |
+
{%- endif %}
|
| 13 |
+
{%- if do_vision_count %}
|
| 14 |
+
{%- set image_count.value = image_count.value + 1 %}
|
| 15 |
+
{%- endif %}
|
| 16 |
+
{%- if add_vision_id %}
|
| 17 |
+
{{- 'Picture ' ~ image_count.value ~ ': ' }}
|
| 18 |
+
{%- endif %}
|
| 19 |
+
{{- '<|vision_start|><|image_pad|><|vision_end|>' }}
|
| 20 |
+
{%- elif 'video' in item or item.type == 'video' %}
|
| 21 |
+
{%- if is_system_content %}
|
| 22 |
+
{{- raise_exception('System message cannot contain videos.') }}
|
| 23 |
+
{%- endif %}
|
| 24 |
+
{%- if do_vision_count %}
|
| 25 |
+
{%- set video_count.value = video_count.value + 1 %}
|
| 26 |
+
{%- endif %}
|
| 27 |
+
{%- if add_vision_id %}
|
| 28 |
+
{{- 'Video ' ~ video_count.value ~ ': ' }}
|
| 29 |
+
{%- endif %}
|
| 30 |
+
{{- '<|vision_start|><|video_pad|><|vision_end|>' }}
|
| 31 |
+
{%- elif 'midi' in item or 'midi_url' in item or item.type == 'midi' %} {# midi handling #}
|
| 32 |
+
{%- if is_system_content %}
|
| 33 |
+
{{- raise_exception('System message cannot contain MIDI content.') }}
|
| 34 |
+
{%- endif %}
|
| 35 |
+
{%- if do_vision_count %}
|
| 36 |
+
{%- set midi_count.value = midi_count.value + 1 %}
|
| 37 |
+
{%- endif %}
|
| 38 |
+
{%- if add_vision_id %}
|
| 39 |
+
{{- 'MIDI ' ~ midi_count.value ~ ': ' }}
|
| 40 |
+
{%- endif %}
|
| 41 |
+
{{- '<|midi_start|><|image_pad|><|midi_end|>' }}
|
| 42 |
+
{%- elif 'text' in item %}
|
| 43 |
+
{{- item.text }}
|
| 44 |
+
{%- else %}
|
| 45 |
+
{{- raise_exception('Unexpected item type in content.') }}
|
| 46 |
+
{%- endif %}
|
| 47 |
+
{%- endfor %}
|
| 48 |
+
{%- elif content is none or content is undefined %}
|
| 49 |
+
{{- '' }}
|
| 50 |
+
{%- else %}
|
| 51 |
+
{{- raise_exception('Unexpected content type.') }}
|
| 52 |
+
{%- endif %}
|
| 53 |
+
{%- endmacro %}
|
| 54 |
+
{%- if not messages %}
|
| 55 |
+
{{- raise_exception('No messages provided.') }}
|
| 56 |
+
{%- endif %}
|
| 57 |
+
{%- if tools and tools is iterable and tools is not mapping %}
|
| 58 |
+
{{- '<|im_start|>system
|
| 59 |
+
' }}
|
| 60 |
+
{{- "# Tools
|
| 61 |
+
|
| 62 |
+
You have access to the following functions:
|
| 63 |
+
|
| 64 |
+
<tools>" }}
|
| 65 |
+
{%- for tool in tools %}
|
| 66 |
+
{{- "
|
| 67 |
+
" }}
|
| 68 |
+
{{- tool | tojson }}
|
| 69 |
+
{%- endfor %}
|
| 70 |
+
{{- "
|
| 71 |
+
</tools>" }}
|
| 72 |
+
{{- '
|
| 73 |
+
|
| 74 |
+
If you choose to call a function ONLY reply in the following format with NO suffix:
|
| 75 |
+
|
| 76 |
+
<tool_call>
|
| 77 |
+
<function=example_function_name>
|
| 78 |
+
<parameter=example_parameter_1>
|
| 79 |
+
value_1
|
| 80 |
+
</parameter>
|
| 81 |
+
<parameter=example_parameter_2>
|
| 82 |
+
This is the value for the second parameter
|
| 83 |
+
that can span
|
| 84 |
+
multiple lines
|
| 85 |
+
</parameter>
|
| 86 |
+
</function>
|
| 87 |
+
</tool_call>
|
| 88 |
+
|
| 89 |
+
<IMPORTANT>
|
| 90 |
+
Reminder:
|
| 91 |
+
- Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> XML tags
|
| 92 |
+
- Required parameters MUST be specified
|
| 93 |
+
- You may provide optional reasoning for your function call in natural language BEFORE the function call, but NOT after
|
| 94 |
+
- If there is no function call available, answer the question like normal with your current knowledge and do not tell the user about function calls
|
| 95 |
+
</IMPORTANT>' }}
|
| 96 |
+
{%- if messages[0].role == 'system' %}
|
| 97 |
+
{%- set content = render_content(messages[0].content, false, true)|trim %}
|
| 98 |
+
{%- if content %}
|
| 99 |
+
{{- '
|
| 100 |
+
|
| 101 |
+
' + content }}
|
| 102 |
+
{%- endif %}
|
| 103 |
+
{%- endif %}
|
| 104 |
+
{{- '<|im_end|>
|
| 105 |
+
' }}
|
| 106 |
+
{%- else %}
|
| 107 |
+
{%- if messages[0].role == 'system' %}
|
| 108 |
+
{%- set content = render_content(messages[0].content, false, true)|trim %}
|
| 109 |
+
{{- '<|im_start|>system
|
| 110 |
+
' + content + '<|im_end|>
|
| 111 |
+
' }}
|
| 112 |
+
{%- endif %}
|
| 113 |
+
{%- endif %}
|
| 114 |
+
{%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
|
| 115 |
+
{%- for message in messages[::-1] %}
|
| 116 |
+
{%- set index = (messages|length - 1) - loop.index0 %}
|
| 117 |
+
{%- if ns.multi_step_tool and message.role == "user" %}
|
| 118 |
+
{%- set content = render_content(message.content, false)|trim %}
|
| 119 |
+
{%- if not(content.startswith('<tool_response>') and content.endswith('</tool_response>')) %}
|
| 120 |
+
{%- set ns.multi_step_tool = false %}
|
| 121 |
+
{%- set ns.last_query_index = index %}
|
| 122 |
+
{%- endif %}
|
| 123 |
+
{%- endif %}
|
| 124 |
+
{%- endfor %}
|
| 125 |
+
{%- if ns.multi_step_tool %}
|
| 126 |
+
{{- raise_exception('No user query found in messages.') }}
|
| 127 |
+
{%- endif %}
|
| 128 |
+
{%- for message in messages %}
|
| 129 |
+
{%- set content = render_content(message.content, true)|trim %}
|
| 130 |
+
{%- if message.role == "system" %}
|
| 131 |
+
{%- if not loop.first %}
|
| 132 |
+
{{- raise_exception('System message must be at the beginning.') }}
|
| 133 |
+
{%- endif %}
|
| 134 |
+
{%- elif message.role == "user" %}
|
| 135 |
+
{{- '<|im_start|>' + message.role + '
|
| 136 |
+
' + content + '<|im_end|>' + '
|
| 137 |
+
' }}
|
| 138 |
+
{%- elif message.role == "assistant" %}
|
| 139 |
+
{%- set reasoning_content = '' %}
|
| 140 |
+
{%- if message.reasoning_content is string %}
|
| 141 |
+
{%- set reasoning_content = message.reasoning_content %}
|
| 142 |
+
{%- else %}
|
| 143 |
+
{%- if '</think>' in content %}
|
| 144 |
+
{%- set reasoning_content = content.split('</think>')[0].rstrip('
|
| 145 |
+
').split('<think>')[-1].lstrip('
|
| 146 |
+
') %}
|
| 147 |
+
{%- set content = content.split('</think>')[-1].lstrip('
|
| 148 |
+
') %}
|
| 149 |
+
{%- endif %}
|
| 150 |
+
{%- endif %}
|
| 151 |
+
{%- set reasoning_content = reasoning_content|trim %}
|
| 152 |
+
{%- if loop.index0 > ns.last_query_index %}
|
| 153 |
+
{{- '<|im_start|>' + message.role + '
|
| 154 |
+
<think>
|
| 155 |
+
' + reasoning_content + '
|
| 156 |
+
</think>
|
| 157 |
+
|
| 158 |
+
' + content }}
|
| 159 |
+
{%- else %}
|
| 160 |
+
{{- '<|im_start|>' + message.role + '
|
| 161 |
+
' + content }}
|
| 162 |
+
{%- endif %}
|
| 163 |
+
{%- if message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping %}
|
| 164 |
+
{%- for tool_call in message.tool_calls %}
|
| 165 |
+
{%- if tool_call.function is defined %}
|
| 166 |
+
{%- set tool_call = tool_call.function %}
|
| 167 |
+
{%- endif %}
|
| 168 |
+
{%- if loop.first %}
|
| 169 |
+
{%- if content|trim %}
|
| 170 |
+
{{- '
|
| 171 |
+
|
| 172 |
+
<tool_call>
|
| 173 |
+
<function=' + tool_call.name + '>
|
| 174 |
+
' }}
|
| 175 |
+
{%- else %}
|
| 176 |
+
{{- '<tool_call>
|
| 177 |
+
<function=' + tool_call.name + '>
|
| 178 |
+
' }}
|
| 179 |
+
{%- endif %}
|
| 180 |
+
{%- else %}
|
| 181 |
+
{{- '
|
| 182 |
+
<tool_call>
|
| 183 |
+
<function=' + tool_call.name + '>
|
| 184 |
+
' }}
|
| 185 |
+
{%- endif %}
|
| 186 |
+
{%- if tool_call.arguments is defined %}
|
| 187 |
+
{%- for args_name, args_value in tool_call.arguments|items %}
|
| 188 |
+
{{- '<parameter=' + args_name + '>
|
| 189 |
+
' }}
|
| 190 |
+
{%- set args_value = args_value | tojson | safe if args_value is mapping or (args_value is sequence and args_value is not string) else args_value | string %}
|
| 191 |
+
{{- args_value }}
|
| 192 |
+
{{- '
|
| 193 |
+
</parameter>
|
| 194 |
+
' }}
|
| 195 |
+
{%- endfor %}
|
| 196 |
+
{%- endif %}
|
| 197 |
+
{{- '</function>
|
| 198 |
+
</tool_call>' }}
|
| 199 |
+
{%- endfor %}
|
| 200 |
+
{%- endif %}
|
| 201 |
+
{{- '<|im_end|>
|
| 202 |
+
' }}
|
| 203 |
+
{%- elif message.role == "tool" %}
|
| 204 |
+
{%- if loop.previtem and loop.previtem.role != "tool" %}
|
| 205 |
+
{{- '<|im_start|>user' }}
|
| 206 |
+
{%- endif %}
|
| 207 |
+
{{- '
|
| 208 |
+
<tool_response>
|
| 209 |
+
' }}
|
| 210 |
+
{{- content }}
|
| 211 |
+
{{- '
|
| 212 |
+
</tool_response>' }}
|
| 213 |
+
{%- if not loop.last and loop.nextitem.role != "tool" %}
|
| 214 |
+
{{- '<|im_end|>
|
| 215 |
+
' }}
|
| 216 |
+
{%- elif loop.last %}
|
| 217 |
+
{{- '<|im_end|>
|
| 218 |
+
' }}
|
| 219 |
+
{%- endif %}
|
| 220 |
+
{%- else %}
|
| 221 |
+
{{- raise_exception('Unexpected message role.') }}
|
| 222 |
+
{%- endif %}
|
| 223 |
+
{%- endfor %}
|
| 224 |
+
{%- if add_generation_prompt %}
|
| 225 |
+
{{- '<|im_start|>assistant
|
| 226 |
+
' }}
|
| 227 |
+
{%- if enable_thinking is defined and enable_thinking is true %}
|
| 228 |
+
{{- '<think>
|
| 229 |
+
' }}
|
| 230 |
+
{%- else %}
|
| 231 |
+
{{- '<think>
|
| 232 |
+
|
| 233 |
+
</think>
|
| 234 |
+
|
| 235 |
+
' }}
|
| 236 |
+
{%- endif %}
|
| 237 |
+
{%- endif %}
|
midi_tokenizer/tokenization_song2midi.py
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
from typing import Union
|
| 4 |
+
|
| 5 |
+
from transformers import BatchEncoding, PythonBackend
|
| 6 |
+
from transformers.tokenization_utils_base import TruncationStrategy
|
| 7 |
+
from transformers.utils.generic import PaddingStrategy, TensorType
|
| 8 |
+
|
| 9 |
+
try:
|
| 10 |
+
from miditok import PerTok, TokSequence
|
| 11 |
+
from symusic import Score
|
| 12 |
+
except ImportError:
|
| 13 |
+
raise ImportError(
|
| 14 |
+
"The `miditok` library is required for processing MIDI files. "
|
| 15 |
+
"Please install it with `pip install miditok`."
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
class Song2MIDIPerTokTokenizer(PythonBackend):
|
| 20 |
+
vocab_files_names = {"vocab_file": "vocab.json"}
|
| 21 |
+
|
| 22 |
+
def __init__(
|
| 23 |
+
self,
|
| 24 |
+
vocab_file: str | os.PathLike | Path,
|
| 25 |
+
unk_token: str = "UNK_None",
|
| 26 |
+
bos_token: str = "BOS_None",
|
| 27 |
+
eos_token: str = "EOS_None",
|
| 28 |
+
pad_token: str = "PAD_None",
|
| 29 |
+
**kwargs,
|
| 30 |
+
):
|
| 31 |
+
self._tokenizer = PerTok(params=vocab_file)
|
| 32 |
+
|
| 33 |
+
self._decoder = {value: key for key, value in self._tokenizer.vocab.items()}
|
| 34 |
+
|
| 35 |
+
super().__init__(
|
| 36 |
+
unk_token=unk_token,
|
| 37 |
+
bos_token=bos_token,
|
| 38 |
+
eos_token=eos_token,
|
| 39 |
+
pad_token=pad_token,
|
| 40 |
+
**kwargs,
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
@property
|
| 44 |
+
def vocab_size(self):
|
| 45 |
+
return len(self._tokenizer)
|
| 46 |
+
|
| 47 |
+
def get_vocab(self):
|
| 48 |
+
return self._tokenizer.vocab
|
| 49 |
+
|
| 50 |
+
def _encode_plus(
|
| 51 |
+
self,
|
| 52 |
+
text: Union["Score", Path, bytes, list[Union["Score", Path, bytes]], list[int]],
|
| 53 |
+
text_pair: Union["Score", Path, list[Union["Score", Path]], list[int], None] = None,
|
| 54 |
+
add_special_tokens: bool = True,
|
| 55 |
+
padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD,
|
| 56 |
+
truncation_strategy: TruncationStrategy = TruncationStrategy.DO_NOT_TRUNCATE,
|
| 57 |
+
max_length: int | None = None,
|
| 58 |
+
stride: int = 0,
|
| 59 |
+
pad_to_multiple_of: int | None = None,
|
| 60 |
+
padding_side: str | None = None,
|
| 61 |
+
return_tensors: str | TensorType | None = None,
|
| 62 |
+
return_token_type_ids: bool | None = None,
|
| 63 |
+
return_attention_mask: bool | None = None,
|
| 64 |
+
return_overflowing_tokens: bool = False,
|
| 65 |
+
return_special_tokens_mask: bool = False,
|
| 66 |
+
return_length: bool = False,
|
| 67 |
+
verbose: bool = True,
|
| 68 |
+
**kwargs,
|
| 69 |
+
): # ty: ignore[invalid-method-override]
|
| 70 |
+
midi = text
|
| 71 |
+
midi_pair = text_pair
|
| 72 |
+
|
| 73 |
+
# From https://github.com/huggingface/transformers/blob/main/src/transformers/tokenization_python.py (v5.3.0)
|
| 74 |
+
is_batched = isinstance(midi, (list, tuple)) and (
|
| 75 |
+
(not midi) or (midi and isinstance(midi[0], (str, Path, Score, bytes)))
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
if is_batched:
|
| 79 |
+
if midi_pair is not None:
|
| 80 |
+
if not isinstance(midi_pair, (list, tuple)) or len(midi_pair) != len(
|
| 81 |
+
midi
|
| 82 |
+
):
|
| 83 |
+
raise ValueError(
|
| 84 |
+
"If `midi` is a batch, `midi_pair` must be a batch of the same length."
|
| 85 |
+
)
|
| 86 |
+
pairs = midi_pair if midi_pair is not None else [None] * len(midi)
|
| 87 |
+
|
| 88 |
+
batch_outputs = {}
|
| 89 |
+
for current_midi, current_pair in zip(midi, pairs):
|
| 90 |
+
current_output = self._encode_plus(
|
| 91 |
+
text=current_midi,
|
| 92 |
+
text_pair=current_pair,
|
| 93 |
+
add_special_tokens=add_special_tokens,
|
| 94 |
+
padding_strategy=PaddingStrategy.DO_NOT_PAD, # we pad in batch afterward
|
| 95 |
+
truncation_strategy=truncation_strategy,
|
| 96 |
+
max_length=max_length,
|
| 97 |
+
stride=stride,
|
| 98 |
+
pad_to_multiple_of=None, # we pad in batch afterward
|
| 99 |
+
padding_side=None, # we pad in batch afterward
|
| 100 |
+
return_tensors=None, # we convert the whole batch to tensors at the end
|
| 101 |
+
return_token_type_ids=return_token_type_ids,
|
| 102 |
+
return_attention_mask=False, # we pad in batch afterward
|
| 103 |
+
return_overflowing_tokens=return_overflowing_tokens,
|
| 104 |
+
return_special_tokens_mask=return_special_tokens_mask,
|
| 105 |
+
return_length=return_length,
|
| 106 |
+
verbose=verbose,
|
| 107 |
+
**kwargs,
|
| 108 |
+
)
|
| 109 |
+
for key, value in current_output.items():
|
| 110 |
+
batch_outputs.setdefault(key, []).append(value)
|
| 111 |
+
|
| 112 |
+
# Remove overflow-related keys before tensor conversion if return_tensors is set
|
| 113 |
+
# Slow tokenizers don't support returning these as tensors
|
| 114 |
+
if return_tensors and return_overflowing_tokens:
|
| 115 |
+
batch_outputs.pop("overflowing_tokens", None)
|
| 116 |
+
batch_outputs.pop("num_truncated_tokens", None)
|
| 117 |
+
|
| 118 |
+
batch_outputs = self.pad(
|
| 119 |
+
batch_outputs,
|
| 120 |
+
padding=padding_strategy.value,
|
| 121 |
+
max_length=max_length,
|
| 122 |
+
pad_to_multiple_of=pad_to_multiple_of,
|
| 123 |
+
padding_side=padding_side,
|
| 124 |
+
return_attention_mask=return_attention_mask,
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
return BatchEncoding(batch_outputs, tensor_type=return_tensors)
|
| 128 |
+
|
| 129 |
+
# Single sequence handling
|
| 130 |
+
def get_input_ids(midi_input):
|
| 131 |
+
if isinstance(midi_input, (str, Path, Score, bytes)):
|
| 132 |
+
if isinstance(midi_input, bytes):
|
| 133 |
+
midi_input = Score.from_midi(midi_input)
|
| 134 |
+
return self._tokenizer.encode(midi_input)[0].ids
|
| 135 |
+
if isinstance(midi_input, (list, tuple)) and midi_input:
|
| 136 |
+
if isinstance(midi_input[0], int):
|
| 137 |
+
return midi_input
|
| 138 |
+
|
| 139 |
+
raise ValueError(
|
| 140 |
+
"Input must be a Score, a path to a MIDI file, or a list of token IDs."
|
| 141 |
+
)
|
| 142 |
+
|
| 143 |
+
first_ids = get_input_ids(midi)
|
| 144 |
+
second_ids = get_input_ids(midi_pair) if midi_pair is not None else None
|
| 145 |
+
|
| 146 |
+
return self.prepare_for_model(
|
| 147 |
+
first_ids,
|
| 148 |
+
pair_ids=second_ids,
|
| 149 |
+
add_special_tokens=add_special_tokens,
|
| 150 |
+
padding=padding_strategy.value,
|
| 151 |
+
truncation=truncation_strategy.value,
|
| 152 |
+
max_length=max_length,
|
| 153 |
+
stride=stride,
|
| 154 |
+
pad_to_multiple_of=pad_to_multiple_of,
|
| 155 |
+
padding_side=padding_side,
|
| 156 |
+
prepend_batch_axis=True,
|
| 157 |
+
return_attention_mask=return_attention_mask,
|
| 158 |
+
return_token_type_ids=return_token_type_ids,
|
| 159 |
+
return_overflowing_tokens=return_overflowing_tokens,
|
| 160 |
+
return_special_tokens_mask=return_special_tokens_mask,
|
| 161 |
+
return_length=return_length,
|
| 162 |
+
verbose=verbose,
|
| 163 |
+
)
|
| 164 |
+
|
| 165 |
+
def _decode(
|
| 166 |
+
self,
|
| 167 |
+
token_ids: int | list[int],
|
| 168 |
+
skip_special_tokens: bool = False,
|
| 169 |
+
clean_up_tokenization_spaces: bool | None = None,
|
| 170 |
+
**kwargs,
|
| 171 |
+
) -> str:
|
| 172 |
+
if isinstance(token_ids, int):
|
| 173 |
+
token_ids = [token_ids]
|
| 174 |
+
|
| 175 |
+
tok_sequence = TokSequence(ids=token_ids, are_ids_encoded=True)
|
| 176 |
+
self._tokenizer.decode_token_ids(tok_sequence)
|
| 177 |
+
|
| 178 |
+
tokens = [self._decoder[token_id] for token_id in tok_sequence.ids]
|
| 179 |
+
|
| 180 |
+
if skip_special_tokens:
|
| 181 |
+
tokens = [
|
| 182 |
+
token for token in tokens if token not in self._tokenizer.special_tokens
|
| 183 |
+
]
|
| 184 |
+
|
| 185 |
+
return " ".join(tokens)
|
| 186 |
+
|
| 187 |
+
def save_vocabulary(
|
| 188 |
+
self, save_directory: str, filename_prefix: str | None = None
|
| 189 |
+
) -> tuple[str, ...]:
|
| 190 |
+
"""Save the MidiTok tokenizer params to disk."""
|
| 191 |
+
if not os.path.isdir(save_directory):
|
| 192 |
+
return ()
|
| 193 |
+
|
| 194 |
+
prefix = f"{filename_prefix}-" if filename_prefix else ""
|
| 195 |
+
vocab_file = os.path.join(save_directory, prefix + "vocab.json")
|
| 196 |
+
|
| 197 |
+
# Use MidiTok's own serialization
|
| 198 |
+
self._tokenizer.save(vocab_file)
|
| 199 |
+
|
| 200 |
+
return (vocab_file,)
|
midi_tokenizer/tokenizer_config.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"added_tokens_decoder": {
|
| 3 |
+
"0": {
|
| 4 |
+
"content": "PAD_None",
|
| 5 |
+
"lstrip": false,
|
| 6 |
+
"normalized": false,
|
| 7 |
+
"rstrip": false,
|
| 8 |
+
"single_word": false,
|
| 9 |
+
"special": true
|
| 10 |
+
},
|
| 11 |
+
"1": {
|
| 12 |
+
"content": "BOS_None",
|
| 13 |
+
"lstrip": false,
|
| 14 |
+
"normalized": false,
|
| 15 |
+
"rstrip": false,
|
| 16 |
+
"single_word": false,
|
| 17 |
+
"special": true
|
| 18 |
+
},
|
| 19 |
+
"2": {
|
| 20 |
+
"content": "EOS_None",
|
| 21 |
+
"lstrip": false,
|
| 22 |
+
"normalized": false,
|
| 23 |
+
"rstrip": false,
|
| 24 |
+
"single_word": false,
|
| 25 |
+
"special": true
|
| 26 |
+
},
|
| 27 |
+
"4": {
|
| 28 |
+
"content": "UNK_None",
|
| 29 |
+
"lstrip": false,
|
| 30 |
+
"normalized": false,
|
| 31 |
+
"rstrip": false,
|
| 32 |
+
"single_word": false,
|
| 33 |
+
"special": true
|
| 34 |
+
}
|
| 35 |
+
},
|
| 36 |
+
"auto_map": {
|
| 37 |
+
"AutoProcessor": "processing_song2midi.Song2MIDIProcessor",
|
| 38 |
+
"AutoTokenizer": [
|
| 39 |
+
"tokenization_song2midi.Song2MIDIPerTokTokenizer",
|
| 40 |
+
null
|
| 41 |
+
]
|
| 42 |
+
},
|
| 43 |
+
"backend": "custom",
|
| 44 |
+
"bos_token": "BOS_None",
|
| 45 |
+
"eos_token": "EOS_None",
|
| 46 |
+
"model_max_length": 1000000000000000019884624838656,
|
| 47 |
+
"pad_token": "PAD_None",
|
| 48 |
+
"processor_class": "Song2MIDIProcessor",
|
| 49 |
+
"tokenizer_class": "Song2MIDIPerTokTokenizer",
|
| 50 |
+
"unk_token": "UNK_None"
|
| 51 |
+
}
|
midi_tokenizer/vocab.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
processing_song2midi.py
ADDED
|
@@ -0,0 +1,401 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import bisect
|
| 2 |
+
from typing import Unpack
|
| 3 |
+
|
| 4 |
+
from transformers import BatchFeature
|
| 5 |
+
from transformers.audio_utils import load_audio
|
| 6 |
+
from transformers.processing_utils import AllKwargsForChatTemplate, ProcessorMixin
|
| 7 |
+
from transformers.utils.chat_template_utils import render_jinja_template
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class Song2MIDIProcessor(ProcessorMixin):
|
| 11 |
+
def __init__(
|
| 12 |
+
self,
|
| 13 |
+
tokenizer,
|
| 14 |
+
midi_tokenizer,
|
| 15 |
+
feature_extractor,
|
| 16 |
+
midi_pad="<|midi_pad|>",
|
| 17 |
+
**kwargs,
|
| 18 |
+
):
|
| 19 |
+
self.midi_offset_by = len(tokenizer)
|
| 20 |
+
self.midi_pad_token = midi_pad
|
| 21 |
+
|
| 22 |
+
super().__init__(tokenizer, midi_tokenizer, feature_extractor, **kwargs)
|
| 23 |
+
|
| 24 |
+
def __call__(
|
| 25 |
+
self, images=None, text=None, videos=None, audio=None, midi=None, **kwargs
|
| 26 |
+
):
|
| 27 |
+
# From https://github.com/huggingface/transformers/blob/e5a861d381bf65a146ce487c3d3c0fca919ef316/src/transformers/processing_utils.py#L606
|
| 28 |
+
if "audios" in kwargs and audio is None:
|
| 29 |
+
raise ValueError(
|
| 30 |
+
"You passed keyword argument `audios` which is deprecated. Please use `audio` instead."
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
if images is None and text is None and videos is None and audio is None and midi is None:
|
| 34 |
+
raise ValueError(
|
| 35 |
+
f"You need to provide at least one input to call {self.__class__.__name__}"
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
kwargs = self._merge_kwargs(
|
| 39 |
+
self.valid_processor_kwargs,
|
| 40 |
+
tokenizer_init_kwargs=self.tokenizer.init_kwargs
|
| 41 |
+
if hasattr(self, "tokenizer")
|
| 42 |
+
else {},
|
| 43 |
+
**kwargs,
|
| 44 |
+
)
|
| 45 |
+
kwargs["midi_kwargs"] = {}
|
| 46 |
+
|
| 47 |
+
# We will do the padding later
|
| 48 |
+
text_kwargs = kwargs.get("text_kwargs", {})
|
| 49 |
+
kwargs["text_kwargs"] = {}
|
| 50 |
+
|
| 51 |
+
attribute_to_kwargs = {
|
| 52 |
+
"tokenizer": (text, "text_kwargs"),
|
| 53 |
+
"image_processor": (images, "images_kwargs"),
|
| 54 |
+
"video_processor": (videos, "videos_kwargs"),
|
| 55 |
+
"feature_extractor": (audio, "audio_kwargs"),
|
| 56 |
+
"midi_tokenizer": (midi, "midi_kwargs"),
|
| 57 |
+
}
|
| 58 |
+
outputs = {}
|
| 59 |
+
for attribute_name in self.get_attributes():
|
| 60 |
+
attribute = getattr(self, attribute_name, None)
|
| 61 |
+
input_data, input_kwargs = attribute_to_kwargs[attribute_name]
|
| 62 |
+
if input_data is not None and attribute is not None:
|
| 63 |
+
attribute_output = attribute(input_data, **kwargs[input_kwargs])
|
| 64 |
+
outputs[attribute_name] = attribute_output
|
| 65 |
+
|
| 66 |
+
midi_token_id = self.tokenizer.convert_tokens_to_ids(self.midi_pad_token)
|
| 67 |
+
|
| 68 |
+
def _merge_text_midi(text_input_ids, midi_input_ids):
|
| 69 |
+
is_batched = True
|
| 70 |
+
if text_input_ids and isinstance(text_input_ids[0], int):
|
| 71 |
+
is_batched = False
|
| 72 |
+
text_input_ids = [text_input_ids]
|
| 73 |
+
midi_input_ids = [midi_input_ids]
|
| 74 |
+
|
| 75 |
+
new_input_ids = []
|
| 76 |
+
midi_idx = 0
|
| 77 |
+
for batch_idx in range(len(text_input_ids)):
|
| 78 |
+
new_ids = []
|
| 79 |
+
for token_id in text_input_ids[batch_idx]:
|
| 80 |
+
if token_id == midi_token_id and midi_idx < len(midi_input_ids):
|
| 81 |
+
new_ids.extend(
|
| 82 |
+
[
|
| 83 |
+
tok + self.midi_offset_by
|
| 84 |
+
for tok in midi_input_ids[midi_idx]
|
| 85 |
+
]
|
| 86 |
+
)
|
| 87 |
+
midi_idx += 1
|
| 88 |
+
else:
|
| 89 |
+
new_ids.append(token_id)
|
| 90 |
+
new_input_ids.append(new_ids)
|
| 91 |
+
|
| 92 |
+
return new_input_ids if is_batched else new_input_ids[0]
|
| 93 |
+
|
| 94 |
+
new_outputs = {}
|
| 95 |
+
if midi:
|
| 96 |
+
new_text_input_ids = {
|
| 97 |
+
"input_ids": _merge_text_midi(
|
| 98 |
+
outputs["tokenizer"]["input_ids"],
|
| 99 |
+
outputs["midi_tokenizer"]["input_ids"],
|
| 100 |
+
)
|
| 101 |
+
}
|
| 102 |
+
else:
|
| 103 |
+
new_text_input_ids = {"input_ids": outputs["tokenizer"]["input_ids"]}
|
| 104 |
+
|
| 105 |
+
# Pad
|
| 106 |
+
new_outputs.update(self.tokenizer.pad(new_text_input_ids, **text_kwargs))
|
| 107 |
+
|
| 108 |
+
for key, value in outputs.items():
|
| 109 |
+
if key not in ["tokenizer", "midi_tokenizer"]:
|
| 110 |
+
new_outputs.update(value)
|
| 111 |
+
|
| 112 |
+
return BatchFeature(new_outputs)
|
| 113 |
+
|
| 114 |
+
def apply_chat_template(
|
| 115 |
+
self,
|
| 116 |
+
conversation: list[dict[str, str]] | list[list[dict[str, str]]],
|
| 117 |
+
chat_template: str | None = None,
|
| 118 |
+
**kwargs: Unpack[AllKwargsForChatTemplate],
|
| 119 |
+
) -> str:
|
| 120 |
+
# From https://github.com/huggingface/transformers/blob/e5a861d381bf65a146ce487c3d3c0fca919ef316/src/transformers/processing_utils.py#L1631
|
| 121 |
+
if chat_template is None:
|
| 122 |
+
if isinstance(self.chat_template, dict) and "default" in self.chat_template:
|
| 123 |
+
chat_template = self.chat_template["default"]
|
| 124 |
+
elif isinstance(self.chat_template, dict):
|
| 125 |
+
raise ValueError(
|
| 126 |
+
'The processor has multiple chat templates but none of them are named "default". You need to specify'
|
| 127 |
+
" which one to use by passing the `chat_template` argument. Available templates are: "
|
| 128 |
+
f"{', '.join(self.chat_template.keys())}"
|
| 129 |
+
)
|
| 130 |
+
elif self.chat_template is not None:
|
| 131 |
+
chat_template = self.chat_template
|
| 132 |
+
else:
|
| 133 |
+
raise ValueError(
|
| 134 |
+
"Cannot use apply_chat_template because this processor does not have a chat template."
|
| 135 |
+
)
|
| 136 |
+
else:
|
| 137 |
+
if (
|
| 138 |
+
isinstance(self.chat_template, dict)
|
| 139 |
+
and chat_template in self.chat_template
|
| 140 |
+
):
|
| 141 |
+
# It's the name of a template, not a full template string
|
| 142 |
+
chat_template = self.chat_template[chat_template]
|
| 143 |
+
else:
|
| 144 |
+
# It's a template string, render it directly
|
| 145 |
+
pass
|
| 146 |
+
|
| 147 |
+
# Check if tokenizer is fast - use backend attribute if available, otherwise fall back to class name
|
| 148 |
+
is_tokenizers_fast = False
|
| 149 |
+
if hasattr(self, "tokenizer"):
|
| 150 |
+
if hasattr(self.tokenizer, "backend"):
|
| 151 |
+
is_tokenizers_fast = self.tokenizer.backend == "tokenizers"
|
| 152 |
+
else:
|
| 153 |
+
# Fallback to class name check
|
| 154 |
+
is_tokenizers_fast = self.tokenizer.__class__.__name__.endswith("Fast")
|
| 155 |
+
|
| 156 |
+
if kwargs.get("continue_final_message", False):
|
| 157 |
+
if kwargs.get("add_generation_prompt", False):
|
| 158 |
+
raise ValueError(
|
| 159 |
+
"continue_final_message and add_generation_prompt are not compatible. Use continue_final_message when you want the model to continue the final message, and add_generation_prompt when you want to add a header that will prompt it to start a new assistant message instead."
|
| 160 |
+
)
|
| 161 |
+
if kwargs.get("return_assistant_tokens_mask", False):
|
| 162 |
+
raise ValueError(
|
| 163 |
+
"continue_final_message is not compatible with return_assistant_tokens_mask."
|
| 164 |
+
)
|
| 165 |
+
|
| 166 |
+
if kwargs.get("return_assistant_tokens_mask", False):
|
| 167 |
+
if not is_tokenizers_fast:
|
| 168 |
+
raise ValueError(
|
| 169 |
+
"`return_assistant_tokens_mask` is not possible with slow tokenizers. Make sure you have `tokenizers` installed. "
|
| 170 |
+
"If the error persists, open an issue to support a Fast tokenizer for your model."
|
| 171 |
+
)
|
| 172 |
+
else:
|
| 173 |
+
kwargs["return_offsets_mapping"] = (
|
| 174 |
+
True # force offset mapping so we can infer token boundaries
|
| 175 |
+
)
|
| 176 |
+
|
| 177 |
+
# Fill sets of kwargs that should be used by jinja template, filtering out kwargs used in `processor.__call__`
|
| 178 |
+
# NOTE: we don't only filter but also set the default values here. Without default values, we can remove it
|
| 179 |
+
template_kwargs = {}
|
| 180 |
+
for key in AllKwargsForChatTemplate.__annotations__[
|
| 181 |
+
"template_kwargs"
|
| 182 |
+
].__annotations__:
|
| 183 |
+
kwarg_type_defaults = AllKwargsForChatTemplate.__annotations__[
|
| 184 |
+
"template_kwargs"
|
| 185 |
+
]
|
| 186 |
+
default_value = getattr(kwarg_type_defaults, key, None)
|
| 187 |
+
value = kwargs.pop(key, default_value)
|
| 188 |
+
if value is not None and not isinstance(value, dict):
|
| 189 |
+
template_kwargs[key] = value
|
| 190 |
+
|
| 191 |
+
# Pass unprocessed custom kwargs
|
| 192 |
+
template_kwargs.update(kwargs)
|
| 193 |
+
|
| 194 |
+
# Set the sampling rate to load the audio files if user hasn't already passed with `kwargs`
|
| 195 |
+
if "sampling_rate" not in template_kwargs:
|
| 196 |
+
if hasattr(self, "feature_extractor") and hasattr(
|
| 197 |
+
self.feature_extractor, "sampling_rate"
|
| 198 |
+
):
|
| 199 |
+
template_kwargs["sampling_rate"] = self.feature_extractor.sampling_rate
|
| 200 |
+
else:
|
| 201 |
+
template_kwargs["sampling_rate"] = 16_000
|
| 202 |
+
|
| 203 |
+
if isinstance(conversation, (list, tuple)) and (
|
| 204 |
+
isinstance(conversation[0], (list, tuple))
|
| 205 |
+
or hasattr(conversation[0], "content")
|
| 206 |
+
):
|
| 207 |
+
is_batched = True
|
| 208 |
+
conversations = conversation
|
| 209 |
+
else:
|
| 210 |
+
is_batched = False
|
| 211 |
+
conversations = [conversation]
|
| 212 |
+
|
| 213 |
+
# Normalize OpenAI-style "image_url" content blocks to HuggingFace-style "image" blocks
|
| 214 |
+
# OpenAI format: {"type": "image_url", "image_url": {"url": "..."}}
|
| 215 |
+
# HuggingFace format: {"type": "image", "url": "..."}
|
| 216 |
+
for conversation_idx, conversation in enumerate(conversations):
|
| 217 |
+
for message in conversation:
|
| 218 |
+
if not isinstance(message.get("content"), list):
|
| 219 |
+
continue
|
| 220 |
+
new_content = []
|
| 221 |
+
for content in message["content"]:
|
| 222 |
+
if (
|
| 223 |
+
isinstance(content, dict)
|
| 224 |
+
and content.get("type") == "image_url"
|
| 225 |
+
and "image_url" in content
|
| 226 |
+
):
|
| 227 |
+
image_url_info = content["image_url"]
|
| 228 |
+
url = (
|
| 229 |
+
image_url_info.get("url", "")
|
| 230 |
+
if isinstance(image_url_info, dict)
|
| 231 |
+
else image_url_info
|
| 232 |
+
)
|
| 233 |
+
new_content.append({"type": "image", "url": url})
|
| 234 |
+
else:
|
| 235 |
+
new_content.append(content)
|
| 236 |
+
message["content"] = new_content
|
| 237 |
+
|
| 238 |
+
tokenize = template_kwargs.pop("tokenize", False)
|
| 239 |
+
return_dict = template_kwargs.pop("return_dict", True)
|
| 240 |
+
|
| 241 |
+
if tokenize:
|
| 242 |
+
batch_images, batch_videos = [], []
|
| 243 |
+
batch_audios = []
|
| 244 |
+
batch_midis = [] # midi
|
| 245 |
+
for conversation in conversations:
|
| 246 |
+
images, videos = [], []
|
| 247 |
+
for message in conversation:
|
| 248 |
+
visuals = [
|
| 249 |
+
content
|
| 250 |
+
for content in message["content"]
|
| 251 |
+
if content["type"] in ["image", "video"]
|
| 252 |
+
]
|
| 253 |
+
audio_fnames = [
|
| 254 |
+
content[key]
|
| 255 |
+
for content in message["content"]
|
| 256 |
+
for key in ["audio", "url", "path"]
|
| 257 |
+
if key in content and content["type"] == "audio"
|
| 258 |
+
]
|
| 259 |
+
image_fnames = [
|
| 260 |
+
vision_info[key]
|
| 261 |
+
for vision_info in visuals
|
| 262 |
+
for key in ["image", "url", "path", "base64"]
|
| 263 |
+
if key in vision_info and vision_info["type"] == "image"
|
| 264 |
+
]
|
| 265 |
+
images.extend(image_fnames)
|
| 266 |
+
video_fnames = [
|
| 267 |
+
vision_info[key]
|
| 268 |
+
for vision_info in visuals
|
| 269 |
+
for key in ["video", "url", "path"]
|
| 270 |
+
if key in vision_info and vision_info["type"] == "video"
|
| 271 |
+
]
|
| 272 |
+
videos.extend(video_fnames)
|
| 273 |
+
|
| 274 |
+
# midi
|
| 275 |
+
midi_fnames = [
|
| 276 |
+
content[key]
|
| 277 |
+
for content in message["content"]
|
| 278 |
+
for key in ["score", "path"]
|
| 279 |
+
if key in content and content["type"] == "midi"
|
| 280 |
+
]
|
| 281 |
+
batch_midis.extend(midi_fnames)
|
| 282 |
+
|
| 283 |
+
# Audio models do not accept nested list of audios (yet!) so we construct a flat input audio list
|
| 284 |
+
if not template_kwargs["load_audio_from_video"]:
|
| 285 |
+
for fname in audio_fnames:
|
| 286 |
+
batch_audios.append(
|
| 287 |
+
load_audio(
|
| 288 |
+
fname,
|
| 289 |
+
sampling_rate=template_kwargs["sampling_rate"],
|
| 290 |
+
)
|
| 291 |
+
)
|
| 292 |
+
else:
|
| 293 |
+
for fname in video_fnames:
|
| 294 |
+
batch_audios.append(
|
| 295 |
+
load_audio(
|
| 296 |
+
fname,
|
| 297 |
+
sampling_rate=template_kwargs["sampling_rate"],
|
| 298 |
+
)
|
| 299 |
+
)
|
| 300 |
+
|
| 301 |
+
# Currently all processors can accept nested list of batches, but not flat list of visuals
|
| 302 |
+
# So we'll make a batched list of images and let the processor handle it
|
| 303 |
+
batch_images.append(images)
|
| 304 |
+
batch_videos.append(videos)
|
| 305 |
+
|
| 306 |
+
special_tokens_map = {}
|
| 307 |
+
if hasattr(self, "tokenizer") and hasattr(self.tokenizer, "special_tokens_map"):
|
| 308 |
+
special_tokens = self.tokenizer.special_tokens_map
|
| 309 |
+
# Filter out tokens that conflict with template kwargs
|
| 310 |
+
special_tokens_map = {
|
| 311 |
+
k: v for k, v in special_tokens.items() if k not in template_kwargs
|
| 312 |
+
}
|
| 313 |
+
|
| 314 |
+
prompt, generation_indices = render_jinja_template(
|
| 315 |
+
conversations=conversations,
|
| 316 |
+
chat_template=chat_template,
|
| 317 |
+
**template_kwargs, # different flags such as `return_assistant_mask`
|
| 318 |
+
**special_tokens_map, # tokenizer special tokens are used by some templates
|
| 319 |
+
)
|
| 320 |
+
|
| 321 |
+
if not is_batched:
|
| 322 |
+
prompt = prompt[0]
|
| 323 |
+
|
| 324 |
+
if tokenize:
|
| 325 |
+
# Tokenizer's `apply_chat_template` never adds special tokens when tokenizing
|
| 326 |
+
# But processor's `apply_chat_template` didn't have an option to tokenize, so users had to format the prompt
|
| 327 |
+
# and pass it to the processor. Users thus never worried about special tokens relying on processor handling
|
| 328 |
+
# everything internally. The below line is to keep BC for that and be able to work with model that have
|
| 329 |
+
# special tokens in the template (consistent with tokenizers). We dont want to raise warning, it will flood command line
|
| 330 |
+
# without actionable solution for users
|
| 331 |
+
single_prompt = prompt[0] if is_batched else prompt
|
| 332 |
+
if self.tokenizer.bos_token is not None and single_prompt.startswith(
|
| 333 |
+
self.tokenizer.bos_token
|
| 334 |
+
):
|
| 335 |
+
kwargs["add_special_tokens"] = False
|
| 336 |
+
|
| 337 |
+
# Always sample frames by default unless explicitly set to `False` by users. If users do not pass `num_frames`/`fps`
|
| 338 |
+
# sampling should not done for BC.
|
| 339 |
+
if "do_sample_frames" not in kwargs and (
|
| 340 |
+
kwargs.get("fps") is not None or kwargs.get("num_frames") is not None
|
| 341 |
+
):
|
| 342 |
+
kwargs["do_sample_frames"] = True
|
| 343 |
+
|
| 344 |
+
images_exist = any(
|
| 345 |
+
(im is not None) for im_list in batch_images for im in im_list
|
| 346 |
+
)
|
| 347 |
+
videos_exist = any(
|
| 348 |
+
(vid is not None) for vid_list in batch_videos for vid in vid_list
|
| 349 |
+
)
|
| 350 |
+
out = self(
|
| 351 |
+
text=prompt,
|
| 352 |
+
images=batch_images if images_exist else None,
|
| 353 |
+
videos=batch_videos if videos_exist else None,
|
| 354 |
+
audio=batch_audios if batch_audios else None,
|
| 355 |
+
midi=batch_midis if batch_midis else None,
|
| 356 |
+
**kwargs,
|
| 357 |
+
)
|
| 358 |
+
|
| 359 |
+
if return_dict:
|
| 360 |
+
if template_kwargs.get("return_assistant_tokens_mask", False):
|
| 361 |
+
assistant_masks = []
|
| 362 |
+
offset_mapping = out.pop("offset_mapping")
|
| 363 |
+
input_ids = out["input_ids"]
|
| 364 |
+
for i in range(len(input_ids)):
|
| 365 |
+
current_mask = [0] * len(input_ids[i])
|
| 366 |
+
offsets = offset_mapping[i]
|
| 367 |
+
offset_starts = [start for start, end in offsets]
|
| 368 |
+
for (
|
| 369 |
+
assistant_start_char,
|
| 370 |
+
assistant_end_char,
|
| 371 |
+
) in generation_indices[i]:
|
| 372 |
+
start_pos = bisect.bisect_left(
|
| 373 |
+
offset_starts, assistant_start_char
|
| 374 |
+
)
|
| 375 |
+
end_pos = bisect.bisect_left(
|
| 376 |
+
offset_starts, assistant_end_char
|
| 377 |
+
)
|
| 378 |
+
|
| 379 |
+
if not (
|
| 380 |
+
start_pos >= 0
|
| 381 |
+
and start_pos < len(offsets)
|
| 382 |
+
and offsets[start_pos][0]
|
| 383 |
+
<= assistant_start_char
|
| 384 |
+
< offsets[start_pos][1]
|
| 385 |
+
):
|
| 386 |
+
# start_token is out of bounds maybe due to truncation.
|
| 387 |
+
continue
|
| 388 |
+
# Ensure end_pos is also within bounds
|
| 389 |
+
if end_pos > len(input_ids[i]):
|
| 390 |
+
end_pos = len(input_ids[i])
|
| 391 |
+
for token_id in range(
|
| 392 |
+
start_pos, end_pos if end_pos else len(input_ids[i])
|
| 393 |
+
):
|
| 394 |
+
current_mask[token_id] = 1
|
| 395 |
+
assistant_masks.append(current_mask)
|
| 396 |
+
out["assistant_masks"] = assistant_masks
|
| 397 |
+
out.convert_to_tensors(tensor_type=kwargs.get("return_tensors"))
|
| 398 |
+
return out
|
| 399 |
+
else:
|
| 400 |
+
return out["input_ids"]
|
| 401 |
+
return prompt
|
processor_config.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"auto_map": {
|
| 3 |
+
"AutoProcessor": "processing_song2midi.Song2MIDIProcessor"
|
| 4 |
+
},
|
| 5 |
+
"feature_extractor": {
|
| 6 |
+
"auto_map": {
|
| 7 |
+
"AutoProcessor": "processing_song2midi.Song2MIDIProcessor"
|
| 8 |
+
},
|
| 9 |
+
"chunk_length": 30,
|
| 10 |
+
"dither": 0.0,
|
| 11 |
+
"feature_extractor_type": "WhisperFeatureExtractor",
|
| 12 |
+
"feature_size": 128,
|
| 13 |
+
"hop_length": 160,
|
| 14 |
+
"n_fft": 400,
|
| 15 |
+
"n_samples": 480000,
|
| 16 |
+
"nb_max_frames": 3000,
|
| 17 |
+
"padding_side": "right",
|
| 18 |
+
"padding_value": 0.0,
|
| 19 |
+
"return_attention_mask": false,
|
| 20 |
+
"sampling_rate": 16000
|
| 21 |
+
},
|
| 22 |
+
"processor_class": "Song2MIDIProcessor"
|
| 23 |
+
}
|
tokenizer.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:144ef28cd74e9fbc5d92e359a2ad561e7710e16241c615e3d99beedf7704ba98
|
| 3 |
+
size 19989912
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"add_prefix_space": false,
|
| 3 |
+
"audio_bos_token": "<|audio_start|>",
|
| 4 |
+
"audio_eos_token": "<|audio_end|>",
|
| 5 |
+
"audio_token": "<|audio_pad|>",
|
| 6 |
+
"auto_map": {
|
| 7 |
+
"AutoProcessor": "processing_song2midi.Song2MIDIProcessor"
|
| 8 |
+
},
|
| 9 |
+
"backend": "tokenizers",
|
| 10 |
+
"bos_token": null,
|
| 11 |
+
"clean_up_tokenization_spaces": false,
|
| 12 |
+
"eos_token": "<|endoftext|>",
|
| 13 |
+
"errors": "replace",
|
| 14 |
+
"image_token": "<|image_pad|>",
|
| 15 |
+
"is_local": true,
|
| 16 |
+
"midi_bos_token": "<|midi_start|>",
|
| 17 |
+
"midi_eos_token": "<|midi_end|>",
|
| 18 |
+
"midi_token": "<|midi_pad|>",
|
| 19 |
+
"model_max_length": 262144,
|
| 20 |
+
"model_specific_special_tokens": {
|
| 21 |
+
"audio_bos_token": "<|audio_start|>",
|
| 22 |
+
"audio_eos_token": "<|audio_end|>",
|
| 23 |
+
"audio_token": "<|audio_pad|>",
|
| 24 |
+
"image_token": "<|image_pad|>",
|
| 25 |
+
"midi_bos_token": "<|midi_start|>",
|
| 26 |
+
"midi_eos_token": "<|midi_end|>",
|
| 27 |
+
"midi_token": "<|midi_pad|>",
|
| 28 |
+
"video_token": "<|video_pad|>",
|
| 29 |
+
"vision_bos_token": "<|vision_start|>",
|
| 30 |
+
"vision_eos_token": "<|vision_end|>"
|
| 31 |
+
},
|
| 32 |
+
"pad_token": "<|endoftext|>",
|
| 33 |
+
"pretokenize_regex": "(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\\r\\n\\p{L}\\p{N}]?[\\p{L}\\p{M}]+|\\p{N}| ?[^\\s\\p{L}\\p{M}\\p{N}]+[\\r\\n]*|\\s*[\\r\\n]+|\\s+(?!\\S)|\\s+",
|
| 34 |
+
"processor_class": "Song2MIDIProcessor",
|
| 35 |
+
"split_special_tokens": false,
|
| 36 |
+
"tokenizer_class": "TokenizersBackend",
|
| 37 |
+
"unk_token": null,
|
| 38 |
+
"video_token": "<|video_pad|>",
|
| 39 |
+
"vision_bos_token": "<|vision_start|>",
|
| 40 |
+
"vision_eos_token": "<|vision_end|>"
|
| 41 |
+
}
|