Update src/streamlit_app.py
Browse files- src/streamlit_app.py +125 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,127 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
import streamlit as st
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 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}")
|