jinggujiwoo7
commited on
Commit
•
ca6e93c
1
Parent(s):
0744703
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, render_template, request, redirect, url_for
|
2 |
+
from transformers import pipeline
|
3 |
+
from models import db, Question, Comment
|
4 |
+
import os
|
5 |
+
|
6 |
+
app = Flask(__name__)
|
7 |
+
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
|
8 |
+
app.config['UPLOAD_FOLDER'] = 'uploads/audio'
|
9 |
+
db.init_app(app)
|
10 |
+
|
11 |
+
@app.route('/')
|
12 |
+
def index():
|
13 |
+
questions = Question.query.all()
|
14 |
+
return render_template('index.html', questions=questions)
|
15 |
+
|
16 |
+
@app.route('/add_question', methods=['POST'])
|
17 |
+
def add_question():
|
18 |
+
content = request.form['content']
|
19 |
+
password = request.form['password']
|
20 |
+
question = Question(content=content, password=password)
|
21 |
+
db.session.add(question)
|
22 |
+
db.session.commit()
|
23 |
+
return redirect(url_for('index'))
|
24 |
+
|
25 |
+
@app.route('/question/<int:question_id>', methods=['GET', 'POST'])
|
26 |
+
def question(question_id):
|
27 |
+
question = Question.query.get_or_404(question_id)
|
28 |
+
if request.method == 'POST':
|
29 |
+
username = request.form['username']
|
30 |
+
audio = request.files['audio']
|
31 |
+
if audio:
|
32 |
+
audio_path = os.path.join(app.config['UPLOAD_FOLDER'], audio.filename)
|
33 |
+
audio.save(audio_path)
|
34 |
+
comment = Comment(username=username, audio_file=audio_path, question=question)
|
35 |
+
else:
|
36 |
+
text_reply = request.form['text_reply']
|
37 |
+
comment = Comment(username=username, text_reply=text_reply, question=question)
|
38 |
+
db.session.add(comment)
|
39 |
+
db.session.commit()
|
40 |
+
return render_template('question.html', question=question)
|
41 |
+
|
42 |
+
if __name__ == '__main__':
|
43 |
+
with app.app_context():
|
44 |
+
db.create_all()
|
45 |
+
app.run(debug=True)
|