File size: 4,597 Bytes
353c2cb
 
 
 
a7dac98
 
 
 
 
 
 
353c2cb
a7dac98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
353c2cb
 
 
 
 
 
 
 
 
 
 
 
 
 
a7dac98
353c2cb
 
a7dac98
353c2cb
a7dac98
353c2cb
a7dac98
353c2cb
 
 
a7dac98
353c2cb
 
 
 
 
 
 
 
 
 
 
2723e94
 
a7dac98
353c2cb
a7dac98
 
 
353c2cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# import os
# import json
# import datasets

import os
import json
import datasets

# Hugging Face Dataset Info
DS_NAME = "anna-tch/generation-results"
HF_TOKEN = os.getenv("HF_TOKEN")
PROGRESS_FILE = "progress.json"  # Save progress here

# Load dataset
def load_dataset():
    """Loads the dataset from Hugging Face."""
    try:
        dataset = datasets.load_dataset(DS_NAME, token=HF_TOKEN)["train"]
        return dataset
    except Exception as e:
        print(f"Error loading dataset: {e}")
        return None

# Get generation columns
def get_generation_columns(dataset):
    """Returns a list of generation columns, excluding metadata columns."""
    return [col for col in dataset.column_names if col not in ["comment_id", "manual_annotation"]]

# Load progress from the JSON file
def load_progress():
    """Load the saved annotations progress from a JSON file."""
    if os.path.exists(PROGRESS_FILE):
        with open(PROGRESS_FILE, "r") as f:
            return json.load(f)
    return {}

# Save progress to the JSON file
def save_progress(progress):
    """Save annotations progress to a JSON file."""
    with open(PROGRESS_FILE, "w") as f:
        json.dump(progress, f, indent=4)

# Fetch the next unannotated sample
def get_next_sample(dataset, progress):
    """Finds the next unannotated comment, or None if no more unannotated samples."""
    for sample in dataset:
        if sample["comment_id"] not in progress:
            return sample
    return None  # No more unannotated samples

# Annotate text and update dataset
def annotate_text(dataset, comment_id, grammar_scores, coherence_scores, preferred_text, generation_columns, progress):
    """Annotate the sample and update it in the dataset."""
    
    # Store annotation in the progress
    progress[comment_id] = {
        "grammar": dict(zip(generation_columns, grammar_scores)),
        "coherence": dict(zip(generation_columns, coherence_scores)),
        "preferred_text": preferred_text
    }

    # Save progress after annotating
    save_progress(progress)

    # Find the sample with the given comment_id and update it
    df = dataset.to_pandas()
    df.loc[df["comment_id"] == comment_id, "manual_annotation"] = progress[comment_id]

    # Push the updated dataset to Hugging Face
    dataset.push_to_hub(DS_NAME)

    return dataset


# # Hugging Face Dataset Info
# DS_NAME = "anna-tch/generation-results"
# HF_TOKEN = os.getenv("HF_TOKEN")
# PROGRESS_FILE = "progress.json"

# # Load dataset
# def load_dataset():
#     """Loads the dataset from Hugging Face."""
#     try:
#         dataset = datasets.load_dataset(DS_NAME, token=HF_TOKEN)["train"]
#         return dataset
#     except Exception as e:
#         print(f"Error loading dataset: {e}")
#         return None

# def load_progress():
#     """Loads the progress file."""
#     try:
#         with open(PROGRESS_FILE, "r") as f:
#             return json.load(f)
#     except FileNotFoundError:
#         # Create progress file if it doesn’t exist
#         with open(PROGRESS_FILE, "w") as f:
#             json.dump({}, f)
#             return {}

# # Get generation columns
# def get_generation_columns(dataset):
#     """Returns a list of generation columns, excluding metadata columns."""
#     return [col for col in dataset.column_names if col not in ["comment_id", "manual_annotation"]]

# # Fetch the next unannotated sample
# def get_next_sample(dataset):
#     """Finds the next unannotated comment."""
#     for sample in dataset:
#         if sample["manual_annotation"] is None:
#             return sample
#     return None  # No more samples

# # Update dataset using comment_id
# def annotate_text(dataset, comment_id, grammar_scores, coherence_scores, preferred_text, generation_columns):
#     """Finds the correct sample by comment_id and updates it."""
    
#     def update_sample(example):
#         """Updates only the sample with the given comment_id."""
#         if example["comment_id"] == comment_id:
#             return {
#                 **example,
#                 "manual_annotation": {
#                     "grammar": dict(zip(generation_columns, grammar_scores)),
#                     "coherence": dict(zip(generation_columns, coherence_scores)),
#                     "preferred_text": preferred_text
#                 }
#             }
#         return example  # Return unchanged sample if it doesn’t match

#     # Apply update to dataset
#     dataset = dataset.map(update_sample)

#     # Push updated dataset to Hugging Face
#     dataset.push_to_hub(DS_NAME)

#     return dataset