<commit_before>#!/usr/bin/env python
from __future__ import print_function
import redis
from osim.redis import messages
import json
import numpy as np
import osim
from osim.env import *
import os


class OsimRlRedisService:
    def __init__(self, osim_rl_redis_service_id='osim_rl_redis_service_id', seed_map=False, max_steps=1000, remote_host='127.0.0.1', remote_port=6379, remote_db=0, remote_password=<PASSWORD>, verbose=False):
        """
            TODO: Expose more RunEnv related variables
        """
        print("Attempting to connect to redis server at {}:{}/{}".format(remote_host, remote_port, remote_db))
        self.redis_pool = redis.ConnectionPool(host=remote_host, port=remote_port, db=remote_db, password=<PASSWORD>)
        self.namespace = "osim-rl"
        self.service_id = osim_rl_redis_service_id
        self.command_channel = "{}::{}::commands".format(self.namespace, self.service_id)
        self.env = False
        self.env_available = False
        self.reward = 0
        self.simulation_count = 0
        self.current_step = 0
        self.verbose = verbose
        self.max_steps = max_steps
        self.initalize_seed_map(seed_map)

    def initalize_seed_map(self, seed_map_string):
        if seed_map_string:
            assert type(seed_map_string) == type("")
            seed_map = seed_map_string.split(",")
            seed_map = [int(x) for x in seed_map]
            self.seed_map = seed_map
        else:
            self.seed_map = [np.random.randint(0,10**10)]

    def get_redis_connection(self):
        return redis.Redis(connection_pool=self.redis_pool)

    def _error_template(self, payload):
        _response = {}
        _response['type'] = messages.OSIM_RL.ERROR
        _response['payload'] = payload
        return _response

    def run(self):
        print("Listening for commands at : ", self.command_channel)
        while True:
            _redis = self.get_redis_connection()
            command = _redis.brpop(self.command_channel)[1]
            command_response_channel = "default_response_channel"
            if self.verbose: print("Self.Reward : ", self.reward)
            if self.verbose: print("Current Simulation : ", self.simulation_count)
            if self.seed_map and self.verbose and self.simulation_count < len(self.seed_map): print("Current SEED : ", self.seed_map[self.simulation_count])
            try:
                command = json.loads(command.decode('utf-8'))
                if self.verbose: print("Received Request : ", command)
                command_response_channel = command['response_channel']
                if command['type'] == messages.OSIM_RL.PING:
                    """
                        INITIAL HANDSHAKE : Respond with PONG
                    """
                    _command_response = {}
                    _command_response['type'] = messages.OSIM_RL.PONG
                    _command_response['payload'] = {}
                    if self.verbose: print("Responding with : ", _command_response)
                    _redis.rpush(command_response_channel, json.dumps(_command_response))
                elif command['type'] == messages.OSIM_RL.ENV_CREATE:
                    """
                        ENV_CREATE

                        Respond with initial observation
                    """
                    _payload = command['payload']
                    if 'visualize' not in _payload.keys():
                        _payload['visualize'] = False

                    if self.env: #If env already exists, throw an error
                        _error_message = "Attempt to create environment when one already exists."
                        if self.verbose: print("Responding with : ", self._error_template(_error_message))
                        _redis.rpush( command_response_channel, self._error_template(_error_message))
                        return self._error_template(_error_message)
                    else:
                        self.env = RunEnv(visualize = _payload['visualize'], max_obstacles=10)
                        _observation = self.env.reset(seed=self.seed_map[self.simulation_count], difficulty=2)
                        self.env_available = True
                        self.current_step = 0
                        _observation = np.array(_observation).tolist()
                        _command_response = {}
                        _command_response['type'] = messages.OSIM_RL.ENV_CREATE_RESPONSE
                        _command_response['payload'] = {}
                        _command_response['payload']['observation'] = _observation
                        if self.verbose: print("Responding with : ", _command_response)
                        _redis.rpush(command_response_channel, json.dumps(_command_response))
                elif command['type'] == messages.OSIM_RL.ENV_RESET:
                    """
                        ENV_RESET

                        Respond with observation from next simulation or
                        False if no simulations are left
                    """
                    self.simulation_count += 1
                    if self.seed_map and self.simulation_count < len(self.seed_map):
                        _observation = self.env.reset(seed=self.seed_map[self.simulation_count], difficulty=2)
                        self.env_available = True
                        self.current_step = 0
                        _observation = list(_observation)
                        _command_response = {}
                        _command_response['type'] = messages.OSIM_RL.ENV_RESET_RESPONSE
                        _command_response['payload'] = {}
                        _command_response['payload']['observation'] = _observation
                        if self.verbose: print("Responding with : ", _command_response)
                        _redis.rpush(command_response_channel, json.dumps(_command_response))
                    else:<commit_msg>Make visualize a configurable variable in redis grader service<commit_after>#!/usr/bin/env python
