forked from SaloniSingh1601/RAKSHA
-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
211 lines (168 loc) · 5.94 KB
/
app.py
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
from flask import Flask, render_template, Response, jsonify, request, redirect, url_for
from flask_bootstrap import Bootstrap
from flask_sqlalchemy import SQLAlchemy
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired, URL
from flask_ckeditor import CKEditor, CKEditorField
from datetime import date
from camera import VideoCamera
from scoring import get_score
from chat import get_response
app = Flask(__name__)
app.config['JSONIFY_PRETTYPRINT_REGULAR'] = False
# needs to be put in .env
app.config['SECRET_KEY'] = '8BYkEfBA6O6donzWlSihBXox7C0sKR6b'
ckeditor = CKEditor(app)
Bootstrap(app)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///posts.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class BlogPost(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(250), unique=True, nullable=False)
subtitle = db.Column(db.String(250), nullable=False)
date = db.Column(db.String(250), nullable=False)
body = db.Column(db.Text, nullable=False)
author = db.Column(db.String(250), nullable=False)
img_url = db.Column(db.String(250), nullable=False)
db.create_all()
class CreatePostForm(FlaskForm):
title = StringField("Blog Post Title", validators=[DataRequired()])
subtitle = StringField("Subtitle", validators=[DataRequired()])
author = StringField("Your Name", validators=[DataRequired()])
img_url = StringField("Blog Image URL", validators=[DataRequired(), URL()])
body = CKEditorField("Blog Content", validators=[DataRequired()])
submit = SubmitField("Submit Post")
video_camera = None
global_frame = None
category = 'Hammer Strike'
@app.route('/record_status', methods=['POST'])
def record_status():
global video_camera
if video_camera == None:
video_camera = VideoCamera()
json = request.get_json()
status = json['status']
if status == "true":
video_camera.start_record()
return jsonify(result="started")
else:
video_camera.stop_record()
return jsonify(result="stopped")
def video_stream():
global video_camera
global global_frame
if video_camera == None:
video_camera = VideoCamera()
while True:
frame = video_camera.get_frame()
if frame != None:
global_frame = frame
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
else:
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + global_frame + b'\r\n\r\n')
@app.route('/',methods = ['GET'])
def index():
return render_template('index.html')
@app.route('/predict',methods = ['POST'])
def predict():
text = request.get_json().get("message")
response = get_response(text)
message = {"answer": response}
return jsonify(message)
@app.route('/elements')
def elements():
return render_template('elements.html')
@app.route('/generic')
def generic():
return render_template('generic.html')
@app.route('/helpline')
def helpline():
return render_template('helpline.html')
@app.route('/laws')
def laws():
return render_template('laws.html')
@app.route('/k')
def leaderboard():
return render_template('k.html')
@app.route('/login')
def login():
return render_template('login.html')
@app.route('/blog')
def get_all_posts():
posts = BlogPost.query.all()
return render_template("blog.html", all_posts=posts)
@app.route("/post/<int:post_id>")
def show_post(post_id):
requested_post = BlogPost.query.get(post_id)
return render_template("post.html", post=requested_post)
@app.route("/about")
def about():
return render_template("about.html")
@app.route("/contact")
def contact():
return render_template("contact.html")
@app.route("/new-post", methods=["GET", "POST"])
def add_new_post():
form = CreatePostForm()
if form.validate_on_submit():
new_post = BlogPost(
title=form.title.data,
subtitle=form.subtitle.data,
body=form.body.data,
img_url=form.img_url.data,
author=form.author.data,
date=date.today().strftime("%B %d, %Y")
)
db.session.add(new_post)
db.session.commit()
return redirect(url_for("get_all_posts"))
return render_template("make-post.html", form=form)
@app.route("/edit-post/<int:post_id>", methods=["GET", "POST"])
def edit_post(post_id):
post = BlogPost.query.get(post_id)
edit_form = CreatePostForm(
title=post.title,
subtitle=post.subtitle,
img_url=post.img_url,
author=post.author,
body=post.body
)
if edit_form.validate_on_submit():
post.title = edit_form.title.data
post.subtitle = edit_form.subtitle.data
post.img_url = edit_form.img_url.data
post.author = edit_form.author.data
post.body = edit_form.body.data
db.session.commit()
return redirect(url_for("show_post", post_id=post.id))
return render_template("make-post.html", form=edit_form, is_edit=True)
@app.route("/delete/<int:post_id>")
def delete_post(post_id):
post_to_delete = BlogPost.query.get(post_id)
db.session.delete(post_to_delete)
db.session.commit()
return redirect(url_for('get_all_posts'))
@app.route('/quiz')
def quiz():
return render_template('quiz.html')
@app.route('/video_viewer')
def video_viewer():
return Response(video_stream(),
mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/pose', methods=['GET', 'POST'])
def pose():
global category
"""Video streaming"""
category = request.form.get('detect')
return render_template('pose.html')
@app.route('/update', methods=['GET', 'POST'])
def update_score():
return render_template('score.html', score=get_score(category))
if __name__ == '__main__':
app.run(debug=True, threaded=True)