Esempio n. 1
0
    def __init__(self):

        _db = settings.db_host.split(':')
        db_host = _db[0]
        db_port = int(_db[1])
        connection_user = ''

        if settings.db_user and settings.db_password:
            db_user = urllib.parse.quote_plus(settings.db_user)
            db_password = urllib.parse.quote_plus(settings.db_password)
            connection_user = f'{db_user}:{db_password}@'

        connection_string = f'mongodb://{connection_user}{db_host}:{db_port}'

        self.db_client = pymongo.MongoClient(connection_string,
                                             serverSelectionTimeoutMS=1000,
                                             connect=False)

        db_name = settings.db_name

        if not self.db_client:
            raise DBConnectionError('Failed to connect to the database server')

        if not db_name:
            raise DBConfigError('Database name not found in configuration')

        self.db = self.db_client[settings.db_name]
        self._logger = Logger().logger()
Esempio n. 2
0
def adapt_critical(exception):
    try:
        result = '{"status": "critical", "message": "' + exception + '"}\n'
        response = Response(result, status=200, mimetype='application/json')
        return response
    except Exception as ex:
        Logger.critical('There was an error while creating critical response', ex)
        raise ex
Esempio n. 3
0
def adapt_one_success(data):
    try:
        result = '{"status": "OK", "total": ' + \
                      ', "data": "' + str(data) + '"}\n'

        response = Response(result, status=200, mimetype='application/json')
        return response
    except Exception as ex:
        Logger.critical('There was an error while creating success response', ex)
        raise ex
Esempio n. 4
0
def user_unsubscribe(bot, chat_id):
    try:
        text = "I won't be sending notifications anymore."
        bot.send_message(chat_id, text)

        hacker_news = HackerNews()
        hacker_news.db.users.update_one({'_id': chat_id},
                                        {'$set': {
                                            'subscribed': False
                                        }},
                                        upsert=True)

        Logger().logger().info(f'user {chat_id} unsubscribed')
    except Exception as exc:
        Logger().logger().error(exc)
        raise exc
Esempio n. 5
0
def user_subscribe(bot, chat_id):
    try:
        text = "From now on you'll be receiving notifications with links to new posts!"
        bot.send_message(chat_id=chat_id, text=text)

        hacker_news = HackerNews()
        hacker_news.db.users.update_one({'_id': chat_id},
                                        {'$set': {
                                            'subscribed': True
                                        }},
                                        upsert=True)

        Logger().logger().info(f'user {chat_id} subscribed')

    except Exception as exc:
        Logger().logger().error(exc)
        raise exc
Esempio n. 6
0
def play_game(black: AI, white: AI):
    players = {"B": black, "W": white}
    engines = {"B": black.get_engine(), "W": white.get_engine()}
    tag = f"{black.name} vs {white.name}"
    try:
        game = Game(Logger(),
                    engines,
                    game_properties={
                        "SZ": BOARDSIZE,
                        "PW": white.strategy,
                        "PB": black.strategy
                    })
        game.root.add_list_property("PW", [white.name])
        game.root.add_list_property("PB", [black.name])
        game.root.properties["AP"] = ["kt-selfplay"]
        start_time = time.time()
        while not game.end_result and game.current_node.depth < 300:
            p = game.current_node.next_player
            move, node = generate_ai_move(game, players[p].strategy,
                                          players[p].ai_settings)
        while not game.current_node.analysis_complete:
            time.sleep(0.001)
        game.game_id += f"_{game.current_node.format_score()}"
        if OUTPUT_SGF:
            sgf_out_msg = game.write_sgf("sgf_selfplay_simple/",
                                         trainer_config={
                                             "eval_show_ai": True,
                                             "save_feedback": [True],
                                             "eval_thresholds": [0]
                                         })
        else:
            sgf_out_msg = "<not saved>"
        print(
            f"{tag}\tGame finished in {time.time()-start_time:.1f}s @ move {game.current_node.depth} {game.current_node.format_score()} -> {sgf_out_msg}",
            file=sys.stderr,
        )
        score = game.current_node.score
        if score > 0.3:
            black.elo_comp.beat(white.elo_comp)
        elif score < -0.3:
            white.elo_comp.beat(black.elo_comp)
        else:
            black.elo_comp.tied(white.elo_comp)

        results[tag].append(score)
        all_results.append((black.name, white.name, score))

    except Exception as e:
        print(f"Exception in playing {tag}: {e}")
        print(f"Exception in playing {tag}: {e}", file=sys.stderr)
        traceback.print_exc()
        traceback.print_exc(file=sys.stderr)
Esempio n. 7
0
def notify_user(chat_id, post):
    try:
        bot = telegram.Bot(settings.api_token)
        text = '*{}*\n[{}]({})\n[comments]({})'.format(post['title'],
                                                       post['url'],
                                                       post['url'],
                                                       post['link'])
        bot.send_message(chat_id=chat_id,
                         text=text,
                         parse_mode=telegram.ParseMode.MARKDOWN)

    except Exception as exc:
        Logger().logger().error(exc)
Esempio n. 8
0
    AI_INFLUENCE,
)
from katrain.core.engine import KataGoEngine
from katrain.core.game import Game

from settings import Logger


class SPLogger(Logger):
    def players_info(self):
        return {bw: Player(player=bw, player_type=PLAYER_AI) for bw in "BW"}


DB_FILENAME = "tournament_ai_performance.pickle"

logger = Logger()

with open("config.json") as f:
    settings = json.load(f)
    DEFAULT_AI_SETTINGS = settings["ai"]

INIT_RATING = 1000


