Skip to content

Commit

Permalink
ruff check --fix
Browse files Browse the repository at this point in the history
  • Loading branch information
extreme4all committed Feb 3, 2024
1 parent 70620c9 commit 5ef27fb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
13 changes: 5 additions & 8 deletions api/app.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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"}
Expand All @@ -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()
Expand All @@ -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
Expand All @@ -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"])
Expand Down
18 changes: 9 additions & 9 deletions api/cogs/predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,40 @@ 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(
binary_pred, index=hiscores.index, columns=["Real_Player", "Unknown_bot"]
)

# 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")

Expand All @@ -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


Expand All @@ -83,15 +83,15 @@ 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)
output["created"] = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())
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"
Expand Down
22 changes: 15 additions & 7 deletions notes.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
```

0 comments on commit 5ef27fb

Please sign in to comment.