Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,16 +5,12 @@ from gradio_client import Client
|
|
| 5 |
|
| 6 |
SPACE = "pockit-cloud/main"
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
raise Exception("HF_TOKEN environment variable not set")
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
# --------------------
|
| 15 |
# Login
|
| 16 |
-
# --------------------
|
| 17 |
-
|
| 18 |
def login(user_id, password):
|
| 19 |
try:
|
| 20 |
files, status = client.predict(
|
|
@@ -24,72 +20,100 @@ def login(user_id, password):
|
|
| 24 |
)
|
| 25 |
|
| 26 |
return status, user_id, password, files
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
except Exception as e:
|
| 28 |
-
return str(e),
|
| 29 |
|
| 30 |
-
# --------------------
|
| 31 |
-
# Change Password
|
| 32 |
-
# --------------------
|
| 33 |
|
|
|
|
|
|
|
|
|
|
| 34 |
def change_password(user_id, stored_pw, old_pw, new_pw):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
if old_pw != stored_pw:
|
| 36 |
return "Old password incorrect"
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
# --------------------
|
| 49 |
|
| 50 |
-
def list_files(user_id, password):
|
| 51 |
-
files, status = client.predict(
|
| 52 |
-
user_id=user_id,
|
| 53 |
-
password=password,
|
| 54 |
-
api_name="/get_files_secure"
|
| 55 |
-
)
|
| 56 |
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
# --------------------
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
|
|
|
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
|
| 76 |
-
|
| 77 |
-
f.write(r.content)
|
| 78 |
|
| 79 |
-
|
|
|
|
|
|
|
| 80 |
|
| 81 |
|
| 82 |
-
# --------------------
|
| 83 |
# UI
|
| 84 |
-
# --------------------
|
| 85 |
|
| 86 |
with gr.Blocks(title="Pockit Cloud Client") as app:
|
| 87 |
|
| 88 |
user_state = gr.State("")
|
| 89 |
pass_state = gr.State("")
|
| 90 |
|
| 91 |
-
gr.Markdown("# Pockit Cloud Client")
|
| 92 |
|
|
|
|
|
|
|
|
|
|
| 93 |
with gr.Tab("Login"):
|
| 94 |
|
| 95 |
user = gr.Textbox(label="User ID")
|
|
@@ -105,6 +129,9 @@ with gr.Blocks(title="Pockit Cloud Client") as app:
|
|
| 105 |
outputs=[login_status, user_state, pass_state]
|
| 106 |
)
|
| 107 |
|
|
|
|
|
|
|
|
|
|
| 108 |
with gr.Tab("Files"):
|
| 109 |
|
| 110 |
refresh_btn = gr.Button("Refresh File List")
|
|
@@ -119,7 +146,7 @@ with gr.Blocks(title="Pockit Cloud Client") as app:
|
|
| 119 |
outputs=[refresh_status, file_list]
|
| 120 |
)
|
| 121 |
|
| 122 |
-
download_btn = gr.Button("Download")
|
| 123 |
|
| 124 |
download_output = gr.File(label="Downloaded File")
|
| 125 |
|
|
@@ -129,6 +156,9 @@ with gr.Blocks(title="Pockit Cloud Client") as app:
|
|
| 129 |
outputs=download_output
|
| 130 |
)
|
| 131 |
|
|
|
|
|
|
|
|
|
|
| 132 |
with gr.Tab("Password"):
|
| 133 |
|
| 134 |
old_pw = gr.Textbox(label="Old Password", type="password")
|
|
|
|
| 5 |
|
| 6 |
SPACE = "pockit-cloud/main"
|
| 7 |
|
| 8 |
+
# Connect to the backend Space
|
| 9 |
+
client = Client(SPACE)
|
|
|
|
| 10 |
|
| 11 |
+
# -------------------------
|
|
|
|
|
|
|
| 12 |
# Login
|
| 13 |
+
# -------------------------
|
|
|
|
| 14 |
def login(user_id, password):
|
| 15 |
try:
|
| 16 |
files, status = client.predict(
|
|
|
|
| 20 |
)
|
| 21 |
|
| 22 |
return status, user_id, password, files
|
| 23 |
+
|
| 24 |
+
except Exception as e:
|
| 25 |
+
return f"Login error: {e}", "", "", []
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
# -------------------------
|
| 29 |
+
# List files
|
| 30 |
+
# -------------------------
|
| 31 |
+
def list_files(user_id, password):
|
| 32 |
+
if not user_id or not password:
|
| 33 |
+
return "Not logged in", []
|
| 34 |
+
|
| 35 |
+
try:
|
| 36 |
+
files, status = client.predict(
|
| 37 |
+
user_id=user_id,
|
| 38 |
+
password=password,
|
| 39 |
+
api_name="/get_files_secure"
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
return status, files
|
| 43 |
+
|
| 44 |
except Exception as e:
|
| 45 |
+
return str(e), []
|
| 46 |
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
# -------------------------
|
| 49 |
+
# Change password
|
| 50 |
+
# -------------------------
|
| 51 |
def change_password(user_id, stored_pw, old_pw, new_pw):
|
| 52 |
+
|
| 53 |
+
if not user_id:
|
| 54 |
+
return "Not logged in"
|
| 55 |
+
|
| 56 |
if old_pw != stored_pw:
|
| 57 |
return "Old password incorrect"
|
| 58 |
|
| 59 |
+
try:
|
| 60 |
+
result = client.predict(
|
| 61 |
+
user_id=user_id,
|
| 62 |
+
new_password=new_pw,
|
| 63 |
+
api_name="/update_password"
|
| 64 |
+
)
|
| 65 |
|
| 66 |
+
return result
|
| 67 |
|
| 68 |
+
except Exception as e:
|
| 69 |
+
return str(e)
|
|
|
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
+
# -------------------------
|
| 73 |
+
# Download file
|
| 74 |
+
# -------------------------
|
| 75 |
+
def download_file(user_id, password, filename):
|
| 76 |
|
| 77 |
+
if not filename:
|
| 78 |
+
return None
|
|
|
|
| 79 |
|
| 80 |
+
try:
|
| 81 |
+
link = client.predict(
|
| 82 |
+
user_id=user_id,
|
| 83 |
+
password=password,
|
| 84 |
+
filename=filename,
|
| 85 |
+
api_name="/get_download_link_action"
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
r = requests.get(link)
|
| 89 |
|
| 90 |
+
os.makedirs("downloads", exist_ok=True)
|
| 91 |
+
path = f"downloads/{filename}"
|
| 92 |
|
| 93 |
+
with open(path, "wb") as f:
|
| 94 |
+
f.write(r.content)
|
| 95 |
|
| 96 |
+
return path
|
|
|
|
| 97 |
|
| 98 |
+
except Exception as e:
|
| 99 |
+
print(e)
|
| 100 |
+
return None
|
| 101 |
|
| 102 |
|
| 103 |
+
# -------------------------
|
| 104 |
# UI
|
| 105 |
+
# -------------------------
|
| 106 |
|
| 107 |
with gr.Blocks(title="Pockit Cloud Client") as app:
|
| 108 |
|
| 109 |
user_state = gr.State("")
|
| 110 |
pass_state = gr.State("")
|
| 111 |
|
| 112 |
+
gr.Markdown("# ☁️ Pockit Cloud Client")
|
| 113 |
|
| 114 |
+
# ---------------------
|
| 115 |
+
# LOGIN TAB
|
| 116 |
+
# ---------------------
|
| 117 |
with gr.Tab("Login"):
|
| 118 |
|
| 119 |
user = gr.Textbox(label="User ID")
|
|
|
|
| 129 |
outputs=[login_status, user_state, pass_state]
|
| 130 |
)
|
| 131 |
|
| 132 |
+
# ---------------------
|
| 133 |
+
# FILES TAB
|
| 134 |
+
# ---------------------
|
| 135 |
with gr.Tab("Files"):
|
| 136 |
|
| 137 |
refresh_btn = gr.Button("Refresh File List")
|
|
|
|
| 146 |
outputs=[refresh_status, file_list]
|
| 147 |
)
|
| 148 |
|
| 149 |
+
download_btn = gr.Button("Download Selected File")
|
| 150 |
|
| 151 |
download_output = gr.File(label="Downloaded File")
|
| 152 |
|
|
|
|
| 156 |
outputs=download_output
|
| 157 |
)
|
| 158 |
|
| 159 |
+
# ---------------------
|
| 160 |
+
# PASSWORD TAB
|
| 161 |
+
# ---------------------
|
| 162 |
with gr.Tab("Password"):
|
| 163 |
|
| 164 |
old_pw = gr.Textbox(label="Old Password", type="password")
|