yongyeol commited on
Commit
3ebd507
ยท
verified ยท
1 Parent(s): 32ccf3d

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +125 -38
src/streamlit_app.py CHANGED
@@ -1,40 +1,127 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
 
5
 
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import requests
4
  import streamlit as st
5
+ from datetime import datetime
6
 
7
+ # โœ… Streamlit ๊ธฐ๋ณธ ์„ค์ •
8
+ st.set_page_config(page_title="ํ•™์‚ฌ์ผ์ • ์บ˜๋ฆฐ๋”", layout="centered")
9
+ st.title("๐Ÿ“… ํ•™์‚ฌ์ผ์ • ์บ˜๋ฆฐ๋” + AI ์š”์•ฝ")
10
+ st.markdown("NEIS API์—์„œ ํ•™์‚ฌ์ผ์ •์„ ๋ถˆ๋Ÿฌ์˜ค๊ณ  FullCalendar๋กœ ์‹œ๊ฐํ™”ํ•ฉ๋‹ˆ๋‹ค.")
11
+
12
+ # โœ… ํ•™๊ต ์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ
13
+ def get_school_info(region_code, school_name, api_key):
14
+ url = f"https://open.neis.go.kr/hub/schoolInfo?KEY={api_key}&Type=json&pIndex=1&pSize=1&SCHUL_NM={school_name}&ATPT_OFCDC_SC_CODE={region_code}"
15
+ res = requests.get(url)
16
+ data = res.json()
17
+ school = data.get("schoolInfo", [{}])[1].get("row", [{}])[0]
18
+ return school.get("SD_SCHUL_CODE"), school.get("ATPT_OFCDC_SC_CODE")
19
+
20
+ # โœ… ํ•™์‚ฌ์ผ์ • ๊ฐ€์ ธ์˜ค๊ธฐ
21
+ def get_schedule(region_code, school_code, year, month, api_key):
22
+ from_ymd = f"{year}{month:02}01"
23
+ to_ymd = f"{year}{month:02}31"
24
+ url = f"https://open.neis.go.kr/hub/SchoolSchedule?KEY={api_key}&Type=json&pIndex=1&pSize=100&ATPT_OFCDC_SC_CODE={region_code}&SD_SCHUL_CODE={school_code}&AA_FROM_YMD={from_ymd}&AA_TO_YMD={to_ymd}"
25
+ res = requests.get(url)
26
+ data = res.json()
27
+ rows = data.get("SchoolSchedule", [{}])[1].get("row", [])
28
+ return rows
29
+
30
+ # โœ… ์š”์•ฝ ์ƒ์„ฑ (vLLM API ํ˜ธ์ถœ)
31
+ def summarize_schedule(rows, school_name, year):
32
+ if not rows:
33
+ return "์ผ์ •์ด ์—†์–ด ์š”์•ฝํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."
34
+
35
+ lines = []
36
+ for row in rows:
37
+ date = row["AA_YMD"]
38
+ dt = datetime.strptime(date, "%Y%m%d").strftime("%-m์›” %-d์ผ")
39
+ event = row["EVENT_NM"]
40
+ lines.append(f"{dt}: {event}")
41
+ text = "\n".join(lines)
42
+
43
+ prompt = f"{school_name}๊ฐ€ {year}๋…„๋„์— ๊ฐ€์ง€๋Š” ํ•™์‚ฌ์ผ์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค:\n{text}\n์ฃผ์š” ์ผ์ •์„ ์š”์•ฝํ•ด์ฃผ์„ธ์š”."
44
+
45
+ payload = {
46
+ "model": "skt/A.X-4.0-Light",
47
+ "messages": [
48
+ {"role": "system", "content": "๋‹น์‹ ์€ ํ•™์‚ฌ์ผ์ •์„ ์š”์•ฝํ•ด์ฃผ๋Š” AI์ž…๋‹ˆ๋‹ค."},
49
+ {"role": "user", "content": prompt}
50
+ ],
51
+ "max_tokens": 256,
52
+ "temperature": 0.0
53
+ }
54
+
55
+ try:
56
+ res = requests.post("http://localhost:8000/v1/chat/completions", json=payload)
57
+ res.raise_for_status()
58
+ return res.json()["choices"][0]["message"]["content"].strip()
59
+ except Exception as e:
60
+ return f"โŒ ์š”์•ฝ ์‹คํŒจ: {e}"
61
+
62
+ # โœ… ์ง€์—ญ/ํ•™๊ต/๋…„๋„/์›” ์„ ํƒ UI
63
+ region_options = {
64
+ "B10": "์„œ์šธ", "C10": "๋ถ€์‚ฐ", "D10": "๋Œ€๊ตฌ", "E10": "์ธ์ฒœ", "F10": "๊ด‘์ฃผ", "G10": "๋Œ€์ „",
65
+ "H10": "์šธ์‚ฐ", "I10": "์„ธ์ข…", "J10": "๊ฒฝ๊ธฐ", "K10": "๊ฐ•์›", "M10": "์ถฉ๋ถ", "N10": "์ถฉ๋‚จ",
66
+ "P10": "์ „๋ถ", "Q10": "์ „๋‚จ", "R10": "๊ฒฝ๋ถ", "S10": "๊ฒฝ๋‚จ", "T10": "์ œ์ฃผ"
67
+ }
68
+
69
+ with st.form("query_form"):
70
+ region = st.selectbox("์ง€์—ญ ๊ต์œก์ฒญ", options=list(region_options.keys()), format_func=lambda x: f"{region_options[x]} ({x})")
71
+ school_name = st.text_input("ํ•™๊ต๋ช…", placeholder="์˜ˆ: ์ƒ๋ฆฌ์ดˆ๋“ฑํ•™๊ต")
72
+ year = st.selectbox("๋…„๋„", options=[2022, 2023, 2024, 2025], index=2)
73
+ month = st.selectbox("์›”", options=list(range(1, 13)), index=6)
74
+ submitted = st.form_submit_button("๐Ÿ“… ํ•™์‚ฌ์ผ์ • ๋ถˆ๋Ÿฌ์˜ค๊ธฐ")
75
+
76
+ # โœ… ์ œ์ถœ ์ฒ˜๋ฆฌ
77
+ if submitted:
78
+ with st.spinner("์ผ์ • ๋ถˆ๋Ÿฌ์˜ค๋Š” ์ค‘..."):
79
+ api_key = os.environ.get("NEIS_API_KEY", "a69e08342c8947b4a52cd72789a5ecaf")
80
+ school_code, region_code = get_school_info(region, school_name, api_key)
81
+ if not school_code:
82
+ st.error("ํ•™๊ต ์ •๋ณด๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
83
+ else:
84
+ schedule_rows = get_schedule(region_code, school_code, year, month, api_key)
85
+ if not schedule_rows:
86
+ st.info("ํ•ด๋‹น ์กฐ๊ฑด์˜ ํ•™์‚ฌ์ผ์ •์ด ์—†์Šต๋‹ˆ๋‹ค.")
87
+ else:
88
+ events = [
89
+ {
90
+ "title": row["EVENT_NM"],
91
+ "start": datetime.strptime(row["AA_YMD"], "%Y%m%d").strftime("%Y-%m-%d")
92
+ }
93
+ for row in schedule_rows
94
+ if "AA_YMD" in row and "EVENT_NM" in row
95
+ ]
96
+ event_json = json.dumps(events, ensure_ascii=False)
97
+
98
+ st.components.v1.html(f"""
99
+ <html>
100
+ <head>
101
+ <link href='https://cdn.jsdelivr.net/npm/[email protected]/index.global.min.css' rel='stylesheet' />
102
+ <script src='https://cdn.jsdelivr.net/npm/[email protected]/index.global.min.js'></script>
103
+ <script>
104
+ document.addEventListener('DOMContentLoaded', function() {{
105
+ var calendarEl = document.getElementById('calendar');
106
+ var calendar = new FullCalendar.Calendar(calendarEl, {{
107
+ initialView: 'dayGridMonth',
108
+ locale: 'ko',
109
+ height: 600,
110
+ events: {event_json}
111
+ }});
112
+ calendar.render();
113
+ }});
114
+ </script>
115
+ </head>
116
+ <body>
117
+ <div id='calendar'></div>
118
+ </body>
119
+ </html>
120
+ """, height=650)
121
+
122
+ with st.expander("โœจ 1๋…„์น˜ ์š”์•ฝ ๋ณด๊ธฐ", expanded=False):
123
+ if st.button("๐Ÿค– ์š”์•ฝ ์ƒ์„ฑํ•˜๊ธฐ"):
124
+ with st.spinner("๋ชจ๋ธ์ด ์š”์•ฝ ์ค‘..."):
125
+ summary = summarize_schedule(schedule_rows, school_name, year)
126
+ st.success("์š”์•ฝ ์™„๋ฃŒ!")
127
+ st.markdown(f"**{school_name} {year}๋…„ {month}์›” ์ผ์ • ์š”์•ฝ:**\n\n{summary}")