ObsidianStyleGraphViewer / streamlit_app.py
jeongsoo's picture
Initial commit
a5b44fe
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"