Jamari commited on
Commit
7c873b6
·
verified ·
1 Parent(s): 3f27271

Create generate-cot.py

Browse files
Files changed (1) hide show
  1. generate-cot.py +323 -0
generate-cot.py ADDED
@@ -0,0 +1,323 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import random
2
+ import math
3
+
4
+ def get_closest_numbers(time_str):
5
+ hours, minutes = map(int, time_str.split(':'))
6
+ hours = hours % 12 or 12
7
+ hour_degrees = (hours * 30) + (minutes * 0.5)
8
+ minute_degrees = minutes * 6
9
+
10
+ def get_position_info(degrees):
11
+ nearest_mark = round(degrees / 30)
12
+ nearest_mark_degrees = nearest_mark * 30
13
+ position = 'before' if degrees < nearest_mark_degrees else 'after'
14
+ number = nearest_mark % 12 or 12
15
+
16
+ return {
17
+ 'closest': number,
18
+ 'position': position
19
+ }
20
+
21
+ return {
22
+ 'hour': get_position_info(hour_degrees),
23
+ 'min': get_position_info(minute_degrees)
24
+ }
25
+
26
+ def generate_time_description(clock_data):
27
+
28
+ clock_params = clock_data['clock_params']
29
+ clock_coordinates = clock_data['clock_coordinates']
30
+ time_str = clock_params['time']
31
+ is_unreadable = clock_params.get('isUnreadable', False)
32
+ hours_format = clock_params.get('hours_format', 'unknown')
33
+ hours_to_generate = clock_params.get('hours_to_generate', [])
34
+ has_second_hand = clock_params.get('has_second_hand', False)
35
+ has_smaller_ticks = clock_params.get('has_smaller_ticks', False)
36
+ smaller_tick_frequency = clock_params.get('smaller_tick_frequency', 0)
37
+ hour_marker_points = clock_coordinates.get('hour_marker_points', {})
38
+ hand_tips = clock_coordinates.get('hand_tips', {})
39
+ minute_hand_tip = hand_tips.get('minute', [0, 0])
40
+ hour_hand_tip = hand_tips.get('hour', [0, 0])
41
+ should_have_decal = clock_params.get('should_have_decal', False)
42
+ decal_position = clock_params.get('decal_position', 'unknown')
43
+ decal_center = clock_coordinates.get('decal_center', [0, 0])
44
+ bg_width = clock_params.get('bgWidth', 1)
45
+ bg_height = clock_params.get('bgHeight', 1)
46
+
47
+ if is_unreadable:
48
+ if hours_format == 'line' and clock_params.get('should_tilt_hours', False) and not should_have_decal:
49
+ return random.choice([
50
+ "The clock is unreadable because the hour markers are lines and the clock is tilted, making it impossible to determine the 12 o'clock position without any landmarks on the clock.",
51
+ "Due to the tilted clock and line-shaped hour markers, it's impossible to identify the 12 o'clock position without any reference points, rendering the clock unreadable.",
52
+ "The clock's readability is compromised by its tilted position and the use of lines as hour markers, making it impossible to discern the 12 o'clock position without additional landmarks.",
53
+ "Without any landmarks and with line-shaped hour markers on a tilted clock, it's not possible to determine the 12 o'clock position, making the clock unreadable.",
54
+ "The combination of a tilted clock face and line-shaped hour markers makes it impossible to identify the 12 o'clock position, rendering the clock unreadable without additional reference points."
55
+ ])
56
+ elif hours_format == 'none' and not should_have_decal:
57
+ return random.choice([
58
+ "The clock is unreadable because there are no hour markers or decals to indicate the 12 o'clock position.",
59
+ "Without any hour markers or decals, it's impossible to determine the 12 o'clock position, making the clock unreadable.",
60
+ "The absence of hour markers and decals renders the clock unreadable, as there's no way to identify the 12 o'clock position.",
61
+ "Due to the lack of hour markers or decals, the clock is unreadable since the 12 o'clock position cannot be determined.",
62
+ "The clock's readability is compromised by the absence of hour markers or decals, making it impossible to locate the 12 o'clock position."
63
+ ])
64
+ else:
65
+ return random.choice([
66
+ "The clock is unreadable due to insufficient markers or orientation cues.",
67
+ "Lack of adequate markers or orientation references makes the clock unreadable.",
68
+ "The clock cannot be read because of insufficient indicators or directional guides.",
69
+ "Inadequate markings or orientation points render the clock unreadable.",
70
+ "The clock can't be read because of a lack of sufficient markers or orientation cues."
71
+ ])
72
+
73
+ # Extract hour and minute from the time
74
+ hour, minute, _ = map(int, time_str.split(":"))
75
+
76
+ # Calculate the angle between the hour and minute hands
77
+ hour_angle = (hour % 12 + minute / 60) * 30
78
+ minute_angle = minute * 6
79
+ angle = abs(hour_angle - minute_angle)
80
+ angle = min(angle, 360 - angle)
81
+
82
+ # Helper function to calculate distance between two points
83
+ def distance(p1, p2):
84
+ return math.hypot(p1[0] - p2[0], p1[1] - p2[1])
85
+
86
+ # Helper function to get the next and previous markers
87
+ def get_adjacent_markers(marker_list, current_marker):
88
+ marker_list = sorted(marker_list, key=lambda x: int(x) if x.isdigit() else x)
89
+ index = marker_list.index(current_marker)
90
+ before_marker = marker_list[index - 1] if index > 0 else marker_list[-1]
91
+ after_marker = marker_list[(index + 1) % len(marker_list)]
92
+ return before_marker, after_marker
93
+
94
+ # Function to convert coordinates to percentages and round them
95
+ def coord_to_percent(coord):
96
+ x_percent = round((coord[0] / bg_width) * 100, 2)
97
+ y_percent = round((coord[1] / bg_height) * 100, 2)
98
+ return x_percent, y_percent
99
+
100
+ # Function to format the <point> tag
101
+ def format_point(coord, alt_text):
102
+ x_percent, y_percent = coord_to_percent(coord)
103
+ return f'<point x="{x_percent}" y="{y_percent}" alt="{alt_text}">{alt_text}</point>'
104
+
105
+ # Convert hand tip coordinates
106
+ hour_hand_tip_coord = coord_to_percent(hour_hand_tip)
107
+ minute_hand_tip_coord = coord_to_percent(minute_hand_tip)
108
+
109
+ # Find the closest hour marker to the hour hand
110
+ hour_distances = {marker: distance(pos, hour_hand_tip) for marker, pos in hour_marker_points.items()}
111
+ closest_hour_marker = min(hour_distances, key=hour_distances.get)
112
+ before_hour_marker, after_hour_marker = get_adjacent_markers(list(hour_marker_points.keys()), closest_hour_marker)
113
+
114
+ real_closest_res = get_closest_numbers(":".join(time_str.split(":")[:2]))
115
+
116
+ real_closest_hour = real_closest_res['hour']
117
+ real_closest_min = real_closest_res['min']
118
+
119
+ # Hour hand closest
120
+ before_distance = distance(hour_marker_points[before_hour_marker], hour_hand_tip)
121
+ after_distance = distance(hour_marker_points[after_hour_marker], hour_hand_tip)
122
+
123
+ if before_distance < after_distance:
124
+ hour_hand_relation = f"After the {format_point(hour_marker_points[before_hour_marker], before_hour_marker)} o'clock marker"
125
+ relevant_marker = before_hour_marker
126
+ else:
127
+ hour_hand_relation = f"Before the {format_point(hour_marker_points[after_hour_marker], after_hour_marker)} o'clock marker"
128
+ relevant_marker = after_hour_marker
129
+
130
+ if not before_hour_marker == real_closest_hour['closest']:
131
+ hour_hand_relation = f"Between the {format_point(hour_marker_points[before_hour_marker], before_hour_marker)} and {format_point(hour_marker_points[after_hour_marker], after_hour_marker)} o'clock markers, {real_closest_hour['position']} where {real_closest_hour['closest']} would be"
132
+
133
+
134
+
135
+
136
+ # Find the closest minute marker to the minute hand, don't use tick positions i dont think the model can see them
137
+
138
+ # tick_positions = clock_coordinates.get('tick_positions', {})
139
+ # if tick_positions:
140
+ # minute_distances = {int(minute): distance(pos, minute_hand_tip) for minute, pos in tick_positions.items()}
141
+ # closest_minute_tick = min(minute_distances, key=minute_distances.get)
142
+ # before_minute_tick = (closest_minute_tick - smaller_tick_frequency) % 360
143
+ # after_minute_tick = (closest_minute_tick + smaller_tick_frequency) % 360
144
+
145
+ # before_distance = distance(tick_positions.get(str(before_minute_tick), minute_hand_tip), minute_hand_tip)
146
+ # after_distance = distance(tick_positions.get(str(after_minute_tick), minute_hand_tip), minute_hand_tip)
147
+
148
+ # if before_distance < after_distance:
149
+ # minute_hand_relation = f"just after the {format_point(tick_positions[str(before_minute_tick)], before_minute_tick // 6)} minute mark"
150
+ # relevant_minute = before_minute_tick
151
+ # else:
152
+ # minute_hand_relation = f"just before the {format_point(tick_positions[str(after_minute_tick)], after_minute_tick // 6)} minute mark"
153
+ # relevant_minute = after_minute_tick
154
+
155
+ # # Occasionally mention between markers
156
+ # if random.choice([True, False]):
157
+ # minute_hand_relation = f"between the {format_point(tick_positions[str(before_minute_tick)], before_minute_tick // 6)} and {format_point(tick_positions[str(after_minute_tick)], after_minute_tick // 6)} minute marks"
158
+
159
+
160
+ minute_distances = {int(marker): distance(pos, minute_hand_tip) for marker, pos in hour_marker_points.items()}
161
+ closest_minute_marker = min(minute_distances, key=minute_distances.get)
162
+ before_minute_marker, after_minute_marker = get_adjacent_markers(list(hour_marker_points.keys()), str(closest_minute_marker))
163
+
164
+ before_distance = distance(hour_marker_points[before_minute_marker], minute_hand_tip)
165
+ after_distance = distance(hour_marker_points[after_minute_marker], minute_hand_tip)
166
+
167
+ if before_distance < after_distance:
168
+ minute_hand_relation = f"just after the {format_point(hour_marker_points[before_minute_marker], before_minute_marker)} o'clock marker"
169
+ relevant_minute_marker = before_minute_marker
170
+ else:
171
+ minute_hand_relation = f"just before the {format_point(hour_marker_points[after_minute_marker], after_minute_marker)} o'clock marker"
172
+ relevant_minute_marker = after_minute_marker
173
+
174
+ # Occasionally mention between markers
175
+ if not before_hour_marker == real_closest_min['closest']:
176
+ minute_hand_relation = f"between the {format_point(hour_marker_points[before_minute_marker], before_minute_marker)} and {format_point(hour_marker_points[after_minute_marker], after_minute_marker)} o'clock markers, {real_closest_min['position']} where {real_closest_min['closest']} would be"
177
+
178
+ # Generate phrases based on the clock parameters
179
+ hour_format_phrases = {
180
+ 'roman': "The clock uses Roman numerals for hour markers.",
181
+ 'integer': "The clock uses integers for hour markers.",
182
+ 'line': "The clock uses lines as hour markers.",
183
+ 'none': "The clock has no hour markers.",
184
+ 'unknown': "The clock has hour markers of an unknown format."
185
+ }
186
+ hour_marker_phrase = hour_format_phrases.get(hours_format, hour_format_phrases['unknown'])
187
+ hour_marker_count_phrase = f"There are {len(hours_to_generate)} hour markers on the clock."
188
+ smaller_tick_phrase = (
189
+ f"Smaller ticks are present every {smaller_tick_frequency} degrees."
190
+ if has_smaller_ticks else "There are no smaller ticks on the clock."
191
+ )
192
+ second_hand_phrase = (
193
+ "The clock has a second hand." if has_second_hand else "There is no second hand on the clock."
194
+ )
195
+
196
+ # give a point the model can orient around, will always have 12, 6, or 3, but prefer 12 then 6
197
+ decal_phrase = ""
198
+ if hours_format in ['line', 'none'] and should_have_decal:
199
+ print('aaaaa')
200
+ if '12' in hour_marker_points:
201
+ decal_phrase = (
202
+ f"Using the decal on the clock's {decal_position}, we know where {format_point(hour_marker_points['12'], '12')} is on this clock and can orient around it."
203
+ )
204
+ elif '6' in hour_marker_points:
205
+ decal_phrase = (
206
+ f"Using the decal on the clock's {decal_position}, we know where {format_point(hour_marker_points['6'], '6')} is on this clock and can orient around it."
207
+ )
208
+ elif '3' in hour_marker_points:
209
+ decal_phrase = (
210
+ f"Using the decal on the clock's {decal_position}, we know where {format_point(hour_marker_points['3'], '3')} is on this clock and can orient around it."
211
+ )
212
+
213
+
214
+
215
+ hour_hand_phrases = [
216
+ f"The {format_point(hour_hand_tip, 'hour hand')} is {hour_hand_relation}.",
217
+ f"At position {format_point(hour_hand_tip, 'hour hand tip')}, the hour hand lies {hour_hand_relation}.",
218
+ f"The hour hand points {hour_hand_relation}, located at {format_point(hour_hand_tip, 'hour hand tip')}."
219
+ ]
220
+ minute_hand_phrases = [
221
+ f"The {format_point(minute_hand_tip, 'minute hand')} is {minute_hand_relation}.",
222
+ f"At position {format_point(minute_hand_tip, 'minute hand tip')}, the minute hand lies {minute_hand_relation}.",
223
+ f"The minute hand points {minute_hand_relation}, located at {format_point(minute_hand_tip, 'minute hand tip')}."
224
+ ]
225
+ angle_phrase = f"The angle between the hour and minute hands is approximately {angle:.2f} degrees."
226
+ conclusion_phrases = [
227
+ f"Therefore, the time displayed is {hour:02d}:{minute:02d}.",
228
+ f"Thus, we can read the time as {hour:02d}:{minute:02d}.",
229
+ f"As a result, the time is {hour:02d}:{minute:02d}."
230
+ ]
231
+
232
+ # Assemble the description
233
+ description = " ".join(filter(None, [
234
+ hour_marker_phrase,
235
+ hour_marker_count_phrase,
236
+ smaller_tick_phrase,
237
+ second_hand_phrase,
238
+ decal_phrase,
239
+ random.choice(hour_hand_phrases),
240
+ random.choice(minute_hand_phrases),
241
+ angle_phrase,
242
+ random.choice(conclusion_phrases)
243
+ ]))
244
+
245
+ return description
246
+
247
+
248
+ clock_data = {
249
+ "clock_params": {
250
+ "fontSize": 82,
251
+ "hour_marker_radius": 100,
252
+ "hours_to_generate": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
253
+ "should_tilt_hours": True,
254
+ "font": "Times New Roman",
255
+ "hours_format": "line",
256
+ "outer_border_color": [0, 0, 0],
257
+ "outer_border_width": 9,
258
+ "smaller_tick_frequency": 6,
259
+ "has_smaller_ticks": True,
260
+ "has_larger_ticks": False,
261
+ "time": "01:38:25",
262
+ "using_image_hands": True,
263
+ "has_second_hand": False,
264
+ "hour_hand_front_len": 55,
265
+ "hour_hand_back_len": 5.5,
266
+ "hour_hand_width": 45,
267
+ "minute_hand_front_len": 86,
268
+ "minute_hand_back_len": 7.166666666666667,
269
+ "minute_hand_width": 30,
270
+ "second_hand_front_len": 66,
271
+ "second_hand_back_len": 6.6,
272
+ "second_hand_width": 4,
273
+ "decal_image_index": 0,
274
+ "hr_image_index": 4,
275
+ "min_image_index": 4,
276
+ "decal_scale": 0.3077502934453228,
277
+ "should_have_decal": True,
278
+ "decal_position": "right",
279
+ "ticks_inside": True,
280
+ "ticks_are_dots": False,
281
+ "use_rounded_line_endings": True,
282
+ "rotationX": -54.67108931132457,
283
+ "rotationY": 32.946092388345136,
284
+ "rotationZ": 0,
285
+ "translationX": 0.7226896871641983,
286
+ "translationY": 0.32557091472462885,
287
+ "clockScale": 0.2944735397200734,
288
+ "is_square_clock": False,
289
+ "isUnreadable": False,
290
+ "background_image": "background-0.jpg",
291
+ "background_rotation": 0,
292
+ "invert_background": False,
293
+ "reverse_background": True,
294
+ "bgWidth": 1024,
295
+ "bgHeight": 696
296
+ },
297
+ "clock_coordinates": {
298
+ "hour_marker_points": {
299
+ # "1": [840.7229584915374, 388.8294035200205],
300
+ # "2": [864.708426987622, 405.3574644148344],
301
+ "3": [873.4877177788603, 427.9352154724463],
302
+ # "4": [864.708426987622, 450.51296653005824],
303
+ # "5": [840.7229584915374, 467.04102742487214],
304
+ "6": [807.9581992042145, 473.09071758767016],
305
+ # "7": [775.1934399168915, 467.04102742487214],
306
+ # "8": [751.2079714208069, 450.51296653005824],
307
+ "9": [742.4286806295686, 427.9352154724463],
308
+ # "10": [751.2079714208069, 405.3574644148344],
309
+ # "11": [775.1934399168915, 388.8294035200205],
310
+ "12": [807.9581992042145, 382.7797133572225]
311
+ },
312
+ "hand_tips": {
313
+ "hour": [846.4389307129645, 404.88473026091174],
314
+ "minute": [747.3718624157557, 462.3506187558278]
315
+ },
316
+ "tick_positions": {
317
+ # Tick positions data (omitted for brevity)
318
+ },
319
+ "decal_center": [849.3226328451655, 427.9352154724463]
320
+ }
321
+ }
322
+
323
+ print(generate_time_description(clock_data))