Cycle-Navigator / app (34).py
openfree's picture
Upload 2 files
0bec42c verified
raw
history blame
3.09 kB
import json, math, numpy as np, plotly.graph_objects as go
import gradio as gr, os, openai, pathlib
# ----- config ----------
CENTER = 2025
CYCLES = {
"K-Wave (50 yr)": 50,
"Business Cycle (Juglar 9 yr)": 9,
"Finance Cycle (80 yr)": 80,
"Hegemony Cycle (250 yr)": 250
}
COLOR = {50:"#ff3333",9:"#66ff66",80:"#ffcc00",250:"#66ccff"}
AMPL = {50:1.0,9:0.6,80:1.6,250:4.0}
# ----- load events -----
events_path = pathlib.Path(__file__).with_name("cycle_events.json")
with open(events_path, encoding="utf-8") as f:
EVENTS = {int(e['year']): e['events'] for e in json.load(f)}
def half_sine(xs, period, amp):
phase = np.mod(xs - CENTER, period)
y = amp * np.sin(np.pi * phase / period)
y[y < 0] = 0
return y
def build_fig(start, end):
xs = np.linspace(start, end, (end-start)*4)
fig = go.Figure()
# draw towers as lines
for period in sorted(set(CYCLES.values())):
y = half_sine(xs, period, AMPL[period])
fig.add_trace(go.Scatter(x=xs, y=y,
mode="lines",
line=dict(color=COLOR[period], width=1),
hoverinfo="skip",
showlegend=False))
# points with hover
for year, evs in EVENTS.items():
if start <= year <= end:
for ev in evs:
period = CYCLES[ev['cycle']+' Cycle'] if ev['cycle']!='K-Wave' else 50
# assume first event determines period amplitude
per = CYCLES.get(ev['cycle'] if ev['cycle']!='K-Wave' else "K-Wave (50 yr)",50)
y = half_sine(np.array([year]), per, AMPL[per])[0]
txt_en = "<br>".join(e['event_en'] for e in evs)
txt_ko = "<br>".join(e['event_ko'] for e in evs)
fig.add_trace(go.Scatter(
x=[year], y=[y],
mode="markers",
marker=dict(color="white", size=6),
customdata=[[txt_en, txt_ko]],
hovertemplate="Year %{x}<br>%{customdata[0]}<br>%{customdata[1]}<extra></extra>",
showlegend=False
))
fig.update_layout(
template="plotly_dark",
height=400,
margin=dict(t=30, l=40, r=40, b=40)
)
fig.update_xaxes(title="Year", range=[start, end])
fig.update_yaxes(title="Relative amplitude", showticklabels=False)
return fig
# ----- gradio -----
def make_app():
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown("## 🔭 **CycleNavigator (Interactive)**")
gr.Markdown("<sub>K-Wave 50y • Business 9y • Finance 80y • Hegemony 250y</sub>")
with gr.Row():
start = gr.Number(value=1775, label="Start Year")
end = gr.Number(value=2025, label="End Year")
plot = gr.Plot(value=build_fig(1775,2025))
def update(s,e):
return build_fig(int(s), int(e))
start.change(update, [start,end], plot)
end.change(update, [start,end], plot)
gr.File(value=events_path, label="Download cycle_events.json")
return demo
if __name__ == "__main__":
make_app().launch()