Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +38 -22
src/streamlit_app.py
CHANGED
@@ -3,9 +3,10 @@ import json
|
|
3 |
import requests
|
4 |
import streamlit as st
|
5 |
from datetime import datetime
|
6 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
|
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"
|
@@ -15,23 +16,25 @@ 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 = "
|
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(
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
-
|
35 |
|
36 |
# β
νκ΅ μ 보 κ°μ Έμ€κΈ°
|
37 |
def get_school_info(region_code, school_name, api_key):
|
@@ -41,7 +44,7 @@ def get_school_info(region_code, school_name, api_key):
|
|
41 |
school = data.get("schoolInfo", [{}])[1].get("row", [{}])[0]
|
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"
|
@@ -49,13 +52,13 @@ def get_schedule(region_code, school_code, year, month, api_key):
|
|
49 |
res = requests.get(url)
|
50 |
data = res.json()
|
51 |
rows = data.get("SchoolSchedule", [{}])[1].get("row", [])
|
52 |
-
st.write("π¦ λΆλ¬μ¨ μΌμ raw data:", rows)
|
53 |
return rows
|
54 |
|
55 |
# β
μμ½ μμ±
|
56 |
def summarize_schedule(rows, school_name, year):
|
57 |
if not rows:
|
58 |
return "μΌμ μ΄ μμ΄ μμ½ν μ μμ΅λλ€."
|
|
|
59 |
lines = []
|
60 |
for row in rows:
|
61 |
date = row["AA_YMD"]
|
@@ -63,11 +66,26 @@ def summarize_schedule(rows, school_name, year):
|
|
63 |
event = row["EVENT_NM"]
|
64 |
lines.append(f"{dt}: {event}")
|
65 |
text = "\n".join(lines)
|
|
|
66 |
prompt = f"{school_name}κ° {year}λ
λμ κ°μ§λ νμ¬μΌμ μ λ€μκ³Ό κ°μ΅λλ€:\n{text}\nμ£Όμ μΌμ μ μμ½ν΄μ£ΌμΈμ."
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
# β
μ§μ/νκ΅/λ
λ/μ μ ν UI
|
73 |
region_options = {
|
@@ -83,6 +101,7 @@ with st.form("query_form"):
|
|
83 |
month = st.selectbox("μ", options=list(range(1, 13)), index=6)
|
84 |
submitted = st.form_submit_button("π
νμ¬μΌμ λΆλ¬μ€κΈ°")
|
85 |
|
|
|
86 |
if submitted:
|
87 |
with st.spinner("μΌμ λΆλ¬μ€λ μ€..."):
|
88 |
api_key = os.environ.get("NEIS_API_KEY", "a69e08342c8947b4a52cd72789a5ecaf")
|
@@ -94,7 +113,6 @@ if submitted:
|
|
94 |
if not schedule_rows:
|
95 |
st.info("ν΄λΉ 쑰건μ νμ¬μΌμ μ΄ μμ΅λλ€.")
|
96 |
else:
|
97 |
-
# β
μΌμ μΆλ ₯μ© FullCalendar μμ±
|
98 |
events = [
|
99 |
{
|
100 |
"title": row["EVENT_NM"],
|
@@ -103,7 +121,6 @@ if submitted:
|
|
103 |
for row in schedule_rows
|
104 |
if "AA_YMD" in row and "EVENT_NM" in row
|
105 |
]
|
106 |
-
st.write("π
FullCalendarμ μ λ¬ν events:", events)
|
107 |
event_json = json.dumps(events, ensure_ascii=False)
|
108 |
|
109 |
st.components.v1.html(f"""
|
@@ -130,10 +147,9 @@ if submitted:
|
|
130 |
</html>
|
131 |
""", height=650)
|
132 |
|
133 |
-
# β
μμ½ μμ± λ²νΌ μΆκ°
|
134 |
with st.expander("β¨ 1λ
μΉ μμ½ λ³΄κΈ°", expanded=False):
|
135 |
if st.button("π€ μμ½ μμ±νκΈ°"):
|
136 |
-
with st.spinner("
|
137 |
summary = summarize_schedule(schedule_rows, school_name, year)
|
138 |
st.success("μμ½ μλ£!")
|
139 |
st.markdown(f"**{school_name} {year}λ
{month}μ μΌμ μμ½:**\n\n{summary}")
|
|
|
3 |
import requests
|
4 |
import streamlit as st
|
5 |
from datetime import datetime
|
6 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
7 |
+
import torch
|
8 |
|
9 |
+
# β
μμ ν μΊμ κ²½λ‘ μ€μ
|
10 |
os.environ["TRANSFORMERS_CACHE"] = "/tmp/hf_cache"
|
11 |
os.environ["HF_HOME"] = "/tmp/hf_cache"
|
12 |
os.environ["HF_DATASETS_CACHE"] = "/tmp/hf_cache"
|
|
|
16 |
st.title("π
νμ¬μΌμ μΊλ¦°λ + AI μμ½")
|
17 |
st.markdown("NEIS APIμμ νμ¬μΌμ μ λΆλ¬μ€κ³ FullCalendarλ‘ μκ°νν©λλ€.")
|
18 |
|
19 |
+
# β
λͺ¨λΈ λ‘λ© ν¨μ (skt/A.X-4.0-Light)
|
|
|
|
|
|
|
|
|
|
|
20 |
@st.cache_resource
|
21 |
def load_model():
|
22 |
token = os.environ.get("HUGGINGFACE_TOKEN")
|
23 |
+
model_id = "skt/A.X-4.0-Light"
|
24 |
cache_dir = "/tmp/hf_cache"
|
25 |
|
26 |
tokenizer = AutoTokenizer.from_pretrained(model_id, use_auth_token=token, cache_dir=cache_dir)
|
27 |
+
model = AutoModelForCausalLM.from_pretrained(
|
28 |
+
model_id,
|
29 |
+
use_auth_token=token,
|
30 |
+
torch_dtype=torch.bfloat16,
|
31 |
+
device_map="auto",
|
32 |
+
cache_dir=cache_dir
|
33 |
+
)
|
34 |
+
model.eval()
|
35 |
+
return tokenizer, model
|
36 |
|
37 |
+
tokenizer, model = load_model()
|
38 |
|
39 |
# β
νκ΅ μ 보 κ°μ Έμ€κΈ°
|
40 |
def get_school_info(region_code, school_name, api_key):
|
|
|
44 |
school = data.get("schoolInfo", [{}])[1].get("row", [{}])[0]
|
45 |
return school.get("SD_SCHUL_CODE"), school.get("ATPT_OFCDC_SC_CODE")
|
46 |
|
47 |
+
# β
νμ¬μΌμ κ°μ Έμ€κΈ°
|
48 |
def get_schedule(region_code, school_code, year, month, api_key):
|
49 |
from_ymd = f"{year}{month:02}01"
|
50 |
to_ymd = f"{year}{month:02}31"
|
|
|
52 |
res = requests.get(url)
|
53 |
data = res.json()
|
54 |
rows = data.get("SchoolSchedule", [{}])[1].get("row", [])
|
|
|
55 |
return rows
|
56 |
|
57 |
# β
μμ½ μμ±
|
58 |
def summarize_schedule(rows, school_name, year):
|
59 |
if not rows:
|
60 |
return "μΌμ μ΄ μμ΄ μμ½ν μ μμ΅λλ€."
|
61 |
+
|
62 |
lines = []
|
63 |
for row in rows:
|
64 |
date = row["AA_YMD"]
|
|
|
66 |
event = row["EVENT_NM"]
|
67 |
lines.append(f"{dt}: {event}")
|
68 |
text = "\n".join(lines)
|
69 |
+
|
70 |
prompt = f"{school_name}κ° {year}λ
λμ κ°μ§λ νμ¬μΌμ μ λ€μκ³Ό κ°μ΅λλ€:\n{text}\nμ£Όμ μΌμ μ μμ½ν΄μ£ΌμΈμ."
|
71 |
+
|
72 |
+
messages = [
|
73 |
+
{"role": "system", "content": "λΉμ μ νμ¬μΌμ μ μμ½ν΄μ£Όλ AIμ
λλ€."},
|
74 |
+
{"role": "user", "content": prompt}
|
75 |
+
]
|
76 |
+
|
77 |
+
input_ids = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
|
78 |
+
|
79 |
+
with torch.no_grad():
|
80 |
+
output = model.generate(
|
81 |
+
input_ids,
|
82 |
+
max_new_tokens=256,
|
83 |
+
do_sample=False,
|
84 |
+
)
|
85 |
+
|
86 |
+
len_prompt = input_ids.shape[1]
|
87 |
+
response = tokenizer.decode(output[0][len_prompt:], skip_special_tokens=True).strip()
|
88 |
+
return response
|
89 |
|
90 |
# β
μ§μ/νκ΅/λ
λ/μ μ ν UI
|
91 |
region_options = {
|
|
|
101 |
month = st.selectbox("μ", options=list(range(1, 13)), index=6)
|
102 |
submitted = st.form_submit_button("π
νμ¬μΌμ λΆλ¬μ€κΈ°")
|
103 |
|
104 |
+
# β
μ μΆ μ²λ¦¬
|
105 |
if submitted:
|
106 |
with st.spinner("μΌμ λΆλ¬μ€λ μ€..."):
|
107 |
api_key = os.environ.get("NEIS_API_KEY", "a69e08342c8947b4a52cd72789a5ecaf")
|
|
|
113 |
if not schedule_rows:
|
114 |
st.info("ν΄λΉ 쑰건μ νμ¬μΌμ μ΄ μμ΅λλ€.")
|
115 |
else:
|
|
|
116 |
events = [
|
117 |
{
|
118 |
"title": row["EVENT_NM"],
|
|
|
121 |
for row in schedule_rows
|
122 |
if "AA_YMD" in row and "EVENT_NM" in row
|
123 |
]
|
|
|
124 |
event_json = json.dumps(events, ensure_ascii=False)
|
125 |
|
126 |
st.components.v1.html(f"""
|
|
|
147 |
</html>
|
148 |
""", height=650)
|
149 |
|
|
|
150 |
with st.expander("β¨ 1λ
μΉ μμ½ λ³΄κΈ°", expanded=False):
|
151 |
if st.button("π€ μμ½ μμ±νκΈ°"):
|
152 |
+
with st.spinner("λͺ¨λΈμ΄ μμ½ μ€..."):
|
153 |
summary = summarize_schedule(schedule_rows, school_name, year)
|
154 |
st.success("μμ½ μλ£!")
|
155 |
st.markdown(f"**{school_name} {year}λ
{month}μ μΌμ μμ½:**\n\n{summary}")
|