Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,8 @@ import gradio as gr
|
|
2 |
import os
|
3 |
import requests
|
4 |
import base64
|
|
|
|
|
5 |
|
6 |
# GitHub personal access token (replace this with your actual token)
|
7 |
GITHUB_TOKEN = "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" # Use your own token!
|
@@ -9,40 +11,20 @@ OWNER = "LAMENTIS1" # GitHub username or organization
|
|
9 |
REPO = "face_recog" # GitHub repository name
|
10 |
BRANCH = "master" # Branch to commit to (could be 'main' or any other branch)
|
11 |
|
12 |
-
# Ensure the uploads directory exists
|
13 |
UPLOAD_FOLDER = 'uploads'
|
14 |
if not os.path.exists(UPLOAD_FOLDER):
|
15 |
os.makedirs(UPLOAD_FOLDER)
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
if
|
22 |
-
|
23 |
-
return f"Folder '
|
24 |
else:
|
25 |
-
return f"Folder '
|
26 |
-
|
27 |
-
def create_dummy_file(person_name):
|
28 |
-
api_url = f"https://api.github.com/repos/{OWNER}/{REPO}/contents/labeled_images/{person_name.replace(' ', '%20')}/.dummy"
|
29 |
-
data = {
|
30 |
-
"message": f"Create folder for {person_name}",
|
31 |
-
"branch": BRANCH,
|
32 |
-
"content": base64.b64encode(b"").decode(), # Empty content for the dummy file
|
33 |
-
}
|
34 |
-
|
35 |
-
headers = {
|
36 |
-
"Authorization": f"Bearer {GITHUB_TOKEN}",
|
37 |
-
"Content-Type": "application/json",
|
38 |
-
}
|
39 |
-
|
40 |
-
response = requests.put(api_url, json=data, headers=headers)
|
41 |
-
|
42 |
-
if response.status_code == 201:
|
43 |
-
print(f"Folder 'labeled_images/{person_name}' created successfully.")
|
44 |
-
else:
|
45 |
-
print(f"Failed to create folder: {response.status_code} - {response.text}")
|
46 |
|
47 |
def upload_image(image_path, person_name, file_name):
|
48 |
api_url = f"https://api.github.com/repos/{OWNER}/{REPO}/contents/labeled_images/{person_name.replace(' ', '%20')}/{file_name}"
|
@@ -66,13 +48,18 @@ def upload_image(image_path, person_name, file_name):
|
|
66 |
return response.status_code == 201
|
67 |
|
68 |
def upload_files_to_github(person_name, image1, image2):
|
69 |
-
|
70 |
-
|
|
|
|
|
|
|
|
|
71 |
|
72 |
-
|
73 |
-
|
|
|
74 |
|
75 |
-
success_message =
|
76 |
|
77 |
success1 = upload_image(image1_path, person_name, "1.jpg")
|
78 |
success2 = upload_image(image2_path, person_name, "2.jpg")
|
@@ -98,4 +85,4 @@ iface = gr.Interface(
|
|
98 |
)
|
99 |
|
100 |
if __name__ == "__main__":
|
101 |
-
iface.launch()
|
|
|
2 |
import os
|
3 |
import requests
|
4 |
import base64
|
5 |
+
import numpy as np
|
6 |
+
from PIL import Image
|
7 |
|
8 |
# GitHub personal access token (replace this with your actual token)
|
9 |
GITHUB_TOKEN = "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" # Use your own token!
|
|
|
11 |
REPO = "face_recog" # GitHub repository name
|
12 |
BRANCH = "master" # Branch to commit to (could be 'main' or any other branch)
|
13 |
|
14 |
+
# Ensure the uploads directory exists locally
|
15 |
UPLOAD_FOLDER = 'uploads'
|
16 |
if not os.path.exists(UPLOAD_FOLDER):
|
17 |
os.makedirs(UPLOAD_FOLDER)
|
18 |
|
19 |
+
# Create the folder on the GitHub repo for the person
|
20 |
+
def create_folder(person_name):
|
21 |
+
folder_path = os.path.join(UPLOAD_FOLDER, person_name)
|
22 |
+
# If the folder doesn't exist locally, create it
|
23 |
+
if not os.path.exists(folder_path):
|
24 |
+
os.makedirs(folder_path)
|
25 |
+
return f"Folder '{folder_path}' created locally."
|
26 |
else:
|
27 |
+
return f"Folder '{folder_path}' already exists."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
def upload_image(image_path, person_name, file_name):
|
30 |
api_url = f"https://api.github.com/repos/{OWNER}/{REPO}/contents/labeled_images/{person_name.replace(' ', '%20')}/{file_name}"
|
|
|
48 |
return response.status_code == 201
|
49 |
|
50 |
def upload_files_to_github(person_name, image1, image2):
|
51 |
+
# Create folder locally for the user
|
52 |
+
create_folder(person_name)
|
53 |
+
|
54 |
+
# Save images to temporary files
|
55 |
+
image1_path = os.path.join(UPLOAD_FOLDER, person_name, "1.jpg")
|
56 |
+
image2_path = os.path.join(UPLOAD_FOLDER, person_name, "2.jpg")
|
57 |
|
58 |
+
# Convert numpy.ndarray to an image file and save
|
59 |
+
Image.fromarray(image1).save(image1_path)
|
60 |
+
Image.fromarray(image2).save(image2_path)
|
61 |
|
62 |
+
success_message = f"Folder '{person_name}' is ready."
|
63 |
|
64 |
success1 = upload_image(image1_path, person_name, "1.jpg")
|
65 |
success2 = upload_image(image2_path, person_name, "2.jpg")
|
|
|
85 |
)
|
86 |
|
87 |
if __name__ == "__main__":
|
88 |
+
iface.launch(share=True) # This will create a public link
|