Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,17 @@
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
-
import
|
| 4 |
from datetime import datetime
|
| 5 |
from transformers import pipeline
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
| 11 |
def get_school_info(region_code, school_name, api_key):
|
| 12 |
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}"
|
| 13 |
res = requests.get(url)
|
|
@@ -28,79 +32,82 @@ def summarize_schedule(rows, school_name, year):
|
|
| 28 |
lines = []
|
| 29 |
for row in rows:
|
| 30 |
date = row["AA_YMD"]
|
| 31 |
-
dt = datetime.strptime(date, "%Y%m%d").strftime("%-m
|
| 32 |
event = row["EVENT_NM"]
|
| 33 |
lines.append(f"{dt}: {event}")
|
| 34 |
text = "\n".join(lines)
|
| 35 |
-
prompt = f"{school_name}
|
| 36 |
result = llm([{"role": "user", "content": prompt}])
|
| 37 |
return result[0]["generated_text"].replace(prompt, "").strip()
|
| 38 |
|
| 39 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
region_options = {
|
| 41 |
"B10": "μμΈ", "C10": "λΆμ°", "D10": "λꡬ", "E10": "μΈμ²", "F10": "κ΄μ£Ό", "G10": "λμ ",
|
| 42 |
"H10": "μΈμ°", "I10": "μΈμ’
", "J10": "κ²½κΈ°", "K10": "κ°μ", "M10": "μΆ©λΆ", "N10": "μΆ©λ¨",
|
| 43 |
"P10": "μ λΆ", "Q10": "μ λ¨", "R10": "κ²½λΆ", "S10": "κ²½λ¨", "T10": "μ μ£Ό"
|
| 44 |
}
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
demo.launch()
|
|
|
|
| 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)
|
|
|
|
| 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}")
|
|
|