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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
import xmltodict
|
4 |
+
import pandas as pd
|
5 |
+
from datetime import datetime, timedelta
|
6 |
+
import streamlit.components.v1 as components
|
7 |
+
import plotly.express as px
|
8 |
+
import time
|
9 |
+
import plotly.io as pio
|
10 |
+
from openai import OpenAI
|
11 |
+
from elevenlabs import ElevenLabs
|
12 |
+
|
13 |
+
# plotly์ JSON ์ง๋ ฌํ ์์ง์ ๊ธฐ๋ณธ json์ผ๋ก ์ค์
|
14 |
+
pio.json.config.default_engine = 'json'
|
15 |
+
|
16 |
+
# ํ์ด์ง ์ค์
|
17 |
+
st.set_page_config(
|
18 |
+
page_title="์ฐ๋ฆฌ์ง ๋ ์จ ์ ๋ณด",
|
19 |
+
page_icon="๐ค๏ธ",
|
20 |
+
layout="wide",
|
21 |
+
menu_items={
|
22 |
+
'Get Help': None,
|
23 |
+
'Report a bug': None,
|
24 |
+
'About': None
|
25 |
+
}
|
26 |
+
)
|
27 |
+
|
28 |
+
# CSS ์คํ์ผ
|
29 |
+
st.markdown("""
|
30 |
+
<style>
|
31 |
+
section[data-testid="stSidebar"] {
|
32 |
+
display: none;
|
33 |
+
}
|
34 |
+
#MainMenu {
|
35 |
+
display: none;
|
36 |
+
}
|
37 |
+
header {
|
38 |
+
display: none;
|
39 |
+
}
|
40 |
+
.block-container {
|
41 |
+
padding: 0 !important;
|
42 |
+
max-width: 100% !important;
|
43 |
+
}
|
44 |
+
.element-container {
|
45 |
+
margin: 0 !important;
|
46 |
+
}
|
47 |
+
.stApp > header {
|
48 |
+
display: none;
|
49 |
+
}
|
50 |
+
#other-info {
|
51 |
+
display: none;
|
52 |
+
}
|
53 |
+
.stPlotlyChart {
|
54 |
+
width: 100%;
|
55 |
+
margin: 0 !important;
|
56 |
+
padding: 0 !important;
|
57 |
+
}
|
58 |
+
[data-testid="stMetricValue"] {
|
59 |
+
font-size: 3rem;
|
60 |
+
}
|
61 |
+
.time-container {
|
62 |
+
width: 100%;
|
63 |
+
text-align: center;
|
64 |
+
margin: 0 auto;
|
65 |
+
padding: 15px 0;
|
66 |
+
}
|
67 |
+
.date-text {
|
68 |
+
font-size: 6em !important;
|
69 |
+
font-weight: bold !important;
|
70 |
+
color: rgb(0, 0, 0) !important;
|
71 |
+
font-family: Arial, sans-serif !important;
|
72 |
+
text-shadow: none !important;
|
73 |
+
background: transparent !important;
|
74 |
+
display: block !important;
|
75 |
+
line-height: 1.2 !important;
|
76 |
+
margin-bottom: 0.5px !important;
|
77 |
+
}
|
78 |
+
h1, h2, h3, h4, h5, h6, p, .stMetric > div > div {
|
79 |
+
color: black !important;
|
80 |
+
}
|
81 |
+
.plotly-graph-div {
|
82 |
+
overflow-x: scroll !important;
|
83 |
+
min-width: 100% !important;
|
84 |
+
}
|
85 |
+
div[data-testid="stVerticalBlock"] > div {
|
86 |
+
padding: 0 !important;
|
87 |
+
}
|
88 |
+
.main {
|
89 |
+
padding: 0 !important;
|
90 |
+
}
|
91 |
+
.stApp {
|
92 |
+
margin: 0 !important;
|
93 |
+
}
|
94 |
+
[data-testid="stHeader"] {
|
95 |
+
display: none;
|
96 |
+
}
|
97 |
+
.section-container {
|
98 |
+
position: absolute;
|
99 |
+
top: 0;
|
100 |
+
left: 0;
|
101 |
+
width: 100%;
|
102 |
+
height: 100vh;
|
103 |
+
padding: 1rem;
|
104 |
+
box-sizing: border-box;
|
105 |
+
background-color: inherit;
|
106 |
+
}
|
107 |
+
.graph-container {
|
108 |
+
width: 100%;
|
109 |
+
height: calc(100vh - 100px);
|
110 |
+
display: flex;
|
111 |
+
flex-direction: column;
|
112 |
+
align-items: center;
|
113 |
+
}
|
114 |
+
iframe {
|
115 |
+
margin: 0 !important;
|
116 |
+
padding: 0 !important;
|
117 |
+
}
|
118 |
+
[data-testid="column"] {
|
119 |
+
padding: 0 !important;
|
120 |
+
}
|
121 |
+
[data-testid="stVerticalBlock"] {
|
122 |
+
padding: 0 !important;
|
123 |
+
gap: 0 !important;
|
124 |
+
}
|
125 |
+
.dust-status {
|
126 |
+
font-size: 2em;
|
127 |
+
font-weight: bold;
|
128 |
+
color: black;
|
129 |
+
padding: 0.3rem 1rem;
|
130 |
+
border-radius: 1rem;
|
131 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
132 |
+
display: inline-block;
|
133 |
+
}
|
134 |
+
@keyframes scroll-text {
|
135 |
+
from {
|
136 |
+
transform: translateX(100%);
|
137 |
+
}
|
138 |
+
to {
|
139 |
+
transform: translateX(-100%);
|
140 |
+
}
|
141 |
+
}
|
142 |
+
.scroll-container {
|
143 |
+
position: fixed;
|
144 |
+
bottom: 20px;
|
145 |
+
left: 0;
|
146 |
+
width: 100%;
|
147 |
+
overflow: hidden;
|
148 |
+
background-color: rgba(255, 255, 255, 0.9);
|
149 |
+
padding: 10px 0;
|
150 |
+
z-index: 1000;
|
151 |
+
}
|
152 |
+
.scroll-text {
|
153 |
+
display: inline-block;
|
154 |
+
white-space: nowrap;
|
155 |
+
animation: scrolling 60s linear infinite;
|
156 |
+
font-size: 1.5em;
|
157 |
+
font-weight: bold;
|
158 |
+
color: #333;
|
159 |
+
}
|
160 |
+
@keyframes scrolling {
|
161 |
+
0% {transform: translateX(100%);}
|
162 |
+
100% {transform: translateX(-100%);}
|
163 |
+
}
|
164 |
+
</style>
|
165 |
+
""", unsafe_allow_html=True)
|
166 |
+
|
167 |
+
|
168 |
+
def get_korean_weekday(date):
|
169 |
+
weekday = date.strftime('%a')
|
170 |
+
weekday_dict = {
|
171 |
+
'Mon': '์',
|
172 |
+
'Tue': 'ํ',
|
173 |
+
'Wed': '์',
|
174 |
+
'Thu': '๋ชฉ',
|
175 |
+
'Fri': '๊ธ',
|
176 |
+
'Sat': 'ํ ',
|
177 |
+
'Sun': '์ผ'
|
178 |
+
}
|
179 |
+
return weekday_dict[weekday]
|
180 |
+
|
181 |
+
@st.cache_data(ttl=300) # 5๋ถ๋ง๋ค ์บ์ ๊ฐฑ์
|
182 |
+
def get_weather_data():
|
183 |
+
url = "http://openapi.seoul.go.kr:8088/77544e69764a414d363647424a655a/xml/citydata/1/5/์ ๋ฆผ์ญ"
|
184 |
+
try:
|
185 |
+
response = requests.get(url)
|
186 |
+
response.raise_for_status() # HTTPError์ ๋ํด ์์ธ ๋ฐ์
|
187 |
+
if not response.text.strip(): # ๋น ์๋ต ์ฒ๋ฆฌ
|
188 |
+
raise ValueError("์๋ต์ด ๋น์ด ์์ต๋๋ค.")
|
189 |
+
data = xmltodict.parse(response.text)
|
190 |
+
return data['SeoulRtd.citydata']['CITYDATA']['WEATHER_STTS']['WEATHER_STTS']
|
191 |
+
except requests.exceptions.RequestException as e:
|
192 |
+
st.error(f"API ํธ์ถ ์ค ์ค๋ฅ ๋ฐ์: {e}")
|
193 |
+
except Exception as e:
|
194 |
+
st.error(f"๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํ๋ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {e}")
|
195 |
+
return None
|
196 |
+
|
197 |
+
def get_background_color(pm10_value):
|
198 |
+
try:
|
199 |
+
pm10 = float(pm10_value)
|
200 |
+
if pm10 <= 30:
|
201 |
+
return "#87CEEB" # ํ๋ (์ข์)
|
202 |
+
elif pm10 <= 80:
|
203 |
+
return "#90EE90" # ์ด๋ก (๋ณดํต)
|
204 |
+
elif pm10 <= 150:
|
205 |
+
return "#FFD700" # ๋
ธ๋ (๋์จ)
|
206 |
+
else:
|
207 |
+
return "#FF6B6B" # ๋นจ๊ฐ (๋งค์ฐ ๋์จ)
|
208 |
+
except:
|
209 |
+
return "#FFFFFF" # ๊ธฐ๋ณธ ํฐ์
|
210 |
+
|
211 |
+
|
212 |
+
def get_current_sky_status(data):
|
213 |
+
current_time = datetime.utcnow() + timedelta(hours=9)
|
214 |
+
current_hour = current_time.hour
|
215 |
+
|
216 |
+
forecast_data = data['FCST24HOURS']['FCST24HOURS']
|
217 |
+
if not isinstance(forecast_data, list):
|
218 |
+
forecast_data = [forecast_data]
|
219 |
+
|
220 |
+
closest_forecast = None
|
221 |
+
min_time_diff = float('inf')
|
222 |
+
|
223 |
+
for forecast in forecast_data:
|
224 |
+
forecast_hour = int(forecast['FCST_DT'][8:10])
|
225 |
+
time_diff = abs(forecast_hour - current_hour)
|
226 |
+
if time_diff < min_time_diff:
|
227 |
+
min_time_diff = time_diff
|
228 |
+
closest_forecast = forecast
|
229 |
+
|
230 |
+
return closest_forecast['SKY_STTS'] if closest_forecast else "์ ๋ณด์์"
|
231 |
+
|
232 |
+
def format_news_message(news_list):
|
233 |
+
if not isinstance(news_list, list):
|
234 |
+
news_list = [news_list]
|
235 |
+
|
236 |
+
current_warnings = []
|
237 |
+
for news in news_list:
|
238 |
+
if not isinstance(news, dict):
|
239 |
+
continue
|
240 |
+
|
241 |
+
warn_val = news.get('WARN_VAL', '')
|
242 |
+
warn_stress = news.get('WARN_STRESS', '')
|
243 |
+
command = news.get('COMMAND', '')
|
244 |
+
warn_msg = news.get('WARN_MSG', '')
|
245 |
+
announce_time = news.get('ANNOUNCE_TIME', '')
|
246 |
+
|
247 |
+
if announce_time and len(announce_time) == 12:
|
248 |
+
year = announce_time[0:4]
|
249 |
+
month = announce_time[4:6]
|
250 |
+
day = announce_time[6:8]
|
251 |
+
hour = announce_time[8:10]
|
252 |
+
minute = announce_time[10:12]
|
253 |
+
formatted_time = f"({year}๋
{month}์{day}์ผ{hour}์{minute}๋ถ)"
|
254 |
+
else:
|
255 |
+
formatted_time = ""
|
256 |
+
|
257 |
+
if command == 'ํด์ ':
|
258 |
+
warning_text = f"โ
{warn_val}{warn_stress} ํด์ {formatted_time} {warn_msg}"
|
259 |
+
else:
|
260 |
+
warning_text = f"โ ๏ธ {warn_val}{warn_stress} ๋ฐ๋ น {formatted_time} {warn_msg}"
|
261 |
+
current_warnings.append(warning_text)
|
262 |
+
|
263 |
+
return ' | '.join(current_warnings)
|
264 |
+
|
265 |
+
def show_weather_info(data):
|
266 |
+
st.markdown('<div class="section-container">', unsafe_allow_html=True)
|
267 |
+
|
268 |
+
# Add update time display using the last API call timestamp (already in KST)
|
269 |
+
refresh_time = datetime.fromtimestamp(st.session_state.last_api_call) if st.session_state.last_api_call else (datetime.utcnow() + timedelta(hours=9))
|
270 |
+
st.markdown(f'''
|
271 |
+
<div style="text-align: center; font-size: 0.8em; color: gray;">
|
272 |
+
Data refreshed at: {refresh_time.strftime('%Y-%m-%d %H:%M:%S')}
|
273 |
+
</div>
|
274 |
+
''', unsafe_allow_html=True)
|
275 |
+
|
276 |
+
# Add this code to define formatted_date
|
277 |
+
current_time = datetime.utcnow() + timedelta(hours=9)
|
278 |
+
weekday = get_korean_weekday(current_time)
|
279 |
+
formatted_date = f"{current_time.strftime('%Y-%m-%d')}({weekday})"
|
280 |
+
|
281 |
+
pm10 = float(data['PM10'])
|
282 |
+
if pm10 <= 30:
|
283 |
+
dust_status = "์ข์"
|
284 |
+
dust_color = "#87CEEB" # Blue
|
285 |
+
elif pm10 <= 80:
|
286 |
+
dust_status = "๋ณดํต"
|
287 |
+
dust_color = "#90EE90" # Green
|
288 |
+
elif pm10 <= 150:
|
289 |
+
dust_status = "๋์จ"
|
290 |
+
dust_color = "#FFD700" # Yellow
|
291 |
+
else:
|
292 |
+
dust_status = "๋งค์ฐ๋์จ"
|
293 |
+
dust_color = "#FF6B6B" # Red
|
294 |
+
|
295 |
+
temp = data.get('TEMP', "์ ๋ณด์์")
|
296 |
+
precip_type = data.get('PRECPT_TYPE', "์ ๋ณด์์")
|
297 |
+
|
298 |
+
try:
|
299 |
+
temp = f"{float(temp):.1f}ยฐC"
|
300 |
+
except:
|
301 |
+
temp = "์ ๋ณด์์"
|
302 |
+
|
303 |
+
# ๋ด์ผ ์์นจ 6์ ์๋ณด ์ฐพ๊ธฐ
|
304 |
+
tomorrow_morning_weather = "์์"
|
305 |
+
forecast_data = data['FCST24HOURS']['FCST24HOURS']
|
306 |
+
if not isinstance(forecast_data, list):
|
307 |
+
forecast_data = [forecast_data]
|
308 |
+
|
309 |
+
# ํ์ฌ ์๊ฐ ๊ธฐ์ค์ผ๋ก ๋ด์ผ ๋ ์ง ๊ณ์ฐ
|
310 |
+
current_time = datetime.utcnow() + timedelta(hours=9)
|
311 |
+
tomorrow = current_time + timedelta(days=1)
|
312 |
+
tomorrow_date = tomorrow.strftime('%Y%m%d')
|
313 |
+
|
314 |
+
for forecast in forecast_data:
|
315 |
+
# ๋ ์ง์ ์๊ฐ์ด ๋ชจ๋ ์ผ์นํ๋์ง ํ์ธ
|
316 |
+
if forecast['FCST_DT'][:8] == tomorrow_date and forecast['FCST_DT'][8:10] == '06':
|
317 |
+
tomorrow_temp = forecast['TEMP']
|
318 |
+
precip_type = forecast['PRECPT_TYPE']
|
319 |
+
|
320 |
+
# ๊ฐ์ ํ์
์ ๋ฐ๋ฅธ ํ์
|
321 |
+
precip_mark = ""
|
322 |
+
if precip_type == "๋น":
|
323 |
+
precip_mark = "(๋น)"
|
324 |
+
elif precip_type == "๋":
|
325 |
+
precip_mark = "(๋)"
|
326 |
+
elif precip_type == "๋น/๋":
|
327 |
+
precip_mark = "(๋น/๋)"
|
328 |
+
|
329 |
+
tomorrow_morning_weather = f"{tomorrow_temp}ยฐC{precip_mark}"
|
330 |
+
break
|
331 |
+
|
332 |
+
# ํ๋ฉด์ ํ์
|
333 |
+
st.markdown(f'''
|
334 |
+
<div class="time-container">
|
335 |
+
<div style="text-align: center; margin-bottom: 0.5rem; font-size: 10em; font-weight: bold; color: black;">
|
336 |
+
{temp} / {tomorrow_morning_weather}
|
337 |
+
</div>
|
338 |
+
<span class="date-text">{formatted_date}</span>
|
339 |
+
</div>
|
340 |
+
''', unsafe_allow_html=True)
|
341 |
+
|
342 |
+
|
343 |
+
clock_html = """
|
344 |
+
<div style="width: 100%; max-width: 1200px; margin: 0 auto; padding: 0 20px;">
|
345 |
+
<div style="text-align: center; height: 300px; display: flex; align-items: center; justify-content: center;">
|
346 |
+
<span id="clock" style="font-size: 15em; font-weight: bold; color: black; line-height: 1.2; white-space: nowrap;"></span>
|
347 |
+
</div>
|
348 |
+
</div>
|
349 |
+
<script>
|
350 |
+
function updateClock() {
|
351 |
+
const now = new Date();
|
352 |
+
const options = {
|
353 |
+
timeZone: 'Asia/Seoul',
|
354 |
+
hour12: true,
|
355 |
+
hour: 'numeric',
|
356 |
+
minute: '2-digit'
|
357 |
+
};
|
358 |
+
document.getElementById('clock').textContent = now.toLocaleTimeString('ko-KR', options);
|
359 |
+
}
|
360 |
+
setInterval(updateClock, 1000);
|
361 |
+
updateClock();
|
362 |
+
</script>
|
363 |
+
"""
|
364 |
+
components.html(clock_html, height=300)
|
365 |
+
|
366 |
+
st.button("์๊ฐ๋๋ณ ์จ๋ ๋ณด๊ธฐ", on_click=lambda: st.session_state.update({'current_section': 'temperature'}))
|
367 |
+
|
368 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
369 |
+
|
370 |
+
|
371 |
+
def show_temperature_graph(data):
|
372 |
+
st.markdown('<div class="section-container">', unsafe_allow_html=True)
|
373 |
+
st.markdown('<h1 style="text-align: center; margin-bottom: 1rem;">์๊ฐ๋๋ณ ์จ๋</h1>', unsafe_allow_html=True)
|
374 |
+
|
375 |
+
forecast_data = data['FCST24HOURS']['FCST24HOURS']
|
376 |
+
if not isinstance(forecast_data, list):
|
377 |
+
forecast_data = [forecast_data]
|
378 |
+
|
379 |
+
# Sort forecast data by FCST_DT to ensure correct time ordering
|
380 |
+
forecast_data = sorted(forecast_data, key=lambda x: x['FCST_DT'])
|
381 |
+
|
382 |
+
# ํ์ฌ ์๊ฐ ๊ธฐ์ค์ผ๋ก ์ ํจํ ์๋ณด ๋ฐ์ดํฐ๋ง ํํฐ๋ง
|
383 |
+
current_time = datetime.utcnow() + timedelta(hours=9)
|
384 |
+
current_time_str = current_time.strftime('%Y%m%d%H%M')
|
385 |
+
|
386 |
+
# ํ์ฌ ์๊ฐ ์ดํ์ ์๋ณด ๋ฐ์ดํฐ๋ง ํํฐ๋ง
|
387 |
+
valid_forecast_data = [fcst for fcst in forecast_data if fcst['FCST_DT'] >= current_time_str]
|
388 |
+
|
389 |
+
# ์ ํจํ ๋ฐ์ดํฐ๊ฐ ์์ผ๋ฉด ์ ์ฒด ๋ฐ์ดํฐ ์ฌ์ฉ
|
390 |
+
if not valid_forecast_data:
|
391 |
+
valid_forecast_data = forecast_data
|
392 |
+
|
393 |
+
times = []
|
394 |
+
temps = []
|
395 |
+
weather_icons = []
|
396 |
+
weather_descriptions = []
|
397 |
+
date_changes = []
|
398 |
+
|
399 |
+
# Find the index closest to current time
|
400 |
+
time_differences = []
|
401 |
+
for fcst in valid_forecast_data:
|
402 |
+
forecast_time = datetime.strptime(fcst['FCST_DT'], '%Y%m%d%H%M')
|
403 |
+
time_diff = abs((forecast_time - current_time).total_seconds())
|
404 |
+
time_differences.append(time_diff)
|
405 |
+
|
406 |
+
current_index = time_differences.index(min(time_differences))
|
407 |
+
|
408 |
+
# Reorder forecast data to start from current time
|
409 |
+
valid_forecast_data = valid_forecast_data[current_index:] + valid_forecast_data[:current_index]
|
410 |
+
|
411 |
+
for i, forecast in enumerate(valid_forecast_data):
|
412 |
+
time_str = forecast['FCST_DT']
|
413 |
+
date = time_str[6:8]
|
414 |
+
hour = time_str[8:10]
|
415 |
+
|
416 |
+
if i > 0 and valid_forecast_data[i-1]['FCST_DT'][6:8] != date:
|
417 |
+
date_changes.append(i)
|
418 |
+
times.append(f"{hour}์")
|
419 |
+
temps.append(float(forecast['TEMP']))
|
420 |
+
|
421 |
+
sky_status = forecast['SKY_STTS']
|
422 |
+
precip_type = forecast['PRECPT_TYPE']
|
423 |
+
|
424 |
+
if precip_type == "๋น":
|
425 |
+
icon = "๐ง๏ธ"
|
426 |
+
description = "๋น"
|
427 |
+
elif precip_type == "๋":
|
428 |
+
icon = "๐จ๏ธ"
|
429 |
+
description = "๋"
|
430 |
+
elif precip_type == "๋น/๋":
|
431 |
+
icon = "๐จ๏ธ๐ง๏ธ"
|
432 |
+
description = "๋น/๋"
|
433 |
+
elif sky_status == "๋ง์":
|
434 |
+
icon = "โ๏ธ"
|
435 |
+
description = "๋ง์"
|
436 |
+
elif sky_status == "๊ตฌ๋ฆ๋ง์":
|
437 |
+
icon = "โ
"
|
438 |
+
description = "๊ตฌ๋ฆ<br>๋ง์"
|
439 |
+
elif sky_status == "ํ๋ฆผ":
|
440 |
+
icon = "โ๏ธ"
|
441 |
+
description = "ํ๋ฆผ"
|
442 |
+
else:
|
443 |
+
icon = "โ๏ธ"
|
444 |
+
description = "์ ๋ณด์์"
|
445 |
+
|
446 |
+
weather_icons.append(icon)
|
447 |
+
weather_descriptions.append(description)
|
448 |
+
|
449 |
+
df = pd.DataFrame({
|
450 |
+
'์๊ฐ': times,
|
451 |
+
'๊ธฐ์จ': temps,
|
452 |
+
'๋ ์จ': weather_icons,
|
453 |
+
'์ค๋ช
': weather_descriptions,
|
454 |
+
'FCST_DT': [f['FCST_DT'] for f in valid_forecast_data]
|
455 |
+
})
|
456 |
+
|
457 |
+
fig = px.line(df, x='์๊ฐ', y='๊ธฐ์จ', markers=True)
|
458 |
+
|
459 |
+
# Add nighttime overlay (18:00-06:00)
|
460 |
+
for i in range(len(times)):
|
461 |
+
hour = int(times[i].replace('์', ''))
|
462 |
+
if hour >= 18 or hour < 6:
|
463 |
+
fig.add_vrect(
|
464 |
+
x0=times[i],
|
465 |
+
x1=times[i+1] if i < len(times)-1 else times[-1],
|
466 |
+
fillcolor='rgba(0, 0, 0, 0.1)',
|
467 |
+
layer='below',
|
468 |
+
line_width=0,
|
469 |
+
annotation_text="",
|
470 |
+
annotation_position="top left"
|
471 |
+
)
|
472 |
+
|
473 |
+
# ํ์ฌ ์๊ฐ๊ณผ ๊ฐ์ฅ ๊ฐ๊น์ด ์๋ณด ์๊ฐ ์ฐพ๊ธฐ
|
474 |
+
current_time = datetime.utcnow() + timedelta(hours=9)
|
475 |
+
|
476 |
+
# ๋
น์ ์ธ๋ก์ ์ถ๊ฐ ๋ฐ "ํ์ฌ" ํ
์คํธ ํ์ - ์ด์ ํญ์ ์ฒซ ๋ฒ์งธ ๋ฐ์ดํฐ ํฌ์ธํธ์ ํ์
|
477 |
+
fig.add_vline(x=times[0], line_width=2, line_dash="dash", line_color="green")
|
478 |
+
fig.add_annotation(
|
479 |
+
x=times[0],
|
480 |
+
y=max(temps) + 4,
|
481 |
+
text="<b>ํ์ฌ</b>",
|
482 |
+
showarrow=True,
|
483 |
+
arrowhead=2,
|
484 |
+
)
|
485 |
+
|
486 |
+
bold_times = ["00์", "06์", "12์", "18์", "24์"]
|
487 |
+
for time in bold_times:
|
488 |
+
if time in times:
|
489 |
+
index = times.index(time)
|
490 |
+
fig.add_annotation(
|
491 |
+
x=time,
|
492 |
+
y=min(temps),
|
493 |
+
text=time,
|
494 |
+
showarrow=False,
|
495 |
+
font=dict(size=30, color="black", family="Arial")
|
496 |
+
)
|
497 |
+
|
498 |
+
fig.add_vline(x='12์', line_width=2, line_dash="dash", line_color="rgba(0,0,0,0.5)")
|
499 |
+
|
500 |
+
# ์ค๋๊ณผ ๋ด์ผ, ์ค์ ๊ณผ ์คํ ํ
์คํธ๋ ํด๋น ์๊ฐ๋์ ๋ฐ์ดํฐ๊ฐ ์์ ๋๋ง ํ์
|
501 |
+
time_set = set(times)
|
502 |
+
|
503 |
+
if '11์' in time_set:
|
504 |
+
fig.add_annotation(x='11์', y=max(temps) + 4, text="์ค์ ", showarrow=False, font=dict(size=24))
|
505 |
+
|
506 |
+
if '13์' in time_set:
|
507 |
+
fig.add_annotation(x='13์', y=max(temps) + 4, text="์คํ", showarrow=False, font=dict(size=24))
|
508 |
+
|
509 |
+
if '23์' in time_set:
|
510 |
+
fig.add_annotation(x='23์', y=max(temps) + 4, text="์ค๋", showarrow=False, font=dict(size=24))
|
511 |
+
|
512 |
+
if '01์' in time_set:
|
513 |
+
fig.add_annotation(x='01์', y=max(temps) + 4, text="๋ด์ผ", showarrow=False, font=dict(size=24))
|
514 |
+
|
515 |
+
fig.update_traces(
|
516 |
+
line_color='#FF6B6B',
|
517 |
+
marker=dict(size=10, color='#FF6B6B'),
|
518 |
+
textposition="top center",
|
519 |
+
mode='lines+markers+text',
|
520 |
+
text=[f"<b>{int(round(temp))}ยฐ</b>" for temp in df['๊ธฐ์จ']],
|
521 |
+
textfont=dict(size=24)
|
522 |
+
)
|
523 |
+
|
524 |
+
for i, (icon, description) in enumerate(zip(weather_icons, weather_descriptions)):
|
525 |
+
fig.add_annotation(
|
526 |
+
x=times[i],
|
527 |
+
y=max(temps) + 3,
|
528 |
+
text=f"{icon}",
|
529 |
+
showarrow=False,
|
530 |
+
font=dict(size=30)
|
531 |
+
)
|
532 |
+
fig.add_annotation(
|
533 |
+
x=times[i],
|
534 |
+
y=max(temps) + 2,
|
535 |
+
text=f"{description}",
|
536 |
+
showarrow=False,
|
537 |
+
font=dict(size=16),
|
538 |
+
textangle=0
|
539 |
+
)
|
540 |
+
|
541 |
+
for date_change in date_changes:
|
542 |
+
fig.add_vline(
|
543 |
+
x=times[date_change],
|
544 |
+
line_width=2,
|
545 |
+
line_dash="dash",
|
546 |
+
line_color="rgba(255, 0, 0, 0.7)"
|
547 |
+
)
|
548 |
+
|
549 |
+
fig.update_layout(
|
550 |
+
title=None,
|
551 |
+
xaxis_title='',
|
552 |
+
yaxis_title='๊ธฐ์จ (ยฐC)',
|
553 |
+
height=600,
|
554 |
+
width=7200,
|
555 |
+
showlegend=False,
|
556 |
+
plot_bgcolor='rgba(255,255,255,0.9)',
|
557 |
+
paper_bgcolor='rgba(0,0,0,0)',
|
558 |
+
margin=dict(l=50, r=50, t=0, b=0),
|
559 |
+
xaxis=dict(
|
560 |
+
tickangle=0,
|
561 |
+
tickfont=dict(size=14),
|
562 |
+
gridcolor='rgba(0,0,0,0.1)',
|
563 |
+
dtick=1,
|
564 |
+
tickmode='array',
|
565 |
+
ticktext=[f"{i:02d}์" for i in range(24)],
|
566 |
+
tickvals=[f"{i:02d}์" for i in range(24)]
|
567 |
+
),
|
568 |
+
yaxis=dict(
|
569 |
+
tickfont=dict(size=14),
|
570 |
+
gridcolor='rgba(0,0,0,0.1)'
|
571 |
+
)
|
572 |
+
)
|
573 |
+
|
574 |
+
st.plotly_chart(fig, use_container_width=True)
|
575 |
+
|
576 |
+
# ๋ ์จ ์๋ณด ์์ฑ ๋ฐ ํ์ ๋ถ๋ถ์ ์ธ์
์ํ๋ก ๊ด๋ฆฌ
|
577 |
+
if 'weather_forecast' not in st.session_state:
|
578 |
+
client = OpenAI(
|
579 |
+
api_key="glhf_9ea0e0babe1e45353dd03b44cb979e22",
|
580 |
+
base_url="https://glhf.chat/api/openai/v1"
|
581 |
+
)
|
582 |
+
|
583 |
+
forecast_data_str = "\n".join([f"{time}: {temp}๋, {description}" for time, temp, description in zip(times, temps, weather_descriptions)])
|
584 |
+
|
585 |
+
response = client.chat.completions.create(
|
586 |
+
model="hf:Nexusflow/Athene-V2-Chat",
|
587 |
+
messages=[
|
588 |
+
{"role": "system", "content": "๋น์ ์ ๋ ์จ ์๋ณด๊ด์
๋๋ค. ์ฃผ์ด์ง ์๊ฐ๋๋ณ ๋ ์จ ๋ฐ์ดํฐ๋ฅผ ๋ฐํ์ผ๋ก ์ ํํ ๋ ์จ ์๋ณด๋ฅผ ์์ฑํด์ฃผ์ธ์."},
|
589 |
+
{"role": "user", "content": f"๋ค์ ์๊ฐ๋๋ณ ๋ ์จ ๋ฐ์ดํฐ๋ฅผ ๋ณด๊ณ ์ค์ ๋ ์จ ์ํฉ์ ๋ง๋ ์ ํํ ๋ ์จ ์๋ณด๋ฅผ 200์๋ก ๋ง๋ค์ด์ฃผ์ธ์. ๋น๋ ๋ ์๋ณด๊ฐ ์๋ ๊ฒฝ์ฐ์๋ง ์ฐ์ฐ์ ์ค๋นํ๋๋ก ์๋ดํด์ฃผ์ธ์. ์ท์ฐจ๋ฆผ์ ๋ค์์ ์ฐธ๊ณ ํ์ธ์. \n27ยฐC์ด์:๋ฐํํฐ, ๋ฐ๋ฐ์ง, ๋ฏผ์๋งค.\n23ยฐC~26ยฐC: ์์ ์
์ธ , ๋ฐํํฐ, ๋ฐ๋ฐ์ง, ๋ฉด๋ฐ์ง.\n20ยฐC~22ยฐC: ์์ ๊ฐ๋๊ฑด, ๊ธดํํฐ, ๊ธด๋ฐ์ง.\n17ยฐC~19ยฐC: ์์ ๋ํธ, ๊ฐ๋๊ฑด, ๋งจํฌ๋งจ, ์์ ์์ผ, ๊ธด๋ฐ์ง.\n12ยฐC~16ยฐC: ์์ผ, ๊ฐ๋๊ฑด, ์ผ์, ๋งจํฌ๋งจ, ๋ํธ, ์คํํน, ๊ธด๋ฐ์ง\n9ยฐC~11ยฐC: ํธ๋ ์น์ฝํธ, ์ผ์, ๊ฐ์ฃฝ ์์ผ, ์คํํน, ๊ธด๋ฐ์ง\n5ยฐC~8ยฐC: ์ฝํธ, ํํธํ
, ๋ํธ, ๊ธด๋ฐ์ง\n4ยฐC์ดํ: ํจ๋ฉ, ๋๊บผ์ด ์ฝํธ, ๋ชฉ๋๋ฆฌ, ๊ธฐ๋ชจ์ ํ: {forecast_data_str}"}
|
590 |
+
]
|
591 |
+
)
|
592 |
+
st.session_state.weather_forecast = response.choices[0].message.content
|
593 |
+
|
594 |
+
# ์ ์ฅ๋ ๋ ์จ ์๋ณด ํ์
|
595 |
+
st.markdown(f'''
|
596 |
+
<div class="scroll-container">
|
597 |
+
<div class="scroll-text">{st.session_state.weather_forecast}</div>
|
598 |
+
</div>
|
599 |
+
''', unsafe_allow_html=True)
|
600 |
+
|
601 |
+
|
602 |
+
# ์ฐ๋ฆฌ์ง ๋ ์จ ์ ๋ณด๋ก ๋์๊ฐ๊ธฐ ๋ฒํผ ์ถ๊ฐ
|
603 |
+
st.button("์ฐ๋ฆฌ์ง ๋ ์จ ์ ๋ณด๋ก ๋์๊ฐ๊ธฐ", on_click=lambda: st.session_state.update({'current_section': 'weather'}))
|
604 |
+
|
605 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
606 |
+
|
607 |
+
def main():
|
608 |
+
if 'current_section' not in st.session_state:
|
609 |
+
st.session_state.current_section = 'weather'
|
610 |
+
st.session_state.last_api_call = 0
|
611 |
+
st.session_state.weather_data = None
|
612 |
+
|
613 |
+
# ํ์ฌ ์๊ฐ์ ์์ธ ์๊ฐ์ผ๋ก ๊ฐ์ ธ์ต๋๋ค.
|
614 |
+
current_time = datetime.utcnow() + timedelta(hours=9)
|
615 |
+
current_timestamp = current_time.timestamp()
|
616 |
+
|
617 |
+
# ๋ฐ์ดํฐ ์๋ก๊ณ ์นจ ์ฒดํฌ
|
618 |
+
if 'last_api_call' not in st.session_state:
|
619 |
+
st.session_state.last_api_call = 0
|
620 |
+
|
621 |
+
time_since_last_call = current_timestamp - st.session_state.last_api_call
|
622 |
+
|
623 |
+
# ์๋ ์๋ก๊ณ ์นจ์ ์ํ placeholder
|
624 |
+
refresh_placeholder = st.empty()
|
625 |
+
|
626 |
+
# ๋ฐ์ดํฐ ๊ฐฑ์ ์ด ํ์ํ ๊ฒฝ์ฐ
|
627 |
+
if not st.session_state.weather_data or time_since_last_call >= 300:
|
628 |
+
try:
|
629 |
+
new_data = get_weather_data()
|
630 |
+
if new_data: # ์ ๋ฐ์ดํฐ๋ฅผ ์ฑ๊ณต์ ์ผ๋ก ๋ฐ์์์ ๋๋ง ์
๋ฐ์ดํธ
|
631 |
+
st.session_state.weather_data = new_data
|
632 |
+
st.session_state.last_api_call = current_timestamp
|
633 |
+
st.rerun() # ํ์ด์ง ์๋ก๊ณ ์นจ
|
634 |
+
except Exception as e:
|
635 |
+
st.error(f"Failed to refresh data: {str(e)}")
|
636 |
+
|
637 |
+
data = st.session_state.weather_data
|
638 |
+
if data:
|
639 |
+
pm10_value = data['PM10']
|
640 |
+
background_color = get_background_color(pm10_value)
|
641 |
+
|
642 |
+
st.markdown(f"""
|
643 |
+
<style>
|
644 |
+
.stApp {{
|
645 |
+
background-color: {background_color};
|
646 |
+
}}
|
647 |
+
</style>
|
648 |
+
""", unsafe_allow_html=True)
|
649 |
+
|
650 |
+
if st.session_state.current_section == 'weather':
|
651 |
+
show_weather_info(data)
|
652 |
+
else:
|
653 |
+
show_temperature_graph(data)
|
654 |
+
|
655 |
+
# ์๋ ์๋ก๊ณ ์นจ์ ์ํ ํ์ด๋จธ
|
656 |
+
with refresh_placeholder:
|
657 |
+
if time_since_last_call < 300:
|
658 |
+
remaining_time = 300 - time_since_last_call
|
659 |
+
time.sleep(min(remaining_time, 1)) # ์ต๋ 1์ด์ฉ ๋๊ธฐ
|
660 |
+
st.rerun()
|
661 |
+
|
662 |
+
if __name__ == "__main__":
|
663 |
+
main()
|