openfree commited on
Commit
7040a7c
ยท
verified ยท
1 Parent(s): 32ad39d

Delete app-backup.py

Browse files
Files changed (1) hide show
  1. 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()