File size: 15,542 Bytes
ff27984
68fece7
 
 
 
 
 
ed0deaf
8702770
43f1594
 
68fece7
43f1594
 
 
8702770
43f1594
 
 
 
f5b5cdc
8702770
43f1594
 
 
 
 
8702770
43f1594
 
8702770
43f1594
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5b5cdc
43f1594
 
 
 
 
 
 
 
f5b5cdc
8702770
43f1594
f5b5cdc
43f1594
 
f5b5cdc
43f1594
 
 
 
 
f5b5cdc
 
43f1594
 
 
 
 
 
28c3672
43f1594
 
 
 
 
 
 
 
 
 
 
 
 
8702770
a63a0bc
 
43f1594
a63a0bc
 
43f1594
e9b7506
 
43f1594
e9b7506
 
43f1594
a63a0bc
 
 
8702770
 
43f1594
8702770
 
43f1594
e9b7506
 
43f1594
 
 
 
 
 
 
 
 
 
 
 
 
 
a63a0bc
43f1594
e9b7506
f5b5cdc
8702770
43f1594
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5b5cdc
 
8702770
43f1594
 
 
 
 
 
 
f5b5cdc
43f1594
 
 
 
f5b5cdc
43f1594
 
 
 
 
 
 
 
 
f5b5cdc
43f1594
 
f5b5cdc
 
8702770
f5b5cdc
8702770
43f1594
 
 
 
f5b5cdc
43f1594
 
f5b5cdc
 
43f1594
f5b5cdc
 
8702770
43f1594
 
e9b7506
43f1594
 
8702770
 
43f1594
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8702770
43f1594
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8702770
43f1594
68fece7
 
8702770
43f1594
8702770
43f1594
 
8702770
 
43f1594
 
 
f5b5cdc
43f1594
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5b5cdc
43f1594
68fece7
43f1594
8702770
 
43f1594
 
8702770
a63a0bc
 
 
43f1594
8702770
43f1594
8702770
43f1594
8702770
a63a0bc
43f1594
 
 
 
 
 
 
 
 
 
 
 
 
 
a63a0bc
43f1594
 
8702770
 
 
 
 
43f1594
 
 
 
 
 
 
 
 
8702770
43f1594
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8702770
43f1594
a63a0bc
 
43f1594
8702770
43f1594
 
 
 
 
 
 
 
 
 
 
 
 
ff27984
68fece7
 
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import streamlit as st
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.stats import norm, skew
import platform
import os
import matplotlib.font_manager as fm
import warnings
warnings.filterwarnings('ignore')

