import gradio as gr import os import requests import base64 import numpy as np from PIL import Image # 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 locally UPLOAD_FOLDER = 'uploads' if not os.path.exists(UPLOAD_FOLDER): os.makedirs(UPLOAD_FOLDER) # Create the folder on the GitHub repo for the person def create_folder(person_name): folder_path = os.path.join(UPLOAD_FOLDER, person_name) # If the folder doesn't exist locally, create it 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 locally for the user create_folder(person_name) # Save images to temporary files image1_path = os.path.join(UPLOAD_FOLDER, person_name, "1.jpg") image2_path = os.path.join(UPLOAD_FOLDER, person_name, "2.jpg") # Convert numpy.ndarray to an image file and save 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) # 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(share=True) # This will create a public link