-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
117 lines (105 loc) · 3.59 KB
/
app.py
File metadata and controls
117 lines (105 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# 파이썬 라이브러리
import json
from flask import Flask, jsonify, request, redirect
from apscheduler.schedulers.background import BackgroundScheduler
from flasgger import Swagger
# 모듈
from question_processing import load_data, select_best_question
from crawler import *
app = Flask(__name__)
scheduler = BackgroundScheduler()
swagger = Swagger(app)
# 사이트 1 크롤링 및 저장
def crawl_site1():
data = crawl_notices()
print(1)
with open('dataset\crawling\data_site1.json', 'w') as file:
json.dump(data, file)
# 사이트 2 크롤링 및 저장
def crawl_site2():
data = crawl_academic_schedule()
print(2)
with open('dataset\crawling\data_site2.json', 'w') as file:
json.dump(data, file)
# 사이트 3 크롤링 및 저장
def crawl_site3():
data = crawl_library_seats()
data = json.loads(data)
print(3)
with open('dataset\crawling\data_site3.json', 'w') as file:
json.dump(data['body']['SectorUsingList'], file)
# 사이트 4 크롤링 및 저장
def crawl_site4():
data = crawl_cafeteria_menu()
print(4)
with open('dataset\crawling\data_site4.json', 'w') as file:
json.dump(data, file)
# 스케줄러에 작업 추가
## 전체 공지: 1시간, 학사일정: 1일,
## 도서관 좌석: 10초, 학식 메뉴: 1주일(월)
scheduler.add_job(crawl_site1, 'interval', hours=1)
scheduler.add_job(crawl_site2, 'interval', days=1)
scheduler.add_job(crawl_site3, 'interval', seconds=10)
scheduler.add_job(crawl_site4, 'interval', weeks=1, start_date='2024-01-22')
scheduler.start()
@app.route('/')
def hello_world():
return 'Hello, World!'
# 전역 변수로 모델, 임베딩, 데이터프레임을 로드
model, embedding_question, df = load_data()
# URL에서 질문을 받아 처리하는 라우트
@app.route('/get-question/<question>', methods=['GET'])
def get_question(question):
"""
Q&A 시스템에서 가장 적합한 질문과 답변을 반환합니다.
---
parameters:
- name: question
in: path
type: string
required: true
description: 사용자의 질문
responses:
200:
description: 질문에 대한 응답
schema:
id: answer_response
properties:
answer:
type: string
description: 가장 적합한 답변
button_name:
type: string
description: 버튼 이름
link:
type: string
description: 관련 링크
question:
type: string
description: 처리된 질문
"""
print(question)
question, answer, link, button_name = select_best_question(question, model, embedding_question, df)
return jsonify({"answer": answer,
"button_name": button_name,
"link": link,
"question" : question})
# # 데이터를 반환하는 라우트
# @app.route('/get-data/<site>')
# def get_data(site):
# filename = f'dataset\crawling\data_site{site}.json'
# try:
# with open(filename, 'r') as file:
# data = json.load(file)
# return jsonify(data)
# except FileNotFoundError:
# return jsonify({"error": "Data not found"}), 404
if __name__ == '__main__':
# 깃에 올릴때는 지우기
app.debug = True
# 서버 시작 시 크롤링 바로 실행
crawl_site1()
crawl_site2()
crawl_site3()
crawl_site4()
app.run(host='0.0.0.0', port=5000)