openfree commited on
Commit
9db4c9b
ยท
verified ยท
1 Parent(s): 0bec42c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +151 -160
app.py CHANGED
@@ -1,188 +1,179 @@
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):
 
 
 
 
 
 
 
 
 
 
 
26
  phase = np.mod(xs - CENTER, period)
27
  y = amp * np.sin(np.pi * phase / period)
28
  y[y < 0] = 0
29
  return y
30
 
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")
66
- ax.grid(axis="y", color="white", ls="--", lw=.3, alpha=.3)
67
-
68
- # alignment marker
69
- ax.axvline(CENTER, color="white", ls="--", lw=1, alpha=.6)
70
- arrow_y = AMPLITUDE_MAP[250] * 1.05
71
- ax.annotate("", xy=(CENTER - 125, arrow_y), xytext=(CENTER + 125, arrow_y),
72
- arrowprops=dict(arrowstyle="<|-|>", color="white", lw=1.2))
73
- ax.text(CENTER, arrow_y + .15, "250 yr", color="white",
74
- ha="center", va="bottom", fontsize=10, fontweight="bold")
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):
91
- msgs = [{"role": "system", "content": BASE_PROMPT}]
92
- if chart_summary != "No chart yet.":
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(
100
  model="gpt-3.5-turbo",
101
- messages=msgs,
102
  max_tokens=600,
103
  temperature=0.7
104
- ).choices[0].message.content.strip()
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")
137
-
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))
146
- return fig, summ
147
-
148
- start_year.change(refresh_chart, [start_year, end_year],
149
- [plot_out, chart_summary_state])
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"):
173
- chatbot = gr.Chatbot(label="Assistant")
174
- user_in = gr.Textbox(lines=3, placeholder="๋ฉ”์‹œ์ง€๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”โ€ฆ")
175
- send_btn = gr.Button("Send", variant="primary")
176
-
177
- def respond(chat_hist, user_msg, summary):
178
- ans = chat_with_gpt(chat_hist, user_msg, summary)
179
- chat_hist.append((user_msg, ans))
180
- return chat_hist, gr.Textbox(value="", interactive=True)
181
-
182
- send_btn.click(respond, [chatbot, user_in, chart_summary_state],
183
- [chatbot, user_in])
184
- user_in.submit(respond, [chatbot, user_in, chart_summary_state],
185
- [chatbot, user_in])
 
 
 
 
 
 
 
 
186
 
187
  if __name__ == "__main__":
188
- demo.launch()
 
1
+ # -------- app_final.py --------
2
+ import os, json, math, pathlib
3
+ import numpy as np
4
+ import plotly.graph_objects as go
5
+ import gradio as gr
6
  import openai
7
 
8
+ # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
9
  # 0. OpenAI API key
10
+ # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
11
  if "OPENAI_API_KEY" not in os.environ:
12
  os.environ["OPENAI_API_KEY"] = input("๐Ÿ”‘ Enter your OpenAI API key: ").strip()
13
  openai.api_key = os.environ["OPENAI_API_KEY"]
14
 
15
+ # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
16
+ # 1. Cycle config
17
+ # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
18
+ CENTER = 2025
19
  CYCLES = {
20
+ "K-Wave": 50, # Kondratiev long wave
21
+ "Business": 9, # Juglar investment cycle
22
+ "Finance": 80, # Long credit cycle
23
+ "Hegemony": 250 # Power-shift cycle
24
  }
