theguywhosucks commited on
Commit
aa8c7dc
·
verified ·
1 Parent(s): 438c3dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -17
app.py CHANGED
@@ -5,24 +5,29 @@ import time
5
  import gradio as gr
6
  from gradio_client import Client, handle_file
7
 
 
 
 
 
8
  SPACE = "pockit-cloud/main"
9
  client = Client(SPACE)
10
 
11
  SPACE_URL = os.getenv("SPACE_URL", "https://pockit-cloud-main.hf.space")
12
- HF_TOKEN = os.getenv("HF_TOKEN")
13
 
14
  TMP_DIR = "tmp"
15
  os.makedirs(TMP_DIR, exist_ok=True)
16
 
17
  # -------------------------
18
- # CLEAN OLD TEMP FILES
19
  # -------------------------
20
  def cleanup_tmp():
21
  now = time.time()
22
  for f in os.listdir(TMP_DIR):
23
  path = os.path.join(TMP_DIR, f)
24
- if os.path.isfile(path) and now - os.path.getmtime(path) > 3600:
25
- os.remove(path)
 
26
 
27
  # -------------------------
28
  # LOGIN
@@ -59,7 +64,7 @@ def list_files(user_id, password):
59
  return str(e), []
60
 
61
  # -------------------------
62
- # UPLOAD FILE
63
  # -------------------------
64
  def upload_file(user_id, password, file, custom_name):
65
 
@@ -82,19 +87,21 @@ def upload_file(user_id, password, file, custom_name):
82
  return str(e)
83
 
84
  # -------------------------
85
- # DOWNLOAD USING WGET MIRROR
 
86
  # -------------------------
87
  def download_file(user_id, password, filename):
88
 
89
  if not user_id or not password:
90
- return "Username and password required"
91
 
92
  if not filename:
93
- return "No file selected"
94
 
95
  try:
96
  cleanup_tmp()
97
 
 
98
  link = client.predict(
99
  user_id=user_id,
100
  password=password,
@@ -114,18 +121,20 @@ def download_file(user_id, password, filename):
114
  link
115
  ]
116
 
 
117
  if HF_TOKEN:
118
  cmd.insert(1, f"--header=Authorization: Bearer {HF_TOKEN}")
119
 
120
  subprocess.run(cmd, check=True)
121
 
122
- return f"{SPACE_URL}/file={temp_path}"
123
 
124
  except Exception as e:
125
- return str(e)
 
126
 
127
  # -------------------------
128
- # DELETE FILE
129
  # -------------------------
130
  def delete_file(user_id, password, filename):
131
 
@@ -197,10 +206,10 @@ with gr.Blocks(title="Pockit Cloud Client") as app:
197
  outputs=[status_box, file_list]
198
  )
199
 
200
- gr.Markdown("### Download File")
201
 
202
- download_btn = gr.Button("Download")
203
- download_output = gr.Textbox(label="Download Link")
204
 
205
  download_btn.click(
206
  download_file,
@@ -208,7 +217,7 @@ with gr.Blocks(title="Pockit Cloud Client") as app:
208
  outputs=download_output
209
  )
210
 
211
- gr.Markdown("### Upload File")
212
 
213
  upload_input = gr.File(label="Select File")
214
  custom_name = gr.Textbox(label="Custom Name (Optional)")
@@ -221,7 +230,7 @@ with gr.Blocks(title="Pockit Cloud Client") as app:
221
  outputs=upload_status
222
  )
223
 
224
- gr.Markdown("### Delete File")
225
 
226
  delete_btn = gr.Button("Delete Selected File")
227
  delete_status = gr.Textbox(label="Delete Status")
@@ -243,5 +252,5 @@ with gr.Blocks(title="Pockit Cloud Client") as app:
243
  outputs=change_status
244
  )
245
 
246
- # IMPORTANT: allow tmp folder
247
  app.launch(allowed_paths=[TMP_DIR])
 
5
  import gradio as gr
6
  from gradio_client import Client, handle_file
7
 
8
+ # -------------------------
9
+ # CONFIG
10
+ # -------------------------
11
+
12
  SPACE = "pockit-cloud/main"
13
  client = Client(SPACE)
14
 
15
  SPACE_URL = os.getenv("SPACE_URL", "https://pockit-cloud-main.hf.space")
16
+ HF_TOKEN = os.getenv("HF_TOKEN") # Secret for private dataset access
17
 
18
  TMP_DIR = "tmp"
19
  os.makedirs(TMP_DIR, exist_ok=True)
20
 
21
  # -------------------------
22
+ # CLEAN TEMP FILES
23
  # -------------------------
24
  def cleanup_tmp():
25
  now = time.time()
26
  for f in os.listdir(TMP_DIR):
27
  path = os.path.join(TMP_DIR, f)
28
+ if os.path.isfile(path):
29
+ if now - os.path.getmtime(path) > 3600: # 1 hour
30
+ os.remove(path)
31
 
32
  # -------------------------
33
  # LOGIN
 
64
  return str(e), []
65
 
66
  # -------------------------
67
+ # UPLOAD
68
  # -------------------------
69
  def upload_file(user_id, password, file, custom_name):
70
 
 
87
  return str(e)
88
 
89
  # -------------------------
90
+ # DOWNLOAD (PRIVATE SAFE VERSION)
91
+ # Returns file directly to gr.File
92
  # -------------------------
93
  def download_file(user_id, password, filename):
94
 
95
  if not user_id or not password:
96
+ return None
97
 
98
  if not filename:
99
+ return None
100
 
101
  try:
102
  cleanup_tmp()
103
 
104
+ # Get private download link
105
  link = client.predict(
106
  user_id=user_id,
107
  password=password,
 
121
  link
122
  ]
123
 
124
+ # Add HuggingFace token if available
125
  if HF_TOKEN:
126
  cmd.insert(1, f"--header=Authorization: Bearer {HF_TOKEN}")
127
 
128
  subprocess.run(cmd, check=True)
129
 
130
+ return temp_path # IMPORTANT: return path to gr.File
131
 
132
  except Exception as e:
133
+ print(e)
134
+ return None
135
 
136
  # -------------------------
137
+ # DELETE
138
  # -------------------------
139
  def delete_file(user_id, password, filename):
140
 
 
206
  outputs=[status_box, file_list]
207
  )
208
 
209
+ gr.Markdown("### Download")
210
 
211
+ download_btn = gr.Button("Download File")
212
+ download_output = gr.File(label="Downloaded File")
213
 
214
  download_btn.click(
215
  download_file,
 
217
  outputs=download_output
218
  )
219
 
220
+ gr.Markdown("### Upload")
221
 
222
  upload_input = gr.File(label="Select File")
223
  custom_name = gr.Textbox(label="Custom Name (Optional)")
 
230
  outputs=upload_status
231
  )
232
 
233
+ gr.Markdown("### Delete")
234
 
235
  delete_btn = gr.Button("Delete Selected File")
236
  delete_status = gr.Textbox(label="Delete Status")
 
252
  outputs=change_status
253
  )
254
 
255
+ # IMPORTANT: allow tmp folder for serving
256
  app.launch(allowed_paths=[TMP_DIR])