-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
113 lines (86 loc) · 3.79 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
from flask import Flask, render_template, request, redirect
import online_test_method
import pandas as pd
app = Flask(__name__)
history_df = pd.DataFrame(columns=['Username', 'Question', 'Label'])
users_df = pd.DataFrame(columns=['username', 'password'])
#users_df = pd.read_csv('data/users.csv')
current_user = None
current_username = None
#defaults onto signin page
@app.route('/')
def signin_page():
return redirect('/signin')
@app.route('/signin', methods=['GET', 'POST'])
def signin():
if request.method == 'POST':
un = request.form['username']
pw = request.form['password']
# pre-existing user
user = users_df[(users_df['username'] == un) & (users_df['password'] == pw)]
if not user.empty:
global current_user
current_user = user.iloc[0]
global current_username
current_username = un
return redirect('/home')
return render_template('signin.html', error=True)
return render_template('signin.html', error=False)
@app.route('/create', methods=['GET', 'POST'])
def create_account():
if request.method == 'POST':
un = request.form['username']
pw = request.form['password']
global users_df
if un in users_df['username'].values:
return render_template('create.html', error=True)
new = pd.DataFrame({'username': [un], 'password': [pw]})
users_df = users_df.append(new, ignore_index=True)
# To save the user data in memory onto a csv file
#users_df.to_csv('data/users.csv', index=False)
return redirect('/signin')
return render_template('create.html', error=False)
@app.route('/home', methods=['GET', 'POST'])
def home():
if request.method == 'POST':
return redirect('/history')
return render_template('home.html', messages=[])
@app.route('/history')
def history():
form_history = history_df.to_dict('records')
return render_template('history.html', form_history=form_history)
@app.route('/ask', methods=['GET', 'POST'])
def ask_problem():
if request.method == 'POST':
subject = request.form['subject']
prompt = f"Ask a high school {subject} word problem. Only include the problem. Don't show explanations. Don't show the answer. Do it in 2 sentences."
generated_question = online_test_method.bard(prompt)['content'].split('>')[1]
generated_question = generated_question.split('?')[0] + '?'
global history_df
history_df = history_df.append({'Username': current_user.iloc[0], 'Question': generated_question, 'Label': subject},
ignore_index=True)
# Redirect to the response page with the generated prompt
return redirect(f'/response?question={generated_question}')
return render_template('ask.html')
@app.route('/response')
def show_response():
question = request.args.get('question', '')
# Render the response page with the prompt
return render_template('bard_response.html', question=question)
@app.route('/send', methods=['POST'])
def send():
global history_df
# username = request.form['username']
messages = []
message = request.form['message']
messages.append(('Question', message))
# bot's response generated from our algorithm
bot_response = online_test_method.online(message)
messages.append(('Label', bot_response))
history_df = history_df.append({'Username': current_user.iloc[0], 'Question': message, 'Label': bot_response},
ignore_index=True)
# To save the history of the chat in memory onto a csv file
# history_df.to_csv('data/history.csv', index=False) # Save the dataframe to CSV
return render_template('response.html', messages=messages)
if __name__ == '__main__':
app.run(debug=True)