elselse commited on
Commit
0bfd012
·
verified ·
1 Parent(s): 2c94aa9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -7
app.py CHANGED
@@ -25,19 +25,31 @@ with open("child_to_parent_mapping.json", "r") as f:
25
 
26
  def predict_cwe(commit_message: str):
27
  """
28
- Predict CWE(s) from a commit message and map to parent CWEs.
29
  """
30
  results = classifier(commit_message)[0]
31
  sorted_results = sorted(results, key=lambda x: x["score"], reverse=True)
32
-
33
  threshold = 0.2
34
  filtered_results = [item for item in sorted_results if item["score"] >= threshold]
35
 
36
- # Map predictions to parent CWE (if available)
37
- mapped_results = {}
38
- for item in sorted_results[:5]:
39
- mapped_results[item["label"]] = round(float(item["score"]), 4)
40
- return mapped_results
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
  # Gradio UI
43
  demo = gr.Interface(
 
25
 
26
  def predict_cwe(commit_message: str):
27
  """
28
+ Predict CWE(s) from a commit message and map to parent CWEs if applicable.
29
  """
30
  results = classifier(commit_message)[0]
31
  sorted_results = sorted(results, key=lambda x: x["score"], reverse=True)
32
+
33
  threshold = 0.2
34
  filtered_results = [item for item in sorted_results if item["score"] >= threshold]
35
 
36
+ parent_scores = {}
37
+
38
+ for item in filtered_results:
39
+ label = item["label"].replace("CWEEEE_", "")
40
+ score = float(item["score"])
41
+
42
+ parent_label = child_to_parent.get(label, label)
43
+
44
+ if parent_label in parent_scores:
45
+ parent_scores[parent_label] += score
46
+ else:
47
+ parent_scores[parent_label] = score
48
+
49
+ # Sort by score descending and round
50
+ sorted_parent_scores = sorted(parent_scores.items(), key=lambda x: x[1], reverse=True)
51
+ return {f"CWE-{k}": round(v, 4) for k, v in sorted_parent_scores[:5]}
52
+
53
 
54
  # Gradio UI
55
  demo = gr.Interface(