This repository was archived by the owner on Aug 26, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEkgApp.py
More file actions
136 lines (119 loc) · 4.54 KB
/
EkgApp.py
File metadata and controls
136 lines (119 loc) · 4.54 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
import logging
import logging.handlers as handlers
from models.TextLabel_schema import LabelDatum
from gevent.pywsgi import WSGIServer
from flask_sqlalchemy import SQLAlchemy
from flask import Flask, render_template, request, jsonify
app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False
app.config['TEMPLATES_AUTO_RELOAD'] = True
# 本地部署
app.config[
'SQLALCHEMY_DATABASE_URI'
] = 'mysql+pymysql://root:12345678@localhost:3306/TextLabel_schema'
# 云服务器部署 lft
# app.config[
# 'SQLALCHEMY_DATABASE_URI'
# ] = 'mysql+pymysql://root:123456@localhost:3306/TextLabel_schema'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
db = SQLAlchemy(app)
@app.route('/')
def index():
"""默认页面
"""
return render_template('index.html')
@app.route('/api/get_text', methods=['GET'])
def get_text():
"""读取未标注数据,并更新状态信息为正在标注(labeling)
"""
if request.method == 'GET':
try:
labelData = (
db.session.query(LabelDatum)
.filter_by(abstract_label=None, label_flag=None)
.first()
)
db.session.query(LabelDatum).filter_by(abstract=labelData.abstract).update(
{LabelDatum.label_flag: 'labeling'}
)
db.session.commit()
return jsonify({'abstract': labelData.abstract})
except Exception:
db.session.rollback()
return jsonify({'abstract': None})
@app.route('/api/saveAndNext', methods=['POST'])
def saveAndNext():
"""保存已标注数据,更新状态信息为已标注(labeled);
重新读取一条新的未标注数据,并更新状态信息为正在标注(labeling)
"""
if request.method == 'POST':
try:
result = request.get_json()
db.session.query(LabelDatum).filter(
LabelDatum.abstract == str(result['abstract'])
).update(
{
LabelDatum.abstract_label: str(result['abstract_label']).replace(
"'", "\""
),
LabelDatum.label_flag: 'labeled',
}
)
labelData = (
db.session.query(LabelDatum)
.filter_by(abstract_label=None, label_flag=None)
.first()
)
db.session.query(LabelDatum).filter_by(abstract=labelData.abstract).update(
{LabelDatum.label_flag: 'labeling'}
)
db.session.commit()
return jsonify({'abstract': labelData.abstract})
except Exception:
db.session.rollback()
return jsonify({'abstract': None})
@app.route('/api/reset_label_flag', methods=['POST'])
def reset_label_flag():
"""取消当前正在标注的数据,并更新状态信息为为标注(None)
"""
if request.method == 'POST':
result = request.get_json()
try:
db.session.query(LabelDatum).filter(
LabelDatum.abstract == str(result['abstract'])
).update({LabelDatum.label_flag: None})
db.session.commit()
return jsonify({'abstract': None})
except Exception:
db.session.rollback()
return jsonify({'abstract': None})
@app.errorhandler(Exception)
def handle_exception_error(e):
"""捕捉所有错误信息,并返回至初始界面(index.html)
"""
return render_template('index.html')
if __name__ == "__main__":
"""
%(asctime)s 即日志记录时间,精确到毫秒
%(levelname)s 即此条日志级别
%(filename)s 即触发日志记录的python文件名
%(funcName)s 即触发日志记录的函数名
%(lineno)s 即触发日志记录代码的行号
%(message)s 这项即调用如app.logger.info(‘info log’)中的参数,即message
"""
from werkzeug.debug import DebuggedApplication
logger = logging.getLogger('app')
logger.setLevel(10)
logger_format = logging.Formatter(
'%(asctime)s - %(levelname)s - %(filename)s - %(funcName)s - %(lineno)s - %(message)s'
)
logHandler = handlers.RotatingFileHandler(
'log/flask.log', maxBytes=1000000, backupCount=1, encoding='utf-8'
)
logHandler.setFormatter(logger_format)
logger.addHandler(logHandler)
logger.info("Logging configuration done")
dapp = DebuggedApplication(app, evalex=True)
http_server = WSGIServer(('0.0.0.0', 5000), app, log=logger)
print('======>>> App running at Local: http://localhost:5000/<<<======')
http_server.serve_forever()