Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,7 @@
|
|
| 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 |
# -------------------------
|
|
@@ -12,22 +11,7 @@ from gradio_client import Client, handle_file
|
|
| 12 |
SPACE = "pockit-cloud/main"
|
| 13 |
client = Client(SPACE)
|
| 14 |
|
| 15 |
-
|
| 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,31 +48,8 @@ def list_files(user_id, password):
|
|
| 64 |
return str(e), []
|
| 65 |
|
| 66 |
# -------------------------
|
| 67 |
-
#
|
| 68 |
-
#
|
| 69 |
-
def upload_file(user_id, password, file, custom_name):
|
| 70 |
-
|
| 71 |
-
if not user_id or not password:
|
| 72 |
-
return "Username and password required"
|
| 73 |
-
|
| 74 |
-
if not file:
|
| 75 |
-
return "No file selected"
|
| 76 |
-
|
| 77 |
-
try:
|
| 78 |
-
result = client.predict(
|
| 79 |
-
user_id=user_id,
|
| 80 |
-
password=password,
|
| 81 |
-
filepath=handle_file(file),
|
| 82 |
-
custom_name=custom_name if custom_name else os.path.basename(file),
|
| 83 |
-
api_name="/upload_file_secure"
|
| 84 |
-
)
|
| 85 |
-
return result[0]
|
| 86 |
-
except Exception as e:
|
| 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 |
|
|
@@ -99,9 +60,7 @@ def download_file(user_id, password, filename):
|
|
| 99 |
return None
|
| 100 |
|
| 101 |
try:
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
# Get private download link
|
| 105 |
link = client.predict(
|
| 106 |
user_id=user_id,
|
| 107 |
password=password,
|
|
@@ -109,30 +68,49 @@ def download_file(user_id, password, filename):
|
|
| 109 |
api_name="/get_download_link_action"
|
| 110 |
)
|
| 111 |
|
| 112 |
-
|
| 113 |
-
temp_path = os.path.join(TMP_DIR, temp_name)
|
| 114 |
-
|
| 115 |
-
cmd = [
|
| 116 |
-
"wget",
|
| 117 |
-
"--tries=3",
|
| 118 |
-
"--no-check-certificate",
|
| 119 |
-
"-O",
|
| 120 |
-
temp_path,
|
| 121 |
-
link
|
| 122 |
-
]
|
| 123 |
-
|
| 124 |
-
# Add HuggingFace token if available
|
| 125 |
if HF_TOKEN:
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
-
return
|
| 131 |
|
| 132 |
except Exception as e:
|
| 133 |
print(e)
|
| 134 |
return None
|
| 135 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
# -------------------------
|
| 137 |
# DELETE
|
| 138 |
# -------------------------
|
|
@@ -252,5 +230,4 @@ with gr.Blocks(title="Pockit Cloud Client") as app:
|
|
| 252 |
outputs=change_status
|
| 253 |
)
|
| 254 |
|
| 255 |
-
|
| 256 |
-
app.launch(allowed_paths=[TMP_DIR])
|
|
|
|
| 1 |
import os
|
| 2 |
import uuid
|
|
|
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
+
import requests
|
| 5 |
from gradio_client import Client, handle_file
|
| 6 |
|
| 7 |
# -------------------------
|
|
|
|
| 11 |
SPACE = "pockit-cloud/main"
|
| 12 |
client = Client(SPACE)
|
| 13 |
|
| 14 |
+
HF_TOKEN = os.getenv("HF_TOKEN") # Secret for private dataset
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# -------------------------
|
| 17 |
# LOGIN
|
|
|
|
| 48 |
return str(e), []
|
| 49 |
|
| 50 |
# -------------------------
|
| 51 |
+
# DOWNLOAD (STREAMED + PRIVATE SAFE)
|
| 52 |
+
# Returns direct file for automatic link generation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
# -------------------------
|
| 54 |
def download_file(user_id, password, filename):
|
| 55 |
|
|
|
|
| 60 |
return None
|
| 61 |
|
| 62 |
try:
|
| 63 |
+
# Get private download link from backend
|
|
|
|
|
|
|
| 64 |
link = client.predict(
|
| 65 |
user_id=user_id,
|
| 66 |
password=password,
|
|
|
|
| 68 |
api_name="/get_download_link_action"
|
| 69 |
)
|
| 70 |
|
| 71 |
+
headers = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
if HF_TOKEN:
|
| 73 |
+
headers["Authorization"] = f"Bearer {HF_TOKEN}"
|
| 74 |
+
|
| 75 |
+
r = requests.get(link, headers=headers, stream=True)
|
| 76 |
+
r.raise_for_status()
|
| 77 |
+
|
| 78 |
+
temp_name = f"{uuid.uuid4()}_{filename}"
|
| 79 |
|
| 80 |
+
with open(temp_name, "wb") as f:
|
| 81 |
+
for chunk in r.iter_content(chunk_size=8192):
|
| 82 |
+
if chunk:
|
| 83 |
+
f.write(chunk)
|
| 84 |
|
| 85 |
+
return temp_name # Gradio auto-generates real link
|
| 86 |
|
| 87 |
except Exception as e:
|
| 88 |
print(e)
|
| 89 |
return None
|
| 90 |
|
| 91 |
+
# -------------------------
|
| 92 |
+
# UPLOAD
|
| 93 |
+
# -------------------------
|
| 94 |
+
def upload_file(user_id, password, file, custom_name):
|
| 95 |
+
|
| 96 |
+
if not user_id or not password:
|
| 97 |
+
return "Username and password required"
|
| 98 |
+
|
| 99 |
+
if not file:
|
| 100 |
+
return "No file selected"
|
| 101 |
+
|
| 102 |
+
try:
|
| 103 |
+
result = client.predict(
|
| 104 |
+
user_id=user_id,
|
| 105 |
+
password=password,
|
| 106 |
+
filepath=handle_file(file),
|
| 107 |
+
custom_name=custom_name if custom_name else os.path.basename(file),
|
| 108 |
+
api_name="/upload_file_secure"
|
| 109 |
+
)
|
| 110 |
+
return result[0]
|
| 111 |
+
except Exception as e:
|
| 112 |
+
return str(e)
|
| 113 |
+
|
| 114 |
# -------------------------
|
| 115 |
# DELETE
|
| 116 |
# -------------------------
|
|
|
|
| 230 |
outputs=change_status
|
| 231 |
)
|
| 232 |
|
| 233 |
+
app.launch()
|
|
|