openfree commited on
Commit
dceecec
ยท
verified ยท
1 Parent(s): f4534af

Update app-backup.py

Browse files
Files changed (1) hide show
  1. app-backup.py +54 -192
app-backup.py CHANGED
@@ -1,24 +1,25 @@
1
- # cycles_chat_app.py
2
  import os, math, numpy as np, matplotlib.pyplot as plt, gradio as gr
3
  import openai
4
 
5
  # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
6
- # 0. OpenAI key
7
  # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
8
  if "OPENAI_API_KEY" not in os.environ:
9
  os.environ["OPENAI_API_KEY"] = input("๐Ÿ”‘ Enter your OpenAI API key: ").strip()
10
  openai.api_key = os.environ["OPENAI_API_KEY"]
11
 
12
  # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
13
- # 1. Wave-style chart utilities
14
  # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
15
  CYCLES = {
16
- "Tech Cycle (50 yr)": 50,
17
- "Finance Cycle (80 yr)": 80,
18
- "Hegemony Cycle (250 yr)": 250,
 
19
  }
20
- COLOR_MAP = {50: "#ff3333", 80: "#ffcc00", 250: "#66ccff"}
21
- AMPLITUDE_MAP = {50: 1.0, 80: 1.6, 250: 4.0}
22
  CENTER = 2025 # alignment reference
23
 
24
  def _half_sine(xs, period, amp):
@@ -30,36 +31,35 @@ def _half_sine(xs, period, amp):
30
  def build_wave_chart_and_summary(start: int, end: int):
31
  xs = np.linspace(start, end, (end - start) * 4)
32
  fig, ax = plt.subplots(figsize=(14, 6))
33
- fig.subplots_adjust(top=0.9) # prevent title clipping
34
 
35
  summaries, align_years, all_year_labels = [], None, set()
36
 
37
- for period in sorted(CYCLES.values()):
38
  col, amp = COLOR_MAP[period], AMPLITUDE_MAP[period]
39
  for frac in np.linspace(amp / 30, amp, 30):
40
- ax.plot(xs, _half_sine(xs, period, frac), color=col, alpha=0.85, lw=0.6)
 
41
 
42
  years = [CENTER + n * period for n in range(
43
  math.ceil((start - CENTER) / period),
44
  math.floor((end - CENTER) / period) + 1)]
45
- summaries.append(f"{period}-yr cycle peaks: {years}")
 
46
  align_years = set(years) if align_years is None else align_years & set(years)
47
  all_year_labels.update(years)
48
 
49
- # baseline year labels (small font, duplicates removed)
50
  for y in sorted(all_year_labels):
51
  if start <= y <= end:
52
  ax.text(y, -0.1, str(y), ha="center", va="top",
53
  fontsize=6, color="white", rotation=90)
54
 
55
- ax.set_facecolor("black")
56
- fig.patch.set_facecolor("black")
57
  ax.set_xlim(start, end)
58
  ax.set_ylim(-0.2, max(AMPLITUDE_MAP.values()) + 0.2)
59
  ax.set_xlabel("Year", color="white")
60
  ax.set_ylabel("Relative Amplitude", color="white")
61
- ax.set_title("Technology ยท Finance ยท Hegemony Cycles", color="white",
62
- pad=35, fontsize=14, fontweight="bold")
63
  ax.tick_params(colors="white")
64
  for spine in ax.spines.values():
65
  spine.set_color("white")
@@ -75,16 +75,16 @@ def build_wave_chart_and_summary(start: int, end: int):
75
 
76
  summary = (f"Range {start}-{end}\n"
77
  + "\n".join(summaries)
78
- + f"\nAlignment year inside range: "
79
  + (", ".join(map(str, sorted(align_years))) if align_years else "None"))
80
  return fig, summary
81
 
82
  # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
83
- # 2. GPT chat
84
  # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
