|
import os |
|
import requests |
|
import gradio as gr |
|
from datetime import datetime |
|
from transformers import pipeline |
|
|
|
|
|
llm = pipeline("text-generation", model="google/gemma-2-2b-it", max_new_tokens=512) |
|
|
|
|
|
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, api_key): |
|
from_ymd = f"{year}0101" |
|
to_ymd = f"{year}1231" |
|
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}" |
|
res = requests.get(url) |
|
data = res.json() |
|
rows = data.get("SchoolSchedule", [{}])[1].get("row", []) |
|
return rows |
|
|
|
def summarize_schedule(rows, school_name, year): |
|
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μ΄ λ΄μ©μ μμ½ν΄μ€. μ£Όμ μΌμ μ€μ¬μΌλ‘ κ°κ²°νκ²." |
|
result = llm([{"role": "user", "content": prompt}]) |
|
return result[0]["generated_text"].replace(prompt, "").strip() |
|
|
|
|
|
def load_and_summarize(region_code, school_name, year, month): |
|
api_key = os.environ.get("NEIS_API_KEY", "a69e08342c8947b4a52cd72789a5ecaf") |
|
|
|
if not school_name.strip(): |
|
return "β νκ΅λͺ
μ μ
λ ₯ν΄μ£ΌμΈμ.", "", "" |
|
|
|
school_code, confirmed_region = get_school_info(region_code, school_name, api_key) |
|
if not school_code: |
|
return "β νκ΅ μ 보λ₯Ό μ°Ύμ μ μμ΅λλ€. μ
λ ₯μ νμΈν΄μ£ΌμΈμ.", "", "" |
|
|
|
schedule_rows = get_schedule(confirmed_region, school_code, year, api_key) |
|
if not schedule_rows: |
|
return "βΉοΈ νμ¬μΌμ μ λ³΄κ° μμ΅λλ€.", "", "" |
|
|
|
|
|
month_rows = [r for r in schedule_rows if r["AA_YMD"].startswith(f"{year}{month:02}")] |
|
month_display = "\n".join( |
|
f"{datetime.strptime(r['AA_YMD'], '%Y%m%d').strftime('%Y-%m-%d')} - {r['EVENT_NM']}" |
|
for r in month_rows |
|
) or "π μ νν μμ ν΄λΉνλ μΌμ μ΄ μμ΅λλ€." |
|
|
|
|
|
summary = summarize_schedule(schedule_rows, school_name, year) |
|
|
|
return f"β
{school_name}μ {year}λ
{month}μ μΌμ μ
λλ€.", month_display, summary |
|
|
|
|
|
region_options = { |
|
"B10": "μμΈ", "C10": "λΆμ°", "D10": "λꡬ", "E10": "μΈμ²", "F10": "κ΄μ£Ό", "G10": "λμ ", |
|
"H10": "μΈμ°", "I10": "μΈμ’
", "J10": "κ²½κΈ°", "K10": "κ°μ", "M10": "μΆ©λΆ", "N10": "μΆ©λ¨", |
|
"P10": "μ λΆ", "Q10": "μ λ¨", "R10": "κ²½λΆ", "S10": "κ²½λ¨", "T10": "μ μ£Ό" |
|
} |
|
|
|
with gr.Blocks(title="νμ¬μΌμ μμ½ μΊλ¦°λ") as demo: |
|
gr.Markdown("## π« νμ¬μΌμ μ‘°ν λ° AI μμ½ (google/gemma-2-2b-it)") |
|
with gr.Row(): |
|
region = gr.Dropdown(label="μ§μ κ΅μ‘μ²", choices=list(region_options.keys()), value="B10", interactive=True) |
|
school_name = gr.Textbox(label="νκ΅λͺ
", placeholder="μ: μ리μ΄λ±νκ΅") |
|
with gr.Row(): |
|
year = gr.Dropdown(label="λ
λ", choices=[2022, 2023, 2024, 2025], value=2024) |
|
month = gr.Slider(label="μ", minimum=1, maximum=12, value=7, step=1) |
|
|
|
btn = gr.Button("π
μΌμ λΆλ¬μ€κΈ° λ° μμ½") |
|
|
|
status = gr.Markdown() |
|
month_output = gr.Textbox(label="π μ νν μμ μΌμ ", lines=8) |
|
summary_output = gr.Textbox(label="π 1λ
νμ¬μΌμ μμ½ (Gemma)", lines=6) |
|
|
|
btn.click(fn=load_and_summarize, inputs=[region, school_name, year, month], outputs=[status, month_output, summary_output]) |
|
|
|
demo.launch() |
|
|