|
import gradio as gr |
|
import os |
|
import requests |
|
import base64 |
|
import numpy as np |
|
from PIL import Image |
|
|
|
|
|
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 create_folder(person_name): |
|
folder_path = os.path.join(UPLOAD_FOLDER, person_name) |
|
|
|
if not os.path.exists(folder_path): |
|
os.makedirs(folder_path) |
|
return f"Folder '{folder_path}' created locally." |
|
else: |
|
return f"Folder '{folder_path}' already exists." |
|
|
|
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): |
|
|
|
create_folder(person_name) |
|
|
|
|
|
image1_path = os.path.join(UPLOAD_FOLDER, person_name, "1.jpg") |
|
image2_path = os.path.join(UPLOAD_FOLDER, person_name, "2.jpg") |
|
|
|
|
|
Image.fromarray(image1).save(image1_path) |
|
Image.fromarray(image2).save(image2_path) |
|
|
|
success_message = f"Folder '{person_name}' is ready." |
|
|
|
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(share=True) |
|
|