एनोटेशन को Hugging Face Datasets में एक्सपोर्ट करना
Potato के एनोटेशन को Hugging Face Datasets प्रारूप में बदलिए: JSON एक्सपोर्ट का ढाँचा, dataset card बनाना, Hub पर अपलोड, और transformers की ट्रेनिंग से जोड़ना।
अगर आप मॉडल प्रशिक्षित कर रहे हैं तो देर-सबेर डेटा को Hugging Face dataset की शक्ल लेनी ही पड़ेगी। बाक़ी सारा तंत्र यही उम्मीद करता है। यह गाइड दिखाती है कि Potato के एनोटेशन आउटपुट को कुछ छोटे Python स्क्रिप्ट से उस प्रारूप में कैसे बदलें, चाहे आप अपनी मशीन पर प्रशिक्षण कर रहे हों या डेटासेट Hub पर साझा कर रहे हों।
Hugging Face प्रारूप की ज़हमत क्यों उठाएँ
औज़ार पहले से यही ज़बान बोलते हैं, इसलिए हर लाइब्रेरी के लिए जोड़-तोड़ वाला कोड नहीं लिखना पड़ता। डेटासेट Arrow में रखे जाते हैं, जो बड़े होने पर भी तेज़ी से लोड होते हैं। साझा करना एक push_to_hub कॉल भर है। और Trainer उसे सीधे पढ़ लेता है, इसलिए प्रशिक्षण से पहले कोई अलग बदलाव का क़दम नहीं रहता।
Python से बुनियादी एक्सपोर्ट
Potato एनोटेशन JSONL में लिखता है। datasets लाइब्रेरी उसे Hugging Face dataset में बदल देती है।
Potato के एनोटेशन लोड करना
import json
from datasets import Dataset
# Load Potato annotation output
annotations = []
with open("annotation_output/annotated_instances.jsonl", "r") as f:
for line in f:
annotations.append(json.loads(line))
# Convert to Hugging Face Dataset
dataset = Dataset.from_list([
{
"text": ann["text"],
"label": ann["label_annotations"]["sentiment"]["label"]
}
for ann in annotations
])
# Save locally
dataset.save_to_disk("my_dataset")
# Or push to Hub
dataset.push_to_hub("username/my-dataset")Train/Test में बाँटना
from sklearn.model_selection import train_test_split
# Split annotations
train_data, temp_data = train_test_split(annotations, test_size=0.2, random_state=42)
val_data, test_data = train_test_split(temp_data, test_size=0.5, random_state=42)
# Create datasets
train_dataset = Dataset.from_list(train_data)
val_dataset = Dataset.from_list(val_data)
test_dataset = Dataset.from_list(test_data)
# Combine into DatasetDict
from datasets import DatasetDict
dataset = DatasetDict({
"train": train_dataset,
"validation": val_dataset,
"test": test_dataset
})काम के हिसाब से एक्सपोर्ट
टेक्स्ट वर्गीकरण
from datasets import Dataset, ClassLabel
# Load and process sentiment annotations
dataset = Dataset.from_dict({
"text": [ann["text"] for ann in annotations],
"label": [ann["label_annotations"]["sentiment"]["label"] for ann in annotations]
})
# Define label mapping
dataset = dataset.cast_column(
"label",
ClassLabel(names=["Positive", "Negative", "Neutral"])
)Named entity recognition
# Convert span annotations to IOB format
def convert_to_iob(text, spans):
tokens = text.split()
labels = ["O"] * len(tokens)
for span in spans:
# Map character offsets to token indices
start_token, end_token = char_to_token(text, span["start"], span["end"])
labels[start_token] = f"B-{span['annotation']}"
for i in range(start_token + 1, end_token):
labels[i] = f"I-{span['annotation']}"
return tokens, labels
# Potato stores span annotations in span_annotations field
dataset = Dataset.from_dict({
"tokens": [convert_to_iob(a["text"], a.get("span_annotations", {}).get("entities", []))[0] for a in annotations],
"ner_tags": [convert_to_iob(a["text"], a.get("span_annotations", {}).get("entities", []))[1] for a in annotations]
})ऑडियो वर्गीकरण
from datasets import Audio
# For audio annotation tasks
dataset = Dataset.from_dict({
"audio": [ann["audio"] for ann in annotations],
"label": [ann["label_annotations"]["emotion"]["label"] for ann in annotations]
})
# Cast to Audio feature
dataset = dataset.cast_column("audio", Audio(sampling_rate=16000))इमेज वर्गीकरण
from datasets import Image
# For image annotation tasks
dataset = Dataset.from_dict({
"image": [ann["image"] for ann in annotations],
"label": [ann["label_annotations"]["category"]["label"] for ann in annotations]
})
dataset = dataset.cast_column("image", Image())कई एनोटेटर वाला एक्सपोर्ट
जब हर आइटम पर कई एनोटेटर हों, तो आप अलग-अलग प्रारूपों में एक्सपोर्ट कर सकते हैं:
# Long format (one row per annotation)
# Each annotator's work is saved in a separate file: annotator_{id}.jsonl
import glob
records = []
for filepath in glob.glob("annotation_output/annotator_*.jsonl"):
annotator_id = filepath.split("_")[-1].replace(".jsonl", "")
with open(filepath) as f:
for line in f:
ann = json.loads(line)
records.append({
"id": ann["id"],
"text": ann["text"],
"label": ann["label_annotations"]["sentiment"]["label"],
"annotator": annotator_id
})
dataset = Dataset.from_list(records)
# Or aggregate annotations per item
from collections import defaultdict
from statistics import mode
items = defaultdict(list)
for record in records:
items[record["id"]].append(record)
aggregated = []
for item_id, anns in items.items():
labels = [a["label"] for a in anns]
aggregated.append({
"id": item_id,
"text": anns[0]["text"],
"label": mode(labels), # Majority vote
"num_annotators": len(labels)
})
dataset = Dataset.from_list(aggregated)Hugging Face Hub पर भेजना
from huggingface_hub import login
# Login (or use HF_TOKEN env var)
login()
# Push dataset
dataset.push_to_hub(
"username/my-sentiment-dataset",
private=False,
token=None # Uses cached token
)
# With dataset card
dataset.push_to_hub(
"username/my-sentiment-dataset",
commit_message="Initial upload of sentiment annotations",
)Dataset card
अपने डेटासेट के लिए README.md बनाइए:
---
license: cc-by-4.0
task_categories:
- text-classification
language:
- en
size_categories:
- 1K<n<10K
---
# My Sentiment Dataset
## Dataset Description
Sentiment annotations collected using [Potato](https://potato.iro.umich.edu).
## Dataset Structure
- **train**: 8,000 examples
- **validation**: 1,000 examples
- **test**: 1,000 examples
### Labels
- Positive
- Negative
- Neutral
## Annotation Process
Annotated by 3 workers per item on Prolific.
Inter-annotator agreement (Fleiss' Kappa): 0.75
## Citation
@article{...}अपना डेटासेट लोड करना
from datasets import load_dataset
# From Hub
dataset = load_dataset("username/my-sentiment-dataset")
# From local
dataset = load_dataset("my_dataset/")
# Use for training
from transformers import Trainer
trainer = Trainer(
model=model,
train_dataset=dataset["train"],
eval_dataset=dataset["validation"],
...
)कुछ आदतें जो बनाए रखने लायक हैं
लिख रखिए कि डेटा कहाँ से आया, एनोटेशन कैसे हुआ, और सहमति कितनी निकली, क्योंकि जो लोग डेटासेट दोबारा इस्तेमाल करेंगे वे यही पूछेंगे। हर लेबल की परिभाषा सीधे शब्दों में दीजिए, यह मानकर मत चलिए कि नाम से ही सब समझ आ जाएगा। डेटासेट का संस्करण रखिए, ताकि पता चले कि दो रिलीज़ के बीच क्या बदला। एनोटेशन की पद्धति का श्रेय दीजिए। और लाइसेंस शुरू में ही बता दीजिए, ताकि किसी को अंदाज़ा न लगाना पड़े कि उसे इस्तेमाल की इजाज़त है या नहीं।
Potato जो एक्सपोर्ट विकल्प ख़ुद देता है, उनके लिए Hugging Face एक्सपोर्ट दस्तावेज़ देखें।
एक्सपोर्ट के पूरे दस्तावेज़ डेटा प्रारूप पर हैं।