Spaces:
Running
Running
Commit
·
e9b675c
1
Parent(s):
5e2289d
Workflow to ensure codebase integrity
Browse files
.github/workflows/restore_cost_benefit.yml
CHANGED
@@ -1,97 +1,89 @@
|
|
1 |
-
|
|
|
2 |
|
3 |
on:
|
4 |
push:
|
5 |
-
branches:
|
6 |
-
- main
|
7 |
pull_request:
|
8 |
-
branches:
|
9 |
-
- main
|
10 |
|
11 |
jobs:
|
12 |
-
restore
|
13 |
runs-on: ubuntu-latest
|
|
|
|
|
|
|
14 |
|
15 |
steps:
|
16 |
-
- name: Checkout
|
17 |
uses: actions/checkout@v3
|
|
|
|
|
|
|
18 |
|
19 |
- name: Recreate src/cost_benefit.py
|
20 |
run: |
|
21 |
mkdir -p src
|
22 |
-
cat
|
23 |
-
import argparse
|
24 |
-
|
25 |
-
import
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
def get_best_model(runtime_env: str, use_local_only=False, use_api_only=False) -> dict:
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
"note": "Defaulted due to no models fitting filters"
|
77 |
-
}
|
78 |
-
|
79 |
-
return {
|
80 |
-
"model": best_model,
|
81 |
-
"token_cost": static_costs[best_model]["token_cost"],
|
82 |
-
"tokens_sec": static_costs[best_model]["tokens_sec"]
|
83 |
-
}
|
84 |
-
EOF
|
85 |
-
|
86 |
-
- name: Commit and push if src/cost_benefit.py changed
|
87 |
run: |
|
88 |
-
git config user.name
|
89 |
git config user.email "github-actions@github.com"
|
90 |
-
|
91 |
if ! git diff --quiet src/cost_benefit.py; then
|
92 |
-
git add
|
93 |
-
git commit -m "
|
94 |
-
git push
|
95 |
else
|
96 |
-
echo "No changes
|
97 |
-
fi
|
|
|
1 |
+
# .github/workflows/restore_cost_benefit.yml
|
2 |
+
name: Restore src/cost_benefit.py
|
3 |
|
4 |
on:
|
5 |
push:
|
6 |
+
branches: [main]
|
|
|
7 |
pull_request:
|
8 |
+
branches: [main]
|
|
|
9 |
|
10 |
jobs:
|
11 |
+
restore:
|
12 |
runs-on: ubuntu-latest
|
13 |
+
# allow the GITHUB_TOKEN to push a commit back
|
14 |
+
permissions:
|
15 |
+
contents: write
|
16 |
|
17 |
steps:
|
18 |
+
- name: Checkout repository
|
19 |
uses: actions/checkout@v3
|
20 |
+
with:
|
21 |
+
# you need the token available for the later push
|
22 |
+
token: ${{ secrets.GITHUB_TOKEN }}
|
23 |
|
24 |
- name: Recreate src/cost_benefit.py
|
25 |
run: |
|
26 |
mkdir -p src
|
27 |
+
cat > src/cost_benefit.py <<'PY'
|
28 |
+
import argparse
|
29 |
+
import subprocess
|
30 |
+
import time
|
31 |
+
import requests
|
32 |
+
|
33 |
+
def detect_available_budget(runtime_env: str) -> int:
|
34 |
+
"""
|
35 |
+
Return an approximate VRAM‑based budget (MB) when running locally,
|
36 |
+
else default to 100.
|
37 |
+
"""
|
38 |
+
import torch
|
39 |
+
if "local" in runtime_env and torch.cuda.is_available():
|
40 |
+
total_vram_mb = torch.cuda.get_device_properties(0).total_memory // (1024 ** 2)
|
41 |
+
return min(total_vram_mb, 100)
|
42 |
+
return 100
|
43 |
+
|
44 |
+
def get_best_model(runtime_env: str, *, use_local_only: bool = False, use_api_only: bool = False) -> dict:
|
45 |
+
"""
|
46 |
+
Pick the fastest model that fits in the detected budget while
|
47 |
+
respecting the locality filters.
|
48 |
+
"""
|
49 |
+
static_costs = {
|
50 |
+
"llama3.2": {"size": 20, "token_cost": 0.0001, "tokens_sec": 30, "type": "local"},
|
51 |
+
"mistral": {"size": 40, "token_cost": 0.0002, "tokens_sec": 50, "type": "local"},
|
52 |
+
"gemini-2.0-flash": {"size": 60, "token_cost": 0.0005, "tokens_sec": 60, "type": "api"},
|
53 |
+
"gemini-2.5-pro-preview-03-25": {"size": 80, "token_cost": 0.002 , "tokens_sec": 45, "type": "api"},
|
54 |
+
}
|
55 |
+
|
56 |
+
budget = detect_available_budget(runtime_env)
|
57 |
+
best_model, best_speed = None, -1
|
58 |
+
|
59 |
+
for model, info in static_costs.items():
|
60 |
+
if info["size"] > budget:
|
61 |
+
continue
|
62 |
+
if use_local_only and info["type"] != "local":
|
63 |
+
continue
|
64 |
+
if use_api_only and info["type"] != "api":
|
65 |
+
continue
|
66 |
+
if info["tokens_sec"] > best_speed:
|
67 |
+
best_model, best_speed = model, info["tokens_sec"]
|
68 |
+
|
69 |
+
chosen = best_model or "llama3.2" # sensible default
|
70 |
+
return {
|
71 |
+
"model": chosen,
|
72 |
+
"token_cost": static_costs[chosen]["token_cost"],
|
73 |
+
"tokens_sec": static_costs[chosen]["tokens_sec"],
|
74 |
+
"note": None if best_model else "Defaulted because no model met the constraints",
|
75 |
+
}
|
76 |
+
PY
|
77 |
+
|
78 |
+
# only push when the workflow is triggered by a direct push to main
|
79 |
+
- name: Commit & push if the file changed
|
80 |
+
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
run: |
|
82 |
+
git config user.name "github-actions"
|
83 |
git config user.email "github-actions@github.com"
|
|
|
84 |
if ! git diff --quiet src/cost_benefit.py; then
|
85 |
+
git add src/cost_benefit.py
|
86 |
+
git commit -m "chore(ci): auto‑restore src/cost_benefit.py [skip ci]"
|
87 |
+
git push origin HEAD:main
|
88 |
else
|
89 |
+
echo "No changes detected"
|
|