-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathtag_apps.py
More file actions
284 lines (225 loc) · 9.6 KB
/
tag_apps.py
File metadata and controls
284 lines (225 loc) · 9.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/usr/bin/env python3
"""
tag_apps.py — Reads manifest.yaml from each app subdirectory and uses a local
LM Studio model to generate up to 5 descriptive tags, writing them back to the manifest.
"""
import os
import sys
import json
import time
import argparse
import signal
import requests
import yaml
# ── Configuration ─────────────────────────────────────────────────────────────
LM_STUDIO_URL = "http://127.0.0.1:1234/v1/chat/completions"
MODEL_ID = "mistralai/ministral-3b"
MAX_TAGS = 5
REQUEST_TIMEOUT = 30 # seconds
SYSTEM_PROMPT = f"""You are an app categorization assistant for a LED display app store.
Given an app manifest, respond ONLY with a valid JSON object — no explanation, no markdown.
Format:
{{"tags": ["tag1", "tag2", "tag3"], "category": "category_name"}}
Rules:
- Return between 1 and {MAX_TAGS} lowercase tags
- Tags should be short (1-2 words max)
- Choose tags that describe the app's content and purpose
Good tag examples (use these as primary suggestions):
finance, crypto, weather, sports, news, art, clock, transit, fun, utility,
productivity, nature, music, gaming, retro, health, space, food, travel,
driving, smart-home, fitness, calendar, world, calendar, stocks, tides, surf,
skiing, bike, bus, subway, train, flight, airline, tv, movies, anime,
pokemon, steam, chess, twitch, anime, horoscope, flags, history, sunrise-sunset,
moon-phase, iss, earthquakes, air-quality, uv, pollen, temperature, thermostat,
home-assistant, plex, tesla, wifi, qr-code, pets, jokes, nfl, nba, nhl, mlb,
soccer, football, baseball, basketball, hockey, formula-1, tennis, golf,
nascar, cricket, rugby, mls, epl, espn, standings, scores
Category must be one of (use these as primary suggestions):
news, weather, clocks, sports, finance, entertainment, health, lifestyle,
technology, science, utilities, gaming, art, music, food, travel, shopping,
social, education, reference, transit, driving, smart-home, community
Do NOT include the app name as a tag
- Do NOT wrap output in markdown fences
"""
# ── LM Studio call ─────────────────────────────────────────────────────────────
def query_lm_studio(manifest_text: str) -> tuple[list[str], str]:
payload = {
"model": MODEL_ID,
"messages": [
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": f"App manifest:\n\n{manifest_text}"},
],
"temperature": 0.2, # low temp for consistent classification
"max_tokens": 150,
}
try:
resp = requests.post(LM_STUDIO_URL, json=payload, timeout=REQUEST_TIMEOUT)
resp.raise_for_status()
except requests.exceptions.ConnectionError:
print(" ✗ Cannot connect to LM Studio — is it running on port 1234?")
return [], ""
except requests.exceptions.HTTPError as e:
print(f" ✗ HTTP error: {e}")
return [], ""
except requests.exceptions.Timeout:
print(" ✗ Request timed out")
return [], ""
raw = resp.json()["choices"][0]["message"]["content"].strip()
# Strip accidental markdown fences if the model adds them anyway
if raw.startswith("```"):
raw = "\n".join(
line for line in raw.splitlines() if not line.strip().startswith("```")
).strip()
try:
data = json.loads(raw)
tags = data.get("tags", [])
category = data.get("category", "")
# Sanitise: lowercase, strip whitespace, limit count
return (
[str(t).lower().strip() for t in tags[:MAX_TAGS] if t],
str(category).lower().strip() if category else "",
)
except json.JSONDecodeError:
print(f" ✗ Model returned non-JSON: {raw!r}")
return [], ""
# ── Manifest helpers ───────────────────────────────────────────────────────────
def load_manifest(path: str) -> dict:
with open(path, "r", encoding="utf-8") as f:
return yaml.safe_load(f) or {}
def save_manifest(path: str, manifest: dict, tags: list, category: str) -> None:
# Remove existing tags/category keys if present
manifest_copy = {k: v for k, v in manifest.items() if k not in ("tags", "category")}
# Add new tags and category
if category:
manifest_copy["category"] = category
manifest_copy["tags"] = tags
class NoWrapDumper(yaml.SafeDumper):
def write_line_break(self, data=None):
return super().write_line_break(data)
def increase_indent(self, flow=False, indentless=False):
return super().increase_indent(flow, False)
with open(path, "w", encoding="utf-8") as f:
f.write("---\n")
yaml.dump(
manifest_copy,
f,
Dumper=NoWrapDumper,
default_flow_style=False,
allow_unicode=True,
sort_keys=False,
width=float("inf"),
)
# ── Main loop ──────────────────────────────────────────────────────────────────
def process_apps(root_dir: str, overwrite: bool, dry_run: bool) -> None:
root_dir = os.path.abspath(root_dir)
if not os.path.isdir(root_dir):
print(f"Error: '{root_dir}' is not a directory.")
sys.exit(1)
# Collect all app directories that contain a manifest.yaml
app_dirs = sorted(
[
entry.path
for entry in os.scandir(root_dir)
if entry.is_dir()
and os.path.isfile(os.path.join(entry.path, "manifest.yaml"))
]
)
if not app_dirs:
print(f"No subdirectories with manifest.yaml found in '{root_dir}'.")
sys.exit(0)
print(f"Found {len(app_dirs)} app(s) in '{root_dir}'\n")
if dry_run:
print(" *** DRY RUN — manifests will NOT be modified ***\n")
ok_count = 0
skip_count = 0
fail_count = 0
for i, app_dir in enumerate(app_dirs, start=1):
app_name = os.path.basename(app_dir)
manifest_path = os.path.join(app_dir, "manifest.yaml")
print(f"[{i}/{len(app_dirs)}] {app_name}")
manifest = load_manifest(manifest_path)
# Skip if tags key already exists in manifest (unless --overwrite is set)
if "tags" in manifest and not overwrite:
existing_tags = manifest.get("tags", [])
print(f" → skipping (already has tags: {existing_tags})")
skip_count += 1
continue
# Serialise manifest to text for the prompt (exclude existing tags to avoid bias)
manifest_for_prompt = {k: v for k, v in manifest.items() if k != "tags"}
manifest_text = yaml.dump(
manifest_for_prompt, default_flow_style=False, allow_unicode=True
)
tags, category = query_lm_studio(manifest_text)
# Override category to "clocks" if app is a clock
app_name_lower = app_name.lower()
if (
"clock" in app_name_lower
or app_name_lower.endswith("time")
or "world clock" in app_name_lower
):
category = "clocks"
# Override category to "weather" if app is weather-related
weather_keywords = [
"weather",
"temperature",
"temp",
"forecast",
"rain",
"sun",
"snow",
"storm",
"wind",
"humidity",
"uv",
"pollen",
"air quality",
"tide",
"surf",
"ski",
"snow",
]
if any(kw in app_name_lower for kw in weather_keywords):
category = "weather"
if not tags:
print(" ✗ No tags returned — skipping")
fail_count += 1
continue
if manifest.get("supports2x"):
tags.append("wide 2x support")
print(f" ✓ category: {category}, tags: {tags}")
if not dry_run:
save_manifest(manifest_path, manifest, tags, category)
ok_count += 1
# Small delay to avoid hammering the local server
if i < len(app_dirs):
time.sleep(0.3)
print(f"\nDone — {ok_count} tagged, {skip_count} skipped, {fail_count} failed.")
# ── CLI ────────────────────────────────────────────────────────────────────────
def main():
parser = argparse.ArgumentParser(
description="Tag LED display app manifests using a local LM Studio model."
)
parser.add_argument(
"apps_dir",
nargs="?",
default=".",
help="Root directory containing app subdirectories (default: current dir)",
)
parser.add_argument(
"--overwrite",
action="store_true",
help="Re-tag apps that already have tags",
)
parser.add_argument(
"--dry-run",
action="store_true",
help="Print tags without writing them to manifest.yaml",
)
args = parser.parse_args()
def signal_handler(sig, frame):
print("\n\nInterrupted. Quitting gracefully.")
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
process_apps(args.apps_dir, overwrite=args.overwrite, dry_run=args.dry_run)
if __name__ == "__main__":
main()