Nyanpre commited on
Commit
df312cd
·
verified ·
1 Parent(s): 23aa325

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -12
app.py CHANGED
@@ -107,7 +107,6 @@ def analyze_and_output(my_id, my_pw, target_id, freq_type, progress=gr.Progress(
107
  kanjis = re.findall(r'[一-龠]', all_text)
108
  rep_kanji = Counter(kanjis).most_common(1)[0][0] if kanjis else "魂"
109
 
110
- # HTML: 交流相手
111
  reply_html = ""
112
  for handle, count in reply_counts.most_common(3):
113
  info = user_info_cache.get(handle, get_profile_safe(client, handle))
@@ -117,7 +116,6 @@ def analyze_and_output(my_id, my_pw, target_id, freq_type, progress=gr.Progress(
117
  <span><b>{handle}</b></span><span style='margin-left:auto'>{count}回</span>
118
  </div>"""
119
 
120
- # HTML: 全体構造
121
  html = f"""
122
  <div class="dashboard-container">
123
  <div class="card kanji-card">
@@ -141,7 +139,6 @@ def analyze_and_output(my_id, my_pw, target_id, freq_type, progress=gr.Progress(
141
  </a>"""
142
  html += "</div></div>"
143
 
144
- # グラフ作成
145
  df_counts = df.set_index('created_at').resample({"日ごと":"D","週ごと":"W","月ごと":"M"}[freq_type]).size().reset_index(name='count')
146
  fig_bar = px.bar(df_counts, x='created_at', y='count', color_discrete_sequence=['#0085ff'], template="plotly_white", title="投稿頻度")
147
 
@@ -150,7 +147,7 @@ def analyze_and_output(my_id, my_pw, target_id, freq_type, progress=gr.Progress(
150
  heat_data.index = ['月','火','水','木','金','土','日']
151
  fig_heat = px.imshow(heat_data, color_continuous_scale='Blues', title="活動時間帯(JST)", labels=dict(x="時間", y="曜日", color="投稿数"))
152
 
153
- # 相関図作成
154
  top_interactors = [u for u, _ in reply_counts.most_common(8)]
155
  nodes = list(set([target_handle] + top_interactors))
156
  G = nx.Graph()
@@ -159,23 +156,54 @@ def analyze_and_output(my_id, my_pw, target_id, freq_type, progress=gr.Progress(
159
 
160
  pos = nx.spring_layout(G, k=1.5, seed=42)
161
  fig_net = go.Figure()
 
 
162
  for e in G.edges():
163
  fig_net.add_trace(go.Scatter(x=[pos[e[0]][0], pos[e[1]][0]], y=[pos[e[0]][1], pos[e[1]][1]], mode='lines', line=dict(color='#ccc', width=1), hoverinfo='none'))
164
 
165
  node_imgs = []
166
- node_labels = []
 
 
 
 
167
  for n in G.nodes():
168
  info = user_info_cache.get(n, {"avatar": ""})
169
  if info["avatar"]:
170
- node_imgs.append(dict(source=info["avatar"], xref="x", yref="y", x=pos[n][0], y=pos[n][1], sizex=0.2, sizey=0.2, xanchor="center", yanchor="middle", layer="above"))
 
 
 
 
 
171
 
172
- # 本人以外には関係性ラベルを付与
173
- rel = f"<br><span style='color:#0085ff;'>◆{random.choice(RELATIONSHIPS)}</span>" if n != target_handle else "<br><b>(本人)</b>"
174
- node_labels.append(f"<br><br><br><b>{n}</b>{rel}")
 
175
 
176
- fig_net.add_trace(go.Scatter(x=[pos[n][0] for n in nodes], y=[pos[n][1] for n in nodes], mode='text',
177
- text=node_labels, textposition="bottom center", textfont=dict(size=11, color="black")))
178
- fig_net.update_layout(images=node_imgs, showlegend=False, xaxis=dict(visible=False), yaxis=dict(visible=False), plot_bgcolor='white', height=600, margin=dict(t=40, b=0, l=0, r=0))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179
 
180
  return html, fig_bar, fig_heat, fig_net, "解析完了!"
181
  except Exception as e: return f"エラー: {e}", None, None, None, "失敗"
 
107
  kanjis = re.findall(r'[一-龠]', all_text)
108
  rep_kanji = Counter(kanjis).most_common(1)[0][0] if kanjis else "魂"
109
 
 
110
  reply_html = ""
111
  for handle, count in reply_counts.most_common(3):
112
  info = user_info_cache.get(handle, get_profile_safe(client, handle))
 
116
  <span><b>{handle}</b></span><span style='margin-left:auto'>{count}回</span>
117
  </div>"""
118
 
 
119
  html = f"""
120
  <div class="dashboard-container">
121
  <div class="card kanji-card">
 
139
  </a>"""
140
  html += "</div></div>"
141
 
 
142
  df_counts = df.set_index('created_at').resample({"日ごと":"D","週ごと":"W","月ごと":"M"}[freq_type]).size().reset_index(name='count')
143
  fig_bar = px.bar(df_counts, x='created_at', y='count', color_discrete_sequence=['#0085ff'], template="plotly_white", title="投稿頻度")
144
 
 
147
  heat_data.index = ['月','火','水','木','金','土','日']
148
  fig_heat = px.imshow(heat_data, color_continuous_scale='Blues', title="活動時間帯(JST)", labels=dict(x="時間", y="曜日", color="投稿数"))
149
 
150
+ # --- 相関図の修正箇所 ---
151
  top_interactors = [u for u, _ in reply_counts.most_common(8)]
152
  nodes = list(set([target_handle] + top_interactors))
153
  G = nx.Graph()
 
156
 
157
  pos = nx.spring_layout(G, k=1.5, seed=42)
158
  fig_net = go.Figure()
159
+
160
+ # エッジの描画
161
  for e in G.edges():
162
  fig_net.add_trace(go.Scatter(x=[pos[e[0]][0], pos[e[1]][0]], y=[pos[e[0]][1], pos[e[1]][1]], mode='lines', line=dict(color='#ccc', width=1), hoverinfo='none'))
163
 
164
  node_imgs = []
165
+ node_texts = []
166
+
167
+ # アイコンの大きさを定義
168
+ img_size = 0.18
169
+
170
  for n in G.nodes():
171
  info = user_info_cache.get(n, {"avatar": ""})
172
  if info["avatar"]:
173
+ node_imgs.append(dict(
174
+ source=info["avatar"], xref="x", yref="y",
175
+ x=pos[n][0], y=pos[n][1],
176
+ sizex=img_size, sizey=img_size,
177
+ xanchor="center", yanchor="middle", layer="above"
178
+ ))
179
 
180
+ # 関係性ラベル
181
+ rel = f"<span style='color:#0085ff;'>◆{random.choice(RELATIONSHIPS)}</span>" if n != target_handle else "<b>(本人)</b>"
182
+ # アイコンの下に潜り込まないよう、適度な空白を調整
183
+ node_texts.append(f"<b>{n}</b><br>{rel}")
184
 
185
+ # ノード(透明なマーカー + テキスト)の描画
186
+ fig_net.add_trace(go.Scatter(
187
+ x=[pos[n][0] for n in nodes],
188
+ y=[pos[n][1] for n in nodes],
189
+ mode='markers+text',
190
+ text=node_texts,
191
+ # markerのsizeを調整することで、textposition="bottom center"の開始位置をアイコンの外側に逃がす
192
+ marker=dict(size=45, color='rgba(0,0,0,0)'),
193
+ textposition="bottom center",
194
+ textfont=dict(size=11, color="black"),
195
+ hoverinfo='none'
196
+ ))
197
+
198
+ fig_net.update_layout(
199
+ images=node_imgs,
200
+ showlegend=False,
201
+ xaxis=dict(visible=False, range=[-1.2, 1.2]),
202
+ yaxis=dict(visible=False, range=[-1.2, 1.2]),
203
+ plot_bgcolor='white',
204
+ height=600,
205
+ margin=dict(t=40, b=40, l=0, r=0)
206
+ )
207
 
208
  return html, fig_bar, fig_heat, fig_net, "解析完了!"
209
  except Exception as e: return f"エラー: {e}", None, None, None, "失敗"