25
+ ORDERED_PERIODS = sorted(CYCLES.values()) # [9, 50, 80, 250]
26
+ COLOR = {9:"#66ff66", 50:"#ff3333", 80:"#ffcc00", 250:"#66ccff"}
27
+ AMPL = {9:0.6, 50:1.0, 80:1.6, 250:4.0}
28
+ PERIOD_BY_CYCLE = {k:v for k,v in CYCLES.items()}
29
+
30
+ # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
31
+ # 2. Load events JSON
32
+ # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
33
+ EVENTS_PATH = pathlib.Path(__file__).with_name("cycle_events.json")
34
+ with open(EVENTS_PATH, encoding="utf-8") as f:
35
+ EVENTS = {int(item["year"]): item["events"] for item in json.load(f)}
36
+
37
+ # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
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
+ xs = np.linspace(start, end, max(1000, (end-start)*4))
48
+ fig = go.Figure()
49
+
50
+ # draw half-sine โ€œtowersโ€
51
+ for period in ORDERED_PERIODS:
52
+ ys = half_sine(xs, period, AMPL[period])
53
+ fig.add_trace(go.Scatter(
54
+ x=xs, y=ys, mode="lines",
55
+ line=dict(color=COLOR[period], width=1),
56
+ hoverinfo="skip", showlegend=False))
57
+
58
+ # annotate events with hover
59
+ for year, evs in EVENTS.items():
60
+ if start <= year <= end:
61
+ cycle_name = evs[0]["cycle"]
62
+ period = PERIOD_BY_CYCLE.get(cycle_name, 50)
63
+ y = float(half_sine(np.array([year]), period, AMPL[period]))
64
+ txt_en = "<br>".join(e["event_en"] for e in evs)
65
+ txt_ko = "<br>".join(e["event_ko"] for e in evs)
66
+ fig.add_trace(go.Scatter(
67
+ x=[year], y=[y], mode="markers",
68
+ marker=dict(color="white", size=6),
69
+ customdata=[[cycle_name, txt_en, txt_ko]],
70
+ hovertemplate=(
71
+ "Year %{x} โ€ข %{customdata[0]} cycle"
72
+ "<br>%{customdata[1]}"
73
+ "<br>%{customdata[2]}<extra></extra>"
74
+ ),
75
+ showlegend=False
76
+ ))
77
+
78
+ # styling
79
+ fig.update_layout(
80
+ template="plotly_dark",
81
+ height=450,
82
+ margin=dict(t=30, l=40, r=40, b=40),
83
+ hoverlabel=dict(bgcolor="#222", font_size=11)
84
+ )
85
+ fig.update_xaxes(title="Year", range=[start, end])
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
+ # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
92
+ # 4. GPT chat helper
93
+ # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
94
  BASE_PROMPT = (
95
  "You are a concise and accurate Korean assistant. "
96
+ "If a chart summary is provided, incorporate it in your answer."
97
  )
98
 
99
  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},{"role":"assistant","content":a}])
105
+ messages.append({"role":"user","content":user_msg})
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
+
114
+ # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
115
+ # 5. Gradio UI
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
+ "K-Wave 50y โ€ข Business 9y โ€ข Finance 80y โ€ข Hegemony 250y"
123
+ "</sub>"
124
+ )
125
+
126
+ chart_summary_state = gr.State(value="No chart yet.")
127
+
128
+ with gr.Tabs():
129
+ # โ”€โ”€ Chart Tab โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
130
+ with gr.TabItem("Timeline Chart"):
131
+ with gr.Row():
132
+ start_year = gr.Number(label="Start Year", value=1775, precision=0)
133
+ end_year = gr.Number(label="End Year", value=2025, precision=0)
134
+ zoom_in = gr.Button("๐Ÿ” Zoom In")
135
+ zoom_out = gr.Button("๐Ÿ”Ž Zoom Out")
136
+
137
+ fig0, summ0 = build_chart(1775, 2025)
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], [plot, chart_summary_state])
145
+ end_year.change(refresh, [start_year, end_year], [plot, chart_summary_state])
146
+
147
+ def zoom(s, e, factor):
148
+ mid = (s+e)/2
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 Tab โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
163
+ with gr.TabItem("GPT Chat"):
164
+ chatbot = gr.Chatbot(label="Assistant")
165
+ user_input = gr.Textbox(lines=3, placeholder="๋ฉ”์‹œ์ง€๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”โ€ฆ")
166
+ send_btn = gr.Button("Send")
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
+ send_btn.click(respond, [chatbot, user_input, chart_summary_state],
173
+ [chatbot, user_input])
174
+ user_input.submit(respond, [chatbot, user_input, chart_summary_state],
175
+ [chatbot, user_input])
176
+ return demo
177
 
178
  if __name__ == "__main__":
179
+ create_app().launch()