-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
188 lines (145 loc) · 6.33 KB
/
main.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
from chatbot.chatbot import Chatbot
from database.database import Database
from flask import Flask, render_template, request
import os
from dotenv import load_dotenv
import time
from flask import Flask, render_template, url_for, redirect
from flask_sqlalchemy import SQLAlchemy
from flask_login import UserMixin, login_user, LoginManager, login_required, logout_user, current_user
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField
from wtforms.validators import InputRequired, Length, ValidationError
from flask_bcrypt import Bcrypt
import json
import pandas as pd
app = Flask(__name__, template_folder='web', static_folder='web')
db = SQLAlchemy(app)
bcrypt = Bcrypt(app)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
app.config['SECRET_KEY'] = 'thisisasecretkey'
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
load_dotenv()
data = Database(data_file_path='inventory.csv')
data_sales = Database(data_file_path='sales.csv')
try:
chatbot = Chatbot(openai_api_key=os.environ['OPENAI_API_KEY'], time_out=10, data_base=data)
thread_id = chatbot.start_conversation()
chatbot.send_message_and_return_response(thread_id['thread_id'], f"Use this data to answer the customer's questions.{data.data.to_string()}")
except Exception as e:
print("Error: ", e)
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(20), nullable=False, unique=True)
password = db.Column(db.String(80), nullable=False)
recommendations = db.Column(db.String(80), nullable=False, default='')
user_type = db.Column(db.Boolean, default=False)
class RegisterForm(FlaskForm):
username = StringField(validators=[
InputRequired(), Length(min=4, max=20)], render_kw={"placeholder": "Username"})
password = PasswordField(validators=[
InputRequired(), Length(min=8, max=20)], render_kw={"placeholder": "Password"})
submit = SubmitField('Register')
def validate_username(self, username):
existing_user_username = User.query.filter_by(
username=username.data).first()
if existing_user_username:
raise ValidationError(
'That username already exists. Please choose a different one.')
class LoginForm(FlaskForm):
username = StringField(validators=[
InputRequired(), Length(min=4, max=20)], render_kw={"placeholder": "Username"})
password = PasswordField(validators=[
InputRequired(), Length(min=8, max=20)], render_kw={"placeholder": "Password"})
submit = SubmitField('Login')
FIRST_TIME = True
@app.route('/', methods=['GET', 'POST'])
@login_required
def index():
if FIRST_TIME:
chatbot.register_user(thread_id['thread_id'], user_name=current_user.username, like = current_user.recommendations)
return render_template('index.html')
@app.route('/dashboard', methods=['GET', 'POST'])
@login_required
def dashboard():
if not current_user.user_type:
return redirect(url_for('index')) # Redirect non-admins back to the home page
# Dashboard logic for admins
df = data_sales.data # Sample data processing code
df['date_of_sale'] = pd.to_datetime(df['date_of_sale'])
df['month'] = df['date_of_sale'].dt.to_period('M')
monthly_sales = df.groupby('month')['price'].sum().reset_index()
sales_dates = monthly_sales['month'].astype(str).tolist()
sales_amounts = monthly_sales['price'].tolist()
top_products = df.groupby('product')['price'].sum().reset_index()
top_products = top_products.sort_values(by='price', ascending=False).head(5)
top_product_names = top_products['product'].tolist()
top_product_quantities = top_products['price'].tolist()
category_sales = df.groupby('product_type')['price'].sum().reset_index()
category_sales = category_sales.sort_values(by='price', ascending=False)
category_names = category_sales['product_type'].tolist()
category_sales = category_sales['price'].tolist()
return render_template(
'dashboard.html',
sales_dates=sales_dates,
sales_amounts=sales_amounts,
top_product_names=top_product_names,
top_product_quantities=top_product_quantities,
category_names=category_names,
category_sales=category_sales,
name=data.get_columns('name'),
quantity=data.get_columns('quantity'),
)
@app.route('/chat', methods=['GET', 'POST'])
@login_required
def chat():
data = request.json['data']
#user_input = "How many instances of the product 'Pixel 8' are in the inventory?"
response = chatbot.send_message_and_return_response(thread_id['thread_id'], data)
#print(response['response'])
print("Received message for thread ID:", thread_id['thread_id'], "Message:", data, "Response:", response['response'])
#time.sleep(5)
return response['response']
@app.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(username=form.username.data).first()
if user:
if bcrypt.check_password_hash(user.password, form.password.data):
login_user(user)
return redirect(url_for('index'))
return render_template('login.html', form=form)
@app.route('/recomendacion', methods=['GET', 'POST'])
@login_required
def recomendacion():
"asdasd"
return "asdasd"
#@app.route('/dashboard', methods=['GET', 'POST'])
#@login_required
#def dashboard():
# return render_template('dashboard.html')
@app.route('/logout', methods=['GET', 'POST'])
@login_required
def logout():
logout_user()
return redirect(url_for('login'))
@ app.route('/register', methods=['GET', 'POST'])
def register():
form = RegisterForm()
if form.validate_on_submit():
hashed_password = bcrypt.generate_password_hash(form.password.data)
new_user = User(username=form.username.data, password=hashed_password)
db.session.add(new_user)
db.session.commit()
return redirect(url_for('login'))
return render_template('register.html', form=form)
if __name__ == '__main__':
app.run(debug=True)
# if __name__ == "__main__":
# test()