File size: 11,267 Bytes
3a9eacd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f30ef0
 
3a9eacd
 
 
 
 
 
 
 
 
 
9f30ef0
3a9eacd
 
 
 
 
 
 
 
 
 
 
 
9f30ef0
 
 
 
 
 
 
 
 
 
3a9eacd
 
 
 
 
9f30ef0
 
 
3a9eacd
 
 
 
 
 
 
9f30ef0
3a9eacd
9f30ef0
3a9eacd
 
 
 
 
 
 
 
 
 
 
 
 
 
9f30ef0
 
 
 
3a9eacd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f30ef0
 
3a9eacd
 
 
 
9f30ef0
3a9eacd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f30ef0
3a9eacd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f30ef0
 
 
 
 
 
 
 
3a9eacd
 
 
 
 
 
 
 
9f30ef0
 
3a9eacd
9f30ef0
3a9eacd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f30ef0
 
 
3a9eacd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
"""
์‹คํŒจ ๋ชจ๋‹ฌ ์ปดํฌ๋„ŒํŠธ (Gradio 6 ํ˜ธํ™˜ ๋ฒ„์ „)
์ปค์Šคํ…€ Modal ํด๋ž˜์Šค ์‚ฌ์šฉ

๐Ÿ‘จโ€๐Ÿ’ป ๋‹ด๋‹น: ๊ฐœ๋ฐœ์ž B
"""

import gradio as gr
from frontend.components.custom_modal import Modal
from frontend.renderers import render_radar_chart


