import random import math def get_closest_numbers(time_str): hours, minutes = map(int, time_str.split(':')) hours = hours % 12 or 12 hour_degrees = (hours * 30) + (minutes * 0.5) minute_degrees = minutes * 6 def get_position_info(degrees): nearest_mark = round(degrees / 30) nearest_mark_degrees = nearest_mark * 30 position = 'before' if degrees < nearest_mark_degrees else 'after' number = nearest_mark % 12 or 12 return { 'closest': number, 'position': position } return { 'hour': get_position_info(hour_degrees), 'min': get_position_info(minute_degrees) } def generate_time_description(clock_data, should_use_12=True): clock_params = clock_data['clock_params'] clock_coordinates = clock_data['clock_coordinates'] if should_use_12 and clock_params['time'].startswith('00'): clock_params['time'] = '12' + clock_params['time'][2:] time_str = clock_params['time'] is_unreadable = clock_params.get('isUnreadable', False) hours_format = clock_params.get('hours_format', 'unknown') hours_to_generate = clock_params.get('hours_to_generate', []) has_second_hand = clock_params.get('has_second_hand', False) has_smaller_ticks = clock_params.get('has_smaller_ticks', False) smaller_tick_frequency = clock_params.get('smaller_tick_frequency', 0) hour_marker_points = clock_coordinates.get('hour_marker_points', {}) hand_tips = clock_coordinates.get('hand_tips', {}) minute_hand_tip = hand_tips.get('minute', [0, 0]) hour_hand_tip = hand_tips.get('hour', [0, 0]) should_have_decal = clock_params.get('should_have_decal', False) decal_position = clock_params.get('decal_position', 'unknown') decal_center = clock_coordinates.get('decal_center', [0, 0]) bg_width = clock_params.get('bgWidth', 1) bg_height = clock_params.get('bgHeight', 1) hours_generated = clock_params.get('hours_to_generate') if is_unreadable: if hours_format == 'line': return random.choice([ "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.", "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.", "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.", "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.", "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." ]) elif hours_format == 'none': return random.choice([ "The clock is unreadable because there are no hour markers or decals to indicate the 12 o'clock position.", "Without any hour markers or decals, it's impossible to determine the 12 o'clock position, making the clock unreadable.", "The absence of hour markers and decals renders the clock unreadable, as there's no way to identify the 12 o'clock position.", "Due to the lack of hour markers or decals, the clock is unreadable since the 12 o'clock position cannot be determined.", "The clock's readability is compromised by the absence of hour markers or decals, making it impossible to locate the 12 o'clock position." ]) else: #shouldn't get here, but in case we add more return random.choice([ "The clock is unreadable due to insufficient markers or orientation cues.", "Lack of adequate markers or orientation references makes the clock unreadable.", "The clock cannot be read because of insufficient indicators or directional guides.", "Inadequate markings or orientation points render the clock unreadable.", "The clock can't be read because of a lack of sufficient markers or orientation cues." ]) # Extract hour and minute from the time hour, minute, _ = map(int, time_str.split(":")) # Calculate the angle between the hour and minute hands hour_angle = (hour % 12 + minute / 60) * 30 minute_angle = minute * 6 angle = abs(hour_angle - minute_angle) angle = min(angle, 360 - angle) # Helper function to calculate distance between two points def distance(p1, p2): return math.hypot(p1[0] - p2[0], p1[1] - p2[1]) # Helper function to get the next and previous markers def get_adjacent_markers(marker_list, current_marker): marker_list = sorted(marker_list, key=lambda x: int(x) if x.isdigit() else x) index = marker_list.index(current_marker) before_marker = marker_list[index - 1] if index > 0 else marker_list[-1] after_marker = marker_list[(index + 1) % len(marker_list)] return before_marker, after_marker # Function to convert coordinates to percentages and round them def coord_to_percent(coord): x_percent = round((coord[0] / bg_width) * 100, 2) y_percent = round((coord[1] / bg_height) * 100, 2) return x_percent, y_percent # Function to format the tag def format_point(coord, alt_text): x_percent, y_percent = coord_to_percent(coord) return f'{alt_text}' # Convert hand tip coordinates hour_hand_tip_coord = coord_to_percent(hour_hand_tip) minute_hand_tip_coord = coord_to_percent(minute_hand_tip) # Find the closest hour marker to the hour hand hour_distances = {marker: distance(pos, hour_hand_tip) for marker, pos in hour_marker_points.items()} closest_hour_marker = min(hour_distances, key=hour_distances.get) before_hour_marker, after_hour_marker = get_adjacent_markers(list(hour_marker_points.keys()), closest_hour_marker) real_closest_res = get_closest_numbers(":".join(time_str.split(":")[:2])) real_closest_hour = real_closest_res['hour'] real_closest_min = real_closest_res['min'] # Hour hand closest def find_closest_neighbors(arr, target): # Sort the array to make finding neighbors easier sorted_arr = sorted(arr) # If target is smaller than all elements if target < sorted_arr[0]: return str(sorted_arr[-1]), str(sorted_arr[0]) # If target is larger than all elements if target > sorted_arr[-1]: return str(sorted_arr[-1]), str(sorted_arr[0]) # Find the right neighbor (first element larger than target) right_idx = 0 while right_idx < len(sorted_arr) and sorted_arr[right_idx] < target: right_idx += 1 # The left neighbor is the element before the right neighbor left_idx = right_idx - 1 return str(sorted_arr[left_idx]), str(sorted_arr[right_idx]) if real_closest_hour['closest'] in hours_generated: hour_hand_relation = f"{real_closest_hour['position']} the {format_point(hour_marker_points[str(real_closest_hour['closest'])], str(real_closest_hour['closest']))} o'clock marker" else: smaller, larger = find_closest_neighbors(hours_generated, real_closest_hour['closest']) hour_hand_relation = f"between the {format_point(hour_marker_points[smaller], smaller)} and {format_point(hour_marker_points[larger], larger)} o'clock markers, {real_closest_hour['position']} where the {real_closest_hour['closest']} would be" # Find the closest minute marker to the minute hand, don't use tick positions i dont think the model can see them # tick_positions = clock_coordinates.get('tick_positions', {}) # if tick_positions: # minute_distances = {int(minute): distance(pos, minute_hand_tip) for minute, pos in tick_positions.items()} # closest_minute_tick = min(minute_distances, key=minute_distances.get) # before_minute_tick = (closest_minute_tick - smaller_tick_frequency) % 360 # after_minute_tick = (closest_minute_tick + smaller_tick_frequency) % 360 # before_distance = distance(tick_positions.get(str(before_minute_tick), minute_hand_tip), minute_hand_tip) # after_distance = distance(tick_positions.get(str(after_minute_tick), minute_hand_tip), minute_hand_tip) # if before_distance < after_distance: # minute_hand_relation = f"just after the {format_point(tick_positions[str(before_minute_tick)], before_minute_tick // 6)} minute mark" # relevant_minute = before_minute_tick # else: # minute_hand_relation = f"just before the {format_point(tick_positions[str(after_minute_tick)], after_minute_tick // 6)} minute mark" # relevant_minute = after_minute_tick # # Occasionally mention between markers # if random.choice([True, False]): # 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" # minute_distances = {int(marker): distance(pos, minute_hand_tip) for marker, pos in hour_marker_points.items()} if real_closest_min['closest'] in hours_generated: minute_hand_relation = f"{real_closest_min['position']} the {format_point(hour_marker_points[str(real_closest_min['closest'])], str(real_closest_min['closest']))} o'clock marker" else: smaller, larger = find_closest_neighbors(hours_generated, real_closest_min['closest']) minute_hand_relation = f"between the {format_point(hour_marker_points[smaller], smaller)} and {format_point(hour_marker_points[larger], larger)} o'clock markers, {real_closest_min['position']} where the {real_closest_min['closest']} would be" # Generate phrases based on the clock parameters hour_format_phrases = { 'roman': "The clock uses Roman numerals for hour markers.", 'integer': "The clock uses integers for hour markers.", 'line': "The clock uses lines as hour markers.", 'none': "The clock has no hour markers.", 'unknown': "The clock has hour markers of an unknown format." } hour_marker_phrase = hour_format_phrases.get(hours_format, hour_format_phrases['unknown']) hour_marker_count_phrase = f"There are {len(hours_to_generate)} hour markers on the clock." smaller_tick_phrase = ( f"Smaller ticks are present every {smaller_tick_frequency} degrees." if has_smaller_ticks else "There are no smaller ticks on the clock." ) second_hand_phrase = ( "The clock has a second hand." if has_second_hand else "There is no second hand on the clock." ) # give a point the model can orient around, will always have 12, 6, or 3, but prefer 12 then 6 decal_phrase = "" if hours_format in ['line', 'none'] and should_have_decal: if '12' in hour_marker_points: decal_phrase = ( 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." ) elif '6' in hour_marker_points: decal_phrase = ( 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." ) elif '3' in hour_marker_points: decal_phrase = ( 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." ) hour_hand_phrases = [ f"The {format_point(hour_hand_tip, 'hour hand')} is {hour_hand_relation}.", f"The clock's {format_point(hour_hand_tip, 'hour hand tip')} lies {hour_hand_relation}.", f"The {format_point(hour_hand_tip, 'hour hand tip')} points {hour_hand_relation}." ] minute_hand_phrases = [ f"The {format_point(minute_hand_tip, 'minute hand')} is {minute_hand_relation}.", f"The {format_point(minute_hand_tip, 'minute hand')} lies {minute_hand_relation}.", f"The {format_point(minute_hand_tip, 'minute hand tip')} points {minute_hand_relation}." ] angle_phrase = f"The angle between the hour and minute hands is approximately {angle:.2f} degrees." conclusion_phrases = [ f"Therefore, the time displayed is {hour:02d}:{minute:02d}.", f"Thus, we can read the time as {hour:02d}:{minute:02d}.", f"As a result, the time is {hour:02d}:{minute:02d}." ] # ignore smaller ticks and second hand imo description = " ".join(filter(None, [ hour_marker_phrase, hour_marker_count_phrase, # smaller_tick_phrase, # second_hand_phrase, decal_phrase, random.choice(hour_hand_phrases), random.choice(minute_hand_phrases), angle_phrase, random.choice(conclusion_phrases) ])) return description