cal_vLLM / src /streamlit_app.py
yongyeol's picture
Update src/streamlit_app.py
3ebd507 verified
import os
import json
import requests
import streamlit as st
from datetime import datetime
# โœ… Streamlit ๊ธฐ๋ณธ ์„ค์ •
st.set_page_config(page_title="ํ•™์‚ฌ์ผ์ • ์บ˜๋ฆฐ๋”", layout="centered")
st.title("๐Ÿ“… ํ•™์‚ฌ์ผ์ • ์บ˜๋ฆฐ๋” + AI ์š”์•ฝ")
st.markdown("NEIS API์—์„œ ํ•™์‚ฌ์ผ์ •์„ ๋ถˆ๋Ÿฌ์˜ค๊ณ  FullCalendar๋กœ ์‹œ๊ฐํ™”ํ•ฉ๋‹ˆ๋‹ค.")
# โœ… ํ•™๊ต ์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ
def get_school_info(region_code, school_name, api_key):
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}"
res = requests.get(url)
data = res.json()
school = data.get("schoolInfo", [{}])[1].get("row", [{}])[0]
return school.get("SD_SCHUL_CODE"), school.get("ATPT_OFCDC_SC_CODE")
# โœ… ํ•™์‚ฌ์ผ์ • ๊ฐ€์ ธ์˜ค๊ธฐ
def get_schedule(region_code, school_code, year, month, api_key):
from_ymd = f"{year}{month:02}01"
to_ymd = f"{year}{month:02}31"
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}"
res = requests.get(url)
data = res.json()
rows = data.get("SchoolSchedule", [{}])[1].get("row", [])
return rows
# โœ… ์š”์•ฝ ์ƒ์„ฑ (vLLM API ํ˜ธ์ถœ)
def summarize_schedule(rows, school_name, year):
if not rows:
return "์ผ์ •์ด ์—†์–ด ์š”์•ฝํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."
lines = []
for row in rows:
date = row["AA_YMD"]
dt = datetime.strptime(date, "%Y%m%d").strftime("%-m์›” %-d์ผ")
event = row["EVENT_NM"]
lines.append(f"{dt}: {event}")
text = "\n".join(lines)
prompt = f"{school_name}๊ฐ€ {year}๋…„๋„์— ๊ฐ€์ง€๋Š” ํ•™์‚ฌ์ผ์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค:\n{text}\n์ฃผ์š” ์ผ์ •์„ ์š”์•ฝํ•ด์ฃผ์„ธ์š”."
payload = {
"model": "skt/A.X-4.0-Light",
"messages": [
{"role": "system", "content": "๋‹น์‹ ์€ ํ•™์‚ฌ์ผ์ •์„ ์š”์•ฝํ•ด์ฃผ๋Š” AI์ž…๋‹ˆ๋‹ค."},
{"role": "user", "content": prompt}
],
"max_tokens": 256,
"temperature": 0.0
}
try:
res = requests.post("http://localhost:8000/v1/chat/completions", json=payload)
res.raise_for_status()
return res.json()["choices"][0]["message"]["content"].strip()
except Exception as e:
return f"โŒ ์š”์•ฝ ์‹คํŒจ: {e}"
# โœ… ์ง€์—ญ/ํ•™๊ต/๋…„๋„/์›” ์„ ํƒ UI
region_options = {
"B10": "์„œ์šธ", "C10": "๋ถ€์‚ฐ", "D10": "๋Œ€๊ตฌ", "E10": "์ธ์ฒœ", "F10": "๊ด‘์ฃผ", "G10": "๋Œ€์ „",
"H10": "์šธ์‚ฐ", "I10": "์„ธ์ข…", "J10": "๊ฒฝ๊ธฐ", "K10": "๊ฐ•์›", "M10": "์ถฉ๋ถ", "N10": "์ถฉ๋‚จ",
"P10": "์ „๋ถ", "Q10": "์ „๋‚จ", "R10": "๊ฒฝ๋ถ", "S10": "๊ฒฝ๋‚จ", "T10": "์ œ์ฃผ"
}
with st.form("query_form"):
region = st.selectbox("์ง€์—ญ ๊ต์œก์ฒญ", options=list(region_options.keys()), format_func=lambda x: f"{region_options[x]} ({x})")
school_name = st.text_input("ํ•™๊ต๋ช…", placeholder="์˜ˆ: ์ƒ๋ฆฌ์ดˆ๋“ฑํ•™๊ต")
year = st.selectbox("๋…„๋„", options=[2022, 2023, 2024, 2025], index=2)
month = st.selectbox("์›”", options=list(range(1, 13)), index=6)
submitted = st.form_submit_button("๐Ÿ“… ํ•™์‚ฌ์ผ์ • ๋ถˆ๋Ÿฌ์˜ค๊ธฐ")
# โœ… ์ œ์ถœ ์ฒ˜๋ฆฌ
if submitted:
with st.spinner("์ผ์ • ๋ถˆ๋Ÿฌ์˜ค๋Š” ์ค‘..."):
api_key = os.environ.get("NEIS_API_KEY", "a69e08342c8947b4a52cd72789a5ecaf")
school_code, region_code = get_school_info(region, school_name, api_key)
if not school_code:
st.error("ํ•™๊ต ์ •๋ณด๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
else:
schedule_rows = get_schedule(region_code, school_code, year, month, api_key)
if not schedule_rows:
st.info("ํ•ด๋‹น ์กฐ๊ฑด์˜ ํ•™์‚ฌ์ผ์ •์ด ์—†์Šต๋‹ˆ๋‹ค.")
else:
events = [
{
"title": row["EVENT_NM"],
"start": datetime.strptime(row["AA_YMD"], "%Y%m%d").strftime("%Y-%m-%d")
}
for row in schedule_rows
if "AA_YMD" in row and "EVENT_NM" in row
]
event_json = json.dumps(events, ensure_ascii=False)
st.components.v1.html(f"""
<html>
<head>
<link href='https://cdn.jsdelivr.net/npm/[email protected]/index.global.min.css' rel='stylesheet' />
<script src='https://cdn.jsdelivr.net/npm/[email protected]/index.global.min.js'></script>
<script>
document.addEventListener('DOMContentLoaded', function() {{
var calendarEl = document.getElementById('calendar');
var calendar = new FullCalendar.Calendar(calendarEl, {{
initialView: 'dayGridMonth',
locale: 'ko',
height: 600,
events: {event_json}
}});
calendar.render();
}});
</script>
</head>
<body>
<div id='calendar'></div>
</body>
</html>
""", height=650)
with st.expander("โœจ 1๋…„์น˜ ์š”์•ฝ ๋ณด๊ธฐ", expanded=False):
if st.button("๐Ÿค– ์š”์•ฝ ์ƒ์„ฑํ•˜๊ธฐ"):
with st.spinner("๋ชจ๋ธ์ด ์š”์•ฝ ์ค‘..."):
summary = summarize_schedule(schedule_rows, school_name, year)
st.success("์š”์•ฝ ์™„๋ฃŒ!")
st.markdown(f"**{school_name} {year}๋…„ {month}์›” ์ผ์ • ์š”์•ฝ:**\n\n{summary}")