Upload ms-swift/examples/custom/dataset.py with huggingface_hub
Browse files
ms-swift/examples/custom/dataset.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (c) Alibaba, Inc. and its affiliates.
|
| 2 |
+
from typing import Any, Dict, Optional
|
| 3 |
+
|
| 4 |
+
from swift.llm import DatasetMeta, ResponsePreprocessor, load_dataset, register_dataset
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class CustomPreprocessor(ResponsePreprocessor):
|
| 8 |
+
prompt = """Task: Based on the given two sentences, provide a similarity score between 0.0 and 5.0.
|
| 9 |
+
Sentence 1: {text1}
|
| 10 |
+
Sentence 2: {text2}
|
| 11 |
+
Similarity score: """
|
| 12 |
+
|
| 13 |
+
def preprocess(self, row: Dict[str, Any]) -> Optional[Dict[str, Any]]:
|
| 14 |
+
return super().preprocess({
|
| 15 |
+
'query': self.prompt.format(text1=row['text1'], text2=row['text2']),
|
| 16 |
+
'response': f"{row['label']:.1f}"
|
| 17 |
+
})
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
register_dataset(
|
| 21 |
+
DatasetMeta(
|
| 22 |
+
ms_dataset_id='swift/stsb',
|
| 23 |
+
hf_dataset_id='SetFit/stsb',
|
| 24 |
+
preprocess_func=CustomPreprocessor(),
|
| 25 |
+
))
|
| 26 |
+
|
| 27 |
+
if __name__ == '__main__':
|
| 28 |
+
dataset = load_dataset(['swift/stsb'])[0]
|
| 29 |
+
print(f'dataset: {dataset}')
|
| 30 |
+
print(f'dataset[0]: {dataset[0]}')
|