Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,27 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from gradio_client import Client, handle_file
|
| 4 |
|
| 5 |
SPACE = "pockit-cloud/main"
|
| 6 |
client = Client(SPACE)
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
# -------------------------
|
| 9 |
# LOGIN
|
| 10 |
# -------------------------
|
|
@@ -63,7 +80,7 @@ def upload_file(user_id, password, file, custom_name):
|
|
| 63 |
return str(e)
|
| 64 |
|
| 65 |
# -------------------------
|
| 66 |
-
#
|
| 67 |
# -------------------------
|
| 68 |
def download_file(user_id, password, filename):
|
| 69 |
|
|
@@ -74,6 +91,8 @@ def download_file(user_id, password, filename):
|
|
| 74 |
return "No file selected"
|
| 75 |
|
| 76 |
try:
|
|
|
|
|
|
|
| 77 |
link = client.predict(
|
| 78 |
user_id=user_id,
|
| 79 |
password=password,
|
|
@@ -81,7 +100,16 @@ def download_file(user_id, password, filename):
|
|
| 81 |
api_name="/get_download_link_action"
|
| 82 |
)
|
| 83 |
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
except Exception as e:
|
| 86 |
return str(e)
|
| 87 |
|
|
@@ -103,7 +131,6 @@ def delete_file(user_id, password, filename):
|
|
| 103 |
filename=filename,
|
| 104 |
api_name="/delete_file_secure"
|
| 105 |
)
|
| 106 |
-
|
| 107 |
return result
|
| 108 |
except Exception as e:
|
| 109 |
return str(e)
|
|
@@ -165,9 +192,9 @@ with gr.Blocks(title="Pockit Cloud Client") as app:
|
|
| 165 |
outputs=[status_box, file_list]
|
| 166 |
)
|
| 167 |
|
| 168 |
-
gr.Markdown("###
|
| 169 |
|
| 170 |
-
download_btn = gr.Button("
|
| 171 |
download_output = gr.Textbox(label="Download Link")
|
| 172 |
|
| 173 |
download_btn.click(
|
|
|
|
| 1 |
import os
|
| 2 |
+
import uuid
|
| 3 |
+
import subprocess
|
| 4 |
+
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 |
+
TMP_DIR = "tmp"
|
| 12 |
+
os.makedirs(TMP_DIR, exist_ok=True)
|
| 13 |
+
|
| 14 |
+
# -------------------------
|
| 15 |
+
# CLEAN OLD TEMP FILES
|
| 16 |
+
# -------------------------
|
| 17 |
+
def cleanup_tmp():
|
| 18 |
+
now = time.time()
|
| 19 |
+
for f in os.listdir(TMP_DIR):
|
| 20 |
+
path = os.path.join(TMP_DIR, f)
|
| 21 |
+
if os.path.isfile(path):
|
| 22 |
+
if now - os.path.getmtime(path) > 3600: # 1 hour
|
| 23 |
+
os.remove(path)
|
| 24 |
+
|
| 25 |
# -------------------------
|
| 26 |
# LOGIN
|
| 27 |
# -------------------------
|
|
|
|
| 80 |
return str(e)
|
| 81 |
|
| 82 |
# -------------------------
|
| 83 |
+
# DOWNLOAD USING WGET TEMP
|
| 84 |
# -------------------------
|
| 85 |
def download_file(user_id, password, filename):
|
| 86 |
|
|
|
|
| 91 |
return "No file selected"
|
| 92 |
|
| 93 |
try:
|
| 94 |
+
cleanup_tmp()
|
| 95 |
+
|
| 96 |
link = client.predict(
|
| 97 |
user_id=user_id,
|
| 98 |
password=password,
|
|
|
|
| 100 |
api_name="/get_download_link_action"
|
| 101 |
)
|
| 102 |
|
| 103 |
+
temp_name = f"{uuid.uuid4()}_{filename}"
|
| 104 |
+
temp_path = os.path.join(TMP_DIR, temp_name)
|
| 105 |
+
|
| 106 |
+
subprocess.run(
|
| 107 |
+
["wget", "-q", "-O", temp_path, link],
|
| 108 |
+
check=True
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
return f"/file={temp_path}"
|
| 112 |
+
|
| 113 |
except Exception as e:
|
| 114 |
return str(e)
|
| 115 |
|
|
|
|
| 131 |
filename=filename,
|
| 132 |
api_name="/delete_file_secure"
|
| 133 |
)
|
|
|
|
| 134 |
return result
|
| 135 |
except Exception as e:
|
| 136 |
return str(e)
|
|
|
|
| 192 |
outputs=[status_box, file_list]
|
| 193 |
)
|
| 194 |
|
| 195 |
+
gr.Markdown("### Download File")
|
| 196 |
|
| 197 |
+
download_btn = gr.Button("Download (Temporary Mirror)")
|
| 198 |
download_output = gr.Textbox(label="Download Link")
|
| 199 |
|
| 200 |
download_btn.click(
|