-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
81 lines (68 loc) · 2.66 KB
/
Copy pathapp.py
File metadata and controls
81 lines (68 loc) · 2.66 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
from flask import Flask, render_template, request, redirect, jsonify
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
from transformers import pipeline
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
db = SQLAlchemy(app)
class Notes(db.Model):
id = db.Column(db.Integer, primary_key=True)
content = db.Column(db.String(300), nullable=False)
category = db.Column(db.String(300), nullable=True)
summary = db.Column(db.String(300), nullable=True)
date_created = db.Column(db.DateTime, default=datetime.now)
def __repr__(self):
return '<Note %r>' % self.id
classifier = pipeline("zero-shot-classification", model="MoritzLaurer/DeBERTa-v3-base-mnli-fever-anli")
labels = ["Meetings", "Ideas", "Health", "Work", "Finance", "Education", "Shopping", "Others"]
def classify(note):
result = classifier(note, candidate_labels=labels)
return result["labels"][0]
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
def summarize(note):
summary = summarizer(note, max_length=50, min_length=10, do_sample=False)
return summary[0]["summary_text"]
@app.route('/', methods=['GET'])
def index():
notes = Notes.query.order_by(Notes.date_created).all()
return render_template('index.html', tasks=notes)
@app.route('/add_note', methods=['POST'])
def add_note():
data = request.get_json()
note_content = data.get('note')
if not note_content:
return jsonify({"No content in your note!"}), 400
category = classify(note_content)
summary = summarize(note_content)
new_note = Notes(content=note_content, category=category, summary=summary)
try:
db.session.add(new_note)
db.session.commit()
return jsonify({"success": True})
except:
return jsonify({'There was a problem with adding your note.'}), 500
@app.route('/delete/<int:id>')
def delete(id):
note_to_delete = Notes.query.get_or_404(id)
try:
db.session.delete(note_to_delete)
db.session.commit()
return redirect('/')
except:
return "There was a problem with deleting your note."
@app.route('/edit/<int:id>', methods=['GET', 'POST'])
def edit(id):
note = Notes.query.get_or_404(id)
if request.method == 'POST':
note.content = request.form['note']
note.category = request.form['category']
note.summary = request.form['summary']
try:
db.session.commit()
return redirect('/')
except:
return "There was a problem with editing your note."
else:
return render_template('edit.html', task=note)
if __name__ == '__main__':
app.run(debug=True)