import streamlit as st import json import os import uuid import glob from datetime import datetime import numpy as np import platform import networkx as nx import plotly.graph_objects as go from sklearn.metrics.pairwise import cosine_similarity import plotly import matplotlib.pyplot as plt import matplotlib.font_manager as fm from sklearn.manifold import TSNE import warnings warnings.filterwarnings('ignore') # 페이지 설정 st.set_page_config( page_title="한국어 단어 의미 네트워크 시각화", page_icon="🔤", layout="wide" ) # 폴더 경로 설정 DATA_FOLDER = 'data' UPLOAD_FOLDER = 'uploads' # 폴더 생성 if not os.path.exists(UPLOAD_FOLDER): os.makedirs(UPLOAD_FOLDER) # 세션 상태 초기화 if 'model' not in st.session_state: st.session_state.model = None if 'embeddings_cache' not in st.session_state: st.session_state.embeddings_cache = {} if 'graph_cache' not in st.session_state: st.session_state.graph_cache = {} if 'data_files' not in st.session_state: st.session_state.data_files = {} if 'selected_files' not in st.session_state: st.session_state.selected_files = [] if 'threshold' not in st.session_state: st.session_state.threshold = 0.7 # --- 한글 폰트 설정 함수 --- def set_korean_font(): """ 현재 운영체제에 맞는 한글 폰트를 matplotlib 및 Plotly용으로 설정 시도하고, Plotly에서 사용할 폰트 이름을 반환합니다. """ system_name = platform.system() plotly_font_name = None # Plotly에서 사용할 폰트 이름 # Matplotlib 폰트 설정 if system_name == "Windows": font_name = "Malgun Gothic" plotly_font_name = "Malgun Gothic" elif system_name == "Darwin": # MacOS font_name = "AppleGothic" plotly_font_name = "AppleGothic" elif system_name == "Linux": # Linux에서 선호하는 한글 폰트 경로 또는 이름 설정 font_path = "/usr/share/fonts/truetype/nanum/NanumGothic.ttf" plotly_font_name_linux = "NanumGothic" # Plotly는 폰트 '이름'을 주로 사용 if os.path.exists(font_path): font_name = fm.FontProperties(fname=font_path).get_name() plotly_font_name = plotly_font_name_linux else: # 시스템에서 'Nanum' 포함 폰트 찾기 시도 try: available_fonts = [f.name for f in fm.fontManager.ttflist] nanum_fonts = [name for name in available_fonts if 'Nanum' in name] if nanum_fonts: font_name = nanum_fonts[0] # Plotly에서 사용할 이름도 비슷하게 설정 (정확한 이름은 시스템마다 다를 수 있음) plotly_font_name = font_name if 'Nanum' in font_name else plotly_font_name_linux else: # 다른 OS 폰트 시도 if "Malgun Gothic"