Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,8 +9,10 @@ import re
|
|
| 9 |
import os
|
| 10 |
import altair as alt
|
| 11 |
import colorsys
|
| 12 |
-
import networkx as nx
|
| 13 |
import streamlit.components.v1 as components
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# Streamlit 페이지 설정
|
| 16 |
st.set_page_config(layout="wide", page_title="📊 토픽모델링 for SK", page_icon="📊")
|
|
@@ -36,8 +38,7 @@ def generate_colors(n):
|
|
| 36 |
HSV_tuples = [(x * 1.0 / n, 0.5, 0.9) for x in range(n)]
|
| 37 |
return ['#%02x%02x%02x' % tuple(int(x*255) for x in colorsys.hsv_to_rgb(*hsv)) for hsv in HSV_tuples]
|
| 38 |
|
| 39 |
-
|
| 40 |
-
def create_network_graph(topic_results, num_words=30):
|
| 41 |
G = nx.Graph()
|
| 42 |
colors = generate_colors(len(topic_results))
|
| 43 |
|
|
@@ -47,77 +48,39 @@ def create_network_graph(topic_results, num_words=30):
|
|
| 47 |
|
| 48 |
for word in words:
|
| 49 |
if not G.has_node(word):
|
| 50 |
-
G.add_node(word, color=color
|
| 51 |
|
| 52 |
for i in range(len(words)):
|
| 53 |
for j in range(i+1, len(words)):
|
| 54 |
if not G.has_edge(words[i], words[j]):
|
| 55 |
-
G.add_edge(words[i], words[j]
|
| 56 |
|
| 57 |
return G
|
| 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 |
-
edges: edges
|
| 84 |
-
}};
|
| 85 |
-
var options = {{
|
| 86 |
-
nodes: {{
|
| 87 |
-
shape: 'circle',
|
| 88 |
-
size: 30,
|
| 89 |
-
font: {{
|
| 90 |
-
size: 10,
|
| 91 |
-
color: '#ffffff',
|
| 92 |
-
face: 'Arial',
|
| 93 |
-
vadjust: -10
|
| 94 |
-
}},
|
| 95 |
-
borderWidth: 2,
|
| 96 |
-
color: {{
|
| 97 |
-
background: '#4CAF50',
|
| 98 |
-
border: '#45A049'
|
| 99 |
-
}}
|
| 100 |
-
}},
|
| 101 |
-
edges: {{
|
| 102 |
-
width: 1,
|
| 103 |
-
color: {{color: '#999999'}}
|
| 104 |
-
}},
|
| 105 |
-
physics: {{
|
| 106 |
-
stabilization: false,
|
| 107 |
-
barnesHut: {{
|
| 108 |
-
gravitationalConstant: -2000,
|
| 109 |
-
centralGravity: 0.3,
|
| 110 |
-
springLength: 150,
|
| 111 |
-
springConstant: 0.04
|
| 112 |
-
}}
|
| 113 |
-
}}
|
| 114 |
-
}};
|
| 115 |
-
var network = new vis.Network(container, data, options);
|
| 116 |
-
</script>
|
| 117 |
-
</body>
|
| 118 |
-
</html>
|
| 119 |
-
"""
|
| 120 |
-
return html_content
|
| 121 |
|
| 122 |
# 헤더 스타일 변경
|
| 123 |
st.markdown("""
|
|
@@ -322,12 +285,19 @@ if 'run_analysis' in st.session_state and st.session_state.run_analysis:
|
|
| 322 |
|
| 323 |
st.altair_chart(chart, use_container_width=True)
|
| 324 |
|
| 325 |
-
# 네트워크 그래프 생성 및 시각화
|
| 326 |
st.header("토픽 단어 네트워크 그래프")
|
| 327 |
try:
|
| 328 |
-
G = create_network_graph(topic_results)
|
| 329 |
-
|
| 330 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 331 |
except Exception as e:
|
| 332 |
st.error(f"네트워크 그래프 생성 중 오류가 발생했습니다: {str(e)}")
|
| 333 |
|
|
|
|
| 9 |
import os
|
| 10 |
import altair as alt
|
| 11 |
import colorsys
|
|
|
|
| 12 |
import streamlit.components.v1 as components
|
| 13 |
+
import matplotlib.pyplot as plt
|
| 14 |
+
import networkx as nx
|
| 15 |
+
import io
|
| 16 |
|
| 17 |
# Streamlit 페이지 설정
|
| 18 |
st.set_page_config(layout="wide", page_title="📊 토픽모델링 for SK", page_icon="📊")
|
|
|
|
| 38 |
HSV_tuples = [(x * 1.0 / n, 0.5, 0.9) for x in range(n)]
|
| 39 |
return ['#%02x%02x%02x' % tuple(int(x*255) for x in colorsys.hsv_to_rgb(*hsv)) for hsv in HSV_tuples]
|
| 40 |
|
| 41 |
+
def create_network_graph(topic_results, num_words=10):
|
|
|
|
| 42 |
G = nx.Graph()
|
| 43 |
colors = generate_colors(len(topic_results))
|
| 44 |
|
|
|
|
| 48 |
|
| 49 |
for word in words:
|
| 50 |
if not G.has_node(word):
|
| 51 |
+
G.add_node(word, color=color)
|
| 52 |
|
| 53 |
for i in range(len(words)):
|
| 54 |
for j in range(i+1, len(words)):
|
| 55 |
if not G.has_edge(words[i], words[j]):
|
| 56 |
+
G.add_edge(words[i], words[j])
|
| 57 |
|
| 58 |
return G
|
| 59 |
|
| 60 |
+
def plot_network_graph(G):
|
| 61 |
+
plt.figure(figsize=(12, 8))
|
| 62 |
+
pos = nx.spring_layout(G, k=0.5, iterations=50)
|
| 63 |
+
|
| 64 |
+
node_colors = [G.nodes[node]['color'] for node in G.nodes()]
|
| 65 |
+
|
| 66 |
+
nx.draw(G, pos, node_color=node_colors, with_labels=True, node_size=3000, font_size=8,
|
| 67 |
+
font_weight='bold', edge_color='gray', width=0.5)
|
| 68 |
+
|
| 69 |
+
# 노드 라벨을 다시 그려 배경을 하얀색으로 만듭니다
|
| 70 |
+
for node, (x, y) in pos.items():
|
| 71 |
+
plt.text(x, y, node, fontsize=8, fontweight='bold', ha='center', va='center',
|
| 72 |
+
bbox=dict(facecolor='white', edgecolor='none', alpha=0.7, pad=0.5))
|
| 73 |
+
|
| 74 |
+
plt.title("Topic Word Network", fontsize=16)
|
| 75 |
+
plt.axis('off')
|
| 76 |
+
|
| 77 |
+
# 이미지를 바이트 스트림으로 저장
|
| 78 |
+
img_bytes = io.BytesIO()
|
| 79 |
+
plt.savefig(img_bytes, format='png', dpi=300, bbox_inches='tight')
|
| 80 |
+
img_bytes.seek(0)
|
| 81 |
+
plt.close()
|
| 82 |
+
|
| 83 |
+
return img_bytes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
# 헤더 스타일 변경
|
| 86 |
st.markdown("""
|
|
|
|
| 285 |
|
| 286 |
st.altair_chart(chart, use_container_width=True)
|
| 287 |
|
|
|
|
| 288 |
st.header("토픽 단어 네트워크 그래프")
|
| 289 |
try:
|
| 290 |
+
G = create_network_graph(topic_results, num_words=10) # 상위 10개 단어만 사용
|
| 291 |
+
img_bytes = plot_network_graph(G)
|
| 292 |
+
st.image(img_bytes, caption="Topic Word Network", use_column_width=True)
|
| 293 |
+
|
| 294 |
+
# 다운로드 버튼 추가
|
| 295 |
+
st.download_button(
|
| 296 |
+
label="네트워크 그래프 다운로드",
|
| 297 |
+
data=img_bytes,
|
| 298 |
+
file_name="topic_network_graph.png",
|
| 299 |
+
mime="image/png"
|
| 300 |
+
)
|
| 301 |
except Exception as e:
|
| 302 |
st.error(f"네트워크 그래프 생성 중 오류가 발생했습니다: {str(e)}")
|
| 303 |
|