Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
+
import gradio as gr
|
3 |
+
import requests
|
4 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
5 |
+
|
6 |
+
# Load KoAlpaca model
|
7 |
+
model_id = "beomi/KoAlpaca-Polyglot-5.8B"
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
9 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto")
|
10 |
+
generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
11 |
+
|
12 |
+
NEIS_KEY = "a69e08342c8947b4a52cd72789a5ecaf"
|
13 |
+
SCHOOL_INFO_URL = "https://open.neis.go.kr/hub/schoolInfo"
|
14 |
+
SCHEDULE_URL = "https://open.neis.go.kr/hub/SchoolSchedule"
|
15 |
+
|
16 |
+
REGIONS = {
|
17 |
+
"μμΈνΉλ³μκ΅μ‘μ²": "B10",
|
18 |
+
"κ²½μλΆλκ΅μ‘μ²": "R10"
|
19 |
+
}
|
20 |
+
|
21 |
+
MONTH_NAMES = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"]
|
22 |
+
|
23 |
+
|
24 |
+
def get_school_code(region_code, school_name):
|
25 |
+
params = {
|
26 |
+
"KEY": NEIS_KEY,
|
27 |
+
"Type": "json",
|
28 |
+
"pIndex": 1,
|
29 |
+
"pSize": 1,
|
30 |
+
"SCHUL_NM": school_name,
|
31 |
+
"ATPT_OFCDC_SC_CODE": region_code
|
32 |
+
}
|
33 |
+
res = requests.get(SCHOOL_INFO_URL, params=params)
|
34 |
+
data = res.json()
|
35 |
+
try:
|
36 |
+
return data["schoolInfo"][1]["row"][0]["SD_SCHUL_CODE"], data["schoolInfo"][1]["row"][0]["ATPT_OFCDC_SC_CODE"]
|
37 |
+
except:
|
38 |
+
return None, None
|
39 |
+
|
40 |
+
|
41 |
+
def get_schedule(region_code, school_code, year, month):
|
42 |
+
from_ymd = f"{year}{month}01"
|
43 |
+
to_ymd = f"{year}{month}31"
|
44 |
+
params = {
|
45 |
+
"KEY": NEIS_KEY,
|
46 |
+
"Type": "json",
|
47 |
+
"pIndex": 1,
|
48 |
+
"pSize": 100,
|
49 |
+
"ATPT_OFCDC_SC_CODE": region_code,
|
50 |
+
"SD_SCHUL_CODE": school_code,
|
51 |
+
"AA_FROM_YMD": from_ymd,
|
52 |
+
"AA_TO_YMD": to_ymd
|
53 |
+
}
|
54 |
+
res = requests.get(SCHEDULE_URL, params=params)
|
55 |
+
data = res.json()
|
56 |
+
try:
|
57 |
+
rows = data["SchoolSchedule"][1]["row"]
|
58 |
+
return rows
|
59 |
+
except:
|
60 |
+
return []
|
61 |
+
|
62 |
+
|
63 |
+
def generate_answer(region, school_name, year, month, question):
|
64 |
+
region_code = REGIONS.get(region)
|
65 |
+
if not region_code:
|
66 |
+
return "μλͺ»λ κ΅μ‘μ²μ
λλ€."
|
67 |
+
|
68 |
+
school_code, confirmed_region = get_school_code(region_code, school_name)
|
69 |
+
if not school_code:
|
70 |
+
return "νκ΅ μ 보λ₯Ό μ°Ύμ μ μμ΅λλ€."
|
71 |
+
|
72 |
+
schedule_rows = get_schedule(confirmed_region, school_code, year, month)
|
73 |
+
if not schedule_rows:
|
74 |
+
schedule_text = "νμ¬ μΌμ μ 보λ μμ΅λλ€."
|
75 |
+
else:
|
76 |
+
schedule_text = "\n".join(f"{row['AA_YMD']}: {row['EVENT_NM']}" for row in schedule_rows)
|
77 |
+
|
78 |
+
prompt = f"""μΌμ μ 보:
|
79 |
+
{schedule_text}
|
80 |
+
|
81 |
+
μ¬μ©μ μ§λ¬Έ: {question}
|
82 |
+
|
83 |
+
μμ°μ€λ½κ² λλ΅νμΈμ."""
|
84 |
+
|
85 |
+
result = generator(prompt, max_new_tokens=200, temperature=0.7)[0]["generated_text"]
|
86 |
+
return result
|
87 |
+
|
88 |
+
|
89 |
+
def interface_fn(region, school_name, year, month, question):
|
90 |
+
return generate_answer(region, school_name, year, month, question)
|
91 |
+
|
92 |
+
|
93 |
+
with gr.Interface(
|
94 |
+
fn=interface_fn,
|
95 |
+
inputs=[
|
96 |
+
gr.Dropdown(choices=list(REGIONS.keys()), label="κ΅μ‘μ² μ ν"),
|
97 |
+
gr.Textbox(label="νκ΅λͺ
μ
λ ₯"),
|
98 |
+
gr.Textbox(label="λ
λ μ
λ ₯", placeholder="μ: 2025"),
|
99 |
+
gr.Dropdown(choices=MONTH_NAMES, label="μ μ ν (μ: 07)"),
|
100 |
+
gr.Textbox(label="GPT μ§λ¬Έ μ
λ ₯")
|
101 |
+
],
|
102 |
+
outputs=gr.Textbox(label="GPTμ μλ΅"),
|
103 |
+
title="νμ¬μΌμ + GPT μ±λ΄ (KoAlpaca)"
|
104 |
+
) as app:
|
105 |
+
app.launch()
|