File size: 5,385 Bytes
591c7e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import json
from typing import Dict

import tqdm

from ai.classifier import ToxcitiyClassifier
from core.data_manager import DataManager
from models.schemas import AnalyzedTranscript, ChunkedSegment


def batch_analyze_transcripts(
    data_manager: DataManager,
    classifier: ToxcitiyClassifier,
    chunk_size: int = 60,
    overlap: int = 10,
) -> Dict[str, AnalyzedTranscript]:
    """๋ชจ๋“  ํŠธ๋žœ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋ถ„์„ํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅ"""

    # ๊ธฐ์กด ๋ถ„์„ ๊ฒฐ๊ณผ ๋กœ๋“œ
    try:
        with open("./data/analyzed_transcripts.json", "r", encoding="utf-8") as f:
            existing_data = json.load(f)
            analyzed_transcripts = existing_data.get("analyzed_transcripts", {})
    except FileNotFoundError:
        analyzed_transcripts = {}

    # ๋ชจ๋“  ํŠธ๋žœ์Šคํฌ๋ฆฝํŠธ ์ฒ˜๋ฆฌ
    all_transcripts = data_manager.transcript_data

    print(f"์ด {len(all_transcripts)}๊ฐœ์˜ ํŠธ๋žœ์Šคํฌ๋ฆฝํŠธ ์ฒ˜๋ฆฌ ์‹œ์ž‘...")

    for transcript_data in tqdm.tqdm(all_transcripts):
        video_id = transcript_data.get("video_id")

        # ์ด๋ฏธ ๋ถ„์„๋œ ํŠธ๋žœ์Šคํฌ๋ฆฝํŠธ๋Š” ๊ฑด๋„ˆ๋›ฐ๊ธฐ
        if video_id in analyzed_transcripts:
            print(f"Video {video_id}: ์ด๋ฏธ ๋ถ„์„๋จ, ๊ฑด๋„ˆ๋›ฐ๊ธฐ")
            continue

        # ํŠธ๋žœ์Šคํฌ๋ฆฝํŠธ ๋ฐ์ดํ„ฐ ์ค€๋น„
        transcript = data_manager.get_transcript_by_video_id(video_id)
        if transcript is None:
            print(f"Video {video_id}: ํŠธ๋žœ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Œ")
            continue

        try:
            # ์ „์ฒด ์˜์ƒ ๊ธธ์ด ๊ณ„์‚ฐ
            total_duration = max(
                segment["start"] + segment["duration"]
                for segment in transcript.transcript_segments
            )

            # ์ฒญํฌ ์ฒ˜๋ฆฌ
            num_chunks = math.ceil(total_duration / chunk_size)
            chunked_segments = []
            is_toxic = False
            max_toxicity = 0.0

            for i in range(num_chunks):
                chunk_start = i * chunk_size
                chunk_end = (i + 1) * chunk_size

                # ๋ณด๊ฐ„ ๋ฒ”์œ„ ์„ค์ •
                overlap_start = max(0, chunk_start - overlap)
                overlap_end = min(total_duration, chunk_end + overlap)

                # ํ•ด๋‹น ์ฒญํฌ์— ํฌํ•จ๋  ํŠธ๋žœ์Šคํฌ๋ฆฝํŠธ ์ˆ˜์ง‘
                chunk_text = []

                for segment in transcript.transcript_segments:
                    segment_start = segment["start"]
                    segment_end = segment_start + segment["duration"]

                    if not (segment_end < overlap_start or segment_start > overlap_end):
                        chunk_text.append(segment["text"])

                # ์ฒญํฌ ํ…์ŠคํŠธ ์ƒ์„ฑ
                chunk_transcript = " ".join(chunk_text)

                # toxicity inference ์ˆ˜ํ–‰
                if chunk_transcript.strip():  # ๋นˆ ํ…์ŠคํŠธ๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ๋งŒ ๋ถ„์„
                    toxicity_score = classifier.infer(chunk_transcript)
                    max_toxicity = max(max_toxicity, toxicity_score)
                else:
                    toxicity_score = 0.0

                # ์ฒญํฌ ์„ธ๊ทธ๋จผํŠธ ์ƒ์„ฑ
                chunk = ChunkedSegment(
                    start=overlap_start,
                    end=overlap_end,
                    transcript=chunk_transcript,
                    toxicity_score=float(toxicity_score),
                )

                chunked_segments.append(chunk)

            # ์œ ํ•ด์„ฑ ํŒ๋‹จ (์ž„๊ณ„๊ฐ’ 0.5 ์ ์šฉ)
            is_toxic = max_toxicity > 0.5

            # AnalyzedTranscript ๊ฐ์ฒด ์ƒ์„ฑ
            analyzed_transcript = AnalyzedTranscript(
                video_id=video_id,
                chunk_count=len(chunked_segments),
                chunked_segments=chunked_segments,
                is_toxic=is_toxic,
            )

            # ๊ฒฐ๊ณผ ์ €์žฅ
            analyzed_transcripts[video_id] = analyzed_transcript

            # ์ค‘๊ฐ„ ์ €์žฅ (๋งค ์˜์ƒ ๋ถ„์„ ํ›„)
            with open("./data/analyzed_transcripts.json", "w", encoding="utf-8") as f:
                json.dump(
                    {
                        "analyzed_transcripts": {
                            vid: asdict(transcript)
                            for vid, transcript in analyzed_transcripts.items()
                        }
                    },
                    f,
                    ensure_ascii=False,
                    indent=2,
                )

            print(
                f"Video {video_id}: ๋ถ„์„ ์™„๋ฃŒ (์œ ํ•ด์„ฑ: {is_toxic}, ์ตœ๋Œ€ ์ ์ˆ˜: {max_toxicity:.3f})"
            )

        except Exception as e:
            print(f"Video {video_id} ์ฒ˜๋ฆฌ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}")
            continue

    return analyzed_transcripts


if __name__ == "__main__":
    import math
    from dataclasses import asdict

    # ๋ฐ์ดํ„ฐ ๋งค๋‹ˆ์ €์™€ ๋ถ„๋ฅ˜๊ธฐ ์ดˆ๊ธฐํ™”
    data_manager = DataManager()
    classifier = ToxcitiyClassifier()

    # ๋ฐฐ์น˜ ์ฒ˜๋ฆฌ ์‹คํ–‰
    results = batch_analyze_transcripts(data_manager, classifier)

    # ์ตœ์ข… ํ†ต๊ณ„
    total_analyzed = len(results)
    total_toxic = sum(1 for transcript in results.values() if transcript.is_toxic)

    print("\n๋ถ„์„ ์™„๋ฃŒ ํ†ต๊ณ„:")
    print(f"์ด ์ฒ˜๋ฆฌ๋œ ์˜์ƒ: {total_analyzed}")
    print(f"์œ ํ•ด ํŒ์ • ์˜์ƒ: {total_toxic}")
    print(f"์œ ํ•ด ๋น„์œจ: {(total_toxic/total_analyzed)*100:.1f}%")