Spaces:
Running
Running
Delete app (34).py
Browse files- app (34).py +0 -83
app (34).py
DELETED
|
@@ -1,83 +0,0 @@
|
|
| 1 |
-
|
| 2 |
-
import json, math, numpy as np, plotly.graph_objects as go
|
| 3 |
-
import gradio as gr, os, openai, pathlib
|
| 4 |
-
|
| 5 |
-
# ----- config ----------
|
| 6 |
-
CENTER = 2025
|
| 7 |
-
CYCLES = {
|
| 8 |
-
"K-Wave (50 yr)": 50,
|
| 9 |
-
"Business Cycle (Juglar 9 yr)": 9,
|
| 10 |
-
"Finance Cycle (80 yr)": 80,
|
| 11 |
-
"Hegemony Cycle (250 yr)": 250
|
| 12 |
-
}
|
| 13 |
-
COLOR = {50:"#ff3333",9:"#66ff66",80:"#ffcc00",250:"#66ccff"}
|
| 14 |
-
AMPL = {50:1.0,9:0.6,80:1.6,250:4.0}
|
| 15 |
-
|
| 16 |
-
# ----- load events -----
|
| 17 |
-
events_path = pathlib.Path(__file__).with_name("cycle_events.json")
|
| 18 |
-
with open(events_path, encoding="utf-8") as f:
|
| 19 |
-
EVENTS = {int(e['year']): e['events'] for e in json.load(f)}
|
| 20 |
-
|
| 21 |
-
def half_sine(xs, period, amp):
|
| 22 |
-
phase = np.mod(xs - CENTER, period)
|
| 23 |
-
y = amp * np.sin(np.pi * phase / period)
|
| 24 |
-
y[y < 0] = 0
|
| 25 |
-
return y
|
| 26 |
-
|
| 27 |
-
def build_fig(start, end):
|
| 28 |
-
xs = np.linspace(start, end, (end-start)*4)
|
| 29 |
-
fig = go.Figure()
|
| 30 |
-
# draw towers as lines
|
| 31 |
-
for period in sorted(set(CYCLES.values())):
|
| 32 |
-
y = half_sine(xs, period, AMPL[period])
|
| 33 |
-
fig.add_trace(go.Scatter(x=xs, y=y,
|
| 34 |
-
mode="lines",
|
| 35 |
-
line=dict(color=COLOR[period], width=1),
|
| 36 |
-
hoverinfo="skip",
|
| 37 |
-
showlegend=False))
|
| 38 |
-
# points with hover
|
| 39 |
-
for year, evs in EVENTS.items():
|
| 40 |
-
if start <= year <= end:
|
| 41 |
-
for ev in evs:
|
| 42 |
-
period = CYCLES[ev['cycle']+' Cycle'] if ev['cycle']!='K-Wave' else 50
|
| 43 |
-
# assume first event determines period amplitude
|
| 44 |
-
per = CYCLES.get(ev['cycle'] if ev['cycle']!='K-Wave' else "K-Wave (50 yr)",50)
|
| 45 |
-
y = half_sine(np.array([year]), per, AMPL[per])[0]
|
| 46 |
-
txt_en = "<br>".join(e['event_en'] for e in evs)
|
| 47 |
-
txt_ko = "<br>".join(e['event_ko'] for e in evs)
|
| 48 |
-
fig.add_trace(go.Scatter(
|
| 49 |
-
x=[year], y=[y],
|
| 50 |
-
mode="markers",
|
| 51 |
-
marker=dict(color="white", size=6),
|
| 52 |
-
customdata=[[txt_en, txt_ko]],
|
| 53 |
-
hovertemplate="Year %{x}<br>%{customdata[0]}<br>%{customdata[1]}<extra></extra>",
|
| 54 |
-
showlegend=False
|
| 55 |
-
))
|
| 56 |
-
|
| 57 |
-
fig.update_layout(
|
| 58 |
-
template="plotly_dark",
|
| 59 |
-
height=400,
|
| 60 |
-
margin=dict(t=30, l=40, r=40, b=40)
|
| 61 |
-
)
|
| 62 |
-
fig.update_xaxes(title="Year", range=[start, end])
|
| 63 |
-
fig.update_yaxes(title="Relative amplitude", showticklabels=False)
|
| 64 |
-
return fig
|
| 65 |
-
|
| 66 |
-
# ----- gradio -----
|
| 67 |
-
def make_app():
|
| 68 |
-
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 69 |
-
gr.Markdown("## 🔭 **CycleNavigator (Interactive)**")
|
| 70 |
-
gr.Markdown("<sub>K-Wave 50y • Business 9y • Finance 80y • Hegemony 250y</sub>")
|
| 71 |
-
with gr.Row():
|
| 72 |
-
start = gr.Number(value=1775, label="Start Year")
|
| 73 |
-
end = gr.Number(value=2025, label="End Year")
|
| 74 |
-
plot = gr.Plot(value=build_fig(1775,2025))
|
| 75 |
-
def update(s,e):
|
| 76 |
-
return build_fig(int(s), int(e))
|
| 77 |
-
start.change(update, [start,end], plot)
|
| 78 |
-
end.change(update, [start,end], plot)
|
| 79 |
-
gr.File(value=events_path, label="Download cycle_events.json")
|
| 80 |
-
return demo
|
| 81 |
-
|
| 82 |
-
if __name__ == "__main__":
|
| 83 |
-
make_app().launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|