Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
import joblib
|
| 4 |
+
import pandas as pd
|
| 5 |
+
|
| 6 |
+
# Load model
|
| 7 |
+
model = joblib.load("speed_hit_model.pkl")
|
| 8 |
+
|
| 9 |
+
# Label decoding
|
| 10 |
+
label_reverse = {0: "Player attacks twice and counters twice",
|
| 11 |
+
1: "Enemy attacks twice and counters twice",
|
| 12 |
+
2: "Both attack once"}
|
| 13 |
+
|
| 14 |
+
# Define API input structure
|
| 15 |
+
class BattleStats(BaseModel):
|
| 16 |
+
player_speed: int
|
| 17 |
+
player_weight: int
|
| 18 |
+
player_attack_accuracy: float
|
| 19 |
+
player_hit_accuracy: float
|
| 20 |
+
player_avoidance: float
|
| 21 |
+
enemy_speed: int
|
| 22 |
+
enemy_weight: int
|
| 23 |
+
enemy_attack_accuracy: float
|
| 24 |
+
enemy_hit_accuracy: float
|
| 25 |
+
enemy_avoidance: float
|
| 26 |
+
|
| 27 |
+
# Initialize FastAPI app
|
| 28 |
+
app = FastAPI()
|
| 29 |
+
|
| 30 |
+
@app.post("/predict")
|
| 31 |
+
def predict(stats: BattleStats):
|
| 32 |
+
# Compute derived features
|
| 33 |
+
player_base_speed = stats.player_speed - stats.player_weight
|
| 34 |
+
enemy_base_speed = stats.enemy_speed - stats.enemy_weight
|
| 35 |
+
player_hit_chance = stats.player_attack_accuracy * stats.player_hit_accuracy * (1 - stats.enemy_avoidance)
|
| 36 |
+
enemy_hit_chance = stats.enemy_attack_accuracy * stats.enemy_hit_accuracy * (1 - stats.player_avoidance)
|
| 37 |
+
|
| 38 |
+
# Create DataFrame
|
| 39 |
+
features = pd.DataFrame([{
|
| 40 |
+
"Player Speed": stats.player_speed,
|
| 41 |
+
"Player Weight": stats.player_weight,
|
| 42 |
+
"Player Base Speed": player_base_speed,
|
| 43 |
+
"Player Attack Accuracy": stats.player_attack_accuracy,
|
| 44 |
+
"Player Hit Accuracy": stats.player_hit_accuracy,
|
| 45 |
+
"Player Avoidance": stats.player_avoidance,
|
| 46 |
+
"Player Hit Chance": player_hit_chance,
|
| 47 |
+
|
| 48 |
+
"Enemy Speed": stats.enemy_speed,
|
| 49 |
+
"Enemy Weight": stats.enemy_weight,
|
| 50 |
+
"Enemy Base Speed": enemy_base_speed,
|
| 51 |
+
"Enemy Attack Accuracy": stats.enemy_attack_accuracy,
|
| 52 |
+
"Enemy Hit Accuracy": stats.enemy_hit_accuracy,
|
| 53 |
+
"Enemy Avoidance": stats.enemy_avoidance,
|
| 54 |
+
"Enemy Hit Chance": enemy_hit_chance
|
| 55 |
+
}])
|
| 56 |
+
|
| 57 |
+
pred = model.predict(features)[0]
|
| 58 |
+
return {"outcome": label_reverse[pred]}
|