class AI:
    DEFAULT_ENGINE_SETTINGS = {
        "katago": "katrain/KataGo/katago",
        "model": "katrain/models/g170e-b15c192-s1672170752-d466197061.bin.gz",
        #        "config": "lowmem.cfg",
        "config": "kata_config.cfg",
        "max_visits": 1,
Esempio n. 9
0
from flask import Flask
from flask_restful import Api

from settings import Logger
from api import *

Logger.configure()

app = Flask(__name__)
api = Api(app)

api.add_resource(SecurityApi,
                 "/api/security/retrieve/",
                 endpoint="api_security_endpoint_get")

api.add_resource(SecurityApi,
                 "/api/security/check/",
                 endpoint="api_security_endpoint_post")

if __name__ == "__main__":
    app.run(debug=False)
Esempio n. 10
0
class HackerNews:
    def __init__(self):

        _db = settings.db_host.split(':')
        db_host = _db[0]
        db_port = int(_db[1])
        connection_user = ''

        if settings.db_user and settings.db_password:
            db_user = urllib.parse.quote_plus(settings.db_user)
            db_password = urllib.parse.quote_plus(settings.db_password)
            connection_user = f'{db_user}:{db_password}@'

        connection_string = f'mongodb://{connection_user}{db_host}:{db_port}'

        self.db_client = pymongo.MongoClient(connection_string,
                                             serverSelectionTimeoutMS=1000,
                                             connect=False)

        db_name = settings.db_name

        if not self.db_client:
            raise DBConnectionError('Failed to connect to the database server')

        if not db_name:
            raise DBConfigError('Database name not found in configuration')

        self.db = self.db_client[settings.db_name]
        self._logger = Logger().logger()

        #self._logger.info(f'connected to mongo: {self.client.server_info()}')

    def insert_new_posts(self):
        try:
            new_post_ids = self.get_new_post_ids()
            #find those which don't already exist:
            already_exist = self.db.posts.find({'_id': {'$in': new_post_ids}})
            already_exist = [row['_id'] for row in already_exist]

            new_post_ids = [
                post_id for post_id in new_post_ids
                if post_id not in already_exist
            ]

            posts = self.get_posts_details(new_post_ids)
            inserted = []

            for post in posts:
                try:
                    self.db.posts.insert_one(post)
                    inserted.append(post['_id'])
                except pymongo.errors.DuplicateKeyError as exc:
                    pass  # can ignore duplicate key

            self._logger.info(f'new posts inserted: {inserted}')
            return inserted

        except Exception as exc:
            self._logger.error(exc)
            raise exc

    def set_post_sent(self, post_id):
        try:
            self.db.posts.update({'_id': post_id}, {'$set': {'sent': True}})
            return True
        except Exception as exc:
            self._logger.error(exc)
            return False

    def delete_old_posts(self, older_than):
        #todo: delete posts older than specific datetime
        pass

    def get_new_post_ids(self):
        try:
            top_stories_response = requests.get(HN_TOP_STORIES)

            if top_stories_response.status_code != 200:
                raise HNRetriveCurrentPostsError

            new_post_ids = sorted(top_stories_response.json(),
                                  reverse=True)[:POSTS_LIMIT]
            self._logger.info(f'new posts received: {new_post_ids}')
            return new_post_ids

        except HNRetriveCurrentPostsError:
            self._logger.error('Could not retrieve new posts')
            return None

    def _get_multiple_posts_data(self, urls):
        try:
            with concurrent.futures.ThreadPoolExecutor(
                    max_workers=50) as executor:
                future_to_json = {
                    executor.submit(requests.get, post): post
                    for post in urls
                }

                ret = []
                for future in concurrent.futures.as_completed(future_to_json):
                    data = future.result().json()
                    if data:
                        ret.append(data)

                return ret

        except ConnectionError as exc:
            self._logger.error(exc)
        except Exception as exc:
            self._logger.error('_get_multiple_posts_data failed.')
            self._logger.error(exc)

            ret = []

        return ret

    def get_posts_details(self, post_ids):
        urls = [HN_POST_DETAILS.format(post_id) for post_id in post_ids]
        post_details = self._get_multiple_posts_data(urls)

        post_details_stripped = [{
            '_id': post['id'],
            'title': post['title'],
            'url': post.get('url', ''),
            'link': HN_POST_LINK.format(post['id']),
            'received': datetime.datetime.now(),
            'sent': False
        } for post in post_details]

        return post_details_stripped
Esempio n. 11
0
from katrain.core.sgf_parser import Move

from rank_utils import rank_game
from settings import DEFAULT_PORT, Logger, bot_strategies

os.environ["KCFG_KIVY_LOG_LEVEL"] = os.environ.get("KCFG_KIVY_LOG_LEVEL",
                                                   "warning")

bot = sys.argv[1].strip()
port = int(sys.argv[2]) if len(sys.argv) > 2 else DEFAULT_PORT
REPORT_SCORE_THRESHOLD = 1.5
MAX_WAIT_ANALYSIS = 10
MAX_PASS = 3  # after opponent passes this many times, we always pass
len_segment = 80

logger = Logger(output_level=OUTPUT_INFO)

with open("config.json") as f:
    settings = json.load(f)
    all_ai_settings = settings["ai"]
ai_strategy, x_ai_settings, x_engine_settings = bot_strategies[bot]

ENGINE_SETTINGS = {
    "katago": "",  # actual engine settings in engine_server.py
    "model": "",
    "config": "",
    "threads": "",
    "max_visits": 5,
    "max_time": 5.0,
    "_enable_ownership": ai_strategy in [AI_SIMPLE_OWNERSHIP],
    "altcommand": f"python engine_connector.py {port}",
Esempio n. 12
0
def test_logger(capsys):
    logger = Logger().logger()
    logger.debug('this is log test')
    captured = capsys.readouterr()
    assert 'this is log test' in captured.err