Spaces:
Running
Running
Update src/aibom_generator/api.py
Browse files- src/aibom_generator/api.py +29 -6
src/aibom_generator/api.py
CHANGED
@@ -12,6 +12,7 @@ from datetime import datetime
|
|
12 |
from datasets import Dataset, load_dataset, concatenate_datasets
|
13 |
import os
|
14 |
import logging
|
|
|
15 |
|
16 |
# Configure logging
|
17 |
logging.basicConfig(level=logging.INFO)
|
@@ -44,6 +45,22 @@ class StatusResponse(BaseModel):
|
|
44 |
version: str
|
45 |
generator_version: str
|
46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
# --- Add Counter Helper Functions ---
|
48 |
def log_sbom_generation(model_id: str):
|
49 |
"""Logs a successful SBOM generation event to the Hugging Face dataset."""
|
@@ -52,10 +69,12 @@ def log_sbom_generation(model_id: str):
|
|
52 |
return
|
53 |
|
54 |
try:
|
|
|
|
|
55 |
log_data = {
|
56 |
"timestamp": [datetime.utcnow().isoformat()],
|
57 |
"event": ["generated"],
|
58 |
-
"model_id": [
|
59 |
}
|
60 |
ds_new_log = Dataset.from_dict(log_data)
|
61 |
|
@@ -85,7 +104,7 @@ def log_sbom_generation(model_id: str):
|
|
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 {
|
89 |
|
90 |
except Exception as e:
|
91 |
logger.error(f"Failed to log SBOM generation to {HF_REPO}: {e}")
|
@@ -351,6 +370,9 @@ async def generate_form(
|
|
351 |
use_best_practices: bool = Form(True)
|
352 |
):
|
353 |
sbom_count = get_sbom_count() # Get count early for context
|
|
|
|
|
|
|
354 |
try:
|
355 |
# Try different import paths for AIBOMGenerator
|
356 |
generator = None
|
@@ -379,7 +401,8 @@ async def generate_form(
|
|
379 |
|
380 |
# Save AIBOM to file
|
381 |
# Corrected: Removed unnecessary backslashes around '/' and '_'
|
382 |
-
|
|
|
383 |
filepath = os.path.join(OUTPUT_DIR, filename)
|
384 |
|
385 |
with open(filepath, "w") as f:
|
@@ -500,12 +523,12 @@ async def generate_form(
|
|
500 |
"external_references": 10
|
501 |
}
|
502 |
|
503 |
-
# Render the template with all necessary data
|
504 |
return templates.TemplateResponse(
|
505 |
"result.html",
|
506 |
{
|
507 |
"request": request,
|
508 |
-
"model_id":
|
509 |
"aibom": aibom,
|
510 |
"enhancement_report": enhancement_report,
|
511 |
"completeness_score": completeness_score,
|
@@ -522,7 +545,7 @@ async def generate_form(
|
|
522 |
# Ensure count is passed to error template as well
|
523 |
sbom_count = get_sbom_count() # Refresh count just in case
|
524 |
return templates.TemplateResponse(
|
525 |
-
"error.html", {"request": request, "error": str(e), "sbom_count": sbom_count} # Pass count
|
526 |
)
|
527 |
|
528 |
@app.get("/download/{filename}")
|
|
|
12 |
from datasets import Dataset, load_dataset, concatenate_datasets
|
13 |
import os
|
14 |
import logging
|
15 |
+
from urllib.parse import urlparse
|
16 |
|
17 |
# Configure logging
|
18 |
logging.basicConfig(level=logging.INFO)
|
|
|
45 |
version: str
|
46 |
generator_version: str
|
47 |
|
48 |
+
# --- Add Model ID Normalization Helper ---
|
49 |
+
def _normalise_model_id(raw_id: str) -> str:
|
50 |
+
"""
|
51 |
+
Accept either 'owner/model' or a full URL like
|
52 |
+
'https://huggingface.co/owner/model'. Return 'owner/model'.
|
53 |
+
"""
|
54 |
+
if raw_id.startswith(("http://", "https://") ):
|
55 |
+
path = urlparse(raw_id).path.lstrip("/")
|
56 |
+
# path can contain extra segments (e.g. /commit/...), keep first two
|
57 |
+
parts = path.split("/")
|
58 |
+
if len(parts) >= 2:
|
59 |
+
return "/".join(parts[:2])
|
60 |
+
return path # Fallback if path doesn't have owner/model
|
61 |
+
return raw_id
|
62 |
+
# --- End Model ID Normalization Helper ---
|
63 |
+
|
64 |
# --- Add Counter Helper Functions ---
|
65 |
def log_sbom_generation(model_id: str):
|
66 |
"""Logs a successful SBOM generation event to the Hugging Face dataset."""
|
|
|
69 |
return
|
70 |
|
71 |
try:
|
72 |
+
# Normalize model_id before logging
|
73 |
+
normalized_model_id_for_log = _normalise_model_id(model_id) # added to normalize id
|
74 |
log_data = {
|
75 |
"timestamp": [datetime.utcnow().isoformat()],
|
76 |
"event": ["generated"],
|
77 |
+
"model_id": [normalized_model_id_for_log] # use normalized_model_id_for_log
|
78 |
}
|
79 |
ds_new_log = Dataset.from_dict(log_data)
|
80 |
|
|
|
104 |
# Push the updated or new dataset
|
105 |
# Corrected: Removed unnecessary backslash in it's
|
106 |
ds_to_push.push_to_hub(HF_REPO, token=HF_TOKEN, private=True) # Ensure it's private
|
107 |
+
logger.info(f"Successfully logged SBOM generation for {normalized_model_id_for_log} to {HF_REPO}") # use normalized model id
|
108 |
|
109 |
except Exception as e:
|
110 |
logger.error(f"Failed to log SBOM generation to {HF_REPO}: {e}")
|
|
|
370 |
use_best_practices: bool = Form(True)
|
371 |
):
|
372 |
sbom_count = get_sbom_count() # Get count early for context
|
373 |
+
# --- Normalize the model ID for display and filename ---
|
374 |
+
normalized_model_id = _normalise_model_id(model_id)
|
375 |
+
# --- End Normalization ---
|
376 |
try:
|
377 |
# Try different import paths for AIBOMGenerator
|
378 |
generator = None
|
|
|
401 |
|
402 |
# Save AIBOM to file
|
403 |
# Corrected: Removed unnecessary backslashes around '/' and '_'
|
404 |
+
# Save AIBOM to file using normalized ID
|
405 |
+
filename = f"{normalized_model_id.replace('/', '_')}_aibom.json"
|
406 |
filepath = os.path.join(OUTPUT_DIR, filename)
|
407 |
|
408 |
with open(filepath, "w") as f:
|
|
|
523 |
"external_references": 10
|
524 |
}
|
525 |
|
526 |
+
# Render the template with all necessary data, with normalized model ID
|
527 |
return templates.TemplateResponse(
|
528 |
"result.html",
|
529 |
{
|
530 |
"request": request,
|
531 |
+
"model_id": normalized_model_id,
|
532 |
"aibom": aibom,
|
533 |
"enhancement_report": enhancement_report,
|
534 |
"completeness_score": completeness_score,
|
|
|
545 |
# Ensure count is passed to error template as well
|
546 |
sbom_count = get_sbom_count() # Refresh count just in case
|
547 |
return templates.TemplateResponse(
|
548 |
+
"error.html", {"request": request, "error": str(e), "sbom_count": sbom_count, "model_id": normalized_model_id} # Pass count, added normalized model ID
|
549 |
)
|
550 |
|
551 |
@app.get("/download/{filename}")
|