yongyeol commited on
Commit
cdf21bf
Β·
verified Β·
1 Parent(s): d636c24

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -21
app.py CHANGED
@@ -4,10 +4,10 @@ import gradio as gr
4
  from datetime import datetime
5
  from transformers import pipeline
6
 
7
- # ────────────────────────────── λͺ¨λΈ 뢈러였기 ──────────────────────────────
8
  llm = pipeline("text-generation", model="google/gemma-2-2b-it", max_new_tokens=512)
9
 
10
- # ────────────────────────────── API 처리 ν•¨μˆ˜ ──────────────────────────────
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)
@@ -36,20 +36,27 @@ def summarize_schedule(rows, school_name, year):
36
  result = llm([{"role": "user", "content": prompt}])
37
  return result[0]["generated_text"].replace(prompt, "").strip()
38
 
39
- # ────────────────────────────── κ·ΈλΌλ””μ˜€ μΈν„°νŽ˜μ΄μŠ€ ν•¨μˆ˜ ──────────────────────────────
40
- def load_and_summarize(region_code, school_name, year, month):
 
 
 
 
 
 
 
41
  api_key = os.environ.get("NEIS_API_KEY", "a69e08342c8947b4a52cd72789a5ecaf")
42
 
43
  if not school_name.strip():
44
- return "❗ 학ꡐλͺ…을 μž…λ ₯ν•΄μ£Όμ„Έμš”.", "", ""
45
 
46
  school_code, confirmed_region = get_school_info(region_code, school_name, api_key)
47
  if not school_code:
48
- return "❗ 학ꡐ 정보λ₯Ό 찾을 수 μ—†μŠ΅λ‹ˆλ‹€. μž…λ ₯을 ν™•μΈν•΄μ£Όμ„Έμš”.", "", ""
49
 
50
  schedule_rows = get_schedule(confirmed_region, school_code, year, api_key)
51
  if not schedule_rows:
52
- return "ℹ️ 학사일정 정보가 μ—†μŠ΅λ‹ˆλ‹€.", "", ""
53
 
54
  # 월별 일정 필터링
55
  month_rows = [r for r in schedule_rows if r["AA_YMD"].startswith(f"{year}{month:02}")]
@@ -58,20 +65,18 @@ def load_and_summarize(region_code, school_name, year, month):
58
  for r in month_rows
59
  ) or "πŸ“­ μ„ νƒν•œ 월에 ν•΄λ‹Ήν•˜λŠ” 일정이 μ—†μŠ΅λ‹ˆλ‹€."
60
 
61
- # μš”μ•½ 생성
62
- summary = summarize_schedule(schedule_rows, school_name, year)
63
 
64
- return f"βœ… {school_name}의 {year}λ…„ {month}μ›” μΌμ •μž…λ‹ˆλ‹€.", month_display, summary
65
-
66
- # ────────────────────────────── Gradio UI ꡬ성 ──────────────────────────────
67
- region_options = {
68
- "B10": "μ„œμšΈ", "C10": "λΆ€μ‚°", "D10": "λŒ€κ΅¬", "E10": "인천", "F10": "κ΄‘μ£Ό", "G10": "λŒ€μ „",
69
- "H10": "μšΈμ‚°", "I10": "μ„Έμ’…", "J10": "κ²½κΈ°", "K10": "강원", "M10": "좩뢁", "N10": "좩남",
70
- "P10": "전뢁", "Q10": "전남", "R10": "경뢁", "S10": "경남", "T10": "제주"
71
- }
72
 
 
73
  with gr.Blocks(title="학사일정 μš”μ•½ μΊ˜λ¦°λ”") as demo:
74
  gr.Markdown("## 🏫 학사일정 쑰회 및 AI μš”μ•½ (google/gemma-2-2b-it)")
 
75
  with gr.Row():
76
  region = gr.Dropdown(label="μ§€μ—­ ꡐ윑청", choices=list(region_options.keys()), value="B10", interactive=True)
77
  school_name = gr.Textbox(label="학ꡐλͺ…", placeholder="예: μƒλ¦¬μ΄ˆλ“±ν•™κ΅")
@@ -79,12 +84,23 @@ with gr.Blocks(title="학사일정 μš”μ•½ μΊ˜λ¦°λ”") as demo:
79
  year = gr.Dropdown(label="년도", choices=[2022, 2023, 2024, 2025], value=2024)
80
  month = gr.Slider(label="μ›”", minimum=1, maximum=12, value=7, step=1)
81
 
82
- btn = gr.Button("πŸ“… 일정 뢈러였기 및 μš”μ•½")
83
-
84
  status = gr.Markdown()
85
  month_output = gr.Textbox(label="πŸ“† μ„ νƒν•œ μ›”μ˜ 일정", lines=8)
86
- summary_output = gr.Textbox(label="πŸ“ 1λ…„ 학사일정 μš”μ•½ (Gemma)", lines=6)
 
 
 
 
 
 
 
 
 
 
87
 
