File size: 3,468 Bytes
0ff36bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import os
import requests
import base64

# GitHub personal access token (replace this with your actual token)
GITHUB_TOKEN = "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"  # Use your own token!
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)

# Ensure the uploads directory exists
UPLOAD_FOLDER = 'uploads'
if not os.path.exists(UPLOAD_FOLDER):
    os.makedirs(UPLOAD_FOLDER)

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."

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}")

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}"

    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)

    return response.status_code == 201

def upload_files_to_github(person_name, image1, image2):
    image1_path = os.path.join(UPLOAD_FOLDER, image1.name)
    image2_path = os.path.join(UPLOAD_FOLDER, image2.name)

    image1.save(image1_path)
    image2.save(image2_path)

    success_message = check_folder_exists(person_name)

    success1 = upload_image(image1_path, person_name, "1.jpg")
    success2 = upload_image(image2_path, person_name, "2.jpg")

    os.remove(image1_path)
    os.remove(image2_path)

    if success1 and success2:
        success_message += " Images uploaded successfully!"
    else:
        success_message += " Failed to upload images. Please try again."
    
    return success_message

def gradio_interface(person_name, image1, image2):
    return upload_files_to_github(person_name, image1, image2)

# Create a Gradio interface
iface = gr.Interface(
    fn=gradio_interface,
    inputs=[gr.Textbox(label="Person's Name"), gr.Image(label="Image 1"), gr.Image(label="Image 2")],
    outputs="text"
)

if __name__ == "__main__":
    iface.launch()