from __future__ import print_function
import redis
from osim.redis import messages
import json
import numpy as np
import osim
from osim.env import *
import os


class OsimRlRedisService:
    def __init__(self, osim_rl_redis_service_id='osim_rl_redis_service_id', seed_map=False, max_steps=1000, remote_host='127.0.0.1', remote_port=6379, remote_db=0, remote_password=None, visualize=False, verbose=False):
        """
            TODO: Expose more RunEnv related variables
        """
        print("Attempting to connect to redis server at {}:{}/{}".format(remote_host, remote_port, remote_db))
        self.redis_pool = redis.ConnectionPool(host=remote_host, port=remote_port, db=remote_db, password=remote_password)
        self.namespace = "osim-rl"
        self.service_id = osim_rl_redis_service_id
        self.command_channel = "{}::{}::commands".format(self.namespace, self.service_id)
        self.env = False
        self.env_available = False
        self.reward = 0
        self.simulation_count = 0
        self.current_step = 0
        self.verbose = verbose
        self.max_steps = max_steps
        self.initalize_seed_map(seed_map)

    def initalize_seed_map(self, seed_map_string):
        if seed_map_string:
            assert type(seed_map_string) == type("")
            seed_map = seed_map_string.split(",")
            seed_map = [int(x) for x in seed_map]
            self.seed_map = seed_map
        else:
            self.seed_map = [np.random.randint(0,10**10)]

    def get_redis_connection(self):
        return redis.Redis(connection_pool=self.redis_pool)

    def _error_template(self, payload):
        _response = {}
        _response['type'] = messages.OSIM_RL.ERROR
        _response['payload'] = payload
        return _response

    def run(self):
        print("Listening for commands at : ", self.command_channel)
        while True:
            _redis = self.get_redis_connection()
            command = _redis.brpop(self.command_channel)[1]
            command_response_channel = "default_response_channel"
            if self.verbose: print("Self.Reward : ", self.reward)
            if self.verbose: print("Current Simulation : ", self.simulation_count)
            if self.seed_map and self.verbose and self.simulation_count < len(self.seed_map): print("Current SEED : ", self.seed_map[self.simulation_count])
            try:
                command = json.loads(command.decode('utf-8'))
                if self.verbose: print("Received Request : ", command)
                command_response_channel = command['response_channel']
                if command['type'] == messages.OSIM_RL.PING:
                    """
                        INITIAL HANDSHAKE : Respond with PONG
                    """
                    _command_response = {}
                    _command_response['type'] = messages.OSIM_RL.PONG
                    _command_response['payload'] = {}
                    if self.verbose: print("Responding with : ", _command_response)
                    _redis.rpush(command_response_channel, json.dumps(_command_response))
                elif command['type'] == messages.OSIM_RL.ENV_CREATE:
                    """
                        ENV_CREATE

                        Respond with initial observation
                    """
                    _payload = command['payload']

                    if self.env: #If env already exists, throw an error
                        _error_message = "Attempt to create environment when one already exists."
                        if self.verbose: print("Responding with : ", self._error_template(_error_message))
                        _redis.rpush( command_response_channel, self._error_template(_error_message))
                        return self._error_template(_error_message)
                    else:
                        self.env = RunEnv(visualize = visualize, max_obstacles=10)
                        _observation = self.env.reset(seed=self.seed_map[self.simulation_count], difficulty=2)
                        self.env_available = True
                        self.current_step = 0
                        _observation = np.array(_observation).tolist()
                        _command_response = {}
                        _command_response['type'] = messages.OSIM_RL.ENV_CREATE_RESPONSE
                        _command_response['payload'] = {}
                        _command_response['payload']['observation'] = _observation
                        if self.verbose: print("Responding with : ", _command_response)
                        _redis.rpush(command_response_channel, json.dumps(_command_response))
                elif command['type'] == messages.OSIM_RL.ENV_RESET:
                    """
                        ENV_RESET

                        Respond with observation from next simulation or
                        False if no simulations are left
                    """
                    self.simulation_count += 1
                    if self.seed_map and self.simulation_count < len(self.seed_map):
                        _observation = self.env.reset(seed=self.seed_map[self.simulation_count], difficulty=2)
                        self.env_available = True
                        self.current_step = 0
                        _observation = list(_observation)
                        _command_response = {}
                        _command_response['type'] = messages.OSIM_RL.ENV_RESET_RESPONSE
                        _command_response['payload'] = {}
                        _command_response['payload']['observation'] = _observation
                        if self.verbose: print("Responding with : ", _command_response)
                        _redis.rpush(command_response_channel, json.dumps(_command_response))
                    else:
{'fraction_non_alphanumeric': 0.08990562858105831, 'fraction_numerical': 0.005392652510953826, 'mean_word_length': 1.8703748488512697, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5485409', 'n_tokens_mistral': 3222, 'n_tokens_neox': 3013, 'n_words': 595}