Upload folder using huggingface_hub
Browse files- .gitignore +36 -0
- Dockerfile +42 -0
- README.md +0 -12
- poetry.lock +0 -0
- pyproject.toml +22 -0
- scoutai/__init__.py +1 -0
- scoutai/main.py +168 -0
- scoutai/models/json/score_predictor.json +0 -0
- scoutai/models/json/win_predictor.json +0 -0
- scoutai/models/score_predictor.joblib +3 -0
- scoutai/models/win_predictor.joblib +3 -0
- scoutai/services.py +102 -0
.gitignore
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Python
|
| 2 |
+
__pycache__/
|
| 3 |
+
*.pyc
|
| 4 |
+
*.pyo
|
| 5 |
+
*.pyd
|
| 6 |
+
.Python
|
| 7 |
+
env/
|
| 8 |
+
venv/
|
| 9 |
+
.venv/
|
| 10 |
+
dist/
|
| 11 |
+
build/
|
| 12 |
+
*.egg-info/
|
| 13 |
+
|
| 14 |
+
# IDE
|
| 15 |
+
.vscode/
|
| 16 |
+
.idea/
|
| 17 |
+
*.swp
|
| 18 |
+
*.swo
|
| 19 |
+
|
| 20 |
+
# OS
|
| 21 |
+
.DS_Store
|
| 22 |
+
Thumbs.db
|
| 23 |
+
|
| 24 |
+
# Creds
|
| 25 |
+
credentials.json
|
| 26 |
+
client_secret.json
|
| 27 |
+
service.json
|
| 28 |
+
service_priority.json
|
| 29 |
+
.env
|
| 30 |
+
|
| 31 |
+
# Data
|
| 32 |
+
*.csv
|
| 33 |
+
data/
|
| 34 |
+
|
| 35 |
+
# Logs
|
| 36 |
+
*.log
|
Dockerfile
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Stage 1: Builder
|
| 2 |
+
FROM python:3.12-slim as builder
|
| 3 |
+
|
| 4 |
+
RUN apt-get update && apt-get install -y gcc
|
| 5 |
+
|
| 6 |
+
RUN pip install poetry==1.8.3
|
| 7 |
+
|
| 8 |
+
ENV POETRY_NO_INTERACTION=1 \
|
| 9 |
+
POETRY_VIRTUALENVS_IN_PROJECT=1 \
|
| 10 |
+
POETRY_VIRTUALENVS_CREATE=1 \
|
| 11 |
+
POETRY_CACHE_DIR=/tmp/poetry_cache
|
| 12 |
+
|
| 13 |
+
WORKDIR /app
|
| 14 |
+
|
| 15 |
+
COPY pyproject.toml poetry.lock ./
|
| 16 |
+
|
| 17 |
+
# Install dependencies (flag --no-root is important here)
|
| 18 |
+
RUN poetry install --no-root && rm -rf $POETRY_CACHE_DIR
|
| 19 |
+
|
| 20 |
+
# Stage 2: Runtime
|
| 21 |
+
FROM python:3.12-slim as runtime
|
| 22 |
+
|
| 23 |
+
WORKDIR /app
|
| 24 |
+
|
| 25 |
+
ENV VIRTUAL_ENV=/app/.venv \
|
| 26 |
+
PATH="/app/.venv/bin:$PATH"
|
| 27 |
+
|
| 28 |
+
# 1. Create a user for Hugging Face (ID 1000) to avoid permission errors
|
| 29 |
+
RUN useradd -m -u 1000 user
|
| 30 |
+
USER user
|
| 31 |
+
ENV HOME=/home/user \
|
| 32 |
+
PATH=/home/user/.local/bin:$PATH
|
| 33 |
+
|
| 34 |
+
# Copy the environment from builder
|
| 35 |
+
COPY --from=builder --chown=user:user ${VIRTUAL_ENV} ${VIRTUAL_ENV}
|
| 36 |
+
|
| 37 |
+
# Copy your application code
|
| 38 |
+
COPY --chown=user:user . .
|
| 39 |
+
|
| 40 |
+
# 2. CHANGE PORT TO 7860 (Mandatory for Hugging Face)
|
| 41 |
+
# Also updated the import path to match your folder structure 'frc_ai_backend'
|
| 42 |
+
CMD ["uvicorn", "scoutai.main:app", "--host", "0.0.0.0", "--port", "7860"]
|
README.md
CHANGED
|
@@ -1,12 +0,0 @@
|
|
| 1 |
-
---
|
| 2 |
-
title: Scoutai
|
| 3 |
-
emoji: 📚
|
| 4 |
-
colorFrom: yellow
|
| 5 |
-
colorTo: red
|
| 6 |
-
sdk: docker
|
| 7 |
-
pinned: false
|
| 8 |
-
license: apache-2.0
|
| 9 |
-
short_description: FRC Scouting Match Predictor
|
| 10 |
-
---
|
| 11 |
-
|
| 12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
poetry.lock
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
pyproject.toml
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[tool.poetry]
|
| 2 |
+
name = "scoutai"
|
| 3 |
+
version = "0.1.0"
|
| 4 |
+
description = ""
|
| 5 |
+
authors = ["mresath <mresath@outlook.com>"]
|
| 6 |
+
readme = "README.md"
|
| 7 |
+
|
| 8 |
+
[tool.poetry.dependencies]
|
| 9 |
+
python = "^3.12"
|
| 10 |
+
fastapi = "^0.124.4"
|
| 11 |
+
uvicorn = "^0.38.0"
|
| 12 |
+
xgboost = "^3.1.2"
|
| 13 |
+
pandas = "^2.3.3"
|
| 14 |
+
scikit-learn = "^1.8.0"
|
| 15 |
+
numpy = "^2.3.5"
|
| 16 |
+
gspread = "^6.2.1"
|
| 17 |
+
oauth2client = "^4.1.3"
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
[build-system]
|
| 21 |
+
requires = ["poetry-core"]
|
| 22 |
+
build-backend = "poetry.core.masonry.api"
|
scoutai/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
__version__ = "0.1.0"
|
scoutai/main.py
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import time
|
| 3 |
+
|
| 4 |
+
from fastapi import FastAPI, HTTPException
|
| 5 |
+
from starlette.middleware.cors import CORSMiddleware
|
| 6 |
+
|
| 7 |
+
import joblib
|
| 8 |
+
from pydantic import BaseModel
|
| 9 |
+
import pandas as pd
|
| 10 |
+
|
| 11 |
+
from .services import SheetManager
|
| 12 |
+
|
| 13 |
+
# Initialize App
|
| 14 |
+
|
| 15 |
+
startup_timestamp = time.time()
|
| 16 |
+
|
| 17 |
+
app = FastAPI(
|
| 18 |
+
title="ScoutAI",
|
| 19 |
+
description="FRC Scouting Match Predictor API",
|
| 20 |
+
version="1.0",
|
| 21 |
+
openapi_url="/v3/api-docs",
|
| 22 |
+
root_path="/api"
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
app.add_middleware(
|
| 26 |
+
CORSMiddleware,
|
| 27 |
+
allow_origins=["*"],
|
| 28 |
+
allow_credentials=True,
|
| 29 |
+
allow_methods=["*"],
|
| 30 |
+
allow_headers=["*"]
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
# Load Models
|
| 34 |
+
|
| 35 |
+
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 36 |
+
WIN_MODEL_PATH = os.path.join(BASE_DIR, "models/win_predictor.joblib")
|
| 37 |
+
SCORE_MODEL_PATH = os.path.join(BASE_DIR, "models/score_predictor.joblib")
|
| 38 |
+
|
| 39 |
+
print(f"Loading models from: {BASE_DIR}")
|
| 40 |
+
|
| 41 |
+
try:
|
| 42 |
+
# Use joblib.load instead of xgb.load_model
|
| 43 |
+
win_model = joblib.load(WIN_MODEL_PATH)
|
| 44 |
+
score_model = joblib.load(SCORE_MODEL_PATH)
|
| 45 |
+
print("Models Loaded via Joblib.")
|
| 46 |
+
except Exception as e:
|
| 47 |
+
print(f"Error loading models: {e}")
|
| 48 |
+
|
| 49 |
+
# Start Sheet Manager
|
| 50 |
+
|
| 51 |
+
try:
|
| 52 |
+
sheet_manager = SheetManager()
|
| 53 |
+
print("Google Sheets Connected.")
|
| 54 |
+
except Exception as e:
|
| 55 |
+
print(f"Warning: Sheets not connected. {e}")
|
| 56 |
+
sheet_manager = None
|
| 57 |
+
|
| 58 |
+
# Data Models
|
| 59 |
+
|
| 60 |
+
class AllianceStats(BaseModel):
|
| 61 |
+
auto: float
|
| 62 |
+
teleop: float
|
| 63 |
+
endgame: float
|
| 64 |
+
|
| 65 |
+
class MatchRequest(BaseModel):
|
| 66 |
+
red: AllianceStats
|
| 67 |
+
blue: AllianceStats
|
| 68 |
+
|
| 69 |
+
class LiveMatchRequest(BaseModel):
|
| 70 |
+
sheet_id: str
|
| 71 |
+
red_teams: list[int] # [254, 1678, 4414]
|
| 72 |
+
blue_teams: list[int] # [1323, 2910, 973]
|
| 73 |
+
|
| 74 |
+
# Endpoints
|
| 75 |
+
|
| 76 |
+
@app.get("/")
|
| 77 |
+
async def root():
|
| 78 |
+
return {"message": f"Hello World. API Uptime: {int(time.time() - startup_timestamp)}"}
|
| 79 |
+
|
| 80 |
+
@app.post("/predict")
|
| 81 |
+
def predict_match(match: MatchRequest):
|
| 82 |
+
if not win_model or not score_model:
|
| 83 |
+
raise HTTPException(status_code=500, detail="Models not loaded")
|
| 84 |
+
|
| 85 |
+
input_features = pd.DataFrame([{
|
| 86 |
+
'actual_red_auto': match.red.auto,
|
| 87 |
+
'actual_red_teleop': match.red.teleop,
|
| 88 |
+
'actual_red_endgame': match.red.endgame,
|
| 89 |
+
'actual_blue_auto': match.blue.auto,
|
| 90 |
+
'actual_blue_teleop': match.blue.teleop,
|
| 91 |
+
'actual_blue_endgame': match.blue.endgame
|
| 92 |
+
}])
|
| 93 |
+
|
| 94 |
+
win_prob = win_model.predict_proba(input_features)[0][1]
|
| 95 |
+
|
| 96 |
+
pred_red_score = score_model.predict(input_features)[0]
|
| 97 |
+
|
| 98 |
+
input_flipped = pd.DataFrame([{
|
| 99 |
+
'actual_red_auto': match.blue.auto,
|
| 100 |
+
'actual_red_teleop': match.blue.teleop,
|
| 101 |
+
'actual_red_endgame': match.blue.endgame,
|
| 102 |
+
'actual_blue_auto': match.red.auto,
|
| 103 |
+
'actual_blue_teleop': match.red.teleop,
|
| 104 |
+
'actual_blue_endgame': match.red.endgame
|
| 105 |
+
}])
|
| 106 |
+
pred_blue_score = score_model.predict(input_flipped)[0]
|
| 107 |
+
|
| 108 |
+
return {
|
| 109 |
+
"winner_prediction": "Red" if win_prob > 0.5 else "Blue",
|
| 110 |
+
"red_win_confidence": float(win_prob),
|
| 111 |
+
"predicted_score": {
|
| 112 |
+
"red": float(pred_red_score),
|
| 113 |
+
"blue": float(pred_blue_score)
|
| 114 |
+
}
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
@app.post("/predict-live")
|
| 118 |
+
def predict_live_match(match: LiveMatchRequest, use_scouting: bool = True):
|
| 119 |
+
if not sheet_manager:
|
| 120 |
+
raise HTTPException(status_code=503, detail="Google Sheets not configured")
|
| 121 |
+
|
| 122 |
+
red_stats = {"auto": 0.0, "teleop": 0.0, "endgame": 0.0}
|
| 123 |
+
for team in match.red_teams:
|
| 124 |
+
s = sheet_manager.get_team_stats(match.sheet_id, team, use_scouting)
|
| 125 |
+
red_stats["auto"] += s["auto"]
|
| 126 |
+
red_stats["teleop"] += s["teleop"]
|
| 127 |
+
red_stats["endgame"] += s["endgame"]
|
| 128 |
+
|
| 129 |
+
blue_stats = {"auto": 0.0, "teleop": 0.0, "endgame": 0.0}
|
| 130 |
+
for team in match.blue_teams:
|
| 131 |
+
s = sheet_manager.get_team_stats(match.sheet_id, team, use_scouting)
|
| 132 |
+
blue_stats["auto"] += s["auto"]
|
| 133 |
+
blue_stats["teleop"] += s["teleop"]
|
| 134 |
+
blue_stats["endgame"] += s["endgame"]
|
| 135 |
+
|
| 136 |
+
input_features = pd.DataFrame([{
|
| 137 |
+
'actual_red_auto': red_stats["auto"],
|
| 138 |
+
'actual_red_teleop': red_stats["teleop"],
|
| 139 |
+
'actual_red_endgame': red_stats["endgame"],
|
| 140 |
+
'actual_blue_auto': blue_stats["auto"],
|
| 141 |
+
'actual_blue_teleop': blue_stats["teleop"],
|
| 142 |
+
'actual_blue_endgame': blue_stats["endgame"]
|
| 143 |
+
}])
|
| 144 |
+
|
| 145 |
+
win_prob = win_model.predict_proba(input_features)[0][1]
|
| 146 |
+
|
| 147 |
+
pred_red_score = score_model.predict(input_features)[0]
|
| 148 |
+
|
| 149 |
+
input_flipped = pd.DataFrame([{
|
| 150 |
+
'actual_red_auto': blue_stats["auto"],
|
| 151 |
+
'actual_red_teleop': blue_stats["teleop"],
|
| 152 |
+
'actual_red_endgame': blue_stats["endgame"],
|
| 153 |
+
'actual_blue_auto': red_stats["auto"],
|
| 154 |
+
'actual_blue_teleop': red_stats["teleop"],
|
| 155 |
+
'actual_blue_endgame': red_stats["endgame"]
|
| 156 |
+
}])
|
| 157 |
+
pred_blue_score = score_model.predict(input_flipped)[0]
|
| 158 |
+
|
| 159 |
+
return {
|
| 160 |
+
"winner": "Red" if win_prob > 0.5 else "Blue",
|
| 161 |
+
"red_win_confidence": float(win_prob),
|
| 162 |
+
"red_alliance_stats": red_stats,
|
| 163 |
+
"blue_alliance_stats": blue_stats,
|
| 164 |
+
"predicted_score": {
|
| 165 |
+
"red": float(pred_red_score),
|
| 166 |
+
"blue": float(pred_blue_score)
|
| 167 |
+
}
|
| 168 |
+
}
|
scoutai/models/json/score_predictor.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
scoutai/models/json/win_predictor.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
scoutai/models/score_predictor.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6d114969cba217a298012520921d94342cb322f24494271bd4ad60d90878687e
|
| 3 |
+
size 531016
|
scoutai/models/win_predictor.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3340db7b87011aeb6add3eb5b05f57178c9ebe7f11fa5b8cfccfd7d419f7140f
|
| 3 |
+
size 542255
|
scoutai/services.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
import re
|
| 4 |
+
import requests
|
| 5 |
+
|
| 6 |
+
import gspread
|
| 7 |
+
import pandas as pd
|
| 8 |
+
from oauth2client.service_account import ServiceAccountCredentials
|
| 9 |
+
|
| 10 |
+
class SheetManager:
|
| 11 |
+
def __init__(self, creds_file="service.json"):
|
| 12 |
+
|
| 13 |
+
scope = [
|
| 14 |
+
"https://www.googleapis.com/auth/spreadsheets",
|
| 15 |
+
"https://www.googleapis.com/auth/drive"
|
| 16 |
+
]
|
| 17 |
+
|
| 18 |
+
json_creds = os.environ.get("SERVICE")
|
| 19 |
+
|
| 20 |
+
if json_creds:
|
| 21 |
+
print("Loading credentials from Environment Variable...")
|
| 22 |
+
creds_dict = json.loads(json_creds)
|
| 23 |
+
creds = ServiceAccountCredentials.from_json_keyfile_dict(creds_dict, scope)
|
| 24 |
+
else:
|
| 25 |
+
base_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
| 26 |
+
creds_path = os.path.join(base_path, creds_file)
|
| 27 |
+
|
| 28 |
+
if not os.path.exists(creds_path):
|
| 29 |
+
raise FileNotFoundError(f"Missing {creds_file}. Put it in the project root.")
|
| 30 |
+
|
| 31 |
+
creds = ServiceAccountCredentials.from_json_keyfile_name(creds_path, scope)
|
| 32 |
+
|
| 33 |
+
self.client = gspread.authorize(creds)
|
| 34 |
+
|
| 35 |
+
try:
|
| 36 |
+
response = requests.get("https://sheets.googleapis.com", timeout=5)
|
| 37 |
+
print("Google API is reachable.")
|
| 38 |
+
print("Status Code:", response.status_code)
|
| 39 |
+
except Exception as e:
|
| 40 |
+
print("Internet or Firewall issue:", e)
|
| 41 |
+
|
| 42 |
+
def extract_sheet_id(self, sheet_id_or_url: str) -> str:
|
| 43 |
+
"""
|
| 44 |
+
Extracts the ID if the user accidentally pasted the full URL.
|
| 45 |
+
"""
|
| 46 |
+
# If it looks like a URL, grab the ID between /d/ and /
|
| 47 |
+
match = re.search(r"/d/([a-zA-Z0-9-_]+)", sheet_id_or_url)
|
| 48 |
+
if match:
|
| 49 |
+
return match.group(1)
|
| 50 |
+
return sheet_id_or_url
|
| 51 |
+
|
| 52 |
+
def get_team_stats(self, sheet_id: str, team_number: int, use_scouting: bool) -> dict:
|
| 53 |
+
"""
|
| 54 |
+
Fetches team stats from the 'Analysis' tab.
|
| 55 |
+
Prioritizes 'Scouting' columns, falls back to 'Statbotics' columns if scouting data is missing/zero.
|
| 56 |
+
"""
|
| 57 |
+
try:
|
| 58 |
+
sheet = self.client.open_by_key(self.extract_sheet_id(sheet_id)).worksheet("Analysis")
|
| 59 |
+
|
| 60 |
+
data = sheet.get_all_records()
|
| 61 |
+
df = pd.DataFrame(data)
|
| 62 |
+
|
| 63 |
+
team_col = 'Team' if 'Team' in df.columns else 'Team Number'
|
| 64 |
+
|
| 65 |
+
team_row = df[df[team_col] == team_number]
|
| 66 |
+
|
| 67 |
+
if team_row.empty:
|
| 68 |
+
print(f"Team {team_number} not found in Analysis tab. Defaulting to 0.")
|
| 69 |
+
return {"auto": 0.0, "teleop": 0.0, "endgame": 0.0}
|
| 70 |
+
|
| 71 |
+
def get_best_value(row, scout_col, stat_col):
|
| 72 |
+
# Try to get Scouting Data first
|
| 73 |
+
if use_scouting and scout_col in row:
|
| 74 |
+
val = pd.to_numeric(row[scout_col], errors='coerce')
|
| 75 |
+
if not pd.isna(val) and val > 0:
|
| 76 |
+
return float(val)
|
| 77 |
+
|
| 78 |
+
# Fallback to Statbotics
|
| 79 |
+
if stat_col in row:
|
| 80 |
+
val = pd.to_numeric(row[stat_col], errors='coerce')
|
| 81 |
+
if not pd.isna(val):
|
| 82 |
+
return float(val)
|
| 83 |
+
|
| 84 |
+
return 0.0
|
| 85 |
+
|
| 86 |
+
row = team_row.iloc[0]
|
| 87 |
+
|
| 88 |
+
stats = {
|
| 89 |
+
"auto": get_best_value(row, "Auto EPA (Scouting)", "Auto EPA (Statbotics)"),
|
| 90 |
+
"teleop": get_best_value(row, "Teleop EPA (Scouting)", "Teleop EPA (Statbotics)"),
|
| 91 |
+
"endgame": get_best_value(row, "Endgame EPA (Scouting)", "Endgame EPA (Statbotics)")
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
return stats
|
| 95 |
+
|
| 96 |
+
except gspread.exceptions.WorksheetNotFound:
|
| 97 |
+
print(f"Error: Tab named 'Analysis' not found in Sheet {sheet_id}")
|
| 98 |
+
return {"auto": 0.0, "teleop": 0.0, "endgame": 0.0}
|
| 99 |
+
|
| 100 |
+
except Exception as e:
|
| 101 |
+
print(f"Error fetching stats for Team {team_number}: {e}")
|
| 102 |
+
return {"auto": 0.0, "teleop": 0.0, "endgame": 0.0}
|