File size: 1,605 Bytes
ca6e93c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from flask import Flask, render_template, request, redirect, url_for
from transformers import pipeline
from models import db, Question, Comment
import os

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
app.config['UPLOAD_FOLDER'] = 'uploads/audio'
db.init_app(app)

@app.route('/')
def index():
    questions = Question.query.all()
    return render_template('index.html', questions=questions)

@app.route('/add_question', methods=['POST'])
def add_question():
    content = request.form['content']
    password = request.form['password']
    question = Question(content=content, password=password)
    db.session.add(question)
    db.session.commit()
    return redirect(url_for('index'))

@app.route('/question/<int:question_id>', methods=['GET', 'POST'])
def question(question_id):
    question = Question.query.get_or_404(question_id)
    if request.method == 'POST':
        username = request.form['username']
        audio = request.files['audio']
        if audio:
            audio_path = os.path.join(app.config['UPLOAD_FOLDER'], audio.filename)
            audio.save(audio_path)
            comment = Comment(username=username, audio_file=audio_path, question=question)
        else:
            text_reply = request.form['text_reply']
            comment = Comment(username=username, text_reply=text_reply, question=question)
        db.session.add(comment)
        db.session.commit()
    return render_template('question.html', question=question)

if __name__ == '__main__':
    with app.app_context():
        db.create_all()
    app.run(debug=True)