abhicodes commited on
Commit
b109efe
·
verified ·
1 Parent(s): c779f75

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -7
app.py CHANGED
@@ -1,7 +1,6 @@
1
  import gradio as gr
2
  import os
3
  import shutil
4
- import json
5
  from pymongo.mongo_client import MongoClient
6
  from pymongo.server_api import ServerApi
7
  import urllib.parse
@@ -25,6 +24,16 @@ theme = gr.themes.Default(
25
  primary_hue="blue",
26
  secondary_hue="violet",
27
  neutral_hue="slate",
 
 
 
 
 
 
 
 
 
 
28
  )
29
 
30
  def upload(file, secret):
@@ -57,11 +66,13 @@ def get_folder_contents_dict(root_dir):
57
  def generate_markdown(json_data):
58
  urls = json_data.get("urls", [])
59
  markdown_lines = []
 
60
  for url in urls:
61
  encoded_url = urllib.parse.quote(url, safe=':/')
62
  filename = url.split('/')[-1]
63
- encoded_filename = urllib.parse.quote(filename)
64
- markdown_lines.append(f'- <a style="text-decoration:none; font-size:18px" href={encoded_url} target="_blank">{encoded_filename}</a>')
 
65
  return "\n".join(markdown_lines)
66
 
67
  def get_uploads(secret):
@@ -80,7 +91,7 @@ def get_notes(secret):
80
  if secret == os.environ.get('SECRET_KEY'):
81
  result = references_collection.find_one({"_id": ObjectId('6655fe4ca6913aa201819e72')}, {"_id": 0})
82
  if result:
83
- gr.Info("Recieved uploaded files")
84
  return result['notes']
85
  else:
86
  gr.Info("No result found")
@@ -102,6 +113,44 @@ def save_notes(note, secret):
102
  gr.Warning("Invalid Secret Key")
103
  return note
104
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
  js = '''
106
  function test() {
107
  document.title = "File Sharing System";
@@ -137,6 +186,10 @@ function test() {
137
  '''
138
 
139
  css = '''
 
 
 
 
140
  .hero-container {
141
  display: flex;
142
  flex-direction: row;
@@ -170,22 +223,32 @@ with gr.Blocks(theme=theme, js=js, css=css) as demo:
170
  with gr.Row():
171
  with gr.Column():
172
  gr.Markdown('''<h1 style="text-align:center;display:flex;justify-content:center;align-items:center"><img src="https://i.ibb.co/FB6tMdT/upload.png" alt="upload" width="50">Uploader</h1>''')
173
- input = gr.File(label="Input", file_count="multiple")
174
  input.upload(fn=upload, inputs=[input, secret_key])
175
  with gr.Column():
176
  gr.Markdown('''<h1 style="text-align:center;display:flex;justify-content:center;align-items:center"><img src="https://i.ibb.co/K9Bq3j7/download.png" alt="download" width="50">Downloader</h1>''')
177
  uploads = gr.Markdown(label="Uploads")
178
  get_upload_button = gr.Button("Get Uploads", variant='primary')
179
  get_upload_button.click(fn=get_uploads, inputs=secret_key, outputs=uploads)
 
 
 
 
 
 
 
 
 
 
 
180
 
181
  notes = gr.TextArea(label="📝 Share Notes", placeholder="Enter your notes...")
182
  with gr.Row():
183
- get_notes_button = gr.Button("Get Notes", variant="primary")
184
  get_notes_button.click(fn=get_notes, inputs=secret_key, outputs=notes)
185
  save_notes_button = gr.Button("Save", variant="primary")
186
  save_notes_button.click(fn=save_notes, inputs=[notes,secret_key], outputs=notes)
187
 
188
  gr.Markdown('''> To know more read the docs at: [Documentation](https://huggingface.co/spaces/abhicodes/file-sharing-system/blob/main/README.md)''')
189
 
190
-
191
  demo.launch(debug=True)
 
1
  import gradio as gr
2
  import os
3
  import shutil
 
4
  from pymongo.mongo_client import MongoClient
5
  from pymongo.server_api import ServerApi
6
  import urllib.parse
 
24
  primary_hue="blue",
25
  secondary_hue="violet",
26
  neutral_hue="slate",
27
+ ).set(
28
+ button_secondary_background_fill='*secondary_200',
29
+ button_secondary_background_fill_hover='*secondary_300',
30
+ button_secondary_background_fill_dark='*secondary_600',
31
+ button_secondary_background_fill_hover_dark='*secondary_500',
32
+ button_secondary_border_color='*secondary_200',
33
+ button_secondary_border_color_hover='*secondary_300',
34
+ button_secondary_border_color_dark='*secondary_600',
35
+ button_secondary_border_color_hover_dark='*secondary_500',
36
+ button_secondary_text_color='*secondary_700'
37
  )
38
 
39
  def upload(file, secret):
 
66
  def generate_markdown(json_data):
67
  urls = json_data.get("urls", [])
68
  markdown_lines = []
69
+ count = 1
70
  for url in urls:
71
  encoded_url = urllib.parse.quote(url, safe=':/')
72
  filename = url.split('/')[-1]
