Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, render_template, request, redirect, url_for, flash
|
2 |
+
import base64
|
3 |
+
import os
|
4 |
+
import requests
|
5 |
+
|
6 |
+
# Your GitHub personal access token (replace this with your actual token)
|
7 |
+
GITHUB_TOKEN = "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" # Use your own token!
|
8 |
+
|
9 |
+
# GitHub repository details
|
10 |
+
OWNER = "LAMENTIS1" # GitHub username or organization
|
11 |
+
REPO = "face_recog" # GitHub repository name
|
12 |
+
BRANCH = "master" # Branch to commit to (could be 'main' or any other branch)
|
13 |
+
|
14 |
+
# Flask app setup
|
15 |
+
app = Flask(__name__)
|
16 |
+
|
17 |
+
# Set the secret key for flashing messages
|
18 |
+
app.secret_key = 'your_secret_key'
|
19 |
+
|
20 |
+
# Ensure the uploads directory exists
|
21 |
+
UPLOAD_FOLDER = 'uploads'
|
22 |
+
if not os.path.exists(UPLOAD_FOLDER):
|
23 |
+
os.makedirs(UPLOAD_FOLDER)
|
24 |
+
|
25 |
+
# Ensure the directory exists by checking if the file or folder exists in the repo
|
26 |
+
def check_folder_exists(person_name):
|
27 |
+
api_url = f"https://api.github.com/repos/{OWNER}/{REPO}/contents/labeled_images/{person_name.replace(' ', '%20')}"
|
28 |
+
response = requests.get(api_url, headers={"Authorization": f"Bearer {GITHUB_TOKEN}"})
|
29 |
+
|
30 |
+
if response.status_code == 404:
|
31 |
+
create_dummy_file(person_name)
|
32 |
+
return f"Folder 'labeled_images/{person_name}' created successfully."
|
33 |
+
else:
|
34 |
+
return f"Folder 'labeled_images/{person_name}' already exists."
|
35 |
+
|
36 |
+
# Create a dummy file in the folder to ensure it exists
|
37 |
+
def create_dummy_file(person_name):
|
38 |
+
api_url = f"https://api.github.com/repos/{OWNER}/{REPO}/contents/labeled_images/{person_name.replace(' ', '%20')}/.dummy"
|
39 |
+
data = {
|
40 |
+
"message": f"Create folder for {person_name}",
|
41 |
+
"branch": BRANCH,
|
42 |
+
"content": base64.b64encode(b"").decode(), # Empty content for the dummy file
|
43 |
+
}
|
44 |
+
|
45 |
+
headers = {
|
46 |
+
"Authorization": f"Bearer {GITHUB_TOKEN}",
|
47 |
+
"Content-Type": "application/json",
|
48 |
+
}
|
49 |
+
|
50 |
+
response = requests.put(api_url, json=data, headers=headers)
|
51 |
+
|
52 |
+
if response.status_code == 201:
|
53 |
+
print(f"Folder 'labeled_images/{person_name}' created successfully.")
|
54 |
+
else:
|
55 |
+
print(f"Failed to create folder: {response.status_code} - {response.text}")
|
56 |
+
|
57 |
+
# Function to upload a file to GitHub using the API
|
58 |
+
def upload_image(image_path, person_name, file_name):
|
59 |
+
api_url = f"https://api.github.com/repos/{OWNER}/{REPO}/contents/labeled_images/{person_name.replace(' ', '%20')}/{file_name}"
|
60 |
+
|
61 |
+
# Read the image and encode it in base64
|
62 |
+
with open(image_path, "rb") as image_file:
|
63 |
+
encoded_image = base64.b64encode(image_file.read()).decode()
|
64 |
+
|
65 |
+
data = {
|
66 |
+
"message": f"Add {file_name} for {person_name}",
|
67 |
+
"branch": BRANCH,
|
68 |
+
"content": encoded_image,
|
69 |
+
}
|
70 |
+
|
71 |
+
headers = {
|
72 |
+
"Authorization": f"Bearer {GITHUB_TOKEN}",
|
73 |
+
"Content-Type": "application/json",
|
74 |
+
}
|
75 |
+
|
76 |
+
response = requests.put(api_url, json=data, headers=headers)
|
77 |
+
|
78 |
+
if response.status_code == 201:
|
79 |
+
return True
|
80 |
+
else:
|
81 |
+
return False
|
82 |
+
|
83 |
+
@app.route("/", methods=["GET", "POST"])
|
84 |
+
def index():
|
85 |
+
success_message = ""
|
86 |
+
|
87 |
+
if request.method == "POST":
|
88 |
+
# Get username
|
89 |
+
person_name = request.form["username"]
|
90 |
+
|
91 |
+
# Get files from the form
|
92 |
+
image1 = request.files["image1"]
|
93 |
+
image2 = request.files["image2"]
|
94 |
+
|
95 |
+
# Save images locally temporarily
|
96 |
+
image1_path = os.path.join(UPLOAD_FOLDER, image1.filename)
|
97 |
+
image2_path = os.path.join(UPLOAD_FOLDER, image2.filename)
|
98 |
+
image1.save(image1_path)
|
99 |
+
image2.save(image2_path)
|
100 |
+
|
101 |
+
# Ensure the folder for the user exists on GitHub and capture success/failure message
|
102 |
+
success_message = check_folder_exists(person_name)
|
103 |
+
|
104 |
+
# Upload images to GitHub
|
105 |
+
success1 = upload_image(image1_path, person_name, "1.jpg")
|
106 |
+
success2 = upload_image(image2_path, person_name, "2.jpg")
|
107 |
+
|
108 |
+
# Remove the temporary files after uploading
|
109 |
+
os.remove(image1_path)
|
110 |
+
os.remove(image2_path)
|
111 |
+
|
112 |
+
# Show appropriate message based on success or failure
|
113 |
+
if success1 and success2:
|
114 |
+
success_message += " Images uploaded successfully!"
|
115 |
+
else:
|
116 |
+
success_message += " Failed to upload images. Please try again."
|
117 |
+
|
118 |
+
# Pass the success message to the template
|
119 |
+
return render_template("index.html", success_message=success_message)
|
120 |
+
|
121 |
+
return render_template("index.html", success_message=success_message)
|
122 |
+
|
123 |
+
if __name__ == "__main__":
|
124 |
+
app.run(debug=True)
|