theguywhosucks commited on
Commit
950adcc
·
verified ·
1 Parent(s): 93b1b16

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -61
app.py CHANGED
@@ -1,15 +1,15 @@
1
  import os
2
  import requests
3
  import gradio as gr
4
- from gradio_client import Client
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:
@@ -18,59 +18,46 @@ def login(user_id, password):
18
  password=password,
19
  api_name="/get_files_secure"
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
 
@@ -94,85 +81,94 @@ def download_file(user_id, password, filename):
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")
120
- pw = gr.Textbox(label="Password", type="password")
121
 
 
 
122
  login_btn = gr.Button("Login")
123
-
124
  login_status = gr.Textbox(label="Status")
 
125
 
126
  login_btn.click(
127
  login,
128
  inputs=[user, pw],
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")
138
-
139
  file_list = gr.Dropdown(label="Your Files", choices=[])
140
-
141
- refresh_status = gr.Textbox(label="Status")
142
 
143
  refresh_btn.click(
144
  list_files,
145
- inputs=[user_state, pass_state],
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
 
153
  download_btn.click(
154
  download_file,
155
- inputs=[user_state, pass_state, file_list],
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")
165
  new_pw = gr.Textbox(label="New Password", type="password")
166
-
167
  change_btn = gr.Button("Change Password")
168
-
169
  change_status = gr.Textbox(label="Status")
170
 
171
  change_btn.click(
172
  change_password,
173
- inputs=[user_state, pass_state, old_pw, new_pw],
174
  outputs=change_status
175
  )
176
 
177
-
178
  app.launch()
 
1
  import os
2
  import requests
3
  import gradio as gr
4
+ from gradio_client import Client, handle_file
5
 
6
  SPACE = "pockit-cloud/main"
7
 
8
+ # Connect to backend Space
9
  client = Client(SPACE)
10
 
11
  # -------------------------
12
+ # LOGIN (required each time)
13
  # -------------------------
14
  def login(user_id, password):
15
  try:
 
18
  password=password,
19
  api_name="/get_files_secure"
20
  )
21
+ return status, files
 
 
22
  except Exception as e:
23
+ return f"Login error: {e}", []
 
24
 
25
  # -------------------------
26
+ # LIST FILES
27
  # -------------------------
28
  def list_files(user_id, password):
 
 
 
29
  try:
30
  files, status = client.predict(
31
  user_id=user_id,
32
  password=password,
33
  api_name="/get_files_secure"
34
  )
 
35
  return status, files
 
36
  except Exception as e:
37
  return str(e), []
38
 
 
39
  # -------------------------
40
+ # UPLOAD FILE
41
  # -------------------------
42
+ def upload_file(user_id, password, file, custom_name):
 
 
 
43
 
44
+ if not file:
45
+ return "No file selected"
46
 
47
  try:
48
  result = client.predict(
49
  user_id=user_id,
50
+ password=password,
51
+ filepath=handle_file(file),
52
+ custom_name=custom_name if custom_name else os.path.basename(file),
53
+ api_name="/upload_file_secure"
54
  )
55
+ return result[0] # Status
 
 
56
  except Exception as e:
57
  return str(e)
58
 
 
59
  # -------------------------
60
+ # DOWNLOAD FILE
61
  # -------------------------
62
  def download_file(user_id, password, filename):
63
 
 
81
  f.write(r.content)
82
 
83
  return path
 
84
  except Exception as e:
85
  print(e)
86
  return None
87
 
88
+ # -------------------------
89
+ # CHANGE PASSWORD
90
+ # -------------------------
91
+ def change_password(user_id, password, new_password):
92
+
93
+ try:
94
+ result = client.predict(
95
+ user_id=user_id,
96
+ new_password=new_password,
97
+ api_name="/update_password"
98
+ )
99
+ return result
100
+ except Exception as e:
101
+ return str(e)
102
 
103
  # -------------------------
104
  # UI
105
  # -------------------------
 
106
  with gr.Blocks(title="Pockit Cloud Client") as app:
107
 
 
 
 
108
  gr.Markdown("# ☁️ Pockit Cloud Client")
109
 
110
+ # Shared credential inputs (required everywhere)
111
+ user = gr.Textbox(label="User ID", required=True)
112
+ pw = gr.Textbox(label="Password", type="password", required=True)
 
 
 
 
113
 
114
+ # ---------------- LOGIN ----------------
115
+ with gr.Tab("Login"):
116
  login_btn = gr.Button("Login")
 
117
  login_status = gr.Textbox(label="Status")
118
+ login_files = gr.Dropdown(label="Files", choices=[])
119
 
120
  login_btn.click(
121
  login,
122
  inputs=[user, pw],
123
+ outputs=[login_status, login_files]
124
  )
125
 
126
+ # ---------------- FILES ----------------
 
 
127
  with gr.Tab("Files"):
128
 
129
  refresh_btn = gr.Button("Refresh File List")
 
130
  file_list = gr.Dropdown(label="Your Files", choices=[])
131
+ status_box = gr.Textbox(label="Status")
 
132
 
133
  refresh_btn.click(
134
  list_files,
135
+ inputs=[user, pw],
136
+ outputs=[status_box, file_list]
137
  )
138
 
139
  download_btn = gr.Button("Download Selected File")
 
140
  download_output = gr.File(label="Downloaded File")
141
 
142
  download_btn.click(
143
  download_file,
144
+ inputs=[user, pw, file_list],
145
  outputs=download_output
146
  )
147
 
148
+ gr.Markdown("### Upload File")
149
+
150
+ upload_input = gr.File(label="Select File")
151
+ custom_name = gr.Textbox(label="Custom Name (Optional)")
152
+ upload_btn = gr.Button("Upload")
153
+ upload_status = gr.Textbox(label="Upload Status")
154
+
155
+ upload_btn.click(
156
+ upload_file,
157
+ inputs=[user, pw, upload_input, custom_name],
158
+ outputs=upload_status
159
+ )
160
+
161
+ # ---------------- PASSWORD ----------------
162
  with gr.Tab("Password"):
163
 
 
164
  new_pw = gr.Textbox(label="New Password", type="password")
 
165
  change_btn = gr.Button("Change Password")
 
166
  change_status = gr.Textbox(label="Status")
167
 
168
  change_btn.click(
169
  change_password,
170
+ inputs=[user, pw, new_pw],
171
  outputs=change_status
172
  )
173
 
 
174
  app.launch()