# ์ „์—ญ ํฐํŠธ ์„ค์ • - ์•ฑ ์‹œ์ž‘ ์ฆ‰์‹œ ์‹คํ–‰
def configure_matplotlib_korean():
    """matplotlib ํ•œ๊ธ€ ํฐํŠธ ๊ฐ•์ œ ์„ค์ •"""
    
    # matplotlib ๋ฐฑ์—”๋“œ ์„ค์ •
    plt.switch_backend('Agg')
    
    # ๊ธฐ์กด ์„ค์ • ์™„์ „ ์ดˆ๊ธฐํ™”
    plt.rcdefaults()
    
    # ํฐํŠธ ์บ์‹œ ์™„์ „ ์‚ญ์ œ ๋ฐ ์žฌ๊ตฌ์„ฑ
    try:
        fm._load_fontmanager(try_read_cache=False)
    except:
        pass
    
    # ์šด์˜์ฒด์ œ๋ณ„ ํ•œ๊ธ€ ํฐํŠธ ๊ฒฝ๋กœ ์ง์ ‘ ์ง€์ •
    korean_font_paths = []
    
    if platform.system() == 'Windows':
        korean_font_paths = [
            'C:/Windows/Fonts/malgun.ttf',
            'C:/Windows/Fonts/gulim.ttc',
            'C:/Windows/Fonts/batang.ttc',
        ]
        fallback_font = 'Malgun Gothic'
    elif platform.system() == 'Darwin':  # macOS
        korean_font_paths = [
            '/System/Library/Fonts/AppleGothic.ttf',
            '/System/Library/Fonts/Helvetica.ttc',
        ]
        fallback_font = 'AppleGothic'
    else:  # Linux
        korean_font_paths = [
            '/usr/share/fonts/truetype/nanum/NanumGothic.ttf',
            '/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf',
        ]
        fallback_font = 'DejaVu Sans'
    
    # ์‚ฌ์šฉ์ž ์ •์˜ ํฐํŠธ ํŒŒ์ผ ๊ฒฝ๋กœ๋„ ์ถ”๊ฐ€
    user_font = os.path.join(os.getcwd(), "NanumGaRamYeonGgoc.ttf")
    if os.path.exists(user_font):
        korean_font_paths.insert(0, user_font)
    
    selected_font_path = None
    selected_font_name = fallback_font
    
    # ์‹ค์ œ ์กด์žฌํ•˜๋Š” ํฐํŠธ ํŒŒ์ผ ์ฐพ๊ธฐ
    for font_path in korean_font_paths:
        if os.path.exists(font_path):
            try:
                font_prop = fm.FontProperties(fname=font_path)
                selected_font_name = font_prop.get_name()
                selected_font_path = font_path
                
                # ํฐํŠธ ๋งค๋‹ˆ์ €์— ๊ฐ•์ œ ๋“ฑ๋ก
                fm.fontManager.addfont(font_path)
                break
            except Exception as e:
                continue
    
    # matplotlib rcParams ๊ฐ•์ œ ์„ค์ •
    plt.rcParams.update({
        'font.family': 'sans-serif',
        'font.sans-serif': [selected_font_name, 'DejaVu Sans', 'Arial', 'sans-serif'],
        'axes.unicode_minus': False,
        'font.size': 12,
        'figure.dpi': 100,
        'savefig.dpi': 100,
        'figure.facecolor': 'white',
        'axes.facecolor': 'white'
    })
    
    # ์ „์—ญ ํฐํŠธ ์†์„ฑ ๊ฐ์ฒด ์ƒ์„ฑ
    if selected_font_path:
        global KOREAN_FONT_PROP
        KOREAN_FONT_PROP = fm.FontProperties(fname=selected_font_path)
    else:
        KOREAN_FONT_PROP = fm.FontProperties(family=selected_font_name)
    
    return selected_font_name, selected_font_path

# ์•ฑ ์‹œ์ž‘ ์‹œ ์ฆ‰์‹œ ํฐํŠธ ์„ค์ •
FONT_NAME, FONT_PATH = configure_matplotlib_korean()
KOREAN_FONT_PROP = None

def apply_korean_font_to_plot():
    """๊ฐœ๋ณ„ ํ”Œ๋กฏ์— ํ•œ๊ธ€ ํฐํŠธ ์ง์ ‘ ์ ์šฉ"""
    if FONT_PATH and os.path.exists(FONT_PATH):
        font_prop = fm.FontProperties(fname=FONT_PATH)
        return font_prop
    else:
        return fm.FontProperties(family=FONT_NAME)

