-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
141 lines (124 loc) · 4.06 KB
/
app.py
File metadata and controls
141 lines (124 loc) · 4.06 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# app.py — Flask 서버 통합 버전
from flask import Flask, jsonify, send_from_directory, request
from flask_cors import CORS
import random, json, os
app = Flask(__name__, static_folder='.', static_url_path='')
CORS(app)
ANSWER_FILE = "answers.json"
# ======================
# 모범답 관리
# ======================
def generate_answers():
"""국어 정기고사용 랜덤 모범답 생성"""
data = {
"korean_regular": {
"multiple_choice": [random.randint(1, 5) for _ in range(30)],
"essay": {1: "주제", 2: "인물", 3: "배경", 4: "표현"}
}
}
with open(ANSWER_FILE, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=2)
return data
def load_answers():
"""모범답 불러오기 (없으면 생성)"""
if not os.path.exists(ANSWER_FILE):
return generate_answers()
with open(ANSWER_FILE, "r", encoding="utf-8") as f:
return json.load(f)
# ======================
# 메인 페이지 라우팅
# ======================
@app.route('/')
def index():
"""메인 HTML 페이지 반환"""
return send_from_directory('.', 'gache.html')
@app.route('/<path:path>')
def serve_static(path):
"""CSS, JS 등 정적 파일 제공"""
return send_from_directory('.', path)
# ======================
# 국어 정기고사 채점 API
# ======================
@app.route("/grade", methods=["POST"])
def grade():
"""모범답 파일 기반 국어 정기고사 채점"""
data = load_answers()
correct = data["korean_regular"]["multiple_choice"]
user_answers = request.json.get("answers", [])
essay_answers = request.json.get("essay_answers", {})
score = 0
detail = []
for i in range(30):
user = user_answers[i]
correct_ans = correct[i]
is_correct = (user == correct_ans)
if is_correct:
score += 2
detail.append({
"no": i + 1,
"correct": correct_ans,
"user": user,
"result": "✅" if is_correct else "❌"
})
essay_detail = []
for i in range(1, 5):
essay_detail.append({
"no": i,
"expected": data["korean_regular"]["essay"][i],
"user": essay_answers.get(str(i), ""),
"result": "미채점"
})
return jsonify({
"total_score": score,
"multiple_choice": detail,
"essay": essay_detail
})
# ======================
# 일반 랜덤 채점 API (학년/과목별)
# ======================
@app.route('/api/grade', methods=['POST'])
def grade_exam():
"""일반 시험용 랜덤 채점 API"""
try:
data = request.get_json()
grade = data.get('grade')
exam_type = data.get('exam_type')
subject = data.get('subject')
answers = data.get('answers', [])
# 랜덤 모범답 생성
correct = [random.randint(1, 5) for _ in range(len(answers))]
# 채점
details = []
correct_count = 0
for i, (stu_ans, cor_ans) in enumerate(zip(answers, correct), start=1):
is_correct = stu_ans == cor_ans
if is_correct:
correct_count += 1
details.append({
"number": i,
"student": stu_ans,
"correct": cor_ans,
"is_correct": is_correct
})
percent = round(correct_count / len(answers) * 100, 1)
result = {
"ok": True,
"grade": grade,
"exam_type": exam_type,
"subject": subject,
"score": correct_count,
"total": len(answers),
"percent": percent,
"details": details
}
return jsonify(result)
except Exception as e:
print("❌ 오류 발생:", e)
return jsonify({"ok": False, "msg": "서버 처리 중 오류 발생"}), 500
# ======================
# 실행
# ======================
if __name__ == '__main__':
print("[DEBUG] Flask 서버 시작")
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port, debug=True)