-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstats.py
57 lines (44 loc) · 1.85 KB
/
stats.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import json
import os
from collections import defaultdict
from rich import print
from rich.console import Console
console = Console()
def get_json_files(directory):
return [f for f in os.listdir(directory) if f.endswith(".json")]
def load_json(file_path):
with open(file_path, "r") as file:
return json.load(file)
def count_refusal_sentiments(data):
sentiment_counts = defaultdict(list)
explanations = defaultdict(list)
if "layer_candidates" in data:
for candidate in data["layer_candidates"]:
sentiment = candidate["refusal_sentiment"]
number = candidate["number"]
explanation = candidate["explanation"]
sentiment_counts[sentiment].append(number)
explanations[sentiment].append((number, explanation))
return sentiment_counts, explanations
def main():
directory = "out"
json_files = get_json_files(directory)
for json_file in json_files:
file_path = os.path.join(directory, json_file)
data = load_json(file_path)
sentiment_counts, explanations = count_refusal_sentiments(data)
if sentiment_counts:
console.print("\n" + "=" * 40, style="cyan")
console.print(f"File: {json_file}", style="cyan")
console.print("=" * 40, style="cyan")
for sentiment, numbers in sentiment_counts.items():
console.print(f"\n{sentiment.capitalize()}:", style="yellow")
console.print(
f" Completion Numbers: {', '.join(map(str, numbers))}",
style="green",
)
console.print(f" Unique Explanations:", style="magenta")
for number, explanation in explanations[sentiment]:
console.print(f" - {number}: {explanation}", style="white")
if __name__ == "__main__":
main()