Bin
2025-12-16 9e0b2ba2c317b1a86212f24cbae3195ad1f3dbfa
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { Rate } from "antd";
import { inject, observer } from "mobx-react";
import { types } from "mobx-state-tree";
import { StarOutlined } from "@ant-design/icons";
import { ReadOnlyControlMixin } from "../../mixins/ReadOnlyMixin";
 
import RequiredMixin from "../../mixins/Required";
import PerRegionMixin from "../../mixins/PerRegion";
import InfoModal from "../../components/Infomodal/Infomodal";
import Registry from "../../core/Registry";
import { guidGenerator } from "../../core/Helpers";
import ControlBase from "./Base";
import { AnnotationMixin } from "../../mixins/AnnotationMixin";
import ClassificationBase from "./ClassificationBase";
import PerItemMixin from "../../mixins/PerItem";
import { FF_LSDV_4583, isFF } from "../../utils/feature-flags";
 
/**
 * The `Rating` tag adds a rating selection to the labeling interface. Use for labeling tasks involving ratings.
 *
 * Use with the following data types: audio, image, HTML, paragraphs, text, time series, video.
 *
 * @example
 * <!--Basic labeling configuration to rate the content of a text passage -->
 * <View>
 *   <Text name="txt" value="$text" />
 *   <Rating name="rating" toName="txt" maxRating="10" icon="star" size="medium" />
 * </View>
 *
 * @name Rating
 * @meta_title Rating Tag for Ratings
 * @meta_description Customize Label Studio to add ratings to tasks with the Rating tag in your machine learning and data science projects.
 * @param {string} name                       - Name of the element
 * @param {string} toName                     - Name of the element that you want to label
 * @param {number} [maxRating=5]              - Maximum rating value
 * @param {number} [defaultValue=0]           - Default rating value
 * @param {small|medium|large} [size=medium]  - Rating icon size
 * @param {string} hotkey                     - HotKey for changing rating value
 * @param {boolean} [required=false]          - Whether rating validation is required
 * @param {string} [requiredMessage]          - Message to show if validation fails
 * @param {boolean} [perRegion]               - Use this tag to rate regions instead of the whole object
 * @param {boolean} [perItem]                 - Use this tag to rate items inside the object instead of the whole object
 */
const TagAttrs = types.model({
  toname: types.maybeNull(types.string),
 
  maxrating: types.optional(types.string, "5"),
  icon: types.optional(types.string, "star"),
  size: types.optional(types.string, "medium"),
  defaultvalue: types.optional(types.string, "0"),
 
  hotkey: types.maybeNull(types.string),
});
 
const Model = types
  .model({
    pid: types.optional(types.string, guidGenerator),
    type: "rating",
    rating: types.maybeNull(types.number),
  })
  .views((self) => ({
    selectedValues() {
      return self.rating;
    },
 
    get serializableValue() {
      const rating = self.selectedValues();
 
      if (!rating) return null;
      return { rating };
    },
 
    get holdsState() {
      return self.rating > 0;
    },
  }))
  .actions((self) => ({
    getSelectedString() {
      return `${self.rating} star`;
    },
 
    needsUpdate() {
      if (self.result) self.rating = self.result.mainValue;
      else self.rating = null;
    },
 
    unselectAll() {},
 
    setRating(value) {
      self.rating = value;
      self.updateResult();
    },
 
    updateFromResult(value) {
      self.rating = value;
    },
 
    requiredModal() {
      InfoModal.warning(self.requiredmessage || `Rating "${self.name}" is required.`);
    },
 
    increaseValue() {
      if (self.rating >= Number(self.maxrating)) {
        self.setRating(0);
      } else {
        if (self.rating > 0) {
          self.setRating(self.rating + 1);
        } else {
          self.setRating(1);
        }
      }
    },
 
    onHotKey() {
      return self.increaseValue();
    },
  }));
 
const RatingModel = types.compose(
  "RatingModel",
  ControlBase,
  ClassificationBase,
  RequiredMixin,
  ReadOnlyControlMixin,
  PerRegionMixin,
  ...(isFF(FF_LSDV_4583) ? [PerItemMixin] : []),
  AnnotationMixin,
  TagAttrs,
  Model,
);
 
const HtxRating = inject("store")(
  observer(({ item, store }) => {
    let iconSize;
 
    if (item.size === "small") {
      iconSize = 15;
    } else if (item.size === "medium") {
      iconSize = 25;
    } else if (item.size === "large") {
      iconSize = 40;
    }
 
    const visibleStyle = item.perRegionVisible() ? {} : { display: "none" };
 
    // rc-rate component listens for keypress event and hit the star if the key is Enter
    // but it doesn't check for any modifiers, so it removes star during submit (ctrl+enter)
    // so we'll just remove focus from component at the moment any modifier pressed
    const dontBreakSubmit = (e) => {
      if (e.ctrlKey || e.metaKey || e.altKey || e.shiftKey) {
        // should be a star, because that's the only way this event can happen
        const star = document.activeElement;
        const control = e.currentTarget;
 
        // but we'll check that for sure
        if (control.contains(star)) star.blur();
      }
    };
 
    return (
      <div style={visibleStyle} onKeyDownCapture={dontBreakSubmit} ref={item.elementRef}>
        <Rate
          character={<StarOutlined style={{ fontSize: iconSize }} />}
          value={item.rating}
          count={Number(item.maxrating)}
          defaultValue={Number(item.defaultvalue)}
          onChange={item.setRating}
          disabled={item.isReadOnly()}
        />
        {store.settings.enableTooltips && store.settings.enableHotkeys && item.hotkey && (
          <sup style={{ fontSize: "9px" }}>[{item.hotkey}]</sup>
        )}
      </div>
    );
  }),
);
 
Registry.addTag("rating", RatingModel, HtxRating);
 
export { HtxRating, RatingModel };