73
+ encoded_filename = urllib.parse.quote(filename).replace("%20", " ")
74
+ markdown_lines.append(f'> {count}. <a style="text-decoration:none;" href={encoded_url} target="_blank">{encoded_filename}</a>')
75
+ count += 1
76
  return "\n".join(markdown_lines)
77
 
78
  def get_uploads(secret):
 
91
  if secret == os.environ.get('SECRET_KEY'):
92
  result = references_collection.find_one({"_id": ObjectId('6655fe4ca6913aa201819e72')}, {"_id": 0})
93
  if result:
94
+ gr.Info("Recieved shared notes")
95
  return result['notes']
96
  else:
97
  gr.Info("No result found")
 
113
  gr.Warning("Invalid Secret Key")
114
  return note
115
 
116
+ def delete_path(path):
117
+ try:
118
+ if os.path.isfile(path):
119
+ os.remove(path)
120
+ filename = os.path.basename(path)
121
+ gr.Info(f"The file {filename} has been deleted successfully.")
122
+ elif os.path.isdir(path):
123
+ shutil.rmtree(path)
124
+ foldername = os.path.basename(path)
125
+ gr.Info(f"The directory {foldername} has been deleted successfully.")
126
+ else:
127
+ print(f"The path {path} does not exist.")
128
+ gr.Warning("Invalid action.")
129
+ except Exception as e:
130
+ print(f"An error occurred while deleting {path}: {e}")
131
+ gr.Warning("Invalid action.")
132
+
133
+ def delete_files(path, secret):
134
+ if secret == os.environ.get('SECRET_KEY'):
135
+ for paths in path:
136
+ delete_path(paths)
137
+ else:
138
+ gr.Warning("Invalid Secret Key")
139
+
140
+ return gr.update(root_dir="/tmp/gradio")
141
+
142
+ def toogle_visibility(secret):
143
+ if secret == os.environ.get('SECRET_KEY'):
144
+ return gr.update(visible=False), gr.update(visible=True), gr.update(visible=True)
145
+ else:
146
+ raise gr.Error("Invalid Secret Key")
147
+
148
+ def toogle_vis():
149
+ return gr.update(visible=True)
150
+
151
+ def toogle_vis2():
152
+ return gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)
153
+
154
  js = '''
155
  function test() {
156
  document.title = "File Sharing System";
 
186
  '''
187
 
188
  css = '''
189
+ body::-webkit-scrollbar {
190
+ display: none;
191
+ scroll-behavior: smooth;
192
+ }
193
  .hero-container {
194
  display: flex;
195
  flex-direction: row;
 
223
  with gr.Row():
224
  with gr.Column():
225
  gr.Markdown('''<h1 style="text-align:center;display:flex;justify-content:center;align-items:center"><img src="https://i.ibb.co/FB6tMdT/upload.png" alt="upload" width="50">Uploader</h1>''')
226
+ input = gr.File(label="Upload", file_count="multiple")
227
  input.upload(fn=upload, inputs=[input, secret_key])
228
  with gr.Column():
229
  gr.Markdown('''<h1 style="text-align:center;display:flex;justify-content:center;align-items:center"><img src="https://i.ibb.co/K9Bq3j7/download.png" alt="download" width="50">Downloader</h1>''')
230
  uploads = gr.Markdown(label="Uploads")
231
  get_upload_button = gr.Button("Get Uploads", variant='primary')
232
  get_upload_button.click(fn=get_uploads, inputs=secret_key, outputs=uploads)
233
+ initial_delete_button = gr.Button("Delete Uploads?", variant="stop", visible=True)
234
+ cancel_button = gr.Button("Cancel", variant="secondary", visible=False)
235
+ with gr.Column(visible=False) as temp:
236
+ gr.Markdown('''<h1 style="text-align:center;display:flex;justify-content:center;align-items:center"><img src="https://i.ibb.co/K9Bq3j7/download.png" alt="download" width="50">Navigator</h1>''')
237
+ file_exe = gr.FileExplorer(root_dir="/tmp/gradio")
238
+ final_delete_button = gr.Button("Delete", variant="stop", visible=False)
239
+ final_delete_button.click(fn=delete_files, inputs=[file_exe, secret_key], outputs=file_exe)
240
+ file_exe.change(fn=toogle_vis, outputs=final_delete_button)
241
+ initial_delete_button.click(fn=toogle_visibility, inputs=secret_key, outputs=[initial_delete_button, cancel_button, temp])
242
+ cancel_button.click(fn=toogle_vis2, outputs=[temp, initial_delete_button, cancel_button])
243
+
244
 
245
  notes = gr.TextArea(label="📝 Share Notes", placeholder="Enter your notes...")
246
  with gr.Row():
247
+ get_notes_button = gr.Button("Get Notes", variant="secondary")
248
  get_notes_button.click(fn=get_notes, inputs=secret_key, outputs=notes)
249
  save_notes_button = gr.Button("Save", variant="primary")
250
  save_notes_button.click(fn=save_notes, inputs=[notes,secret_key], outputs=notes)
251
 
252
  gr.Markdown('''> To know more read the docs at: [Documentation](https://huggingface.co/spaces/abhicodes/file-sharing-system/blob/main/README.md)''')
253
 
 
254
  demo.launch(debug=True)