88
- btn.click(fn=load_and_summarize, inputs=[region, school_name, year, month], outputs=[status, month_output, summary_output])
 
 
89
 
90
  demo.launch()
 
4
  from datetime import datetime
5
  from transformers import pipeline
6
 
7
+ # ──────── Gemma λͺ¨λΈ λ‘œλ”© (1회 μΊμ‹œ) ────────
8
  llm = pipeline("text-generation", model="google/gemma-2-2b-it", max_new_tokens=512)
9
 
10
+ # ──────── API ν•¨μˆ˜ μ •μ˜ ────────
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)
 
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
+ def fetch_month_schedule(region_code, school_name, year, month):
48
  api_key = os.environ.get("NEIS_API_KEY", "a69e08342c8947b4a52cd72789a5ecaf")
49
 
50
  if not school_name.strip():
51
+ return "❗ 학ꡐλͺ…을 μž…λ ₯ν•΄μ£Όμ„Έμš”.", "", "", None
52
 
53
  school_code, confirmed_region = get_school_info(region_code, school_name, api_key)
54
  if not school_code:
55
+ return "❗ 학ꡐ 정보λ₯Ό 찾을 수 μ—†μŠ΅λ‹ˆλ‹€.", "", "", None
56
 
57
  schedule_rows = get_schedule(confirmed_region, school_code, year, api_key)
58
  if not schedule_rows:
59
+ return "ℹ️ 학사일정 정보가 μ—†μŠ΅λ‹ˆλ‹€.", "", "", None
60
 
61
  # 월별 일정 필터링
62
  month_rows = [r for r in schedule_rows if r["AA_YMD"].startswith(f"{year}{month:02}")]
 
65
  for r in month_rows
66
  ) or "πŸ“­ μ„ νƒν•œ 월에 ν•΄λ‹Ήν•˜λŠ” 일정이 μ—†μŠ΅λ‹ˆλ‹€."
67
 
68
+ return f"βœ… {school_name}의 {year}λ…„ {month}μ›” μΌμ •μž…λ‹ˆλ‹€.", month_display, "✨ [μ•„λž˜ λ²„νŠΌμœΌλ‘œ μš”μ•½ 생성]", schedule_rows
 
69
 
70
+ # ──────── μš”μ•½ λ²„νŠΌμ΄ λˆŒλ Έμ„ λ•Œ μ‹€ν–‰λ˜λŠ” ν•¨μˆ˜ ────────
71
+ def generate_summary(school_name, year, rows):
72
+ if not rows:
73
+ return "⚠️ λ¨Όμ € 일정을 λΆˆλŸ¬μ™€ μ£Όμ„Έμš”."
74
+ return summarize_schedule(rows, school_name, year)
 
 
 
75
 
76
+ # ──────── Gradio μΈν„°νŽ˜μ΄μŠ€ ꡬ성 ────────
77
  with gr.Blocks(title="학사일정 μš”μ•½ μΊ˜λ¦°λ”") as demo:
78
  gr.Markdown("## 🏫 학사일정 쑰회 및 AI μš”μ•½ (google/gemma-2-2b-it)")
79
+
80
  with gr.Row():
81
  region = gr.Dropdown(label="μ§€μ—­ ꡐ윑청", choices=list(region_options.keys()), value="B10", interactive=True)
82
  school_name = gr.Textbox(label="학ꡐλͺ…", placeholder="예: μƒλ¦¬μ΄ˆλ“±ν•™κ΅")
 
84
  year = gr.Dropdown(label="년도", choices=[2022, 2023, 2024, 2025], value=2024)
85
  month = gr.Slider(label="μ›”", minimum=1, maximum=12, value=7, step=1)
86
 
87
+ load_btn = gr.Button("οΏ½οΏ½οΏ½ μ›” 일정 뢈러였기")
 
88
  status = gr.Markdown()
89
  month_output = gr.Textbox(label="πŸ“† μ„ νƒν•œ μ›”μ˜ 일정", lines=8)
90
+ summary_notice = gr.Markdown()
91
+ stored_rows = gr.State()
92
+
93
+ with gr.Accordion("✨ 1λ…„μΉ˜ AI μš”μ•½ 보기", open=False):
94
+ summarize_btn = gr.Button("πŸ€– μš”μ•½ μƒμ„±ν•˜κΈ°")
95
+ summary_output = gr.Textbox(label="πŸ“ 학사일정 μš”μ•½", lines=6)
96
+
97
+ # 이벀트 μ—°κ²°
98
+ load_btn.click(fn=fetch_month_schedule,
99
+ inputs=[region, school_name, year, month],
100
+ outputs=[status, month_output, summary_notice, stored_rows])
101
 
102
+ summarize_btn.click(fn=generate_summary,
103
+ inputs=[school_name, year, stored_rows],
104
+ outputs=summary_output)
105
 
106
  demo.launch()