class FailureModalComponent:
    """์‹คํŒจ ๋ชจ๋‹ฌ ์ปดํฌ๋„ŒํŠธ - custom_modal ์‚ฌ์šฉ"""

    def __init__(self):
        self.modal = None
        self.close_btn = None

    def render(self):
        """
        ์‹คํŒจ ๋ชจ๋‹ฌ UI ๋ Œ๋”๋ง

        Returns:
            gr.HTML: modal_component
        """
        self.modal = Modal(
            visible=False,
            content="",
            elem_id="failure-modal"
        )
        modal_component = self.modal.render()

        return modal_component

    def setup_events(self, on_close=None):
        """
        ์ด๋ฒคํŠธ ๋ฐ”์ธ๋”ฉ (ํ˜„์žฌ๋Š” JS์—์„œ ์ฒ˜๋ฆฌ)

        Args:
            on_close: ๋ชจ๋‹ฌ ๋‹ซํž ๋•Œ ํ˜ธ์ถœํ•  ์ฝœ๋ฐฑ (๋ฏธ์‚ฌ์šฉ)
        """
        # ๋ชจ๋‹ฌ ๋‹ซ๊ธฐ๋Š” JS์—์„œ ์ž๋™ ์ฒ˜๋ฆฌ๋จ
        pass

    @staticmethod
    def create_modal_content(
        recognized_text: str,
        score: int,
        hint: str,
        audio_path: str = None,
        metrics: dict = None,
        user_text: str = None
    ) -> str:
        """
        ๋ชจ๋‹ฌ ๋‚ด์šฉ HTML ์ƒ์„ฑ (์˜ค๋””์˜ค ์žฌ์ƒ + ์˜ค๊ฐ ๊ทธ๋ž˜ํ”„ + ์ ์ˆ˜ํ‘œ + ์กฐ์–ธ)

        Args:
            recognized_text: ์ธ์‹๋œ ํ…์ŠคํŠธ
            score: ์ ์ˆ˜
            hint: ํžŒํŠธ ๋ฉ”์‹œ์ง€
            audio_path: ์ œ์ถœํ•œ ์˜ค๋””์˜ค ํŒŒ์ผ ๊ฒฝ๋กœ
            metrics: ๋ฉ”ํŠธ๋ฆญ ๋ฐ์ดํ„ฐ {pronunciation, tone, pitch, rhythm, energy}
            user_text: ์‚ฌ์šฉ์ž๊ฐ€ ๋งํ•œ ํ…์ŠคํŠธ (STT ๊ฒฐ๊ณผ)

        Returns:
            str: HTML ๋ฌธ์ž์—ด
        """
        # ๊ธฐ๋ณธ ๋ฉ”ํŠธ๋ฆญ
        if metrics is None:
            metrics = {}

        # ์ ์ˆ˜์— ๋”ฐ๋ฅธ ์ƒ‰์ƒ ๊ฒฐ์ • ํ•จ์ˆ˜
        def get_color(val):
            return "#e8a054" if val < 85 else "#4db8ff"

        # ์‚ฌ์šฉ์ž๊ฐ€ ๋งํ•œ ํ…์ŠคํŠธ (STT ๊ฒฐ๊ณผ) HTML
        user_text_html = ""
        if user_text:
            user_text_html = f"""
            <div class="fm-user-text-section">
                <div class="fm-section-title">You said</div>
                <div class="fm-user-text">"{user_text}"</div>
            </div>
            """

        # ์˜ค๋””์˜ค ํ”Œ๋ ˆ์ด์–ด HTML
        audio_html = ""
        if audio_path:
            audio_html = f"""
            <div class="fm-audio-section">
                {user_text_html}

                <audio controls style="width: 100%; height: 36px; margin-top: 16px;" preload="metadata">
                    <source src="/gradio_api/file={audio_path}" type="audio/wav">
                    <source src="/gradio_api/file={audio_path}" type="audio/mpeg">
                    ์˜ค๋””์˜ค๋ฅผ ์žฌ์ƒํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
                </audio>
            </div>
            """


        # ๋ ˆ์ด๋” ์ฐจํŠธ SVG
        radar_svg = render_radar_chart(metrics, size=280) if metrics else ""

        # ์ ์ˆ˜ํ‘œ HTML
        score_table_html = ""
        if metrics:
            score_table_html = f"""
            <div class="fm-score-grid">
                <div style="text-align: center;">
                    <div class="fm-score-label">Pronunciation</div>
                    <div style="font-size: 0.95rem; font-weight: 700; color: {get_color(metrics.get('pronunciation', 0))};">{metrics.get('pronunciation', '-')}</div>
                </div>
                <div style="text-align: center;">
                    <div class="fm-score-label">Pitch</div>
                    <div style="font-size: 0.95rem; font-weight: 700; color: {get_color(metrics.get('pitch', 0))};">{metrics.get('pitch', '-')}</div>
                </div>
                <div style="text-align: center;">
                    <div class="fm-score-label">Line Accuracy</div>
                    <div style="font-size: 0.95rem; font-weight: 700; color: {get_color(metrics.get('tone', 0))};">{metrics.get('tone', '-')}</div>
                </div>
                <div style="text-align: center;">
                    <div class="fm-score-label">Rhythm</div>
                    <div style="font-size: 0.95rem; font-weight: 700; color: {get_color(metrics.get('rhythm', 0))};">{metrics.get('rhythm', '-')}</div>
                </div>
                <div style="text-align: center;">
                    <div class="fm-score-label">Energy</div>
                    <div style="font-size: 0.95rem; font-weight: 700; color: {get_color(metrics.get('energy', 0))};">{metrics.get('energy', '-')}</div>
                </div>
            </div>
            """

        # ๊ทธ๋ž˜ํ”„ + ์ ์ˆ˜ํ‘œ ์˜์—ญ
        chart_section = ""
        if metrics:
            chart_section = f"""
            <div class="fm-chart-section">
                {radar_svg}
                {score_table_html}
            </div>
            """

        # API์—์„œ ๋ฐ›์€ advice ๋˜๋Š” ๊ธฐ๋ณธ ๋ฉ”์‹œ์ง€
        advice_text = hint if hint else "๋ฐœ์Œ์˜ ์ •ํ™•๋„๋ฅผ ๋†’์ด๊ธฐ ์œ„ํ•ด ๊ฐ ์Œ์ ˆ์„ ๋˜๋ฐ•๋˜๋ฐ• ๋ฐœ์Œํ•ด ๋ณด์„ธ์š”. ํŠนํžˆ ๋ฐ›์นจ ๋ฐœ์Œ์— ์ฃผ์˜ํ•˜์‹œ๊ณ , ๋ง์˜ ์†๋„๋ฅผ ์กฐ๊ธˆ ์ฒœ์ฒœํžˆ ํ•˜๋ฉด ์ธ์‹๋ฅ ์ด ํ–ฅ์ƒ๋ฉ๋‹ˆ๋‹ค."
        advice_html = f"""
        <div class="fm-advice-section">
            <div class="fm-advice-title">Advice</div>
            <div class="fm-advice-text">
                {advice_text}
            </div>
        </div>
        """

        backup_recognized_text = """
        <div style="background: #f8f9fa; border-radius: 8px; padding: 8px;">
            <div style="color: #666; font-size: 0.85em; margin-bottom: 4px;">์ธ์‹๋œ ํ…์ŠคํŠธ</div>
            <div style="font-size: 1.05em; color: #333;">{recognized_text}</div>
        </div>
        """

        return f"""
        <style>
            /* ์˜ค๋””์˜ค ์„น์…˜ */
            .fm-audio-section {{
                padding: 12px;
                background: #f8f9fa;
                border-radius: 8px;
            }}
            .fm-section-title {{
                color: #666;
                font-size: 1em;
                margin-bottom: 12px;
                font-weight: 600;
            }}

            /* ์ฐจํŠธ ์„น์…˜ */
            .fm-chart-section {{
                background: #f0f7fc;
                border-radius: 12px;
                padding: 16px;
                display: flex;
                flex-direction: column;
                align-items: center;
            }}
            .fm-score-grid {{
                display: grid;
                grid-template-columns: repeat(5, 1fr);
                gap: 4px;
                width: 100%;
                margin-top: 8px;
            }}
            .fm-score-label {{
                font-size: 0.65rem;
                color: #666;
            }}
            .fm-user-text {{
                color: #5c3d1e;
                font-size: 1.25em;
                font-style: italic;
                line-height: 1.5;
                font-weight: 500;
                text-align: center;
            }}

            /* ์กฐ์–ธ ์„น์…˜ */
            .fm-advice-section {{
                padding: 14px;
                background: linear-gradient(135deg, #e0f2fe 0%, #bae6fd 100%);
                border-radius: 8px;
            }}
            .fm-advice-title {{
                color: #666;
                font-size: 1em;
                margin-bottom: 12px;
                font-weight: 600;
            }}
            .fm-advice-text {{
                color: #075985;
                font-size: 0.9em;
                line-height: 1.5;
            }}

            /* ๋‹ซ๊ธฐ ๋ฒ„ํŠผ */
            .failure-modal-close-btn {{
                font-family: 'Lilita One' !important;
                text-align: center !important;
                background: linear-gradient(135deg, #e8a054 0%, #f29430 100%) !important;
                color: white !important;
                border: none !important;
                padding: 8px 0px !important;
                font-size: 22px !important;
                font-weight: 700 !important;
                text-transform: uppercase !important;
                letter-spacing: 1px !important;
                border-radius: 25px !important;
                cursor: pointer !important;
                transition: all 0.3s ease !important;
                width: 100% !important;
                min-width: 320px !important;
                max-width: 800px !important;
                margin: 0 auto !important;
                display: block !important;
            }}
            .failure-modal-close-btn:hover {{
                background: linear-gradient(135deg, #f29430 0%, #e8a054 100%) !important;
                transform: translateY(-2px) !important;
            }}

            /* ========== ๋‹คํฌ๋ชจ๋“œ ========== */
            .modal-dark .fm-audio-section {{
                background: #2d2d2d !important;
            }}
            .modal-dark .fm-section-title {{
                color: #a0a0a0 !important;
            }}
            .modal-dark .fm-user-text {{
                color: #ffba70 !important;
            }}
            .modal-dark .fm-chart-section {{
                background: #2d2d2d !important;
            }}
            .modal-dark .fm-score-label {{
                color: #a0a0a0 !important;
            }}
            .modal-dark .fm-advice-section {{
                background: linear-gradient(135deg, #1e3a4c 0%, #1a3040 100%) !important;
            }}
            .modal-dark .fm-advice-title {{
                color: #7dd3fc !important;
            }}
            .modal-dark .fm-advice-text {{
                color: #bae6fd !important;
            }}
        </style>

        <div style="text-align: start;">
            <div style="display: flex; align-items: center; justify-content: center; height: fit-content; padding-left: 4px; margin-bottom: 20px;">
                <h2 style="
                    font-family: 'Lilita One', 'Bangers', Impact, sans-serif;
                    text-align: center;
                    color: #dc3545;
                    margin: 0;
                    font-size: 64px;
                    color: #e8a054;
                    letter-spacing: 1.5px;
                    -webkit-text-stroke: 1.5px #8b5a2b;
                    text-shadow:
                        2px 2px 0 #8b5a2b,
                        2px 2px 0 #5c3d1e,
                        2.5px 2.5px 0 #5c3d1e,
                        0 0 20px rgba(232, 160, 84, 0.5);
                ">
                    Try Again!
                </h2>
            </div>

            <div style="display: flex; flex-direction: column; gap: 12px; margin-bottom: 20px;">
                {audio_html}

                {advice_html}

                {chart_section}
            </div>

            <div style="display: flex; justify-content: center;">
                <button class="failure-modal-close-btn" data-modal-close="true">
                    Close
                </button>
            </div>
        </div>
        """

    @staticmethod
    def show(content: str):
        """๋ชจ๋‹ฌ ์—ด๊ธฐ"""
        return Modal.show(content)

    @staticmethod
    def hide():
        """๋ชจ๋‹ฌ ๋‹ซ๊ธฐ"""
        return Modal.hide()