|
import gradio as gr |
|
import os |
|
import requests |
|
import base64 |
|
|
|
|
|
GITHUB_TOKEN = "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" |
|
OWNER = "LAMENTIS1" |
|
REPO = "face_recog" |
|
BRANCH = "master" |
|
|
|
|
|
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(), |
|
} |
|
|
|
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) |
|
|
|
|
|
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() |
|
|