srivatsavdamaraju commited on
Commit
0ff36bb
·
verified ·
1 Parent(s): ab31073

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -0
app.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 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!
8
+ 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
+ def check_folder_exists(person_name):
18
+ api_url = f"https://api.github.com/repos/{OWNER}/{REPO}/contents/labeled_images/{person_name.replace(' ', '%20')}"
19
+ response = requests.get(api_url, headers={"Authorization": f"Bearer {GITHUB_TOKEN}"})
20
+
21
+ if response.status_code == 404:
22
+ create_dummy_file(person_name)
23
+ return f"Folder 'labeled_images/{person_name}' created successfully."
24
+ else:
25
+ return f"Folder 'labeled_images/{person_name}' already exists."
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}"
49
+
50
+ with open(image_path, "rb") as image_file:
51
+ encoded_image = base64.b64encode(image_file.read()).decode()
52
+
53
+ data = {
54
+ "message": f"Add {file_name} for {person_name}",
55
+ "branch": BRANCH,
56
+ "content": encoded_image,
57
+ }
58
+
59
+ headers = {
60
+ "Authorization": f"Bearer {GITHUB_TOKEN}",
61
+ "Content-Type": "application/json",
62
+ }
63
+
64
+ response = requests.put(api_url, json=data, headers=headers)
65
+
66
+ return response.status_code == 201
67
+
68
+ def upload_files_to_github(person_name, image1, image2):
69
+ image1_path = os.path.join(UPLOAD_FOLDER, image1.name)
70
+ image2_path = os.path.join(UPLOAD_FOLDER, image2.name)
71
+
72
+ image1.save(image1_path)
73
+ image2.save(image2_path)
74
+
75
+ success_message = check_folder_exists(person_name)
76
+
77
+ success1 = upload_image(image1_path, person_name, "1.jpg")
78
+ success2 = upload_image(image2_path, person_name, "2.jpg")
79
+
80
+ os.remove(image1_path)
81
+ os.remove(image2_path)
82
+
83
+ if success1 and success2:
84
+ success_message += " Images uploaded successfully!"
85
+ else:
86
+ success_message += " Failed to upload images. Please try again."
87
+
88
+ return success_message
89
+
90
+ def gradio_interface(person_name, image1, image2):
91
+ return upload_files_to_github(person_name, image1, image2)
92
+
93
+ # Create a Gradio interface
94
+ iface = gr.Interface(
95
+ fn=gradio_interface,
96
+ inputs=[gr.Textbox(label="Person's Name"), gr.Image(label="Image 1"), gr.Image(label="Image 2")],
97
+ outputs="text"
98
+ )
99
+
100
+ if __name__ == "__main__":
101
+ iface.launch()