85
  BASE_PROMPT = (
86
- "๋‹น์‹ ์€ ๊ฐ„๊ฒฐํ•˜๊ณ  ์ •ํ™•ํ•œ ํ•œ๊ตญ์–ด ์–ด์‹œ์Šคํ„ดํŠธ์ž…๋‹ˆ๋‹ค. "
87
- "์‚ฌ์šฉ์ž์˜ ์งˆ๋ฌธ์— ๋‹ตํ•  ๋•Œ, ์ œ๊ณต๋œ [Chart summary] ๋‚ด์šฉ์„ ๋ฐ˜๋“œ์‹œ ๋ฐ˜์˜ํ•˜์„ธ์š”."
88
  )
89
 
90
  def chat_with_gpt(history, user_msg, chart_summary):
@@ -93,8 +93,7 @@ def chat_with_gpt(history, user_msg, chart_summary):
93
  msgs.append({"role": "system", "content": f"[Chart summary]\n{chart_summary}"})
94
 
95
  for u, a in history:
96
- msgs += [{"role": "user", "content": u},
97
- {"role": "assistant", "content": a}]
98
  msgs.append({"role": "user", "content": user_msg})
99
 
100
  reply = openai.chat.completions.create(
@@ -106,32 +105,32 @@ def chat_with_gpt(history, user_msg, chart_summary):
106
  return reply
107
 
108
  # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
109
- # 3. Gradio UI + CSS
110
  # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
111
  custom_css = """
112
  #wave_plot {width: 100% !important;}
113
  #wave_plot canvas {width: 100% !important; height: auto !important;}
114
  """
115
 
116
- with gr.Blocks(css=custom_css, theme="apriel") as demo:
117
- # โ”€โ”€ NEW: ์„œ๋น„์Šค ์ œ๋ชฉ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
118
- gr.Markdown("## ๐Ÿš€ **TriPulse Navigator**", elem_id="service_title")
119
 
120
- # โ”€โ”€ NEW: ๊ฐ„๋‹จ ์„ค๋ช… โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
121
  gr.Markdown(
122
- """
123
- **Tech Cycle (50 yr)** โ€“ Innovation booms and busts every half-century.
124
- **Finance Cycle (80 yr)** โ€“ Credit expansions and crises roughly once in a lifetime.
125
- **Hegemony Cycle (250 yr)** โ€“ Rise & decline of world-leading powers over two-and-a-half centuries.
126
- """,
127
- elem_id="cycle_descriptions",
128
- )
129
 
 
 
 
 
 
 
130
 
131
  chart_summary_state = gr.State(value="No chart yet.")
132
 
133
  with gr.Tabs():
134
- # โ–ธ Tab 1 โ€” Chart
135
  with gr.TabItem("Timeline Chart"):
136
  fig0, summ0 = build_wave_chart_and_summary(1500, 2500)
137
  plot_out = gr.Plot(value=fig0, elem_id="wave_plot")
@@ -139,6 +138,8 @@ with gr.Blocks(css=custom_css, theme="apriel") as demo:
139
  with gr.Row():
140
  start_year = gr.Number(label="Start Year", value=1500)
141
  end_year = gr.Number(label="End Year", value=2500)
 
 
142
 
143
  def refresh_chart(s, e):
144
  fig, summ = build_wave_chart_and_summary(int(s), int(e))
@@ -149,162 +150,23 @@ with gr.Blocks(css=custom_css, theme="apriel") as demo:
149
  end_year.change(refresh_chart, [start_year, end_year],
150
  [plot_out, chart_summary_state])
151
 
152
- # โ–ธ Tab 2 โ€” GPT Chat
153
- with gr.TabItem("GPT Chat"):
154
- chatbot = gr.Chatbot(label="Assistant")
155
- user_in = gr.Textbox(lines=3, placeholder="๋ฉ”์‹œ์ง€๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”โ€ฆ")
156
- send_btn = gr.Button("Send", variant="primary")
157
-
158
- def respond(chat_hist, user_msg, summary):
159
- ans = chat_with_gpt(chat_hist, user_msg, summary)
160
- chat_hist.append((user_msg, ans))
161
- return chat_hist, gr.Textbox(value="", interactive=True)
162
-
163
- send_btn.click(respond, [chatbot, user_in, chart_summary_state],
164
- [chatbot, user_in])
165
- user_in.submit(respond, [chatbot, user_in, chart_summary_state],
166
- [chatbot, user_in])
167
-
168
- if __name__ == "__main__":
169
- demo.launch()
170
- # cycles_chat_app.py
171
- import os, math, numpy as np, matplotlib.pyplot as plt, gradio as gr
172
- import openai
173
-
174
- # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
175
- # 0. OpenAI key
176
- # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€๏ฟฝ๏ฟฝโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
177
- if "OPENAI_API_KEY" not in os.environ:
178
- os.environ["OPENAI_API_KEY"] = input("๐Ÿ”‘ Enter your OpenAI API key: ").strip()
179
- openai.api_key = os.environ["OPENAI_API_KEY"]
180
-
181
- # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
182
- # 1. Wave-style chart utilities
183
- # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
184
- CYCLES = {
185
- "Tech Cycle (50 yr)": 50,
186
- "Finance Cycle (80 yr)": 80,
187
- "Hegemony Cycle (250 yr)": 250,
188
- }
189
- COLOR_MAP = {50: "#ff3333", 80: "#ffcc00", 250: "#66ccff"}
190
- AMPLITUDE_MAP = {50: 1.0, 80: 1.6, 250: 4.0}
191
- CENTER = 2025 # alignment reference
192
-
193
- def _half_sine(xs, period, amp):
194
- phase = np.mod(xs - CENTER, period)
195
- y = amp * np.sin(np.pi * phase / period)
196
- y[y < 0] = 0
197
- return y
198
-
199
- def build_wave_chart_and_summary(start: int, end: int):
200
- xs = np.linspace(start, end, (end - start) * 4)
201
- fig, ax = plt.subplots(figsize=(14, 6))
202
- fig.subplots_adjust(top=0.9) # prevent title clipping
203
-
204
- summaries, align_years, all_year_labels = [], None, set()
205
-
206
- for period in sorted(CYCLES.values()):
207
- col, amp = COLOR_MAP[period], AMPLITUDE_MAP[period]
208
- for frac in np.linspace(amp / 30, amp, 30):
209
- ax.plot(xs, _half_sine(xs, period, frac), color=col, alpha=0.85, lw=0.6)
210
-
211
- years = [CENTER + n * period for n in range(
212
- math.ceil((start - CENTER) / period),
213
- math.floor((end - CENTER) / period) + 1)]
214
- summaries.append(f"{period}-yr cycle peaks: {years}")
215
- align_years = set(years) if align_years is None else align_years & set(years)
216
- all_year_labels.update(years)
217
-
218
- # baseline year labels (small font, duplicates removed)
219
- for y in sorted(all_year_labels):
220
- if start <= y <= end:
221
- ax.text(y, -0.1, str(y), ha="center", va="top",
222
- fontsize=6, color="white", rotation=90)
223
-
224
- ax.set_facecolor("black")
225
- fig.patch.set_facecolor("black")
226
- ax.set_xlim(start, end)
227
- ax.set_ylim(-0.2, max(AMPLITUDE_MAP.values()) + 0.2)
228
- ax.set_xlabel("Year", color="white")
229
- ax.set_ylabel("Relative Amplitude", color="white")
230
- ax.set_title("Technology ยท Finance ยท Hegemony Cycles", color="white",
231
- pad=35, fontsize=14, fontweight="bold")
232
- ax.tick_params(colors="white")
233
- for spine in ax.spines.values():
234
- spine.set_color("white")
235
- ax.grid(axis="y", color="white", ls="--", lw=.3, alpha=.3)
236
-
237
- # alignment marker
238
- ax.axvline(CENTER, color="white", ls="--", lw=1, alpha=.6)
239
- arrow_y = AMPLITUDE_MAP[250] * 1.05
240
- ax.annotate("", xy=(CENTER - 125, arrow_y), xytext=(CENTER + 125, arrow_y),
241
- arrowprops=dict(arrowstyle="<|-|>", color="white", lw=1.2))
242
- ax.text(CENTER, arrow_y + .15, "250 yr", color="white",
243
- ha="center", va="bottom", fontsize=10, fontweight="bold")
244
-
245
- summary = (f"Range {start}-{end}\n"
246
- + "\n".join(summaries)
247
- + f"\nAlignment year inside range: "
248
- + (", ".join(map(str, sorted(align_years))) if align_years else "None"))
249
- return fig, summary
250
-
251
- # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
252
- # 2. GPT chat
253
- # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
254
- BASE_PROMPT = (
255
- "๋‹น์‹ ์€ ๊ฐ„๊ฒฐํ•˜๊ณ  ์ •ํ™•ํ•œ ํ•œ๊ตญ์–ด ์–ด์‹œ์Šคํ„ดํŠธ์ž…๋‹ˆ๋‹ค. "
256
- "์‚ฌ์šฉ์ž์˜ ์งˆ๋ฌธ์— ๋‹ตํ•  ๋•Œ, ์ œ๊ณต๋œ [Chart summary] ๋‚ด์šฉ์„ ๋ฐ˜๋“œ์‹œ ๋ฐ˜์˜ํ•˜์„ธ์š”."
257
- )
258
-
259
- def chat_with_gpt(history, user_msg, chart_summary):
260
- msgs = [{"role": "system", "content": BASE_PROMPT}]
261
- if chart_summary != "No chart yet.":
262
- msgs.append({"role": "system", "content": f"[Chart summary]\n{chart_summary}"})
263
-
264
- for u, a in history:
265
- msgs += [{"role": "user", "content": u},
266
- {"role": "assistant", "content": a}]
267
- msgs.append({"role": "user", "content": user_msg})
268
-
269
- reply = openai.chat.completions.create(
270
- model="gpt-3.5-turbo",
271
- messages=msgs,
272
- max_tokens=600,
273
- temperature=0.7
274
- ).choices[0].message.content.strip()
275
- return reply
276
-
277
- # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
278
- # 3. Gradio UI + CSS
279
- # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
280
- custom_css = """
281
- #wave_plot {width: 100% !important;}
282
- #wave_plot canvas {width: 100% !important; height: auto !important;}
283
- """
284
-
285
- with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
286
- gr.Markdown("## ๐Ÿ“Š Wave-style Cycle Timeline & ๐Ÿ’ฌ GPT Chat")
287
-
288
- chart_summary_state = gr.State(value="No chart yet.")
289
-
290
- with gr.Tabs():
291
- # โ–ธ Tab 1 โ€” Chart
292
- with gr.TabItem("Timeline Chart"):
293
- fig0, summ0 = build_wave_chart_and_summary(1500, 2500)
294
- plot_out = gr.Plot(value=fig0, elem_id="wave_plot")
295
-
296
- with gr.Row():
297
- start_year = gr.Number(label="Start Year", value=1500)
298
- end_year = gr.Number(label="End Year", value=2500)
299
-
300
- def refresh_chart(s, e):
301
- fig, summ = build_wave_chart_and_summary(int(s), int(e))
302
- return fig, summ
303
-
304
- start_year.change(refresh_chart, [start_year, end_year],
305
- [plot_out, chart_summary_state])
306
- end_year.change(refresh_chart, [start_year, end_year],
307
- [plot_out, chart_summary_state])
308
 
309
  # โ–ธ Tab 2 โ€” GPT Chat
310
  with gr.TabItem("GPT Chat"):
 
1
+ # cycles_chat_app.py โ€” CycleNavigator (4-Cycle ๋ฒ„์ „)
2
  import os, math, numpy as np, matplotlib.pyplot as plt, gradio as gr
3
  import openai
4
 
5
  # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
6
+ # 0. OpenAI API key
7
  # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
8
  if "OPENAI_API_KEY" not in os.environ:
9
  os.environ["OPENAI_API_KEY"] = input("๐Ÿ”‘ Enter your OpenAI API key: ").strip()
10
  openai.api_key = os.environ["OPENAI_API_KEY"]
11
 
12
  # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
13
+ # 1. Wave-style chart utilities
14
  # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
15
  CYCLES = {
16
+ "K-Wave (50 yr)": 50, # ์žฅ๊ธฐ ์ฝ˜๋“œ๋ผํ‹ฐ์—ํ”„ ํŒŒ๋™
17
+ "Business Cycle (Juglar 9 yr)": 9, # ์„ค๋น„ํˆฌ์žยท์‹ ์šฉ ๊ฒฝ๊ธฐ์ˆœํ™˜
18
+ "Finance Cycle (80 yr)": 80, # ์‹ ์šฉยท๊ธˆ์œต ์žฅ์ฃผ๊ธฐ
19
+ "Hegemony Cycle (250 yr)": 250, # ํŒจ๊ถŒ ํฅ๋ง
20
  }
21
+ COLOR_MAP = {50: "#ff3333", 9: "#66ff66", 80: "#ffcc00", 250: "#66ccff"}
22
+ AMPLITUDE_MAP = {50: 1.0, 9: 0.6, 80: 1.6, 250: 4.0}
23
  CENTER = 2025 # alignment reference
24
 
25
  def _half_sine(xs, period, amp):
 
31
  def build_wave_chart_and_summary(start: int, end: int):
32
  xs = np.linspace(start, end, (end - start) * 4)
33
  fig, ax = plt.subplots(figsize=(14, 6))
34
+ fig.subplots_adjust(top=0.9)
35
 
36
  summaries, align_years, all_year_labels = [], None, set()
37
 
38
+ for period in sorted(set(CYCLES.values())): # iterate periods in ascending order
39
  col, amp = COLOR_MAP[period], AMPLITUDE_MAP[period]
40
  for frac in np.linspace(amp / 30, amp, 30):
41
+ ax.plot(xs, _half_sine(xs, period, frac),
42
+ color=col, alpha=0.85, lw=0.6)
43
 
44
  years = [CENTER + n * period for n in range(
45
  math.ceil((start - CENTER) / period),
46
  math.floor((end - CENTER) / period) + 1)]
47
+ name = [k for k, v in CYCLES.items() if v == period][0]
48
+ summaries.append(f"{name} peaks: {years}")
49
  align_years = set(years) if align_years is None else align_years & set(years)
50
  all_year_labels.update(years)
51
 
52
+ # small baseline labels
53
  for y in sorted(all_year_labels):
54
  if start <= y <= end:
55
  ax.text(y, -0.1, str(y), ha="center", va="top",
56
  fontsize=6, color="white", rotation=90)
57
 
58
+ ax.set_facecolor("black"); fig.patch.set_facecolor("black")
 
59
  ax.set_xlim(start, end)
60
  ax.set_ylim(-0.2, max(AMPLITUDE_MAP.values()) + 0.2)
61
  ax.set_xlabel("Year", color="white")
62
  ax.set_ylabel("Relative Amplitude", color="white")
 
 
63
  ax.tick_params(colors="white")
64
  for spine in ax.spines.values():
65
  spine.set_color("white")
 
75
 
76
  summary = (f"Range {start}-{end}\n"
77
  + "\n".join(summaries)
78
+ + "\nAlignment year inside range: "
79
  + (", ".join(map(str, sorted(align_years))) if align_years else "None"))
80
  return fig, summary
81
 
82
  # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
83
+ # 2. GPT chat helper
84
  # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
85
  BASE_PROMPT = (
86
+ "You are a concise and accurate Korean assistant. "
87
+ "Always incorporate the provided [Chart summary] when replying."
88
  )
89
 
90
  def chat_with_gpt(history, user_msg, chart_summary):
 
93
  msgs.append({"role": "system", "content": f"[Chart summary]\n{chart_summary}"})
94
 
95
  for u, a in history:
96
+ msgs += [{"role": "user", "content": u}, {"role": "assistant", "content": a}]
 
97
  msgs.append({"role": "user", "content": user_msg})
98
 
99
  reply = openai.chat.completions.create(
 
105
  return reply
106
 
107
  # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
108
+ # 3. Gradio UI
109
  # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
110
  custom_css = """
111
  #wave_plot {width: 100% !important;}
112
  #wave_plot canvas {width: 100% !important; height: auto !important;}
113
  """
114
 
115
+ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
116
+ # โ”€โ”€ ์„œ๋น„์Šค ์ œ๋ชฉ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
117
+ gr.Markdown("## ๐Ÿ”ญ **CycleNavigator**")
118
 
119
+ # โ”€โ”€ ๋„ค ์‚ฌ์ดํด ๊ฐ„๋‹จ ์„ค๋ช… (์ž‘์€ ๊ธ€์”จ) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
120
  gr.Markdown(
121
+ "<sub>"
 
 
 
 
 
 
122
 
123
+ "**Business (Juglar 9 yr)** โ€“ Credit-driven capital investment booms & busts.โ€ƒ"
124
+ "**K-Wave (50 yr)** โ€“ Long-wave industrial & technological shifts.โ€ƒ"
125
+ "**Finance (80 yr)** โ€“ Extended credit cycles culminating in crises.โ€ƒ"
126
+ "**Hegemony (250 yr)** โ€“ Rise & decline of world powers."
127
+ "</sub>"
128
+ )
129
 
130
  chart_summary_state = gr.State(value="No chart yet.")
131
 
132
  with gr.Tabs():
133
+ # โ–ธ Tab 1 โ€” Timeline Chart
134
  with gr.TabItem("Timeline Chart"):
135
  fig0, summ0 = build_wave_chart_and_summary(1500, 2500)
136
  plot_out = gr.Plot(value=fig0, elem_id="wave_plot")
 
138
  with gr.Row():
139
  start_year = gr.Number(label="Start Year", value=1500)
140
  end_year = gr.Number(label="End Year", value=2500)
141
+ zoom_in_btn = gr.Button("๐Ÿ” Zoom In")
142
+ zoom_out_btn = gr.Button("๐Ÿ”Ž Zoom Out")
143
 
144
  def refresh_chart(s, e):
145
  fig, summ = build_wave_chart_and_summary(int(s), int(e))
 
150
  end_year.change(refresh_chart, [start_year, end_year],
151
  [plot_out, chart_summary_state])
152
 
153
+ def zoom(s, e, factor):
154
+ mid = (s + e) / 2
155
+ span = (e - s) * factor / 2
156
+ new_s, new_e = int(mid - span), int(mid + span)
157
+ fig, summ = build_wave_chart_and_summary(new_s, new_e)
158
+ return new_s, new_e, fig, summ
159
+
160
+ zoom_in_btn.click(
161
+ lambda s, e: zoom(s, e, 0.5),
162
+ inputs=[start_year, end_year],
163
+ outputs=[start_year, end_year, plot_out, chart_summary_state],
164
+ )
165
+ zoom_out_btn.click(
166
+ lambda s, e: zoom(s, e, 2.0),
167
+ inputs=[start_year, end_year],
168
+ outputs=[start_year, end_year, plot_out, chart_summary_state],
169
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
170
 
171
  # โ–ธ Tab 2 โ€” GPT Chat
172
  with gr.TabItem("GPT Chat"):