Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -299,64 +299,53 @@ def process_account_action(action, account_username, password, first_name=None,
|
|
299 |
|
300 |
################ LOGIN #################################
|
301 |
|
302 |
-
@app.route("/
|
303 |
-
def
|
304 |
-
if
|
305 |
-
|
306 |
-
last_name = request.form['last_name']
|
307 |
-
account_username = request.form['username']
|
308 |
-
password = request.form['password']
|
309 |
-
|
310 |
-
# Handling file upload
|
311 |
-
if 'profile_image' not in request.files:
|
312 |
-
flash('No file part')
|
313 |
-
return redirect(request.url)
|
314 |
-
file = request.files['profile_image']
|
315 |
-
if file.filename == '':
|
316 |
-
flash('No selected file')
|
317 |
-
return redirect(request.url)
|
318 |
-
if file:
|
319 |
-
filename = secure_filename(f"{account_username}.png")
|
320 |
-
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
321 |
-
file.save(file_path)
|
322 |
-
|
323 |
-
result = process_account_action("create", account_username, password, first_name, last_name, file_path)
|
324 |
-
flash(result)
|
325 |
-
if "successfully" in result:
|
326 |
-
return redirect(url_for('login'))
|
327 |
-
|
328 |
-
return render_template('sign-up.html')
|
329 |
-
|
330 |
-
@app.route("/chat")
|
331 |
-
def chat():
|
332 |
-
if 'account_name' not in session or session['account_name'] is None:
|
333 |
-
return redirect(url_for('login'))
|
334 |
-
|
335 |
-
account_name = session.get("account_name")
|
336 |
-
first = session.get("first")
|
337 |
-
last = session.get("last")
|
338 |
-
token2 = session.get("token2")
|
339 |
-
greet = session.get("greet")
|
340 |
-
|
341 |
-
return render_template('ai-chat-bot.html', account_name=account_name, greet=greet, first=first, last=last, token2=token2)
|
342 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
343 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
344 |
|
345 |
-
|
346 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
347 |
message = None
|
348 |
if request.method == "POST":
|
349 |
account_name = request.form.get("username")
|
350 |
password = request.form.get("password")
|
|
|
|
|
351 |
message = process_account_action("login", account_name, password)
|
352 |
flash(message)
|
353 |
if "Login successful." in message:
|
354 |
############### GET INFO ABOUT USER ###############################################
|
355 |
url = f"https://api.github.com/repos/omarnuwrar/{repository_name}/contents/user.json"
|
356 |
-
headers = {
|
357 |
-
"Authorization": f"token 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",
|
358 |
-
"Accept": "application/vnd.github.v3+json"
|
359 |
-
}
|
360 |
response = requests.get(url, headers=headers)
|
361 |
if response.status_code == 200:
|
362 |
file_info = response.json()
|
@@ -364,7 +353,6 @@ def login():
|
|
364 |
users = eval(file_content) # Convert the string to a list of dictionaries
|
365 |
|
366 |
target_username = account_name
|
367 |
-
|
368 |
target_user = None
|
369 |
for user in users:
|
370 |
if user['username'] == target_username:
|
@@ -374,14 +362,35 @@ def login():
|
|
374 |
first = target_user['first_name']
|
375 |
last = target_user['last_name']
|
376 |
token2 = target_user['token']
|
377 |
-
user_name.append(first)
|
378 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
379 |
############## END INFO ABOUT USER #################################################
|
380 |
|
381 |
# Initialize the user input and get the assistant's response
|
382 |
conversation_history = {}
|
383 |
user_input = f"user name : {first} \n \n message : أهلا"
|
384 |
-
greet = get_assistant_response(user_input
|
385 |
|
386 |
# Store data in the session
|
387 |
session['account_name'] = account_name
|
@@ -395,6 +404,11 @@ def login():
|
|
395 |
|
396 |
return render_template("sign-in.html", message=message)
|
397 |
|
|
|
|
|
|
|
|
|
|
|
398 |
############## END LOGIN #############################################
|
399 |
|
400 |
@app.route('/get_response', methods=['POST'])
|
|
|
299 |
|
300 |
################ LOGIN #################################
|
301 |
|
302 |
+
@app.route("/login", methods=["GET", "POST"])
|
303 |
+
def login():
|
304 |
+
# Check if device info matches any stored device info
|
305 |
+
user_agent = request.headers.get('User-Agent')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
306 |
|
307 |
+
url = f"https://api.github.com/repos/omarnuwrar/{repository_name}/contents/user.json"
|
308 |
+
headers = {
|
309 |
+
"Authorization": f"token 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", # Replace with your GitHub token
|
310 |
+
"Accept": "application/vnd.github.v3+json"
|
311 |
+
}
|
312 |
+
response = requests.get(url, headers=headers)
|
313 |
+
if response.status_code == 200:
|
314 |
+
file_info = response.json()
|
315 |
+
file_content = base64.b64decode(file_info['content']).decode()
|
316 |
+
users = eval(file_content) # Convert the string to a list of dictionaries
|
317 |
|
318 |
+
# Check if the device info matches any user
|
319 |
+
for user in users:
|
320 |
+
if 'device_info' in user and user['device_info'] == user_agent:
|
321 |
+
# Device info matches, automatically log in the user
|
322 |
+
session['account_name'] = user['username']
|
323 |
+
session['first'] = user['first_name']
|
324 |
+
session['last'] = user['last_name']
|
325 |
+
session['token2'] = user['token']
|
326 |
|
327 |
+
# Initialize the user input and get the assistant's response
|
328 |
+
conversation_history = {}
|
329 |
+
user_input = f"user name : {user['first_name']} \n \n message : أهلا"
|
330 |
+
greet = get_assistant_response(user_input, conversation_history)
|
331 |
+
|
332 |
+
session['greet'] = greet
|
333 |
+
|
334 |
+
# Redirect to the chat page
|
335 |
+
return redirect(url_for('chat'))
|
336 |
+
|
337 |
+
# If no match is found or if the user is logging in normally
|
338 |
message = None
|
339 |
if request.method == "POST":
|
340 |
account_name = request.form.get("username")
|
341 |
password = request.form.get("password")
|
342 |
+
remember_me = request.form.get("remember_me") # Check if "Remember Me" was selected
|
343 |
+
|
344 |
message = process_account_action("login", account_name, password)
|
345 |
flash(message)
|
346 |
if "Login successful." in message:
|
347 |
############### GET INFO ABOUT USER ###############################################
|
348 |
url = f"https://api.github.com/repos/omarnuwrar/{repository_name}/contents/user.json"
|
|
|
|
|
|
|
|
|
349 |
response = requests.get(url, headers=headers)
|
350 |
if response.status_code == 200:
|
351 |
file_info = response.json()
|
|
|
353 |
users = eval(file_content) # Convert the string to a list of dictionaries
|
354 |
|
355 |
target_username = account_name
|
|
|
356 |
target_user = None
|
357 |
for user in users:
|
358 |
if user['username'] == target_username:
|
|
|
362 |
first = target_user['first_name']
|
363 |
last = target_user['last_name']
|
364 |
token2 = target_user['token']
|
|
|
365 |
|
366 |
+
############### REMEMBER ME IMPLEMENTATION ########################################
|
367 |
+
if remember_me:
|
368 |
+
user_agent = request.headers.get('User-Agent')
|
369 |
+
target_user['device_info'] = user_agent
|
370 |
+
|
371 |
+
# Convert users list back to JSON string
|
372 |
+
updated_file_content = json.dumps(users, indent=4)
|
373 |
+
|
374 |
+
# Update the user.json file in the repository
|
375 |
+
update_url = f"https://api.github.com/repos/omarnuwrar/{repository_name}/contents/user.json"
|
376 |
+
update_data = {
|
377 |
+
"message": f"Update device info for {account_name}",
|
378 |
+
"content": base64.b64encode(updated_file_content.encode()).decode(),
|
379 |
+
"sha": file_info['sha']
|
380 |
+
}
|
381 |
+
update_response = requests.put(update_url, headers=headers, json=update_data)
|
382 |
+
if update_response.status_code == 200:
|
383 |
+
print("User info updated with device info.")
|
384 |
+
else:
|
385 |
+
print("Failed to update user info.")
|
386 |
+
############### END REMEMBER ME IMPLEMENTATION ####################################
|
387 |
+
|
388 |
############## END INFO ABOUT USER #################################################
|
389 |
|
390 |
# Initialize the user input and get the assistant's response
|
391 |
conversation_history = {}
|
392 |
user_input = f"user name : {first} \n \n message : أهلا"
|
393 |
+
greet = get_assistant_response(user_input, conversation_history)
|
394 |
|
395 |
# Store data in the session
|
396 |
session['account_name'] = account_name
|
|
|
404 |
|
405 |
return render_template("sign-in.html", message=message)
|
406 |
|
407 |
+
@app.route("/logout")
|
408 |
+
def logout():
|
409 |
+
session.clear()
|
410 |
+
flash("You have been logged out.")
|
411 |
+
return redirect(url_for('login'))
|
412 |
############## END LOGIN #############################################
|
413 |
|
414 |
@app.route('/get_response', methods=['POST'])
|