-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
220 lines (182 loc) · 7.43 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
212
213
214
215
216
217
218
219
220
from flask import Flask, render_template, request, session, redirect, url_for
from bson.objectid import ObjectId
from groq import Groq
from captcha.image import ImageCaptcha
import bcrypt
import base64
from io import BytesIO
import pymongo
import os
import markdown
import random
import string
app = Flask(__name__)
app.secret_key = "jhafyBEYDHBF*fhu0_Sd;aspd#Y&*G"
# Database
client = pymongo.MongoClient("MongoDB URI")
db = client.get_database('CLUSTER NAME')
auth = db.register
task_record = db.tasks
#! Groq
groqClient = Groq(
api_key= 'groqClientKey'
)
#! Static Page Routes
@app.route('/')
def index():
return render_template('index.html')
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/terms')
def terms():
return render_template('terms.html')
@app.route('/privacy')
def privacy():
return render_template('privacy.html')
#! User Routes
@app.route('/dashboard')
def dashboard():
if 'email' in session:
email = session['email']
name = session.get('name', 'User') # Use a default if name not found in session
return render_template('dashboard.html', name=name, email=email)
else:
return redirect(url_for('login'))
@app.route('/generate', methods=['GET', 'POST'])
def generate():
questions = ''
if 'email' in session:
email = session['email']
name = session.get('name', 'User') # Use a default if name not found in session
if request.method == 'POST':
text = request.form['text_input']
chat_completion = groqClient.chat.completions.create(
messages=[
{
"role": "user",
"content": "With the following notes, please create questions so that the student can answer and to learn the topic better." + text,
}
],
model="llama3-8b-8192",
)
text_content = chat_completion.choices[0].message.content
questions = markdown.markdown(text_content)
return render_template("generate.html", content=questions)
else:
return redirect(url_for('login'))
todos = []
# Show tasks for a specific user
@app.route('/tasks', methods=['GET'])
def tasks():
if 'email' in session:
user_email = session['email']
todos = list(task_record.find({'email': user_email}))
return render_template('tasks.html', todos=todos)
return redirect(url_for('login'))
@app.route('/tasks/add', methods=['POST'])
def add():
task_text = request.form.get('inputTodo').strip()
if task_text:
task_record.insert_one({'task': task_text, 'done': False, 'email': session['email']})
return redirect(url_for('tasks'))
@app.route('/tasks/check/<task_id>', methods=['POST'])
def check(task_id):
task_record.update_one({'_id': ObjectId(task_id)}, {'$set': {'done': not task['done']}})
return '', 204
@app.route('/tasks/delete/<task_id>', methods=['GET'])
def delete(task_id):
task_record.delete_one({'_id': ObjectId(task_id), 'email': session['email']})
return redirect(url_for('tasks'))
@app.route('/tasks/edit/<task_id>', methods=['POST'])
def edit(task_id):
new_task = request.form['todo']
task_record.update_one({'_id': ObjectId(task_id)}, {'$set': {'task': new_task}})
return redirect(url_for('tasks'))
@app.route('/discord')
def discord():
return redirect('https://discord.gg/vABM9PdWvh')
#! Authentication Routes
@app.route('/register', methods=['GET', 'POST'])
def register():
message = ''
if 'email' in session:
return redirect(url_for('dashboard'))
if request.method == 'POST':
user = request.form.get("name")
email = request.form.get('email')
password = request.form.get('password')
passwordAgain = request.form.get('passwordAgain')
user_found = auth.find_one({'name': user})
email_found = auth.find_one({'email': email})
captchaInput = request.form.get('captcha')
# Check CAPTCHA against the stored session value
if 'captcha_text' not in session or captchaInput != session['captcha_text']:
message = 'Captcha failed, please try again.'
# Re-generate CAPTCHA image in case of failure
captcha_data_uri = generate_captcha()
return render_template('register.html', message=message, captcha_image=captcha_data_uri)
# Other validation checks
if user_found:
message = "There is already a user by that name."
captcha_data_uri = generate_captcha()
return render_template('register.html', message=message, captcha_image=captcha_data_uri)
if email_found:
message = 'There is already a different user with that email address.'
captcha_data_uri = generate_captcha()
return render_template('register.html', message=message, captcha_image=captcha_data_uri)
if password != passwordAgain:
message = 'Passwords should match! Try again.'
captcha_data_uri = generate_captcha()
return render_template('register.html', message=message, captcha_image=captcha_data_uri)
else:
# Hash password and save user in database
hashed = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
user_input = {'name': user, 'email': email, 'password': hashed}
auth.insert_one(user_input)
# Save user details in session after successful registration
session["email"] = email
session["name"] = user
return redirect(url_for('dashboard'))
# GET request: Generate new CAPTCHA on load
captcha_data_uri = generate_captcha()
return render_template('register.html', captcha_image=captcha_data_uri)
def generate_captcha():
"""Generate a CAPTCHA image and save the text in the session."""
captchaText = ''.join(random.choices(string.ascii_lowercase, k=5))
session['captcha_text'] = captchaText # Store CAPTCHA text in session
image = ImageCaptcha(width=280, height=90)
image_stream = BytesIO()
image.write(captchaText, image_stream)
image_stream.seek(0)
base64_image = base64.b64encode(image_stream.getvalue()).decode('utf-8')
return f"data:image/png;base64,{base64_image}"
@app.route('/login', methods=['GET', 'POST'])
def login():
message = 'Please login to your account'
if "email" in session:
return redirect(url_for("dashboard"))
if request.method == "POST":
email = request.form.get("email")
password = request.form.get("password")
email_found = auth.find_one({"email": email})
if email_found:
passwordcheck = email_found['password']
if bcrypt.checkpw(password.encode('utf-8'), passwordcheck):
session["email"] = email_found["email"]
session["name"] = email_found["name"]
return redirect(url_for('dashboard'))
else:
message = 'Wrong password'
return render_template('login.html', message=message)
else:
message = 'Email not found'
return render_template('login.html', message=message)
return render_template('login.html')
@app.route('/signout')
def signout():
session.pop('email', None)
session.pop('name', None)
return render_template('index.html')
if __name__ == "__main__":
app.run(host="0.0.0.0", port=80, debug=True)