Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +23 -31
src/streamlit_app.py
CHANGED
@@ -1,43 +1,34 @@
|
|
1 |
import os
|
2 |
-
os.environ["TRANSFORMERS_CACHE"] = "/tmp/hf_cache"
|
3 |
-
os.environ["HF_HOME"] = "/tmp/hf_cache"
|
4 |
-
os.environ["HF_DATASETS_CACHE"] = "/tmp/hf_cache"
|
5 |
import json
|
6 |
import requests
|
7 |
import streamlit as st
|
8 |
from datetime import datetime
|
9 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
10 |
|
11 |
-
|
|
|
|
|
|
|
12 |
|
13 |
# โ
Streamlit ๊ธฐ๋ณธ ์ค์
|
14 |
st.set_page_config(page_title="ํ์ฌ์ผ์ ์บ๋ฆฐ๋", layout="centered")
|
15 |
st.title("๐
ํ์ฌ์ผ์ ์บ๋ฆฐ๋ + AI ์์ฝ")
|
16 |
st.markdown("NEIS API์์ ํ์ฌ์ผ์ ์ ๋ถ๋ฌ์ค๊ณ FullCalendar๋ก ์๊ฐํํฉ๋๋ค.")
|
17 |
|
18 |
-
# โ
|
|
|
|
|
19 |
st.write("โ
์บ์ ๊ฒฝ๋ก:", os.environ.get("TRANSFORMERS_CACHE"))
|
20 |
-
st.write("๐ ํ ํฐ ์์:", os.environ.get("HUGGINGFACE_TOKEN") is not None)
|
21 |
|
22 |
# โ
Gemma ๋ชจ๋ธ ๋ก๋ฉ ํจ์
|
23 |
@st.cache_resource
|
24 |
def load_model():
|
25 |
token = os.environ.get("HUGGINGFACE_TOKEN")
|
26 |
model_id = "google/gemma-2-2b-it"
|
27 |
-
cache_dir = "/tmp/hf_cache"
|
28 |
-
|
29 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
30 |
-
model_id,
|
31 |
-
use_auth_token=token,
|
32 |
-
cache_dir=cache_dir
|
33 |
-
)
|
34 |
-
|
35 |
-
model = AutoModelForCausalLM.from_pretrained(
|
36 |
-
model_id,
|
37 |
-
use_auth_token=token,
|
38 |
-
cache_dir=cache_dir
|
39 |
-
)
|
40 |
|
|
|
|
|
41 |
return pipeline("text-generation", model=model, tokenizer=tokenizer)
|
42 |
|
43 |
llm = load_model()
|
@@ -51,17 +42,19 @@ def get_school_info(region_code, school_name, api_key):
|
|
51 |
return school.get("SD_SCHUL_CODE"), school.get("ATPT_OFCDC_SC_CODE")
|
52 |
|
53 |
# โ
ํ์ฌ์ผ์ ๊ฐ์ ธ์ค๊ธฐ
|
54 |
-
def get_schedule(region_code, school_code, year, api_key):
|
55 |
-
from_ymd = f"{year}
|
56 |
-
to_ymd = f"{year}
|
57 |
-
url = f"https://open.neis.go.kr/hub/SchoolSchedule?KEY={api_key}&Type=json&pIndex=1&pSize=
|
58 |
res = requests.get(url)
|
59 |
data = res.json()
|
60 |
rows = data.get("SchoolSchedule", [{}])[1].get("row", [])
|
61 |
return rows
|
62 |
|
63 |
-
# โ
|
64 |
def summarize_schedule(rows, school_name, year):
|
|
|
|
|
65 |
lines = []
|
66 |
for row in rows:
|
67 |
date = row["AA_YMD"]
|
@@ -73,7 +66,7 @@ def summarize_schedule(rows, school_name, year):
|
|
73 |
result = llm([{"role": "user", "content": prompt}])
|
74 |
return result[0]["generated_text"].replace(prompt, "").strip()
|
75 |
|
76 |
-
# โ
|
77 |
region_options = {
|
78 |
"B10": "์์ธ", "C10": "๋ถ์ฐ", "D10": "๋๊ตฌ", "E10": "์ธ์ฒ", "F10": "๊ด์ฃผ", "G10": "๋์ ",
|
79 |
"H10": "์ธ์ฐ", "I10": "์ธ์ข
", "J10": "๊ฒฝ๊ธฐ", "K10": "๊ฐ์", "M10": "์ถฉ๋ถ", "N10": "์ถฉ๋จ",
|
@@ -84,9 +77,9 @@ with st.form("query_form"):
|
|
84 |
region = st.selectbox("์ง์ญ ๊ต์ก์ฒญ", options=list(region_options.keys()), format_func=lambda x: f"{region_options[x]} ({x})")
|
85 |
school_name = st.text_input("ํ๊ต๋ช
", placeholder="์: ์๋ฆฌ์ด๋ฑํ๊ต")
|
86 |
year = st.selectbox("๋
๋", options=[2022, 2023, 2024, 2025], index=2)
|
|
|
87 |
submitted = st.form_submit_button("๐
ํ์ฌ์ผ์ ๋ถ๋ฌ์ค๊ธฐ")
|
88 |
|
89 |
-
# โ
์ ์ถ ์ ์ฒ๋ฆฌ
|
90 |
if submitted:
|
91 |
with st.spinner("์ผ์ ๋ถ๋ฌ์ค๋ ์ค..."):
|
92 |
api_key = os.environ.get("NEIS_API_KEY", "a69e08342c8947b4a52cd72789a5ecaf")
|
@@ -94,11 +87,11 @@ if submitted:
|
|
94 |
if not school_code:
|
95 |
st.error("ํ๊ต ์ ๋ณด๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.")
|
96 |
else:
|
97 |
-
schedule_rows = get_schedule(region_code, school_code, year, api_key)
|
98 |
if not schedule_rows:
|
99 |
st.info("ํด๋น ์กฐ๊ฑด์ ํ์ฌ์ผ์ ์ด ์์ต๋๋ค.")
|
100 |
else:
|
101 |
-
#
|
102 |
events = [
|
103 |
{
|
104 |
"title": row["EVENT_NM"],
|
@@ -108,7 +101,6 @@ if submitted:
|
|
108 |
]
|
109 |
event_json = json.dumps(events, ensure_ascii=False)
|
110 |
|
111 |
-
# FullCalendar ์ฝ์
|
112 |
st.components.v1.html(f"""
|
113 |
<html>
|
114 |
<head>
|
@@ -133,10 +125,10 @@ if submitted:
|
|
133 |
</html>
|
134 |
""", height=650)
|
135 |
|
136 |
-
# ์์ฝ ๋ฒํผ
|
137 |
with st.expander("โจ 1๋
์น ์์ฝ ๋ณด๊ธฐ", expanded=False):
|
138 |
if st.button("๐ค ์์ฝ ์์ฑํ๊ธฐ"):
|
139 |
with st.spinner("Gemma ๋ชจ๋ธ์ด ์์ฝ ์ค..."):
|
140 |
summary = summarize_schedule(schedule_rows, school_name, year)
|
141 |
st.success("์์ฝ ์๋ฃ!")
|
142 |
-
st.markdown(f"**{school_name} {year}๋
|
|
|
1 |
import os
|
|
|
|
|
|
|
2 |
import json
|
3 |
import requests
|
4 |
import streamlit as st
|
5 |
from datetime import datetime
|
6 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
7 |
|
8 |
+
# โ
์์ ํ ์บ์ ๊ฒฝ๋ก ์ค์ (์ต์๋จ ํ์)
|
9 |
+
os.environ["TRANSFORMERS_CACHE"] = "/tmp/hf_cache"
|
10 |
+
os.environ["HF_HOME"] = "/tmp/hf_cache"
|
11 |
+
os.environ["HF_DATASETS_CACHE"] = "/tmp/hf_cache"
|
12 |
|
13 |
# โ
Streamlit ๊ธฐ๋ณธ ์ค์
|
14 |
st.set_page_config(page_title="ํ์ฌ์ผ์ ์บ๋ฆฐ๋", layout="centered")
|
15 |
st.title("๐
ํ์ฌ์ผ์ ์บ๋ฆฐ๋ + AI ์์ฝ")
|
16 |
st.markdown("NEIS API์์ ํ์ฌ์ผ์ ์ ๋ถ๋ฌ์ค๊ณ FullCalendar๋ก ์๊ฐํํฉ๋๋ค.")
|
17 |
|
18 |
+
# โ
๋๋ฒ๊น
์ถ๋ ฅ
|
19 |
+
token_present = os.environ.get("HUGGINGFACE_TOKEN") is not None
|
20 |
+
st.write("๐ ํ ํฐ ์์:", token_present)
|
21 |
st.write("โ
์บ์ ๊ฒฝ๋ก:", os.environ.get("TRANSFORMERS_CACHE"))
|
|
|
22 |
|
23 |
# โ
Gemma ๋ชจ๋ธ ๋ก๋ฉ ํจ์
|
24 |
@st.cache_resource
|
25 |
def load_model():
|
26 |
token = os.environ.get("HUGGINGFACE_TOKEN")
|
27 |
model_id = "google/gemma-2-2b-it"
|
28 |
+
cache_dir = "/tmp/hf_cache"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, use_auth_token=token, cache_dir=cache_dir)
|
31 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, use_auth_token=token, cache_dir=cache_dir)
|
32 |
return pipeline("text-generation", model=model, tokenizer=tokenizer)
|
33 |
|
34 |
llm = load_model()
|
|
|
42 |
return school.get("SD_SCHUL_CODE"), school.get("ATPT_OFCDC_SC_CODE")
|
43 |
|
44 |
# โ
ํ์ฌ์ผ์ ๊ฐ์ ธ์ค๊ธฐ
|
45 |
+
def get_schedule(region_code, school_code, year, month, api_key):
|
46 |
+
from_ymd = f"{year}{month:02}01"
|
47 |
+
to_ymd = f"{year}{month:02}31"
|
48 |
+
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}"
|
49 |
res = requests.get(url)
|
50 |
data = res.json()
|
51 |
rows = data.get("SchoolSchedule", [{}])[1].get("row", [])
|
52 |
return rows
|
53 |
|
54 |
+
# โ
์์ฝ ์์ฑ
|
55 |
def summarize_schedule(rows, school_name, year):
|
56 |
+
if not rows:
|
57 |
+
return "์ผ์ ์ด ์์ด ์์ฝํ ์ ์์ต๋๋ค."
|
58 |
lines = []
|
59 |
for row in rows:
|
60 |
date = row["AA_YMD"]
|
|
|
66 |
result = llm([{"role": "user", "content": prompt}])
|
67 |
return result[0]["generated_text"].replace(prompt, "").strip()
|
68 |
|
69 |
+
# โ
์ง์ญ/ํ๊ต/๋
๋/์ ์ ํ UI
|
70 |
region_options = {
|
71 |
"B10": "์์ธ", "C10": "๋ถ์ฐ", "D10": "๋๊ตฌ", "E10": "์ธ์ฒ", "F10": "๊ด์ฃผ", "G10": "๋์ ",
|
72 |
"H10": "์ธ์ฐ", "I10": "์ธ์ข
", "J10": "๊ฒฝ๊ธฐ", "K10": "๊ฐ์", "M10": "์ถฉ๋ถ", "N10": "์ถฉ๋จ",
|
|
|
77 |
region = st.selectbox("์ง์ญ ๊ต์ก์ฒญ", options=list(region_options.keys()), format_func=lambda x: f"{region_options[x]} ({x})")
|
78 |
school_name = st.text_input("ํ๊ต๋ช
", placeholder="์: ์๋ฆฌ์ด๋ฑํ๊ต")
|
79 |
year = st.selectbox("๋
๋", options=[2022, 2023, 2024, 2025], index=2)
|
80 |
+
month = st.selectbox("์", options=list(range(1, 13)), index=6)
|
81 |
submitted = st.form_submit_button("๐
ํ์ฌ์ผ์ ๋ถ๋ฌ์ค๊ธฐ")
|
82 |
|
|
|
83 |
if submitted:
|
84 |
with st.spinner("์ผ์ ๋ถ๋ฌ์ค๋ ์ค..."):
|
85 |
api_key = os.environ.get("NEIS_API_KEY", "a69e08342c8947b4a52cd72789a5ecaf")
|
|
|
87 |
if not school_code:
|
88 |
st.error("ํ๊ต ์ ๋ณด๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.")
|
89 |
else:
|
90 |
+
schedule_rows = get_schedule(region_code, school_code, year, month, api_key)
|
91 |
if not schedule_rows:
|
92 |
st.info("ํด๋น ์กฐ๊ฑด์ ํ์ฌ์ผ์ ์ด ์์ต๋๋ค.")
|
93 |
else:
|
94 |
+
# โ
์ผ์ ์ถ๋ ฅ์ฉ FullCalendar ์์ฑ
|
95 |
events = [
|
96 |
{
|
97 |
"title": row["EVENT_NM"],
|
|
|
101 |
]
|
102 |
event_json = json.dumps(events, ensure_ascii=False)
|
103 |
|
|
|
104 |
st.components.v1.html(f"""
|
105 |
<html>
|
106 |
<head>
|
|
|
125 |
</html>
|
126 |
""", height=650)
|
127 |
|
128 |
+
# โ
์์ฝ ์์ฑ ๋ฒํผ ์ถ๊ฐ
|
129 |
with st.expander("โจ 1๋
์น ์์ฝ ๋ณด๊ธฐ", expanded=False):
|
130 |
if st.button("๐ค ์์ฝ ์์ฑํ๊ธฐ"):
|
131 |
with st.spinner("Gemma ๋ชจ๋ธ์ด ์์ฝ ์ค..."):
|
132 |
summary = summarize_schedule(schedule_rows, school_name, year)
|
133 |
st.success("์์ฝ ์๋ฃ!")
|
134 |
+
st.markdown(f"**{school_name} {year}๋
{month}์ ์ผ์ ์์ฝ:**\n\n{summary}")
|