Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,188 +1,179 @@
|
|
1 |
-
#
|
2 |
-
import os,
|
|
|
|
|
|
|
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.
|
14 |
-
#
|
|
|
15 |
CYCLES = {
|
16 |
-
"K-Wave
|
17 |
-
"Business
|
18 |
-
"Finance
|
19 |
-
"Hegemony
|
20 |
}
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
32 |
-
xs = np.linspace(start, end, (end
|
33 |
-
fig
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
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 |
-
#
|
84 |
-
#
|
85 |
BASE_PROMPT = (
|
86 |
"You are a concise and accurate Korean assistant. "
|
87 |
-
"
|
88 |
)
|
89 |
|
90 |
def chat_with_gpt(history, user_msg, chart_summary):
|
91 |
-
|
92 |
-
if chart_summary
|
93 |
-
|
94 |
-
|
95 |
for u, a in history:
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
reply = openai.chat.completions.create(
|
100 |
model="gpt-3.5-turbo",
|
101 |
-
messages=
|
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 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
186 |
|
187 |
if __name__ == "__main__":
|
188 |
-
|
|
|
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()
|