Skip to content

Commit 327f864

Browse files
refactor: Improve future handling in AI rating and best shot workers
1 parent 5d9c5be commit 327f864

File tree

3 files changed

+6
-35
lines changed

3 files changed

+6
-35
lines changed

src/core/ai/best_shot_pipeline.py

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -300,37 +300,6 @@ def _call_llm(
300300
content = getattr(message, "content", None) or ""
301301
return message, content
302302

303-
def _extract_rating(self, analysis: str) -> Optional[int]:
304-
if not analysis:
305-
return None
306-
307-
# Try JSON parsing first, as the prompt requests structured output
308-
try:
309-
parsed = json.loads(analysis)
310-
if isinstance(parsed, dict) and "rating" in parsed:
311-
return int(round(float(parsed["rating"])))
312-
except (ValueError, TypeError, json.JSONDecodeError):
313-
# Fall back to unstructured parsing if the model returned plain text.
314-
pass
315-
316-
patterns = [
317-
r"\brating\b[^0-9]*([1-5](?:\.[0-9]+)?)",
318-
r"\boverall rating\b[^0-9]*([1-5](?:\.[0-9]+)?)",
319-
r"\bscore\b[^0-9]*([1-5](?:\.[0-9]+)?)",
320-
r"([1-5])\s*/\s*5",
321-
r"([1-5])\s*out of\s*5",
322-
r"([1-5])\s*stars",
323-
]
324-
for pattern in patterns:
325-
match = re.search(pattern, analysis, re.IGNORECASE)
326-
if match:
327-
try:
328-
return int(round(float(match.group(1))))
329-
except (ValueError, TypeError):
330-
continue
331-
332-
return None
333-
334303
def validate_connection(self) -> None:
335304
probe_timeout = min(max(5, int(self._timeout * 0.25)), max(self._timeout, 5))
336305
client = self._with_timeout(probe_timeout)

src/workers/ai_rating_worker.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,9 @@ def run(self) -> None:
154154
start_time = time.perf_counter()
155155
try:
156156
for path in self.image_paths:
157-
futures[executor.submit(self._rate_single, path)] = path
157+
future = executor.submit(self._rate_single, path)
158+
with self._executor_lock:
159+
futures[future] = path
158160

159161
processed = 0
160162
for future in as_completed(futures):

workers/best_shot_worker.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,9 +374,9 @@ def run(self):
374374
break
375375
if not paths:
376376
continue
377-
futures[
378-
executor.submit(self._analyze_cluster, cluster_id, paths)
379-
] = cluster_id
377+
future = executor.submit(self._analyze_cluster, cluster_id, paths)
378+
with self._executor_lock:
379+
futures[future] = cluster_id
380380

381381
total_jobs = len(futures)
382382
if total_jobs == 0:

0 commit comments

Comments
 (0)