Spaces:
Running
Running
Update src/aibom_generator/api.py
Browse files- src/aibom_generator/api.py +29 -21
src/aibom_generator/api.py
CHANGED
@@ -62,7 +62,8 @@ def log_sbom_generation(model_id: str):
|
|
62 |
# Try to load existing dataset to append
|
63 |
try:
|
64 |
# Use trust_remote_code=True if required by the dataset/model on HF
|
65 |
-
|
|
|
66 |
# Check if dataset is empty or has different columns (handle initial creation)
|
67 |
if len(existing_ds) > 0 and set(existing_ds.column_names) == set(log_data.keys()):
|
68 |
ds_to_push = concatenate_datasets([existing_ds, ds_new_log])
|
@@ -76,12 +77,14 @@ def log_sbom_generation(model_id: str):
|
|
76 |
ds_to_push = concatenate_datasets([existing_ds, ds_new_log])
|
77 |
|
78 |
except Exception as load_err:
|
79 |
-
# Handle case where dataset doesn
|
|
|
80 |
logger.info(f"Could not load existing dataset {HF_REPO} (may not exist yet): {load_err}. Pushing new dataset.")
|
81 |
ds_to_push = ds_new_log # ds is already prepared with the new log entry
|
82 |
|
83 |
# Push the updated or new dataset
|
84 |
-
|
|
|
85 |
logger.info(f"Successfully logged SBOM generation for {model_id} to {HF_REPO}")
|
86 |
|
87 |
except Exception as e:
|
@@ -93,9 +96,10 @@ def get_sbom_count() -> str:
|
|
93 |
logger.warning("HF_TOKEN not set. Cannot retrieve SBOM count.")
|
94 |
return "N/A"
|
95 |
try:
|
96 |
-
# Load the dataset - assumes
|
97 |
# Use trust_remote_code=True if required by the dataset/model on HF
|
98 |
-
|
|
|
99 |
count = len(ds)
|
100 |
logger.info(f"Retrieved SBOM count: {count} from {HF_REPO}")
|
101 |
# Format count for display (e.g., add commas for large numbers)
|
@@ -374,7 +378,8 @@ async def generate_form(
|
|
374 |
enhancement_report = generator.get_enhancement_report()
|
375 |
|
376 |
# Save AIBOM to file
|
377 |
-
|
|
|
378 |
filepath = os.path.join(OUTPUT_DIR, filename)
|
379 |
|
380 |
with open(filepath, "w") as f:
|
@@ -388,12 +393,13 @@ async def generate_form(
|
|
388 |
download_url = f"/output/{filename}"
|
389 |
|
390 |
# Create download and UI interaction scripts
|
|
|
391 |
download_script = f"""
|
392 |
<script>
|
393 |
function downloadJSON() {{
|
394 |
-
const a = document.createElement(
|
395 |
-
a.href =
|
396 |
-
a.download =
|
397 |
document.body.appendChild(a);
|
398 |
a.click();
|
399 |
document.body.removeChild(a);
|
@@ -401,31 +407,31 @@ async def generate_form(
|
|
401 |
|
402 |
function switchTab(tabId) {{
|
403 |
// Hide all tabs
|
404 |
-
document.querySelectorAll(
|
405 |
-
tab.classList.remove(
|
406 |
}});
|
407 |
|
408 |
// Deactivate all tab buttons
|
409 |
-
document.querySelectorAll(
|
410 |
-
button.classList.remove(
|
411 |
}});
|
412 |
|
413 |
// Show the selected tab
|
414 |
-
document.getElementById(tabId).classList.add(
|
415 |
|
416 |
// Activate the clicked button
|
417 |
-
event.currentTarget.classList.add(
|
418 |
}}
|
419 |
|
420 |
function toggleCollapsible(element) {{
|
421 |
-
element.classList.toggle(
|
422 |
var content = element.nextElementSibling;
|
423 |
if (content.style.maxHeight) {{
|
424 |
content.style.maxHeight = null;
|
425 |
-
content.classList.remove(
|
426 |
}} else {{
|
427 |
content.style.maxHeight = content.scrollHeight + "px";
|
428 |
-
content.classList.add(
|
429 |
}}
|
430 |
}}
|
431 |
</script>
|
@@ -433,15 +439,17 @@ async def generate_form(
|
|
433 |
|
434 |
# Get completeness score or create a comprehensive one if not available
|
435 |
completeness_score = None
|
436 |
-
|
|
|
437 |
try:
|
438 |
completeness_score = generator.get_completeness_score(model_id)
|
439 |
logger.info("Successfully retrieved completeness_score from generator")
|
440 |
except Exception as e:
|
441 |
logger.error(f"Completeness score error from generator: {str(e)}")
|
442 |
|
443 |
-
# If completeness_score is None or doesn
|
444 |
-
|
|
|
445 |
logger.info("Using comprehensive completeness_score with field_checklist")
|
446 |
completeness_score = create_comprehensive_completeness_score(aibom)
|
447 |
|
|
|
62 |
# Try to load existing dataset to append
|
63 |
try:
|
64 |
# Use trust_remote_code=True if required by the dataset/model on HF
|
65 |
+
# Corrected: Removed unnecessary backslashes around 'train'
|
66 |
+
existing_ds = load_dataset(HF_REPO, token=HF_TOKEN, split='train', trust_remote_code=True)
|
67 |
# Check if dataset is empty or has different columns (handle initial creation)
|
68 |
if len(existing_ds) > 0 and set(existing_ds.column_names) == set(log_data.keys()):
|
69 |
ds_to_push = concatenate_datasets([existing_ds, ds_new_log])
|
|
|
77 |
ds_to_push = concatenate_datasets([existing_ds, ds_new_log])
|
78 |
|
79 |
except Exception as load_err:
|
80 |
+
# Handle case where dataset doesn't exist yet or other loading errors
|
81 |
+
# Corrected: Removed unnecessary backslash in doesn't
|
82 |
logger.info(f"Could not load existing dataset {HF_REPO} (may not exist yet): {load_err}. Pushing new dataset.")
|
83 |
ds_to_push = ds_new_log # ds is already prepared with the new log entry
|
84 |
|
85 |
# Push the updated or new dataset
|
86 |
+
# Corrected: Removed unnecessary backslash in it's
|
87 |
+
ds_to_push.push_to_hub(HF_REPO, token=HF_TOKEN, private=True) # Ensure it's private
|
88 |
logger.info(f"Successfully logged SBOM generation for {model_id} to {HF_REPO}")
|
89 |
|
90 |
except Exception as e:
|
|
|
96 |
logger.warning("HF_TOKEN not set. Cannot retrieve SBOM count.")
|
97 |
return "N/A"
|
98 |
try:
|
99 |
+
# Load the dataset - assumes 'train' split exists after first push
|
100 |
# Use trust_remote_code=True if required by the dataset/model on HF
|
101 |
+
# Corrected: Removed unnecessary backslashes around 'train'
|
102 |
+
ds = load_dataset(HF_REPO, token=HF_TOKEN, split='train', trust_remote_code=True)
|
103 |
count = len(ds)
|
104 |
logger.info(f"Retrieved SBOM count: {count} from {HF_REPO}")
|
105 |
# Format count for display (e.g., add commas for large numbers)
|
|
|
378 |
enhancement_report = generator.get_enhancement_report()
|
379 |
|
380 |
# Save AIBOM to file
|
381 |
+
# Corrected: Removed unnecessary backslashes around '/' and '_'
|
382 |
+
filename = f"{model_id.replace('/', '_')}_aibom.json"
|
383 |
filepath = os.path.join(OUTPUT_DIR, filename)
|
384 |
|
385 |
with open(filepath, "w") as f:
|
|
|
393 |
download_url = f"/output/{filename}"
|
394 |
|
395 |
# Create download and UI interaction scripts
|
396 |
+
# Corrected: Removed unnecessary backslashes in script string
|
397 |
download_script = f"""
|
398 |
<script>
|
399 |
function downloadJSON() {{
|
400 |
+
const a = document.createElement('a');
|
401 |
+
a.href = '{download_url}';
|
402 |
+
a.download = '{filename}';
|
403 |
document.body.appendChild(a);
|
404 |
a.click();
|
405 |
document.body.removeChild(a);
|
|
|
407 |
|
408 |
function switchTab(tabId) {{
|
409 |
// Hide all tabs
|
410 |
+
document.querySelectorAll('.tab-content').forEach(tab => {{
|
411 |
+
tab.classList.remove('active');
|
412 |
}});
|
413 |
|
414 |
// Deactivate all tab buttons
|
415 |
+
document.querySelectorAll('.aibom-tab').forEach(button => {{
|
416 |
+
button.classList.remove('active');
|
417 |
}});
|
418 |
|
419 |
// Show the selected tab
|
420 |
+
document.getElementById(tabId).classList.add('active');
|
421 |
|
422 |
// Activate the clicked button
|
423 |
+
event.currentTarget.classList.add('active');
|
424 |
}}
|
425 |
|
426 |
function toggleCollapsible(element) {{
|
427 |
+
element.classList.toggle('active');
|
428 |
var content = element.nextElementSibling;
|
429 |
if (content.style.maxHeight) {{
|
430 |
content.style.maxHeight = null;
|
431 |
+
content.classList.remove('active');
|
432 |
}} else {{
|
433 |
content.style.maxHeight = content.scrollHeight + "px";
|
434 |
+
content.classList.add('active');
|
435 |
}}
|
436 |
}}
|
437 |
</script>
|
|
|
439 |
|
440 |
# Get completeness score or create a comprehensive one if not available
|
441 |
completeness_score = None
|
442 |
+
# Corrected: Removed unnecessary backslash in 'get_completeness_score'
|
443 |
+
if hasattr(generator, 'get_completeness_score'):
|
444 |
try:
|
445 |
completeness_score = generator.get_completeness_score(model_id)
|
446 |
logger.info("Successfully retrieved completeness_score from generator")
|
447 |
except Exception as e:
|
448 |
logger.error(f"Completeness score error from generator: {str(e)}")
|
449 |
|
450 |
+
# If completeness_score is None or doesn't have field_checklist, use comprehensive one
|
451 |
+
# Corrected: Removed unnecessary backslash in doesn't and 'field_checklist'
|
452 |
+
if completeness_score is None or not isinstance(completeness_score, dict) or 'field_checklist' not in completeness_score:
|
453 |
logger.info("Using comprehensive completeness_score with field_checklist")
|
454 |
completeness_score = create_comprehensive_completeness_score(aibom)
|
455 |
|