Skip to content

Commit 20a83c7

Browse files
committed
refactor: improve code quality and consistency across backend modules
1 parent 61f1403 commit 20a83c7

14 files changed

Lines changed: 100 additions & 88 deletions

__init__.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,8 @@ async def analyze_workflow(request):
637637
total=0,
638638
)
639639

640-
update_analysis_progress = lambda payload: self._update_analysis_progress(analysis_id, payload)
640+
def update_analysis_progress(payload):
641+
self._update_analysis_progress(analysis_id, payload)
641642

642643
# Analyze and find matches
643644
result = await asyncio.to_thread(
@@ -1483,7 +1484,10 @@ async def metadata_build_start_route(request):
14831484
requested_worker_count=worker_count,
14841485
)
14851486

1486-
update_metadata_build_progress = lambda progress_payload: self._update_metadata_build_progress(progress_id, progress_payload)
1487+
def update_metadata_build_progress(progress_payload):
1488+
self._update_metadata_build_progress(
1489+
progress_id, progress_payload
1490+
)
14871491

14881492
def is_metadata_build_cancelled():
14891493
return self.metadata_builder_progress.is_cancelled(progress_id)
@@ -1602,7 +1606,8 @@ async def get_loaded_models(request):
16021606
{"error": "Workflow JSON must be an object"}, status=400
16031607
)
16041608

1605-
update_loaded_progress = lambda *args, **kwargs: self._update_loaded_progress(loaded_id, *args, **kwargs)
1609+
def update_loaded_progress(*args, **kwargs):
1610+
self._update_loaded_progress(loaded_id, *args, **kwargs)
16061611

16071612
def get_workflow_node_count():
16081613
node_count = 0
@@ -1740,7 +1745,13 @@ def build_loaded_models_response():
17401745
total=workflow_node_count,
17411746
)
17421747

1743-
update_workflow_analysis_progress = lambda payload: self._update_workflow_analysis_progress(loaded_id, workflow_node_count, interpolate_percent, payload)
1748+
def update_workflow_analysis_progress(payload):
1749+
self._update_workflow_analysis_progress(
1750+
loaded_id,
1751+
workflow_node_count,
1752+
interpolate_percent,
1753+
payload,
1754+
)
17441755

