diff --git a/api/app.py b/api/app.py index 1b6c9bc..948de43 100644 --- a/api/app.py +++ b/api/app.py @@ -1,10 +1,8 @@ import asyncio import logging -import time from typing import List import pandas as pd -import requests from fastapi import HTTPException from pydantic import BaseModel from sklearn.model_selection import train_test_split @@ -13,7 +11,6 @@ from api.cogs import predict from api.cogs import requests as req from api.MachineLearning import classifier, data -import aiohttp from datetime import date app = config.app @@ -90,7 +87,7 @@ async def manual_startup(secret: str): await req.post_prediction(output) if len(hiscores) < config.BATCH_AMOUNT: - sleep=60 + sleep = 60 logger.info(f"{len(hiscores)=} < {config.BATCH_AMOUNT=}, sleeping: {sleep}") await asyncio.sleep(sleep) return {"detail": "ok"} @@ -105,7 +102,7 @@ async def load(secret: str): This endpoint is used by the detector api to load the latest model. """ if secret != config.secret_token: - raise HTTPException(status_code=404, detail=f"insufficient permissions") + raise HTTPException(status_code=404, detail="insufficient permissions") binary_classifier = binary_classifier.load() multi_classifier = multi_classifier.load() @@ -120,7 +117,7 @@ async def predict_player(secret: str, hiscores, name: name) -> List[dict]: """ logger.debug(f"predicting player {name}") if secret != config.secret_token: - raise HTTPException(status_code=404, detail=f"insufficient permissions") + raise HTTPException(status_code=404, detail="insufficient permissions") name = pd.DataFrame(name.dict()) output = predict.predict(hiscores, name, binary_classifier, multi_classifier) return output @@ -134,14 +131,14 @@ async def train(secret: str): """ logger.debug("training model") if secret != config.secret_token: - raise HTTPException(status_code=404, detail=f"insufficient permissions") + raise HTTPException(status_code=404, detail="insufficient permissions") labels = await req.get_labels() players = [] hiscores = [] for label in labels: - if not label["label"] in config.LABELS: + if label["label"] not in config.LABELS: continue player_data = await req.get_player_data(label_id=label["id"]) diff --git a/api/cogs/predict.py b/api/cogs/predict.py index bc6f691..24a84f2 100644 --- a/api/cogs/predict.py +++ b/api/cogs/predict.py @@ -24,12 +24,12 @@ def predict( If the binary classifier predicts that the player is a bot, then the multi classifier is used to predict the type of bot. The output is a list of dictionaries with the predictions. """ - logger.debug(f"Predicting hiscores for players") + logger.debug("Predicting hiscores for players") hiscores = data.hiscoreData(hiscores) low_level = hiscores.df_low.index hiscores = hiscores.features() - logger.debug(f"Predicting binary for players") + logger.debug("Predicting binary for players") # binary prediction binary_pred = binary_classifier.predict_proba(hiscores) binary_pred = pd.DataFrame( @@ -37,27 +37,27 @@ def predict( ) # multi prediction - logger.debug(f"Predicting multi for players") + logger.debug("Predicting multi for players") multi_pred = multi_classifier.predict_proba(hiscores) multi_pred = pd.DataFrame( multi_pred, index=hiscores.index, columns=np.unique(config.LABELS) ) # remove real players from multi - logger.debug(f"Removing real players from multi for players") + logger.debug("Removing real players from multi for players") real_players = binary_pred.query("Real_Player > 0.5").index mask = ~(multi_pred.index.isin(real_players)) multi_pred = multi_pred[mask] # remove bots from real players - logger.debug(f"Removing bots from binary for players") + logger.debug("Removing bots from binary for players") bots = multi_pred.index mask = ~(binary_pred.index.isin(bots)) binary_pred = binary_pred[mask] # combine binary & player_pred - logger.debug(f"Combining binary and multi for players") + logger.debug("Combining binary and multi for players") output = pd.DataFrame(names).set_index("id") output = output.merge(binary_pred, left_index=True, right_index=True, how="left") @@ -71,7 +71,7 @@ def predict( # cleanup predictions - logger.debug(f"Cleaning up predictions for players") + logger.debug("Cleaning up predictions for players") mask = output["Real_Player"].isna() # all multi class predictions @@ -83,7 +83,7 @@ def predict( output.fillna(0, inplace=True) # add Predictions, Predicted_confidence, created - logger.debug(f"Adding Predictions, Predicted_confidence, created for players") + logger.debug("Adding Predictions, Predicted_confidence, created for players") columns = [c for c in output.columns if c != "name"] output["Predicted_confidence"] = round(output[columns].max(axis=1) * 100, 2) output["Prediction"] = output[columns].idxmax(axis=1) @@ -91,7 +91,7 @@ def predict( output.reset_index(inplace=True) # low level player predictions are not accurate - logger.debug(f"Removing low level players for players") + logger.debug("Removing low level players for players") mask = output['id'].isin(low_level) output.loc[mask, "Prediction"] = "Stats_Too_Low" diff --git a/notes.md b/notes.md index f8d68ee..338539f 100644 --- a/notes.md +++ b/notes.md @@ -1,38 +1,46 @@ # api documentation -``` +```sh http://127.0.0.1:8000/docs http://127.0.0.1:8000/redoc ``` # extra info -``` +```sh POST: to create data. GET: to read data. PUT: to update data. DELETE: to delete data. ``` # keeping fork up to date -``` +```sh git checkout develop git pull --rebase upstream develop git push ``` # setup +## windows creating a python venv to work in and install the project requirements -``` +```sh python -m venv .venv .venv\Scripts\activate python -m pip install --upgrade pip pip install -r requirements.txt ``` +## linux +```sh +python3 -m venv .venv +source .venv/bin/activate +python -m pip install --upgrade pip +pip install -r requirements.txt +``` # for admin purposes saving & upgrading when you added some dependancies update the requirements -``` +```sh venv\Scripts\activate call pip freeze > requirements.txt ``` when you want to upgrade the dependancies -``` +```sh venv\Scripts\activate powershell "(Get-Content requirements.txt) | ForEach-Object { $_ -replace '==', '>=' } | Set-Content requirements.txt" call pip install -r requirements.txt --upgrade @@ -41,6 +49,6 @@ powershell "(Get-Content requirements.txt) | ForEach-Object { $_ -replace '>=', ``` # branch cleanup if your branch gets out of sync and for some reason you have many pushes and pulls, to become insync without pushing some random changes do this -``` +```sh git fetch https://github.com/Bot-detector/bot-detector-ML.git ``` \ No newline at end of file