from flask import Flask, render_template, request, redirect, url_for, flash import base64 import os import requests # Your GitHub personal access token (replace this with your actual token) GITHUB_TOKEN = "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" # Use your own token! # GitHub repository details OWNER = "LAMENTIS1" # GitHub username or organization REPO = "face_recog" # GitHub repository name BRANCH = "master" # Branch to commit to (could be 'main' or any other branch) # Flask app setup app = Flask(__name__) # Set the secret key for flashing messages app.secret_key = 'your_secret_key' # Ensure the uploads directory exists UPLOAD_FOLDER = 'uploads' if not os.path.exists(UPLOAD_FOLDER): os.makedirs(UPLOAD_FOLDER) # Ensure the directory exists by checking if the file or folder exists in the repo def check_folder_exists(person_name): api_url = f"https://api.github.com/repos/{OWNER}/{REPO}/contents/labeled_images/{person_name.replace(' ', '%20')}" response = requests.get(api_url, headers={"Authorization": f"Bearer {GITHUB_TOKEN}"}) if response.status_code == 404: create_dummy_file(person_name) return f"Folder 'labeled_images/{person_name}' created successfully." else: return f"Folder 'labeled_images/{person_name}' already exists." # Create a dummy file in the folder to ensure it exists def create_dummy_file(person_name): api_url = f"https://api.github.com/repos/{OWNER}/{REPO}/contents/labeled_images/{person_name.replace(' ', '%20')}/.dummy" data = { "message": f"Create folder for {person_name}", "branch": BRANCH, "content": base64.b64encode(b"").decode(), # Empty content for the dummy file } headers = { "Authorization": f"Bearer {GITHUB_TOKEN}", "Content-Type": "application/json", } response = requests.put(api_url, json=data, headers=headers) if response.status_code == 201: print(f"Folder 'labeled_images/{person_name}' created successfully.") else: print(f"Failed to create folder: {response.status_code} - {response.text}") # Function to upload a file to GitHub using the API def upload_image(image_path, person_name, file_name): api_url = f"https://api.github.com/repos/{OWNER}/{REPO}/contents/labeled_images/{person_name.replace(' ', '%20')}/{file_name}" # Read the image and encode it in base64 with open(image_path, "rb") as image_file: encoded_image = base64.b64encode(image_file.read()).decode() data = { "message": f"Add {file_name} for {person_name}", "branch": BRANCH, "content": encoded_image, } headers = { "Authorization": f"Bearer {GITHUB_TOKEN}", "Content-Type": "application/json", } response = requests.put(api_url, json=data, headers=headers) if response.status_code == 201: return True else: return False @app.route("/", methods=["GET", "POST"]) def index(): success_message = "" if request.method == "POST": # Get username person_name = request.form["username"] # Get files from the form image1 = request.files["image1"] image2 = request.files["image2"] # Save images locally temporarily image1_path = os.path.join(UPLOAD_FOLDER, image1.filename) image2_path = os.path.join(UPLOAD_FOLDER, image2.filename) image1.save(image1_path) image2.save(image2_path) # Ensure the folder for the user exists on GitHub and capture success/failure message success_message = check_folder_exists(person_name) # Upload images to GitHub success1 = upload_image(image1_path, person_name, "1.jpg") success2 = upload_image(image2_path, person_name, "2.jpg") # Remove the temporary files after uploading os.remove(image1_path) os.remove(image2_path) # Show appropriate message based on success or failure if success1 and success2: success_message += " Images uploaded successfully!" else: success_message += " Failed to upload images. Please try again." # Pass the success message to the template return render_template("index.html", success_message=success_message) return render_template("index.html", success_message=success_message) if __name__ == "__main__": app.run(debug=True)