17451756
# Analyze workflow to get all model references
17461757
all_model_refs = analyze_workflow_models(
@@ -2170,9 +2181,7 @@ def result_hash_matches(result, *, require_filename=False):
21702181
result_hash = extract_result_sha256(result)
21712182
if result_hash != provided_hash:
21722183
return False
2173-
if require_filename and not result_filename_matches(result):
2174-
return False
2175-
return True
2184+
return not require_filename or result_filename_matches(result)
21762185

21772186
def huggingface_page_url(result):
21782187
try:

core/downloader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2213,7 +2213,7 @@ def download_file(
22132213
try:
22142214
if os.path.exists(dest_path):
22152215
os.remove(dest_path)
2216-
except:
2216+
except Exception:
22172217
pass
22182218

22192219
except Exception as e:

core/log_system/logger.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ class AzLogsLogger:
298298

299299
def __new__(cls):
300300
if cls._instance is None:
301-
cls._instance = super(AzLogsLogger, cls).__new__(cls)
301+
cls._instance = super().__new__(cls)
302302
cls._instance._initialized = False
303303
return cls._instance
304304

core/matcher.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,17 +128,15 @@ def find_matches(
128128

129129
# Normalize target filename once for exact match comparisons
130130
target_norm = normalize_filename(target_filename)
131-
target_base = os.path.splitext(target_filename)[0]
132131

133132
for candidate in candidate_models:
134133
# Get filename from candidate (prefer 'filename' key, fallback to extracting from 'path' or 'relative_path')
135134
candidate_filename = candidate.get("filename")
136135
candidate_path = candidate.get("path", "") or candidate.get("relative_path", "")
137136

138137
# If no filename key, try to extract from path or relative_path
139-
if not candidate_filename:
140-
if candidate_path:
141-
candidate_filename = get_filename_from_path(candidate_path)
138+
if not candidate_filename and candidate_path:
139+
candidate_filename = get_filename_from_path(candidate_path)
142140

143141
if not candidate_filename:
144142
continue
@@ -167,10 +165,13 @@ def find_matches(
167165
if candidate_path_normalized and target_model_normalized:
168166
if candidate_path_normalized == target_model_normalized:
169167
path_match = True
170-
elif candidate_relative_path_normalized and target_model_normalized:
168+
elif (
169+
candidate_relative_path_normalized
170+
and target_model_normalized
171+
and candidate_relative_path_normalized == target_model_normalized
172+
):
171173
# Also check if relative path matches the target (which might be relative)
172-
if candidate_relative_path_normalized == target_model_normalized:
173-
path_match = True
174+
path_match = True
174175

175176
if path_match:
176177
# Exact path match after normalization = 100% confidence

core/metadata_builder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ def extract_local_header_snapshot(model_path: str) -> Dict[str, Any]:
191191
if not isinstance(metadata, dict) or not metadata:
192192
return {}
193193

194-
keys = sorted(str(key) for key in metadata.keys())
194+
keys = sorted(str(key) for key in metadata)
195195
limited_metadata = {}
196196
for key in keys[:LOCAL_HEADER_MAX_KEYS]:
197197
limited_metadata[key] = _limited_local_value(metadata.get(key))

core/path_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ def infer_safetensors_base_model(
503503

504504
tensor_keys = [
505505
str(key)
506-
for key in header_json.keys()
506+
for key in header_json
507507
if key != "__metadata__"
508508
][:max_tensor_keys]
509509
keys_text = " ".join(tensor_keys)
@@ -768,7 +768,7 @@ def extract_safetensors_header_metadata(file_path: str) -> Dict[str, Any]:
768768
metadata = {}
769769

770770
result: Dict[str, Any] = {}
771-
metadata_keys = sorted(str(key) for key in metadata.keys())
771+
metadata_keys = sorted(str(key) for key in metadata)
772772
if metadata_keys:
773773
result["header_metadata_keys"] = metadata_keys[:SAFETENSORS_METADATA_KEY_LIMIT]
774774

core/scanner.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -136,22 +136,25 @@ def scan_directory(
136136

137137
dirs[:] = filtered_dirs
138138

139-
if "folder" in (extensions or set()) and root != base_directory:
140-
if category != "diffusers" or "model_index.json" in files:
141-
try:
142-
relative_path = os.path.relpath(root, base_directory)
143-
except ValueError:
144-
relative_path = get_filename_from_path(root)
145-
146-
models.append(
147-
{
148-
"filename": get_filename_from_path(root),
149-
"path": root,
150-
"relative_path": relative_path,
151-
"category": category,
152-
"base_directory": base_directory,
153-
}
154-
)
139+
if (
140+
"folder" in (extensions or set())
141+
and root != base_directory
142+
and (category != "diffusers" or "model_index.json" in files)
143+
):
144+
try:
145+
relative_path = os.path.relpath(root, base_directory)
146+
except ValueError:
147+
relative_path = get_filename_from_path(root)
148+
149+
models.append(
150+
{
151+
"filename": get_filename_from_path(root),
152+
"path": root,
153+
"relative_path": relative_path,
154+
"category": category,
155+
"base_directory": base_directory,
156+
}
157+
)
155158

156159
for filename in files:
157160
# Check if file has a model extension

core/sources/civarchive.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2152,7 +2152,9 @@ def search_civarchive_for_file(
21522152
scored_candidates.sort(key=lambda x: x[0], reverse=True)
21532153

21542154
candidate_count = len(scored_candidates)
2155-
for candidate_index, (prelim_confidence, candidate) in enumerate(scored_candidates, start=1):
2155+
for candidate_index, (_prelim_confidence, candidate) in enumerate(
2156+
scored_candidates, start=1
2157+
):
21562158
if len(seen) >= detail_limit:
21572159
break
21582160
identity = _candidate_identity(candidate)

core/sources/civitai.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from typing import Any, Callable, Dict, List, Optional
1111
from urllib.parse import quote, urlencode
1212

13-
import requests # noqa: F401 used indirectly via unittest.mock patch targets
13+
import requests # noqa: F401 - used indirectly via unittest.mock patch targets
1414

1515
from ..log_system import create_module_logger
1616
from ..matcher import (
@@ -667,17 +667,18 @@ def resolved_version_matches(resolved: Dict[str, Any]) -> bool:
667667
)
668668
if expected_filename == filename_lower:
669669
return True
670-
if not exact_only and _filename_base_partial_match(filename_base, expected_base):
671-
return True
672-
return False
670+
return not exact_only and _filename_base_partial_match(
671+
filename_base, expected_base
672+
)
673673

674674
if preferred_version_id is not None:
675675
resolved = resolve_urn(model_id, preferred_version_id, api_key)
676-
if resolved:
677-
if resolved_version_matches(resolved) and _base_model_matches(
678-
resolved.get("base_model"), base_model_context
679-
):
680-
return build_result_from_resolved_version(resolved, preferred_version_id)
676+
if (
677+
resolved
678+
and resolved_version_matches(resolved)
679+
and _base_model_matches(resolved.get("base_model"), base_model_context)
680+
):
681+
return build_result_from_resolved_version(resolved, preferred_version_id)
681682

682683
best_resolved_result = None
683684
best_resolved_confidence = 0.0
@@ -1605,8 +1606,7 @@ def _apply_safetensors_header_metadata(
16051606
current_name = str(result.get("model_name") or "").strip()
16061607
if header_name and (
16071608
not current_name
1608-
or current_name == stem
1609-
or current_name == filename
1609+
or current_name in (stem, filename)
16101610
or result.get("source") == "local"
16111611
):
16121612
result["model_name"] = header_name

core/sources/huggingface.py

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -446,17 +446,19 @@ def _find_matching_file_in_repo(
446446
file_name_lower = file_name.lower()
447447
file_base = os.path.splitext(file_name_lower)[0]
448448

449-
if not exact_only:
450-
if filename_base in file_base or file_base in filename_base:
451-
if file_path.endswith(".safetensors") or file_path.endswith(".ckpt"):
452-
partial_match = _build_huggingface_result(
453-
repo_id,
454-
file_path,
455-
file_info,
456-
"partial",
457-
headers=headers,
458-
)
459-
break
449+
if (
450+
not exact_only
451+
and (filename_base in file_base or file_base in filename_base)
452+
and (file_path.endswith(".safetensors") or file_path.endswith(".ckpt"))
453+
):
454+
partial_match = _build_huggingface_result(
455+
repo_id,
456+
file_path,
457+
file_info,
458+
"partial",
459+
headers=headers,
460+
)
461+
break
460462

461463
return partial_match
462464

@@ -505,18 +507,17 @@ def _find_matching_file_in_author_index(
505507
file_base = os.path.splitext(file_name_lower)[0]
506508
file_path_lower = file_path.lower()
507509

508-
if filename_base in file_base or file_base in filename_base:
509-
if file_path_lower.endswith(".safetensors") or file_path_lower.endswith(
510-
".ckpt"
511-
):
512-
partial_match = _build_huggingface_result(
513-
repo_id,
514-
file_path,
515-
file_info,
516-
"partial",
517-
headers=headers,
518-
)
519-
break
510+
if (
511+
filename_base in file_base or file_base in filename_base
512+
) and file_path_lower.endswith((".safetensors", ".ckpt")):
513+
partial_match = _build_huggingface_result(
514+
repo_id,
515+
file_path,
516+
file_info,
517+
"partial",
518+
headers=headers,
519+
)
520+
break
520521

521522
return partial_match
522523

0 commit comments

Comments
 (0)