yongyeol commited on
Commit
af72a8e
Β·
verified Β·
1 Parent(s): b1f021b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -0
app.py CHANGED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+ import gradio as gr
4
+ from datetime import datetime
5
+ from llama_cpp import Llama # GPT4ALL용
6
+
7
+ # ───────────────────────── λͺ¨λΈ λ‘œλ”© ─────────────────────────
8
+ llm = Llama(model_path="./ggjt-model.bin")
9
+
10
+ # ───────────────────────── NEIS 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)
14
+ data = res.json()
15
+ school = data.get("schoolInfo", [{}])[1].get("row", [{}])[0]
16
+ return school.get("SD_SCHUL_CODE"), school.get("ATPT_OFCDC_SC_CODE")
17
+
18
+ def get_schedule(region_code, school_code, year, month, api_key):
19
+ from_ymd = f"{year}{month:02}01"
20
+ to_ymd = f"{year}{month:02}31"
21
+ 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}"
22
+ res = requests.get(url)
23
+ data = res.json()
24
+ return data.get("SchoolSchedule", [{}])[1].get("row", [])
25
+
26
+ # ───────────────────────── GPT4ALL 응닡 생성 ─────────────────────────
27
+ def generate_gpt_answer(user_question, events):
28
+ context = "\n".join([f"{e['EVENT_NM']} : {e['AA_YMD']}" for e in events])
29
+ prompt = f"""
30
+ ### Instruction:
31
+ λ‹€μŒμ€ μƒλ¦¬μ΄ˆλ“±ν•™κ΅μ˜ 2024λ…„ 7μ›” ν•™μ‚¬μΌμ •μž…λ‹ˆλ‹€:
32
+
33
+ {context}
34
+
35
+ 질문: "{user_question}"
36
+
37
+ μœ„ 학사일정 μ •λ³΄λ§Œ μ°Έκ³ ν•΄μ„œ μ§ˆλ¬Έμ— ν•΄λ‹Ήν•˜λŠ” λ‚ μ§œλ₯Ό μ°Ύμ•„, μžμ—°μŠ€λŸ½κ³  κ°„κ²°ν•œ ν•œκ΅­μ–΄ λ¬Έμž₯으둜 λ‹΅ν•΄μ£Όμ„Έμš”. 예: "2024λ…„ 7μ›” 24μΌμž…λ‹ˆλ‹€."
38
+ ### Response:
39
+ """
40
+
41
+ result = ""
42
+ for output in llm(prompt, stop=["###"], stream=True):
43
+ result += output["choices"][0]["text"]
44
+ return result.strip()
45
+
46
+ # ───────────────────────── 전체 처리 ν•¨μˆ˜ ─────────────────────────
47
+ def answer_question(user_question):
48
+ api_key = os.environ.get("NEIS_API_KEY", "a69e08342c8947b4a52cd72789a5ecaf")
49
+ school_name = "μƒλ¦¬μ΄ˆλ“±ν•™κ΅"
50
+ region_code = "R10"
51
+ year = 2024
52
+ month = 7
53
+
54
+ school_code, region_code = get_school_info(region_code, school_name, api_key)
55
+ events = get_schedule(region_code, school_code, year, month, api_key)
56
+
57
+ if not events:
58
+ return "학사일정 정보λ₯Ό λΆˆλŸ¬μ˜€μ§€ λͺ»ν–ˆμŠ΅λ‹ˆλ‹€."
59
+
60
+ return generate_gpt_answer(user_question, events)
61
+
62
+ # ───────────────────────── Gradio μΈν„°νŽ˜μ΄μŠ€ ─────────────────────────
63
+ description = "πŸ“… μƒλ¦¬μ΄ˆλ“±ν•™κ΅ 2024λ…„ 7μ›” 학사일정 기반 GPT μ±—λ΄‡μž…λ‹ˆλ‹€. 예: '여름방학은 μ–Έμ œμ•Ό?'"
64
+
65
+ gr.Interface(fn=answer_question,
66
+ inputs=gr.Textbox(lines=2, placeholder="예: 여름방학식은 μ–Έμ œμ•Ό?", label="질문 μž…λ ₯"),
67
+ outputs=gr.Textbox(label="λ‹΅λ³€"),
68
+ title="πŸŽ“ GPT4ALL 학사일정 Q&A",
69
+ description=description).launch()