Spaces:
Running
Running
Update app-backup2.py
Browse files- app-backup2.py +127 -52
app-backup2.py
CHANGED
|
@@ -38,54 +38,109 @@ with open(EVENTS_PATH, encoding="utf-8") as f:
|
|
| 38 |
# 3. Helper functions
|
| 39 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 40 |
def half_sine(xs, period, amp):
|
|
|
|
| 41 |
phase = np.mod(xs - CENTER, period)
|
| 42 |
y = amp * np.sin(np.pi * phase / period)
|
| 43 |
y[y < 0] = 0
|
| 44 |
return y
|
| 45 |
|
| 46 |
def build_chart(start: int, end: int):
|
| 47 |
-
|
|
|
|
| 48 |
fig = go.Figure()
|
| 49 |
|
| 50 |
-
#
|
| 51 |
for period in ORDERED_PERIODS:
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
for year, evs in EVENTS.items():
|
| 60 |
if start <= year <= end:
|
| 61 |
cycle_name = evs[0]["cycle"]
|
| 62 |
-
period
|
| 63 |
-
|
| 64 |
-
txt_en
|
| 65 |
-
txt_ko
|
|
|
|
| 66 |
fig.add_trace(go.Scatter(
|
| 67 |
-
x=[year], y=[
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
| 71 |
"Year %{x} โข %{customdata[0]} cycle"
|
| 72 |
"<br>%{customdata[1]}"
|
| 73 |
"<br>%{customdata[2]}<extra></extra>"
|
| 74 |
),
|
| 75 |
-
showlegend=False
|
| 76 |
))
|
| 77 |
|
| 78 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
fig.update_layout(
|
| 80 |
-
template="plotly_dark",
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
| 84 |
)
|
| 85 |
-
fig.update_xaxes(title="Year",
|
| 86 |
-
fig.update_yaxes(title="Relative amplitude", showticklabels=False)
|
| 87 |
|
| 88 |
-
summary = f"Range {start}-{end} | Events plotted: {sum(1 for y in EVENTS if start<=y<=end)}"
|
| 89 |
return fig, summary
|
| 90 |
|
| 91 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
@@ -100,14 +155,17 @@ def chat_with_gpt(history, user_msg, chart_summary):
|
|
| 100 |
messages = [{"role": "system", "content": BASE_PROMPT}]
|
| 101 |
if chart_summary not in ("", "No chart yet."):
|
| 102 |
messages.append({"role": "system", "content": f"[Chart summary]\n{chart_summary}"})
|
|
|
|
| 103 |
for u, a in history:
|
| 104 |
-
messages.extend([{"role":"user","content":u},
|
| 105 |
-
|
|
|
|
|
|
|
| 106 |
res = openai.chat.completions.create(
|
| 107 |
-
model="gpt-3.5-turbo",
|
| 108 |
-
messages=messages,
|
| 109 |
-
max_tokens=600,
|
| 110 |
-
temperature=0.7
|
| 111 |
)
|
| 112 |
return res.choices[0].message.content.strip()
|
| 113 |
|
|
@@ -116,64 +174,81 @@ def chat_with_gpt(history, user_msg, chart_summary):
|
|
| 116 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 117 |
def create_app():
|
| 118 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
|
|
| 119 |
gr.Markdown("## ๐ญ **CycleNavigator (Interactive)**")
|
|
|
|
| 120 |
gr.Markdown(
|
| 121 |
"<sub>"
|
| 122 |
-
"
|
|
|
|
|
|
|
|
|
|
| 123 |
"</sub>"
|
| 124 |
)
|
|
|
|
| 125 |
|
| 126 |
chart_summary_state = gr.State(value="No chart yet.")
|
| 127 |
|
| 128 |
with gr.Tabs():
|
| 129 |
-
# โโ Chart
|
| 130 |
with gr.TabItem("Timeline Chart"):
|
| 131 |
with gr.Row():
|
| 132 |
-
start_year = gr.Number(label="Start Year", value=
|
| 133 |
-
end_year = gr.Number(label="End Year", value=
|
| 134 |
zoom_in = gr.Button("๐ Zoom In")
|
| 135 |
zoom_out = gr.Button("๐ Zoom Out")
|
| 136 |
|
| 137 |
-
|
|
|
|
| 138 |
plot = gr.Plot(value=fig0)
|
| 139 |
chart_summary_state.value = summ0
|
| 140 |
|
|
|
|
| 141 |
def refresh(s, e):
|
| 142 |
fig, summ = build_chart(int(s), int(e))
|
| 143 |
return fig, summ
|
| 144 |
-
start_year.change(refresh, [start_year, end_year],
|
| 145 |
-
|
|
|
|
|
|
|
| 146 |
|
|
|
|
| 147 |
def zoom(s, e, factor):
|
| 148 |
-
mid
|
| 149 |
-
span = (e-s)*factor/2
|
| 150 |
-
ns, ne = int(mid-span), int(mid+span)
|
| 151 |
fig, summ = build_chart(ns, ne)
|
| 152 |
return ns, ne, fig, summ
|
| 153 |
-
zoom_in.click(lambda s,e: zoom(s,e,0.5),
|
| 154 |
-
inputs=[start_year,end_year],
|
| 155 |
-
outputs=[start_year,end_year,plot,chart_summary_state])
|
| 156 |
-
zoom_out.click(lambda s,e: zoom(s,e,2.0),
|
| 157 |
-
inputs=[start_year,end_year],
|
| 158 |
-
outputs=[start_year,end_year,plot,chart_summary_state])
|
| 159 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
gr.File(value=str(EVENTS_PATH), label="Download cycle_events.json")
|
| 161 |
|
| 162 |
-
# โโ GPT Chat
|
| 163 |
with gr.TabItem("GPT Chat"):
|
| 164 |
-
chatbot
|
| 165 |
user_input = gr.Textbox(lines=3, placeholder="๋ฉ์์ง๋ฅผ ์
๋ ฅํ์ธ์โฆ")
|
| 166 |
-
send_btn
|
| 167 |
|
| 168 |
def respond(history, msg, summ):
|
| 169 |
reply = chat_with_gpt(history, msg, summ)
|
| 170 |
history.append((msg, reply))
|
| 171 |
return history, gr.Textbox(value="")
|
| 172 |
-
|
|
|
|
|
|
|
| 173 |
[chatbot, user_input])
|
| 174 |
-
user_input.submit(respond,
|
|
|
|
| 175 |
[chatbot, user_input])
|
| 176 |
return demo
|
| 177 |
|
|
|
|
| 178 |
if __name__ == "__main__":
|
| 179 |
create_app().launch()
|
|
|
|
| 38 |
# 3. Helper functions
|
| 39 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 40 |
def half_sine(xs, period, amp):
|
| 41 |
+
"""Half-sine โtowerโ with zero bottom floor."""
|
| 42 |
phase = np.mod(xs - CENTER, period)
|
| 43 |
y = amp * np.sin(np.pi * phase / period)
|
| 44 |
y[y < 0] = 0
|
| 45 |
return y
|
| 46 |
|
| 47 |
def build_chart(start: int, end: int):
|
| 48 |
+
"""Return (Plotly figure, summary string) for the requested year range."""
|
| 49 |
+
xs = np.linspace(start, end, max(1000, (end - start) * 4))
|
| 50 |
fig = go.Figure()
|
| 51 |
|
| 52 |
+
# โผ 1) Draw gradient half-sine โtowersโ (30 layers each, like old matplotlib version)
|
| 53 |
for period in ORDERED_PERIODS:
|
| 54 |
+
base_amp = AMPL[period]
|
| 55 |
+
color = COLOR[period]
|
| 56 |
+
|
| 57 |
+
# โโ gradient layers (thin, semi-transparent) โโโโโโโโโโโโโโโโโโโโโโ
|
| 58 |
+
for frac in np.linspace(base_amp / 30, base_amp, 30):
|
| 59 |
+
ys = half_sine(xs, period, frac)
|
| 60 |
+
fig.add_trace(go.Scatter(
|
| 61 |
+
x = xs,
|
| 62 |
+
y = ys,
|
| 63 |
+
mode = "lines",
|
| 64 |
+
line = dict(color=color, width=0.8),
|
| 65 |
+
opacity = 0.6,
|
| 66 |
+
hoverinfo = "skip",
|
| 67 |
+
showlegend = False
|
| 68 |
+
))
|
| 69 |
|
| 70 |
+
# โโ outer edge line (slightly thicker) โโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 71 |
+
ys = half_sine(xs, period, base_amp)
|
| 72 |
+
fig.add_trace(go.Scatter(
|
| 73 |
+
x = xs,
|
| 74 |
+
y = ys,
|
| 75 |
+
mode = "lines",
|
| 76 |
+
line = dict(color=color, width=1.6),
|
| 77 |
+
hoverinfo = "skip",
|
| 78 |
+
showlegend = False
|
| 79 |
+
))
|
| 80 |
+
|
| 81 |
+
# โผ 2) Event markers with bilingual hover text โโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 82 |
for year, evs in EVENTS.items():
|
| 83 |
if start <= year <= end:
|
| 84 |
cycle_name = evs[0]["cycle"]
|
| 85 |
+
period = PERIOD_BY_CYCLE.get(cycle_name, 50)
|
| 86 |
+
y_val = float(half_sine(np.array([year]), period, AMPL[period]))
|
| 87 |
+
txt_en = "<br>".join(e["event_en"] for e in evs)
|
| 88 |
+
txt_ko = "<br>".join(e["event_ko"] for e in evs)
|
| 89 |
+
|
| 90 |
fig.add_trace(go.Scatter(
|
| 91 |
+
x = [year], y = [y_val],
|
| 92 |
+
mode = "markers",
|
| 93 |
+
marker = dict(color="white", size=6),
|
| 94 |
+
customdata = [[cycle_name, txt_en, txt_ko]],
|
| 95 |
+
hovertemplate = (
|
| 96 |
"Year %{x} โข %{customdata[0]} cycle"
|
| 97 |
"<br>%{customdata[1]}"
|
| 98 |
"<br>%{customdata[2]}<extra></extra>"
|
| 99 |
),
|
| 100 |
+
showlegend = False
|
| 101 |
))
|
| 102 |
|
| 103 |
+
# โผ 3) Cosmetic touches: centre line, 250-yr arrow, background, axes โโโโ
|
| 104 |
+
# centre alignment marker
|
| 105 |
+
fig.add_vline(
|
| 106 |
+
x = CENTER,
|
| 107 |
+
line_dash = "dash",
|
| 108 |
+
line_color = "white",
|
| 109 |
+
opacity = 0.6
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
# 250-year span arrow (โ) across the top of chart
|
| 113 |
+
arrow_y = AMPL[250] * 1.05
|
| 114 |
+
fig.add_annotation(
|
| 115 |
+
x = CENTER - 125, y = arrow_y,
|
| 116 |
+
ax = CENTER + 125, ay = arrow_y,
|
| 117 |
+
xref = "x", yref = "y", axref = "x", ayref = "y",
|
| 118 |
+
showarrow = True,
|
| 119 |
+
arrowhead = 3,
|
| 120 |
+
arrowsize = 1,
|
| 121 |
+
arrowwidth = 1.2,
|
| 122 |
+
arrowcolor = "white"
|
| 123 |
+
)
|
| 124 |
+
fig.add_annotation(
|
| 125 |
+
x = CENTER, y = arrow_y + 0.15,
|
| 126 |
+
text = "250 yr",
|
| 127 |
+
showarrow = False,
|
| 128 |
+
font = dict(color="white", size=10)
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
# global styling
|
| 132 |
fig.update_layout(
|
| 133 |
+
template = "plotly_dark",
|
| 134 |
+
paper_bgcolor = "black",
|
| 135 |
+
plot_bgcolor = "black",
|
| 136 |
+
height = 450,
|
| 137 |
+
margin = dict(t=30, l=40, r=40, b=40),
|
| 138 |
+
hoverlabel = dict(bgcolor="#222", font_size=11)
|
| 139 |
)
|
| 140 |
+
fig.update_xaxes(title="Year", range=[start, end], showgrid=False)
|
| 141 |
+
fig.update_yaxes(title="Relative amplitude", showticklabels=False, showgrid=False)
|
| 142 |
|
| 143 |
+
summary = f"Range {start}-{end} | Events plotted: {sum(1 for y in EVENTS if start <= y <= end)}"
|
| 144 |
return fig, summary
|
| 145 |
|
| 146 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
|
|
| 155 |
messages = [{"role": "system", "content": BASE_PROMPT}]
|
| 156 |
if chart_summary not in ("", "No chart yet."):
|
| 157 |
messages.append({"role": "system", "content": f"[Chart summary]\n{chart_summary}"})
|
| 158 |
+
|
| 159 |
for u, a in history:
|
| 160 |
+
messages.extend([{"role": "user", "content": u},
|
| 161 |
+
{"role": "assistant", "content": a}])
|
| 162 |
+
messages.append({"role": "user", "content": user_msg})
|
| 163 |
+
|
| 164 |
res = openai.chat.completions.create(
|
| 165 |
+
model = "gpt-3.5-turbo",
|
| 166 |
+
messages = messages,
|
| 167 |
+
max_tokens = 600,
|
| 168 |
+
temperature = 0.7
|
| 169 |
)
|
| 170 |
return res.choices[0].message.content.strip()
|
| 171 |
|
|
|
|
| 174 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 175 |
def create_app():
|
| 176 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 177 |
+
# โโ Title & subtitle
|
| 178 |
gr.Markdown("## ๐ญ **CycleNavigator (Interactive)**")
|
| 179 |
+
gr.Markdown("### <sub>Interactive visual service delivering insights at a glance into the four major long cyclesโ9-year business cycle, 50-year Kondratiev wave, 80-year financial credit cycle, and 250-year hegemony cycleโthrough dynamic charts and AI chat.</sub>")
|
| 180 |
gr.Markdown(
|
| 181 |
"<sub>"
|
| 182 |
+
"<b>Business 9y</b> (credit-investment business cycle) โข "
|
| 183 |
+
"<b>K-Wave 50y</b> (long technological-industrial wave) โข "
|
| 184 |
+
"<b>Finance 80y</b> (long credit-debt cycle) โข "
|
| 185 |
+
"<b>Hegemony 250y</b> (rise & fall of global powers cycle)"
|
| 186 |
"</sub>"
|
| 187 |
)
|
| 188 |
+
|
| 189 |
|
| 190 |
chart_summary_state = gr.State(value="No chart yet.")
|
| 191 |
|
| 192 |
with gr.Tabs():
|
| 193 |
+
# โโ Tab 1: Timeline Chart โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 194 |
with gr.TabItem("Timeline Chart"):
|
| 195 |
with gr.Row():
|
| 196 |
+
start_year = gr.Number(label="Start Year", value=1500, precision=0)
|
| 197 |
+
end_year = gr.Number(label="End Year", value=2500, precision=0)
|
| 198 |
zoom_in = gr.Button("๐ Zoom In")
|
| 199 |
zoom_out = gr.Button("๐ Zoom Out")
|
| 200 |
|
| 201 |
+
# initial plot
|
| 202 |
+
fig0, summ0 = build_chart(1500, 2500)
|
| 203 |
plot = gr.Plot(value=fig0)
|
| 204 |
chart_summary_state.value = summ0
|
| 205 |
|
| 206 |
+
# refresh on year change
|
| 207 |
def refresh(s, e):
|
| 208 |
fig, summ = build_chart(int(s), int(e))
|
| 209 |
return fig, summ
|
| 210 |
+
start_year.change(refresh, [start_year, end_year],
|
| 211 |
+
[plot, chart_summary_state])
|
| 212 |
+
end_year.change(refresh, [start_year, end_year],
|
| 213 |
+
[plot, chart_summary_state])
|
| 214 |
|
| 215 |
+
# zoom helpers
|
| 216 |
def zoom(s, e, factor):
|
| 217 |
+
mid = (s + e) / 2
|
| 218 |
+
span = (e - s) * factor / 2
|
| 219 |
+
ns, ne = int(mid - span), int(mid + span)
|
| 220 |
fig, summ = build_chart(ns, ne)
|
| 221 |
return ns, ne, fig, summ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
|
| 223 |
+
zoom_in.click(lambda s, e: zoom(s, e, 0.5),
|
| 224 |
+
inputs = [start_year, end_year],
|
| 225 |
+
outputs = [start_year, end_year, plot, chart_summary_state])
|
| 226 |
+
zoom_out.click(lambda s, e: zoom(s, e, 2.0),
|
| 227 |
+
inputs = [start_year, end_year],
|
| 228 |
+
outputs = [start_year, end_year, plot, chart_summary_state])
|
| 229 |
+
|
| 230 |
+
# JSON download
|
| 231 |
gr.File(value=str(EVENTS_PATH), label="Download cycle_events.json")
|
| 232 |
|
| 233 |
+
# โโ Tab 2: GPT Chat โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 234 |
with gr.TabItem("GPT Chat"):
|
| 235 |
+
chatbot = gr.Chatbot(label="Assistant")
|
| 236 |
user_input = gr.Textbox(lines=3, placeholder="๋ฉ์์ง๋ฅผ ์
๋ ฅํ์ธ์โฆ")
|
| 237 |
+
send_btn = gr.Button("Send")
|
| 238 |
|
| 239 |
def respond(history, msg, summ):
|
| 240 |
reply = chat_with_gpt(history, msg, summ)
|
| 241 |
history.append((msg, reply))
|
| 242 |
return history, gr.Textbox(value="")
|
| 243 |
+
|
| 244 |
+
send_btn.click(respond,
|
| 245 |
+
[chatbot, user_input, chart_summary_state],
|
| 246 |
[chatbot, user_input])
|
| 247 |
+
user_input.submit(respond,
|
| 248 |
+
[chatbot, user_input, chart_summary_state],
|
| 249 |
[chatbot, user_input])
|
| 250 |
return demo
|
| 251 |
|
| 252 |
+
|
| 253 |
if __name__ == "__main__":
|
| 254 |
create_app().launch()
|