-
Notifications
You must be signed in to change notification settings - Fork 14
/
app.py
280 lines (236 loc) · 9.61 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
from flask import Flask, render_template, request, jsonify, redirect, url_for, flash,session
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt
from flask_login import LoginManager, UserMixin, login_user, current_user, logout_user, login_required
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField, BooleanField
from wtforms.validators import DataRequired, Length, Email, EqualTo, ValidationError
import google.generativeai as genai
from google.generativeai.types import HarmCategory, HarmBlockThreshold
import os
from urllib.parse import quote
import pickle
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
from dotenv import load_dotenv, dotenv_values
#load env variables
load_dotenv()
#global vars
eemail=''
#render
last_index_page = "index"
def logo_click():
global last_index_page
return redirect(url_for(last_index_page))
# Configure the Flask application
app = Flask(__name__)
app.config['SECRET_KEY'] = 'the random string'
# app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY', 'your_secret_key') # Ensure to replace 'your_secret_key' with a real key or set it in your environment
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'
db = SQLAlchemy(app)
bcrypt = Bcrypt(app)
login_manager = LoginManager(app)
login_manager.login_view = 'login'
login_manager.login_message_category = 'info'
# Configure the Gemini API
genai.configure(api_key="AIzaSyAdhHba59C82frkiMmR0U1a5YzfFuYm8DM") # Replace with your actual API key
# Initializing the limiter
limiter = Limiter(
get_remote_address,
default_limits=["5 per minute"] # Set your desired rate limit here
)
limiter.init_app(app)
# Define characters
characters = {
"character1": "Franklin",
"character2": "Michael",
"character3": "Trevor",
"character4": "Lester",
"character5": "Amanda", #
"character6": "Ron",
"character7": "Lazlow", #
"character8": "Lamar",
"character9": "Jimmy",
"character10": "Tracey", #
"character11": "Packie",
"character12": "Devin",
"character13": "Steve",
}
# Initialize the Gemini AI model
generation_config = {
"temperature": 1,
"top_p": 0.95,
"top_k": 64,
"max_output_tokens": 8192,
"response_mime_type": "text/plain",
}
# File settings
def create_files(email):
for character in characters:
with open(f"{email}_{characters[character]}.pkl","wb") as file:
pickle.dump([],file)
def append_data(character):
email = eemail
data = chat_session.history
with open(f"{email}_{character}.pkl","wb") as file:
pickle.dump(data,file)
def read_data(character):
email=eemail
with open(f"{email}_{character}.pkl","rb") as file:
return pickle.load(file)
# User model
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(20), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
image_file = db.Column(db.String(20), nullable=False, default='default.jpg')
password = db.Column(db.String(60), nullable=False)
def __repr__(self):
return f"User('{self.username}', '{self.email}', '{self.image_file}')"
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
# Forms
class RegistrationForm(FlaskForm):
username = StringField('Username', validators=[DataRequired(), Length(min=2, max=20)])
email = StringField('Email', validators=[DataRequired(), Email()])
password = PasswordField('Password', validators=[DataRequired()])
confirm_password = PasswordField('Confirm Password', validators=[DataRequired(), EqualTo('password')])
submit = SubmitField('Sign Up')
def validate_username(self, username):
user = User.query.filter_by(username=username.data).first()
if user:
raise ValidationError('That username is taken. Please choose a different one.')
def validate_email(self, email):
user = User.query.filter_by(email=email.data).first()
if user:
raise ValidationError('That email is taken. Please choose a different one.')
class LoginForm(FlaskForm):
email = StringField('Email', validators=[DataRequired(), Email()])
password = PasswordField('Password', validators=[DataRequired()])
remember = BooleanField('Remember Me')
submit = SubmitField('Login')
# Function to initialize chat session with the selected character
def initialize_chat_session(character_name):
return genai.GenerativeModel(
model_name="gemini-1.5-pro",
generation_config=generation_config,
safety_settings={
HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_NONE,
HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE,
HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE,
},
system_instruction=f"""you are {character_name} from GTA 5 and now chat with me like that character. You have to behave exactly like the particular character
If someone asks you for your name, then reply with your appropriate name.
If someone asks you for your age, then reply with your appropriate age.
If someone asks you for your gender, then reply with your appropriate gender.
Also, do not reply to any questions that are not related to the game; instead, just say something like "You think I am an idiot asking such obvious questions."""
).start_chat(history=[])
# Routes
@app.route('/')
def index():
session['last_index_page'] = 'index'
return render_template('index.html')
@app.route('/index2.html')
def index2():
session['last_index_page'] = 'index2'
return render_template('index2.html')
@app.route('/index3.html')
def index3():
session['last_index_page'] = 'index3'
return render_template('index3.html')
@app.route('/index4.html')
def index4():
session['last_index_page'] = 'index4'
return render_template('index4.html')
@app.route('/character_page')
def character_page():
return render_template('character_page.html')
@app.route('/logo_click')
def logo_click():
return redirect(url_for(session.get('last_index_page', 'index')))
@app.route('/register', methods=['GET', 'POST'])
def register():
if current_user.is_authenticated:
return redirect(url_for('index'))
form = RegistrationForm()
if form.validate_on_submit():
hashed_password = bcrypt.generate_password_hash(form.password.data).decode('utf-8')
user = User(username=form.username.data, email=form.email.data, password=hashed_password)
create_files(user.email)
db.session.add(user)
db.session.commit()
flash('Your account has been created! You are now able to log in', 'success')
return redirect(url_for('login'))
return render_template('register.html', title='Register', form=form)
@app.route('/login', methods=['GET', 'POST'])
@limiter.limit("5 per minute") #The limiter
def login():
if current_user.is_authenticated:
return redirect(url_for('index'))
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(email=form.email.data).first()
if user and bcrypt.check_password_hash(user.password, form.password.data):
global eemail
eemail = user.email
login_user(user, remember=form.remember.data)
next_page = request.args.get('next')
session['logged_in'] = True
return redirect(next_page) if next_page else redirect(url_for('index'))
else:
flash('Login Unsuccessful. Please check email and password', 'danger')
return render_template('login.html', title='Login', form=form)
@app.route("/logout")
def logout():
logout_user()
session.pop('logged_in', None)
return redirect(url_for('index'))
@app.route('/chat/<character>')
@login_required
def chat_character(character):
if character in characters:
global chat_session
chat_session = initialize_chat_session(characters[character])
return render_template('chat_character1.html', character=characters[character])
else:
return render_template('error.html', error_message="Character not found"), 404
@app.route('/chat', methods=['POST'])
@login_required
def chat_response():
global chat_session
message = request.form['message']
# Send the user's message to the Gemini AI model
response = chat_session.send_message(message)
response_text = response.text.strip()
# Return the AI's response as JSON
return jsonify({'response': response_text})
# @app.route('/logo_click')
# def logo_click():
# global last_index_page
# return redirect(url_for(last_index_page))
@app.route('/Character_cards')
def Character_cards():
return render_template('character_cards.html')
@app.route('/update_last_index/<page>')
def update_last_index(page):
if page in ["index", "index2"]:
session['last_index_page'] = page
return '', 204 # No content response
@app.route('/aboutus')
def about_us():
return render_template('aboutus.html')
# @app.route('/save_changes', methods=['POST'])
# def save_changes():
# # Retrieve data from the request
# data = request.json
# character = data.get('character')
# # Perform your save logic here
# append_data(character)
# # For demonstration, let's just print the data
# print(f"Data received: {data}")
# # Respond to the client
# return jsonify({"message": "Changes saved successfully!"})
if __name__ == '__main__':
app.run(debug=True)