patharanor commited on
Commit
04f0937
1 Parent(s): 6673dd9

initial project

Browse files
Files changed (2) hide show
  1. app.py +156 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import boto3
2
+ import gradio as gr
3
+ import os
4
+ from dotenv import load_dotenv
5
+ load_dotenv()
6
+
7
+ ACCOUNT_ID = os.getenv('AWS_ACCOUNT_ID', '')
8
+ AWS_ACCESS_KEY_ID = os.getenv('AWS_ACCESS_KEY_ID', '')
9
+ AWS_SECRET_ACCESS_KEY = os.getenv('AWS_SECRET_ACCESS_KEY', '')
10
+ AWS_DEFAULT_REGION = os.getenv('AWS_DEFAULT_REGION', '')
11
+ AWS_S3_BUCKET_NAME = os.getenv('AWS_S3_BUCKET_NAME', '')
12
+
13
+ is_dev = os.environ.get('ENV', 'DEV') != 'PROD'
14
+
15
+ s3_client = boto3.client(
16
+ service_name='s3',
17
+ region_name=AWS_DEFAULT_REGION,
18
+ aws_access_key_id=AWS_ACCESS_KEY_ID,
19
+ aws_secret_access_key=AWS_SECRET_ACCESS_KEY
20
+ )
21
+
22
+ def auth(username, password):
23
+ u = os.environ.get('USERNAME')
24
+ p = os.environ.get('PASSWORD')
25
+ return (username == u and password == p)
26
+
27
+ # Function to list images in a specific S3 bucket
28
+ def list_images_in_bucket(bucket_name):
29
+ response = s3_client.list_objects_v2(Bucket=bucket_name)
30
+ return [obj['Key'] for obj in response.get('Contents', []) if obj['Key'].lower().endswith(('.png', '.jpg', '.jpeg'))]
31
+
32
+ # Function to get image URLs
33
+ def get_image_urls(bucket_name, image_keys):
34
+ return [s3_client.generate_presigned_url('get_object', Params={'Bucket': bucket_name, 'Key': key}) for key in image_keys]
35
+
36
+ # Function to upload files to S3
37
+ def upload_files(files, progress=gr.Progress()):
38
+ for file in progress.tqdm(files, desc="Uploading..."):
39
+ file_name = os.path.basename(file.name)
40
+ with open(file.name, 'rb') as f:
41
+ print(f'Uploading "{file_name}"...')
42
+ s3_client.upload_fileobj(f, AWS_S3_BUCKET_NAME, file_name)
43
+
44
+ return display_images()
45
+
46
+ # Function to fetch and display images
47
+ def display_images():
48
+ image_keys = list_images_in_bucket(AWS_S3_BUCKET_NAME)
49
+ image_urls = get_image_urls(AWS_S3_BUCKET_NAME, image_keys)
50
+ # print(image_keys)
51
+ # print(image_urls)
52
+ return image_urls, image_keys
53
+
54
+ # Function to delete a file from S3
55
+ def delete_image(file_key):
56
+ if isinstance(file_key, list):
57
+ for k in file_key:
58
+ print(f'Removing "{k}"...')
59
+ s3_client.delete_object(Bucket=AWS_S3_BUCKET_NAME, Key=k)
60
+ else:
61
+ s3_client.delete_object(Bucket=AWS_S3_BUCKET_NAME, Key=file_key)
62
+ return display_images()
63
+
64
+ def update_dropdown_image(evt: gr.SelectData):
65
+ # print(f"You selected {evt.value} at {evt.index} from {evt.target}")
66
+ # print(f"You selected {evt.value['image']['orig_name']}")
67
+ return evt.value['image']['orig_name']
68
+
69
+ # ---------------------------------------------------------------------------
70
+
71
+ # Function to list images in a specific S3 bucket
72
+ def list_files_in_bucket(bucket_name):
73
+ response = s3_client.list_objects_v2(Bucket=bucket_name)
74
+ return [obj['Key'] for obj in response.get('Contents', [])]
75
+
76
+ # Function to get image URLs
77
+ def get_files_urls(bucket_name, file_keys):
78
+ return [s3_client.generate_presigned_url('get_object', Params={'Bucket': bucket_name, 'Key': key}) for key in file_keys]
79
+
80
+ # Function to fetch and display files
81
+ def display_files():
82
+ file_keys = list_files_in_bucket(AWS_S3_BUCKET_NAME)
83
+ file_urls = get_files_urls(AWS_S3_BUCKET_NAME, file_keys)
84
+ # print(file_keys)
85
+ # print(file_urls)
86
+ return file_urls, file_keys
87
+
88
+ # Function to delete a file from S3
89
+ def delete_files(file_key):
90
+ if isinstance(file_key, list):
91
+ for k in file_key:
92
+ print(f'Removing "{k}"...')
93
+ s3_client.delete_object(Bucket=AWS_S3_BUCKET_NAME, Key=k)
94
+ else:
95
+ s3_client.delete_object(Bucket=AWS_S3_BUCKET_NAME, Key=file_key)
96
+ return display_files()
97
+
98
+ def update_dropdown_files(evt: gr.SelectData):
99
+ #print(f"You selected {evt.value} at {evt.index} from {evt.target}")
100
+ return evt.value.split('AWSAccessKeyId')[0]
101
+
102
+ # Create a Gradio interface
103
+ with gr.Blocks() as demo:
104
+
105
+ gr.Markdown("# **Data collector**")
106
+
107
+ with gr.Row():
108
+ with gr.Column(scale=1):
109
+ gr.Markdown("""## **1. Upload files**
110
+
111
+ 1. โยนไฟล์ (drag/drop) ไปยัง ***ไฟล์ list ทาง panel ทางด้านซ้าย*** ได้มากกว่า 1 ไฟล์
112
+ 2. กด **Upload Files**
113
+ """)
114
+ file_uploader = gr.File(label="Upload Images", file_count="multiple", height=400)
115
+ upload_btn = gr.Button("Upload Files")
116
+
117
+ with gr.Column(scale=3):
118
+ with gr.Tabs():
119
+ with gr.Tab('Images'):
120
+ gr.Markdown("""## **2. Load images**
121
+ กดที่ **Refresh Gallery**
122
+ """)
123
+ img_gallery = gr.Gallery(label="Images from S3", columns=9, height=600, interactive=True)
124
+ img_refresh_btn = gr.Button("Refresh Gallery")
125
+
126
+ gr.Markdown("""### ***Delete images***
127
+ กรณีต้องการลบไฟล์ ท่านสามารถเลือกภาพที่สนใจ แล้วกด **Delete Selected Files** แต่ถ้าต้องการลบมากกว่า 1 file ให��ใส่ชื่อไฟล์ คั่นด้วย comma (,)
128
+ """)
129
+ img_selected_image = gr.Dropdown(choices=[], label="Select Image to Delete", allow_custom_value=True)
130
+ img_delete_btn = gr.Button("Delete Selected Files")
131
+
132
+ with gr.Tab('Files'):
133
+ gr.Markdown("""## **2. Load files**
134
+ กดที่ **Refresh Gallery**
135
+ """)
136
+ file_gallery = gr.File(label="Files from S3", interactive=True)
137
+ file_refresh_btn = gr.Button("Refresh Gallery")
138
+
139
+ gr.Markdown("""### ***Delete files***
140
+ กรณีต้องการลบไฟล์ ท่านสามารถเลือกภาพที่สนใจ แล้วกด **Delete Selected Files** แต่ถ้าต้องการลบมากกว่า 1 file ให้ใส่ชื่อไฟล์ คั่นด้วย comma (,)
141
+ """)
142
+ file_selected = gr.Dropdown(choices=[], label="Select File to Delete", allow_custom_value=True)
143
+ file_delete_btn = gr.Button("Delete Selected Files")
144
+
145
+ upload_btn.click(fn=upload_files, inputs=[file_uploader], outputs=[img_gallery, img_selected_image])
146
+
147
+ img_refresh_btn.click(fn=display_images, outputs=[img_gallery, img_selected_image])
148
+ img_delete_btn.click(fn=delete_image, inputs=img_selected_image, outputs=[img_gallery, img_selected_image])
149
+ img_gallery.select(fn=update_dropdown_image, outputs=img_selected_image)
150
+
151
+ file_refresh_btn.click(fn=display_files, outputs=[file_gallery, file_selected])
152
+ file_delete_btn.click(fn=delete_files, inputs=file_selected, outputs=[file_gallery, file_selected])
153
+ file_gallery.select(fn=update_dropdown_files, outputs=file_selected)
154
+
155
+ # Launch the Gradio app
156
+ demo.launch(auth=auth if not is_dev else None)
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ boto3
2
+ gradio
3
+ pillow
4
+ python-dotenv