Spaces:
Running
Running
Delete app-backup.py
Browse files- app-backup.py +0 -188
app-backup.py
DELETED
@@ -1,188 +0,0 @@
|
|
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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|