Spaces:
Sleeping
Sleeping
Create src/streamlit_app.py
Browse files- src/streamlit_app.py +111 -38
src/streamlit_app.py
CHANGED
@@ -1,40 +1,113 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
-
import pandas as pd
|
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 requests
|
|
|
3 |
import streamlit as st
|
4 |
+
from datetime import datetime
|
5 |
+
from transformers import pipeline
|
6 |
|
7 |
+
# Gemma ๋ชจ๋ธ ๋ก๋ฉ
|
8 |
+
@st.cache_resource
|
9 |
+
def load_model():
|
10 |
+
return pipeline("text-generation", model="google/gemma-2-2b-it", max_new_tokens=512)
|
11 |
+
|
12 |
+
llm = load_model()
|
13 |
+
|
14 |
+
# ํ๊ต ์ ๋ณด ๋ฐ ์ผ์ ๊ฐ์ ธ์ค๊ธฐ ํจ์
|
15 |
+
def get_school_info(region_code, school_name, api_key):
|
16 |
+
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}"
|
17 |
+
res = requests.get(url)
|
18 |
+
data = res.json()
|
19 |
+
school = data.get("schoolInfo", [{}])[1].get("row", [{}])[0]
|
20 |
+
return school.get("SD_SCHUL_CODE"), school.get("ATPT_OFCDC_SC_CODE")
|
21 |
+
|
22 |
+
def get_schedule(region_code, school_code, year, api_key):
|
23 |
+
from_ymd = f"{year}0101"
|
24 |
+
to_ymd = f"{year}1231"
|
25 |
+
url = f"https://open.neis.go.kr/hub/SchoolSchedule?KEY={api_key}&Type=json&pIndex=1&pSize=500&ATPT_OFCDC_SC_CODE={region_code}&SD_SCHUL_CODE={school_code}&AA_FROM_YMD={from_ymd}&AA_TO_YMD={to_ymd}"
|
26 |
+
res = requests.get(url)
|
27 |
+
data = res.json()
|
28 |
+
rows = data.get("SchoolSchedule", [{}])[1].get("row", [])
|
29 |
+
return rows
|
30 |
+
|
31 |
+
def summarize_schedule(rows, school_name, year):
|
32 |
+
lines = []
|
33 |
+
for row in rows:
|
34 |
+
date = row["AA_YMD"]
|
35 |
+
dt = datetime.strptime(date, "%Y%m%d").strftime("%-mๆ %-dๆฅ")
|
36 |
+
event = row["EVENT_NM"]
|
37 |
+
lines.append(f"{dt}: {event}")
|
38 |
+
text = "\n".join(lines)
|
39 |
+
prompt = f"{school_name}๊ฐ {year}๋
๋์ ๊ฐ์ง๋ ํ์ฌ์ผ์ ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค:\n{text}\n์ฃผ์ ์ผ์ ์ ๊ฐ๋ณ์ ์ผ๋ก ์์ฝํด์ฃผ์ธ์."
|
40 |
+
result = llm([{"role": "user", "content": prompt}])
|
41 |
+
return result[0]["generated_text"].replace(prompt, "").strip()
|
42 |
+
|
43 |
+
# Streamlit UI
|
44 |
+
st.set_page_config(page_title="ํ์ฌ์ผ์ ์บ๋ฆฐ๋", layout="centered")
|
45 |
+
st.title("๐
ํ์ฌ์ผ์ ์บ๋ฆฐ๋ + AI ์์ฝ")
|
46 |
+
st.markdown("NEIS API์์ ํ์ฌ์ผ์ ์ ๋ถ๋ฌ์ค๊ณ FullCalendar๋ก ์๊ฐํํฉ๋๋ค.")
|
47 |
+
|
48 |
+
region_options = {
|
49 |
+
"B10": "์์ธ", "C10": "๋ถ์ฐ", "D10": "๋๊ตฌ", "E10": "์ธ์ฒ", "F10": "๊ด์ฃผ", "G10": "๋์ ",
|
50 |
+
"H10": "์ธ์ฐ", "I10": "์ธ์ข
", "J10": "๊ฒฝ๊ธฐ", "K10": "๊ฐ์", "M10": "์ถฉ๋ถ", "N10": "์ถฉ๋จ",
|
51 |
+
"P10": "์ ๋ถ", "Q10": "์ ๋จ", "R10": "๊ฒฝ๋ถ", "S10": "๊ฒฝ๋จ", "T10": "์ ์ฃผ"
|
52 |
+
}
|
53 |
+
|
54 |
+
with st.form("query_form"):
|
55 |
+
region = st.selectbox("์ง์ญ ๊ต์ก์ฒญ", options=list(region_options.keys()), format_func=lambda x: f"{region_options[x]} ({x})")
|
56 |
+
school_name = st.text_input("ํ๊ต๋ช
", placeholder="์: ์๋ฆฌ์ด๋ฑํ๊ต")
|
57 |
+
year = st.selectbox("๋
๋", options=[2022, 2023, 2024, 2025], index=2)
|
58 |
+
submitted = st.form_submit_button("๐
ํ์ฌ์ผ์ ๋ถ๋ฌ์ค๊ธฐ")
|
59 |
+
|
60 |
+
if submitted:
|
61 |
+
with st.spinner("์ผ์ ๋ถ๋ฌ์ค๋ ์ค..."):
|
62 |
+
api_key = os.environ.get("NEIS_API_KEY", "a69e08342c8947b4a52cd72789a5ecaf")
|
63 |
+
school_code, region_code = get_school_info(region, school_name, api_key)
|
64 |
+
if not school_code:
|
65 |
+
st.error("ํ๊ต ์ ๋ณด๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.")
|
66 |
+
else:
|
67 |
+
schedule_rows = get_schedule(region_code, school_code, year, api_key)
|
68 |
+
if not schedule_rows:
|
69 |
+
st.info("ํด๋น ์กฐ๊ฑด์ ํ์ฌ์ผ์ ์ด ์์ต๋๋ค.")
|
70 |
+
else:
|
71 |
+
# ์ผ์ ๋ฐ์ดํฐ๋ฅผ HTML ์บ๋ฆฐ๋์ ๋ฃ๊ธฐ ์ํ JSON ์์ฑ
|
72 |
+
events = [
|
73 |
+
{
|
74 |
+
"title": row["EVENT_NM"],
|
75 |
+
"start": datetime.strptime(row["AA_YMD"], "%Y%m%d").strftime("%Y-%m-%d")
|
76 |
+
}
|
77 |
+
for row in schedule_rows
|
78 |
+
]
|
79 |
+
|
80 |
+
import json
|
81 |
+
event_json = json.dumps(events, ensure_ascii=False)
|
82 |
+
|
83 |
+
# FullCalendar ์ฝ์
|
84 |
+
st.components.v1.html(f"""
|
85 |
+
<html>
|
86 |
+
<head>
|
87 |
+
<link href='https://cdn.jsdelivr.net/npm/[email protected]/index.global.min.css' rel='stylesheet' />
|
88 |
+
<script src='https://cdn.jsdelivr.net/npm/[email protected]/index.global.min.js'></script>
|
89 |
+
<script>
|
90 |
+
document.addEventListener('DOMContentLoaded', function() {{
|
91 |
+
var calendarEl = document.getElementById('calendar');
|
92 |
+
var calendar = new FullCalendar.Calendar(calendarEl, {{
|
93 |
+
initialView: 'dayGridMonth',
|
94 |
+
locale: 'ko',
|
95 |
+
height: 600,
|
96 |
+
events: {event_json}
|
97 |
+
}});
|
98 |
+
calendar.render();
|
99 |
+
}});
|
100 |
+
</script>
|
101 |
+
</head>
|
102 |
+
<body>
|
103 |
+
<div id='calendar'></div>
|
104 |
+
</body>
|
105 |
+
</html>
|
106 |
+
""", height=650)
|
107 |
+
|
108 |
+
with st.expander("โจ 1๋
์น ์์ฝ ๋ณด๊ธฐ", expanded=False):
|
109 |
+
if st.button("๐ค ์์ฝ ์์ฑํ๊ธฐ"):
|
110 |
+
with st.spinner("Gemma ๋ชจ๋ธ์ด ์์ฝ ์ค..."):
|
111 |
+
summary = summarize_schedule(schedule_rows, school_name, year)
|
112 |
+
st.success("์์ฝ ์๋ฃ!")
|
113 |
+
st.markdown(f"**{school_name} {year}๋
ํ์ฌ์ผ์ ์์ฝ:**\n\n{summary}")
|