def analyze_scores(df):
    """๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„์„ ๋ฐ›์•„ ๋ถ„์„ ๊ฒฐ๊ณผ๋ฅผ ํ‘œ์‹œํ•˜๋Š” ํ•จ์ˆ˜"""
    st.subheader("๐Ÿ“‹ ๋ฐ์ดํ„ฐ ๋ฏธ๋ฆฌ๋ณด๊ธฐ (์ƒ์œ„ 5๊ฐœ)")
    st.dataframe(df.head())

    # ์ˆซ์ž ํ˜•์‹์˜ ์—ด๋งŒ ์„ ํƒ์ง€๋กœ ์ œ๊ณต
    numeric_columns = df.select_dtypes(include=np.number).columns.tolist()
    if not numeric_columns:
        st.error("โŒ ๋ฐ์ดํ„ฐ์—์„œ ๋ถ„์„ ๊ฐ€๋Šฅํ•œ ์ˆซ์ž ํ˜•์‹์˜ ์—ด์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
        return

    score_column = st.selectbox("๐Ÿ“Š ๋ถ„์„ํ•  ์ ์ˆ˜ ์—ด(column)์„ ์„ ํƒํ•˜์„ธ์š”:", numeric_columns)

    if score_column:
        scores = df[score_column].dropna()
        
        if len(scores) == 0:
            st.error("โŒ ์„ ํƒํ•œ ์—ด์— ์œ ํšจํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.")
            return
            
        st.subheader(f"๐Ÿ“ˆ '{score_column}' ์ ์ˆ˜ ๋ถ„ํฌ ๋ถ„์„ ๊ฒฐ๊ณผ")

        # 1. ๊ธฐ์ˆ  ํ†ต๊ณ„๋Ÿ‰
        st.write("#### ๐Ÿ“Š ๊ธฐ๋ณธ ํ†ต๊ณ„๋Ÿ‰")
        col1, col2, col3, col4 = st.columns(4)
        with col1:
            st.metric("ํ‰๊ท ", f"{scores.mean():.2f}")
        with col2:
            st.metric("ํ‘œ์ค€ํŽธ์ฐจ", f"{scores.std():.2f}")
        with col3:
            st.metric("์ตœ์†Ÿ๊ฐ’", f"{scores.min():.2f}")
        with col4:
            st.metric("์ตœ๋Œ“๊ฐ’", f"{scores.max():.2f}")
        
        # ์ƒ์„ธ ํ†ต๊ณ„
        st.write("#### ๐Ÿ“‹ ์ƒ์„ธ ํ†ต๊ณ„๋Ÿ‰")
        st.dataframe(scores.describe().to_frame().T)

        # 2. ๋ถ„ํฌ ์‹œ๊ฐํ™” - ๊ฐ•ํ™”๋œ ํ•œ๊ธ€ ํฐํŠธ ์ ์šฉ
        st.write("#### ๐ŸŽจ ์ ์ˆ˜ ๋ถ„ํฌ ์‹œ๊ฐํ™”")
        
        try:
            # ํฐํŠธ ์†์„ฑ ๊ฐ์ฒด ์ƒ์„ฑ
            korean_font = apply_korean_font_to_plot()
            
            # Figure ์ƒ์„ฑ ๋ฐ ์„ค์ •
            fig, ax = plt.subplots(figsize=(14, 8))
            fig.patch.set_facecolor('white')
            
            # ํžˆ์Šคํ† ๊ทธ๋žจ ์ƒ์„ฑ
            n, bins, patches = ax.hist(scores, bins=20, density=True, alpha=0.7, 
                                     color='skyblue', edgecolor='navy', linewidth=0.8)
            
            # KDE ๊ณก์„  ์ถ”๊ฐ€
            try:
                from scipy.stats import gaussian_kde
                kde = gaussian_kde(scores)
                x_range = np.linspace(scores.min(), scores.max(), 200)
                ax.plot(x_range, kde(x_range), 'orange', linewidth=3, label='์‹ค์ œ ๋ถ„ํฌ ๊ณก์„ ')
            except:
                pass
            
            # ์ •๊ทœ๋ถ„ํฌ ๊ณก์„  ์ถ”๊ฐ€
            mu, std = norm.fit(scores)
            x_norm = np.linspace(scores.min(), scores.max(), 100)
            y_norm = norm.pdf(x_norm, mu, std)
            ax.plot(x_norm, y_norm, 'red', linewidth=2, linestyle='--', 
                   label=f'์ •๊ทœ๋ถ„ํฌ (ํ‰๊ท ={mu:.1f}, ํ‘œ์ค€ํŽธ์ฐจ={std:.1f})')
            
            # ํ‰๊ท ์„  ์ถ”๊ฐ€
            ax.axvline(mu, color='red', linestyle=':', linewidth=2, alpha=0.8, label=f'ํ‰๊ท : {mu:.1f}')
            
            # ์ œ๋ชฉ๊ณผ ๋ผ๋ฒจ - ํ•œ๊ธ€ ํฐํŠธ ์ง์ ‘ ์ ์šฉ
            ax.set_title(f'{score_column} ์ ์ˆ˜ ๋ถ„ํฌ ๋ถ„์„', fontproperties=korean_font, fontsize=18, pad=20)
            ax.set_xlabel('์ ์ˆ˜', fontproperties=korean_font, fontsize=14)
            ax.set_ylabel('๋ฐ€๋„', fontproperties=korean_font, fontsize=14)
            
            # ๋ฒ”๋ก€ ์„ค์ •
            legend = ax.legend(prop=korean_font, fontsize=11, loc='upper right')
            legend.get_frame().set_alpha(0.9)
            
            # ๊ฒฉ์ž ์ถ”๊ฐ€
            ax.grid(True, alpha=0.3, linestyle='-', linewidth=0.5)
            
            # ํ†ต๊ณ„ ์ •๋ณด ๋ฐ•์Šค
            stats_text = f'์ƒ˜ํ”Œ ์ˆ˜: {len(scores)}\nํ‰๊ท : {mu:.2f}\nํ‘œ์ค€ํŽธ์ฐจ: {std:.2f}\n์ตœ์†Ÿ๊ฐ’: {scores.min():.1f}\n์ตœ๋Œ“๊ฐ’: {scores.max():.1f}'
            ax.text(0.02, 0.98, stats_text, transform=ax.transAxes, 
                   fontproperties=korean_font, fontsize=10, verticalalignment='top',
                   bbox=dict(boxstyle='round,pad=0.5', facecolor='lightblue', alpha=0.8))
            
            plt.tight_layout()
            st.pyplot(fig)
            
        except Exception as e:
            st.error(f"โŒ ์ƒ์„ธ ๊ทธ๋ž˜ํ”„ ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜: {e}")
            
            # ๋Œ€์ฒด ๊ทธ๋ž˜ํ”„ (์˜์–ด๋งŒ ์‚ฌ์šฉ)
            st.write("**Alternative Chart (English only):**")
            fig2, ax2 = plt.subplots(figsize=(10, 6))
            ax2.hist(scores, bins=15, alpha=0.7, color='lightcoral', edgecolor='black')
            ax2.set_title(f'Distribution of {score_column}', fontsize=14)
            ax2.set_xlabel('Score')
            ax2.set_ylabel('Frequency')
            ax2.grid(True, alpha=0.3)
            st.pyplot(fig2)
            plt.close(fig2)
        finally:
            if 'fig' in locals():
                plt.close(fig)

        # 3. ์™œ๋„ ๋ถ„์„
        st.write("#### ๐Ÿ“ ๋ถ„ํฌ ํ˜•ํƒœ ๋ถ„์„ (์™œ๋„)")
        try:
            skewness = skew(scores)
            col1, col2 = st.columns([1, 2])
            
            with col1:
                st.metric("์™œ๋„ (Skewness)", f"{skewness:.4f}")
            
            with col2:
                if skewness > 0.5:
                    st.success("๐Ÿ”ด **์–‘์˜ ์™œ๋„ (Right Skewed)**: ๋Œ€๋ถ€๋ถ„ ํ•™์ƒ์ด ๋‚ฎ์€ ์ ์ˆ˜๋Œ€์— ๋ถ„ํฌํ•˜๊ณ , ์†Œ์ˆ˜์˜ ๊ณ ๋“์ ์ž๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.")
                elif skewness < -0.5:
                    st.success("๐Ÿ”ต **์Œ์˜ ์™œ๋„ (Left Skewed)**: ๋Œ€๋ถ€๋ถ„ ํ•™์ƒ์ด ๋†’์€ ์ ์ˆ˜๋Œ€์— ๋ถ„ํฌํ•˜๊ณ , ์†Œ์ˆ˜์˜ ์ €๋“์ ์ž๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.")
                else:
                    st.success("๐ŸŸข **๋Œ€์นญ ๋ถ„ํฌ**: ์ ์ˆ˜๊ฐ€ ํ‰๊ท ์„ ์ค‘์‹ฌ์œผ๋กœ ๊ณ ๋ฅด๊ฒŒ ๋ถ„ํฌ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.")
                    
        except Exception as e:
            st.error(f"์™œ๋„ ๊ณ„์‚ฐ ์ค‘ ์˜ค๋ฅ˜: {e}")

        # 4. ์ถ”๊ฐ€ ๋ถ„์„
        st.write("#### ๐Ÿ“‹ ๊ตฌ๊ฐ„๋ณ„ ๋ถ„ํฌ")
        
        # ์ ์ˆ˜ ๊ตฌ๊ฐ„ ๋‚˜๋ˆ„๊ธฐ
        if scores.max() <= 100:  # 100์  ๋งŒ์  ๊ฐ€์ •
            bins_labels = ['0-60', '61-70', '71-80', '81-90', '91-100']
            bins_edges = [0, 60, 70, 80, 90, 100]
        else:
            # ๋™์  ๊ตฌ๊ฐ„ ์ƒ์„ฑ
            min_score, max_score = scores.min(), scores.max()
            interval = (max_score - min_score) / 5
            bins_edges = [min_score + i * interval for i in range(6)]
            bins_labels = [f'{bins_edges[i]:.0f}-{bins_edges[i+1]:.0f}' for i in range(5)]
        
        try:
            score_counts = pd.cut(scores, bins=bins_edges, labels=bins_labels, include_lowest=True).value_counts().sort_index()
            score_percentages = (score_counts / len(scores) * 100).round(1)
            
            result_df = pd.DataFrame({
                '๊ตฌ๊ฐ„': score_counts.index,
                'ํ•™์ƒ ์ˆ˜': score_counts.values,
                '๋น„์œจ (%)': score_percentages.values
            })
            st.dataframe(result_df)
            
        except Exception as e:
            st.warning(f"๊ตฌ๊ฐ„ ๋ถ„์„ ์ค‘ ์˜ค๋ฅ˜: {e}")

def main():
    st.set_page_config(
        page_title="ํ•™์ƒ ์ ์ˆ˜ ๋ถ„์„ ๋„๊ตฌ",
        page_icon="๐Ÿ“Š",
        layout="wide",
        initial_sidebar_state="expanded"
    )
    
    # ์ œ๋ชฉ
    st.title("๐Ÿ“Š ํ•™์ƒ ์ ์ˆ˜ ๋ถ„ํฌ ๋ถ„์„ ๋„๊ตฌ")
    st.markdown("**CSV ํŒŒ์ผ์„ ์—…๋กœ๋“œํ•˜๊ฑฐ๋‚˜ Google Sheets URL์„ ์ž…๋ ฅํ•˜์—ฌ ์ ์ˆ˜ ๋ถ„ํฌ๋ฅผ ๋ถ„์„ํ•˜์„ธ์š”**")
    
    # ํฐํŠธ ์ •๋ณด ํ‘œ์‹œ
    with st.expander("๐Ÿ”ง ํฐํŠธ ์„ค์ • ์ •๋ณด"):
        st.write(f"**ํ˜„์žฌ ํฐํŠธ**: {FONT_NAME}")
        st.write(f"**ํฐํŠธ ๊ฒฝ๋กœ**: {FONT_PATH if FONT_PATH else '์‹œ์Šคํ…œ ๊ธฐ๋ณธ'}")
        
        # ํฐํŠธ ํ…Œ์ŠคํŠธ
        if st.button("ํฐํŠธ ํ…Œ์ŠคํŠธ"):
            try:
                test_fig, test_ax = plt.subplots(figsize=(8, 3))
                korean_font = apply_korean_font_to_plot()
                test_ax.text(0.5, 0.7, 'ํ•œ๊ธ€ ํฐํŠธ ํ…Œ์ŠคํŠธ', ha='center', va='center', 
                           fontproperties=korean_font, fontsize=16)
                test_ax.text(0.5, 0.3, '์ ์ˆ˜ ๋ถ„ํฌ ๋ถ„์„ ๊ทธ๋ž˜ํ”„', ha='center', va='center', 
                           fontproperties=korean_font, fontsize=14)
                test_ax.set_xlim(0, 1)
                test_ax.set_ylim(0, 1)
                test_ax.axis('off')
                st.pyplot(test_fig)
                plt.close(test_fig)
            except Exception as e:
                st.error(f"ํฐํŠธ ํ…Œ์ŠคํŠธ ์‹คํŒจ: {e}")
    
    st.markdown("---")

    # ์‚ฌ์ด๋“œ๋ฐ” - ๋ฐ์ดํ„ฐ ์ž…๋ ฅ
    st.sidebar.title("๐Ÿ“ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ")
    source_option = st.sidebar.radio(
        "๋ฐ์ดํ„ฐ ์†Œ์Šค ์„ ํƒ:", 
        ("๐Ÿ“ค CSV ํŒŒ์ผ ์—…๋กœ๋“œ", "๐Ÿ”— Google Sheets URL", "๐ŸŽฒ ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ")
    )
    
    df = None

    if source_option == "๐Ÿ“ค CSV ํŒŒ์ผ ์—…๋กœ๋“œ":
        uploaded_file = st.sidebar.file_uploader(
            "CSV ํŒŒ์ผ์„ ์„ ํƒํ•˜์„ธ์š”", 
            type=["csv"],
            help="UTF-8, CP949 ๋“ฑ ๋‹ค์–‘ํ•œ ์ธ์ฝ”๋”ฉ์„ ์ž๋™์œผ๋กœ ๊ฐ์ง€ํ•ฉ๋‹ˆ๋‹ค"
        )
        if uploaded_file:
            encodings = ['utf-8-sig', 'utf-8', 'cp949', 'euc-kr', 'latin1']
            for encoding in encodings:
                try:
                    df = pd.read_csv(uploaded_file, encoding=encoding)
                    st.sidebar.success(f"โœ… ํŒŒ์ผ ๋กœ๋”ฉ ์„ฑ๊ณต! (์ธ์ฝ”๋”ฉ: {encoding})")
                    break
                except UnicodeDecodeError:
                    continue
                except Exception as e:
                    st.sidebar.error(f"ํŒŒ์ผ ์ฝ๊ธฐ ์˜ค๋ฅ˜: {e}")
                    break
            
            if df is None:
                st.sidebar.error("โŒ ํŒŒ์ผ ์ธ์ฝ”๋”ฉ์„ ์ธ์‹ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")

    elif source_option == "๐Ÿ”— Google Sheets URL":
        st.sidebar.info("๐Ÿ’ก Google Sheets๋ฅผ '์›น์— ๊ฒŒ์‹œ'ํ•œ ํ›„ CSV URL์„ ์ž…๋ ฅํ•˜์„ธ์š”")
        url = st.sidebar.text_input(
            "Google Sheets CSV URL", 
            placeholder="https://docs.google.com/spreadsheets/d/..."
        )
        
        if url and "docs.google.com" in url:
            try:
                with st.spinner("๐Ÿ“ฅ ๋ฐ์ดํ„ฐ ๋กœ๋”ฉ ์ค‘..."):
                    df = pd.read_csv(url)
                st.sidebar.success("โœ… Google Sheets ๋กœ๋”ฉ ์„ฑ๊ณต!")
            except Exception as e:
                st.sidebar.error(f"โŒ URL ๋กœ๋”ฉ ์‹คํŒจ: {e}")
        elif url:
            st.sidebar.warning("โš ๏ธ ์˜ฌ๋ฐ”๋ฅธ Google Sheets URL์„ ์ž…๋ ฅํ•˜์„ธ์š”")

    elif source_option == "๐ŸŽฒ ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ":
        if st.sidebar.button("์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ ์ƒ์„ฑ"):
            np.random.seed(42)
            sample_size = st.sidebar.slider("์ƒ˜ํ”Œ ํฌ๊ธฐ", 50, 500, 100)
            
            df = pd.DataFrame({
                'ํ•™์ƒ๋ฒˆํ˜ธ': range(1, sample_size + 1),
                '์ˆ˜ํ•™์ ์ˆ˜': np.random.normal(75, 15, sample_size).clip(0, 100).round(1),
                '์˜์–ด์ ์ˆ˜': np.random.normal(80, 12, sample_size).clip(0, 100).round(1),
                '๊ณผํ•™์ ์ˆ˜': np.random.normal(70, 18, sample_size).clip(0, 100).round(1),
                '๊ตญ์–ด์ ์ˆ˜': np.random.normal(77, 14, sample_size).clip(0, 100).round(1)
            })
            st.sidebar.success(f"โœ… {sample_size}๋ช…์˜ ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ ์ƒ์„ฑ!")

    # ๋ฉ”์ธ ๋ถ„์„
    if df is not None and not df.empty:
        st.success(f"๐ŸŽ‰ ๋ฐ์ดํ„ฐ ๋กœ๋”ฉ ์™„๋ฃŒ! **{len(df)}๊ฐœ ํ–‰, {len(df.columns)}๊ฐœ ์—ด**")
        analyze_scores(df)
    else:
        st.info("๐Ÿ‘ˆ **์‚ฌ์ด๋“œ๋ฐ”์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์„ ํƒํ•˜์„ธ์š”**")
        
        # ๊ธฐ๋Šฅ ์•ˆ๋‚ด
        st.markdown("""
        ### ๐Ÿ” ์ฃผ์š” ๊ธฐ๋Šฅ
        - **๐Ÿ“Š ๊ธฐ๋ณธ ํ†ต๊ณ„**: ํ‰๊ท , ํ‘œ์ค€ํŽธ์ฐจ, ์ตœ์†Ÿ๊ฐ’, ์ตœ๋Œ“๊ฐ’ ๋“ฑ
        - **๐Ÿ“ˆ ๋ถ„ํฌ ์‹œ๊ฐํ™”**: ํžˆ์Šคํ† ๊ทธ๋žจ, KDE ๊ณก์„ , ์ •๊ทœ๋ถ„ํฌ ๋น„๊ต
        - **๐Ÿ“ ์™œ๋„ ๋ถ„์„**: ๋ถ„ํฌ์˜ ๋น„๋Œ€์นญ์„ฑ ์ธก์ •
        - **๐Ÿ“‹ ๊ตฌ๊ฐ„๋ณ„ ๋ถ„ํฌ**: ์ ์ˆ˜ ๊ตฌ๊ฐ„๋ณ„ ํ•™์ƒ ์ˆ˜ ๋ฐ ๋น„์œจ
        
        ### ๐Ÿ“ ์ง€์› ํ˜•์‹
        - **CSV ํŒŒ์ผ**: UTF-8, CP949, EUC-KR ๋“ฑ ์ž๋™ ์ธ์ฝ”๋”ฉ ๊ฐ์ง€
        - **Google Sheets**: ์›น์— ๊ฒŒ์‹œ๋œ ์‹œํŠธ์˜ CSV URL
        - **์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ**: ํ…Œ์ŠคํŠธ์šฉ ๊ฐ€์ƒ ์ ์ˆ˜ ๋ฐ์ดํ„ฐ
        """)

if __name__ == '__main__':
    main()