Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,76 +1,237 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
| 2 |
from fastapi import FastAPI
|
| 3 |
from fastapi.responses import FileResponse
|
| 4 |
-
|
| 5 |
|
| 6 |
# -------------------------
|
| 7 |
# CONFIG
|
| 8 |
# -------------------------
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
# -------------------------
|
| 21 |
-
#
|
| 22 |
# -------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
file_path = os.path.join(FILE_DIR, filename)
|
| 29 |
|
| 30 |
-
if
|
| 31 |
-
return
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
)
|
| 35 |
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
# -------------------------
|
| 39 |
-
#
|
| 40 |
# -------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
def generate_link(filename):
|
| 43 |
if not filename:
|
| 44 |
-
return "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
if not
|
| 49 |
-
return "
|
| 50 |
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
# -------------------------
|
| 54 |
# GRADIO UI
|
| 55 |
# -------------------------
|
| 56 |
|
| 57 |
-
with gr.Blocks() as app:
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
inputs=filename_input,
|
| 68 |
-
outputs=link_output
|
| 69 |
-
)
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
-
#
|
| 75 |
app.queue()
|
| 76 |
app.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
+
import uuid
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import requests
|
| 5 |
from fastapi import FastAPI
|
| 6 |
from fastapi.responses import FileResponse
|
| 7 |
+
from gradio_client import Client, handle_file
|
| 8 |
|
| 9 |
# -------------------------
|
| 10 |
# CONFIG
|
| 11 |
# -------------------------
|
| 12 |
|
| 13 |
+
SPACE = "pockit-cloud/main"
|
| 14 |
+
client = Client(SPACE)
|
| 15 |
+
|
| 16 |
+
HF_TOKEN = os.getenv("HF_TOKEN") # Secret for private dataset
|
| 17 |
|
| 18 |
+
# -------------------------
|
| 19 |
+
# LOGIN
|
| 20 |
+
# -------------------------
|
| 21 |
+
def login(user_id, password):
|
| 22 |
+
if not user_id or not password:
|
| 23 |
+
return "Username and password required", []
|
| 24 |
+
|
| 25 |
+
try:
|
| 26 |
+
files, status = client.predict(
|
| 27 |
+
user_id=user_id,
|
| 28 |
+
password=password,
|
| 29 |
+
api_name="/get_files_secure"
|
| 30 |
+
)
|
| 31 |
+
return status, files
|
| 32 |
+
except Exception as e:
|
| 33 |
+
return f"Login error: {e}", []
|
| 34 |
|
| 35 |
# -------------------------
|
| 36 |
+
# LIST FILES
|
| 37 |
# -------------------------
|
| 38 |
+
def list_files(user_id, password):
|
| 39 |
+
if not user_id or not password:
|
| 40 |
+
return "Username and password required", []
|
| 41 |
+
|
| 42 |
+
try:
|
| 43 |
+
files, status = client.predict(
|
| 44 |
+
user_id=user_id,
|
| 45 |
+
password=password,
|
| 46 |
+
api_name="/get_files_secure"
|
| 47 |
+
)
|
| 48 |
+
return status, files
|
| 49 |
+
except Exception as e:
|
| 50 |
+
return str(e), []
|
| 51 |
|
| 52 |
+
# -------------------------
|
| 53 |
+
# DOWNLOAD (UNCHANGED LOGIC)
|
| 54 |
+
# Returns temp file so Gradio generates link
|
| 55 |
+
# -------------------------
|
| 56 |
+
def download_file(user_id, password, filename):
|
| 57 |
|
| 58 |
+
if not user_id or not password:
|
| 59 |
+
return None
|
|
|
|
| 60 |
|
| 61 |
+
if not filename:
|
| 62 |
+
return None
|
| 63 |
+
|
| 64 |
+
try:
|
| 65 |
+
link = client.predict(
|
| 66 |
+
user_id=user_id,
|
| 67 |
+
password=password,
|
| 68 |
+
filename=filename,
|
| 69 |
+
api_name="/get_download_link_action"
|
| 70 |
)
|
| 71 |
|
| 72 |
+
headers = {}
|
| 73 |
+
if HF_TOKEN:
|
| 74 |
+
headers["Authorization"] = f"Bearer {HF_TOKEN}"
|
| 75 |
+
|
| 76 |
+
r = requests.get(link, headers=headers, stream=True)
|
| 77 |
+
r.raise_for_status()
|
| 78 |
+
|
| 79 |
+
temp_name = f"{uuid.uuid4()}_{filename}"
|
| 80 |
+
|
| 81 |
+
with open(temp_name, "wb") as f:
|
| 82 |
+
for chunk in r.iter_content(chunk_size=8192):
|
| 83 |
+
if chunk:
|
| 84 |
+
f.write(chunk)
|
| 85 |
+
|
| 86 |
+
return temp_name # IMPORTANT: keeps auto link behavior
|
| 87 |
+
|
| 88 |
+
except Exception as e:
|
| 89 |
+
print(e)
|
| 90 |
+
return None
|
| 91 |
+
|
| 92 |
+
# -------------------------
|
| 93 |
+
# UPLOAD
|
| 94 |
+
# -------------------------
|
| 95 |
+
def upload_file(user_id, password, file, custom_name):
|
| 96 |
+
|
| 97 |
+
if not user_id or not password:
|
| 98 |
+
return "Username and password required"
|
| 99 |
+
|
| 100 |
+
if not file:
|
| 101 |
+
return "No file selected"
|
| 102 |
+
|
| 103 |
+
try:
|
| 104 |
+
result = client.predict(
|
| 105 |
+
user_id=user_id,
|
| 106 |
+
password=password,
|
| 107 |
+
filepath=handle_file(file),
|
| 108 |
+
custom_name=custom_name if custom_name else os.path.basename(file),
|
| 109 |
+
api_name="/upload_file_secure"
|
| 110 |
+
)
|
| 111 |
+
return result[0]
|
| 112 |
+
except Exception as e:
|
| 113 |
+
return str(e)
|
| 114 |
|
| 115 |
# -------------------------
|
| 116 |
+
# DELETE
|
| 117 |
# -------------------------
|
| 118 |
+
def delete_file(user_id, password, filename):
|
| 119 |
+
|
| 120 |
+
if not user_id or not password:
|
| 121 |
+
return "Username and password required"
|
| 122 |
|
|
|
|
| 123 |
if not filename:
|
| 124 |
+
return "No file selected"
|
| 125 |
+
|
| 126 |
+
try:
|
| 127 |
+
result = client.predict(
|
| 128 |
+
user_id=user_id,
|
| 129 |
+
password=password,
|
| 130 |
+
filename=filename,
|
| 131 |
+
api_name="/delete_file_secure"
|
| 132 |
+
)
|
| 133 |
+
return result
|
| 134 |
+
except Exception as e:
|
| 135 |
+
return str(e)
|
| 136 |
|
| 137 |
+
# -------------------------
|
| 138 |
+
# PASSWORD CHANGE
|
| 139 |
+
# -------------------------
|
| 140 |
+
def change_password(user_id, password, new_password):
|
| 141 |
|
| 142 |
+
if not user_id or not password or not new_password:
|
| 143 |
+
return "All fields required"
|
| 144 |
|
| 145 |
+
try:
|
| 146 |
+
result = client.predict(
|
| 147 |
+
user_id=user_id,
|
| 148 |
+
new_password=new_password,
|
| 149 |
+
api_name="/update_password"
|
| 150 |
+
)
|
| 151 |
+
return result
|
| 152 |
+
except Exception as e:
|
| 153 |
+
return str(e)
|
| 154 |
|
| 155 |
# -------------------------
|
| 156 |
# GRADIO UI
|
| 157 |
# -------------------------
|
| 158 |
|
| 159 |
+
with gr.Blocks(title="Pockit Cloud Client") as app:
|
| 160 |
+
|
| 161 |
+
gr.Markdown("# ☁️ Pockit Cloud Client")
|
| 162 |
+
|
| 163 |
+
user = gr.Textbox(label="User ID")
|
| 164 |
+
pw = gr.Textbox(label="Password", type="password")
|
| 165 |
+
|
| 166 |
+
with gr.Tab("Login"):
|
| 167 |
+
login_btn = gr.Button("Login")
|
| 168 |
+
login_status = gr.Textbox(label="Status")
|
| 169 |
+
login_files = gr.Dropdown(label="Files", choices=[])
|
| 170 |
|
| 171 |
+
login_btn.click(
|
| 172 |
+
login,
|
| 173 |
+
inputs=[user, pw],
|
| 174 |
+
outputs=[login_status, login_files]
|
| 175 |
+
)
|
| 176 |
+
|
| 177 |
+
with gr.Tab("Files"):
|
| 178 |
+
|
| 179 |
+
refresh_btn = gr.Button("Refresh File List")
|
| 180 |
+
file_list = gr.Dropdown(label="Your Files", choices=[])
|
| 181 |
+
status_box = gr.Textbox(label="Status")
|
| 182 |
|
| 183 |
+
refresh_btn.click(
|
| 184 |
+
list_files,
|
| 185 |
+
inputs=[user, pw],
|
| 186 |
+
outputs=[status_box, file_list]
|
| 187 |
+
)
|
| 188 |
+
|
| 189 |
+
gr.Markdown("### Download")
|
| 190 |
|
| 191 |
+
download_btn = gr.Button("Download File")
|
| 192 |
+
download_output = gr.File(label="Downloaded File")
|
|
|
|
|
|
|
|
|
|
| 193 |
|
| 194 |
+
download_btn.click(
|
| 195 |
+
download_file,
|
| 196 |
+
inputs=[user, pw, file_list],
|
| 197 |
+
outputs=download_output
|
| 198 |
+
)
|
| 199 |
+
|
| 200 |
+
gr.Markdown("### Upload")
|
| 201 |
+
|
| 202 |
+
upload_input = gr.File(label="Select File")
|
| 203 |
+
custom_name = gr.Textbox(label="Custom Name (Optional)")
|
| 204 |
+
upload_btn = gr.Button("Upload")
|
| 205 |
+
upload_status = gr.Textbox(label="Upload Status")
|
| 206 |
+
|
| 207 |
+
upload_btn.click(
|
| 208 |
+
upload_file,
|
| 209 |
+
inputs=[user, pw, upload_input, custom_name],
|
| 210 |
+
outputs=upload_status
|
| 211 |
+
)
|
| 212 |
+
|
| 213 |
+
gr.Markdown("### Delete")
|
| 214 |
+
|
| 215 |
+
delete_btn = gr.Button("Delete Selected File")
|
| 216 |
+
delete_status = gr.Textbox(label="Delete Status")
|
| 217 |
+
|
| 218 |
+
delete_btn.click(
|
| 219 |
+
delete_file,
|
| 220 |
+
inputs=[user, pw, file_list],
|
| 221 |
+
outputs=delete_status
|
| 222 |
+
)
|
| 223 |
+
|
| 224 |
+
with gr.Tab("Password"):
|
| 225 |
+
new_pw = gr.Textbox(label="New Password", type="password")
|
| 226 |
+
change_btn = gr.Button("Change Password")
|
| 227 |
+
change_status = gr.Textbox(label="Status")
|
| 228 |
+
|
| 229 |
+
change_btn.click(
|
| 230 |
+
change_password,
|
| 231 |
+
inputs=[user, pw, new_pw],
|
| 232 |
+
outputs=change_status
|
| 233 |
+
)
|
| 234 |
|
| 235 |
+
# IMPORTANT FOR SPACES
|
| 236 |
app.queue()
|
| 237 |
app.launch()
|