# Path: rl_teacher/envs.py
# def make_with_torque_removed(env_id):
#     if '-v' in env_id:
#         env_id = env_id[:env_id.index('-v')].lower()
#     if env_id.startswith('short'):
#         env_id = env_id[len('short'):]
#         short = True
#     else:
#         short = False
#     return task_by_name(env_id, short)  # Use our task_by_name function to get the env
# 
# Path: rl_teacher/video.py
# def write_segment_to_video(segment, fname, env):
#     os.makedirs(osp.dirname(fname), exist_ok=True)
#     frames = [env.render_full_obs(x) for x in segment["human_obs"]]
#     for i in range(int(env.fps * 0.2)):
#         frames.append(frames[-1])
#     export_video(frames, fname, fps=env.fps)
# 
# def upload_to_gcs(local_path, gcs_path):
#     assert osp.isfile(local_path), "%s must be a file" % local_path
#     assert gcs_path.startswith("gs://"), "%s must start with gs://" % gcs_path
# 
#     print("Copying media to %s in a background process" % gcs_path)
#     subprocess.check_call(['gsutil', 'cp', local_path, gcs_path])


import multiprocessing
import os
import os.path as osp
import uuid
import numpy as np
from rl_teacher.envs import make_with_torque_removed
from rl_teacher.video import write_segment_to_video, upload_to_gcs
        from human_feedback_api import Comparison
        from human_feedback_api import Comparison
        from human_feedback_api import Comparison
        return len(self._comparisons)

    @property
    def labeled_comparisons(self):
        return [comp for comp in self._comparisons if comp['label'] is not None]

    @property
    def labeled_decisive_comparisons(self):
        return [comp for comp in self._comparisons if comp['label'] in [0, 1]]

    @property
    def unlabeled_comparisons(self):
        return [comp for comp in self._comparisons if comp['label'] is None]

    def label_unlabeled_comparisons(self):
        for comp in self.unlabeled_comparisons:
            self._add_synthetic_label(comp)

    @staticmethod
    def _add_synthetic_label(comparison):
        left_seg = comparison['left']
        right_seg = comparison['right']
        left_has_more_rew = np.sum(left_seg["original_rewards"]) > np.sum(right_seg["original_rewards"])

        # Mutate the comparison and give it the new label
        comparison['label'] = 0 if left_has_more_rew else 1

def _write_and_upload_video(env_id, gcs_path, local_path, segment):
    env = make_with_torque_removed(env_id)
    write_segment_to_video(segment, fname=local_path, env=env)

# Path: rl_teacher/envs.py
# def make_with_torque_removed(env_id):
#     if '-v' in env_id:
#         env_id = env_id[:env_id.index('-v')].lower()
#     if env_id.startswith('short'):
#         env_id = env_id[len('short'):]
#         short = True
#     else:
#         short = False
#     return task_by_name(env_id, short)  # Use our task_by_name function to get the env
# 
# Path: rl_teacher/video.py
# def write_segment_to_video(segment, fname, env):
#     os.makedirs(osp.dirname(fname), exist_ok=True)
#     frames = [env.render_full_obs(x) for x in segment["human_obs"]]
#     for i in range(int(env.fps * 0.2)):
#         frames.append(frames[-1])
#     export_video(frames, fname, fps=env.fps)
# 
# def upload_to_gcs(local_path, gcs_path):
#     assert osp.isfile(local_path), "%s must be a file" % local_path
#     assert gcs_path.startswith("gs://"), "%s must start with gs://" % gcs_path
# 
#     print("Copying media to %s in a background process" % gcs_path)
#     subprocess.check_call(['gsutil', 'cp', local_path, gcs_path])


# Path: rl_teacher/comparison_collectors.py
import multiprocessing
import os
import os.path as osp
import uuid
import numpy as np
from rl_teacher.envs import make_with_torque_removed
from rl_teacher.video import write_segment_to_video, upload_to_gcs
        from human_feedback_api import Comparison
        from human_feedback_api import Comparison
        from human_feedback_api import Comparison

        return len(self._comparisons)

    @property
    def labeled_comparisons(self):
        return [comp for comp in self._comparisons if comp['label'] is not None]

    @property
    def labeled_decisive_comparisons(self):
        return [comp for comp in self._comparisons if comp['label'] in [0, 1]]

    @property
    def unlabeled_comparisons(self):
        return [comp for comp in self._comparisons if comp['label'] is None]

    def label_unlabeled_comparisons(self):
        for comp in self.unlabeled_comparisons:
            self._add_synthetic_label(comp)

    @staticmethod
    def _add_synthetic_label(comparison):
        left_seg = comparison['left']
        right_seg = comparison['right']
        left_has_more_rew = np.sum(left_seg["original_rewards"]) > np.sum(right_seg["original_rewards"])

        # Mutate the comparison and give it the new label
        comparison['label'] = 0 if left_has_more_rew else 1

def _write_and_upload_video(env_id, gcs_path, local_path, segment):
    env = make_with_torque_removed(env_id)
    write_segment_to_video(segment, fname=local_path, env=env)

    upload_to_gcs(local_path, gcs_path)