theguywhosucks commited on
Commit
d6438c8
·
verified ·
1 Parent(s): 00dc5de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +148 -0
app.py CHANGED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ HF_TOKEN = os.getenv("HF_TOKEN")
9
+ if HF_TOKEN is None:
10
+ raise Exception("HF_TOKEN environment variable not set")
11
+
12
+ client = Client(SPACE, hf_token=HF_TOKEN)
13
+
14
+ # --------------------
15
+ # Login
16
+ # --------------------
17
+
18
+ def login(user_id, password):
19
+ try:
20
+ files, status = client.predict(
21
+ user_id=user_id,
22
+ password=password,
23
+ api_name="/get_files_secure"
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
+ result = client.predict(
39
+ user_id=user_id,
40
+ new_password=new_pw,
41
+ api_name="/update_password"
42
+ )
43
+
44
+ return result
45
+
46
+ # --------------------
47
+ # List Files
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
+ return status, files
58
+
59
+ # --------------------
60
+ # Download File
61
+ # --------------------
62
+
63
+ def download_file(user_id, password, filename):
64
+ link = client.predict(
65
+ user_id=user_id,
66
+ password=password,
67
+ filename=filename,
68
+ api_name="/get_download_link_action"
69
+ )
70
+
71
+ r = requests.get(link)
72
+
73
+ path = f"downloads/{filename}"
74
+ os.makedirs("downloads", exist_ok=True)
75
+
76
+ with open(path, "wb") as f:
77
+ f.write(r.content)
78
+
79
+ return path
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")
96
+ pw = gr.Textbox(label="Password", type="password")
97
+
98
+ login_btn = gr.Button("Login")
99
+
100
+ login_status = gr.Textbox(label="Status")
101
+
102
+ login_btn.click(
103
+ login,
104
+ inputs=[user, pw],
105
+ outputs=[login_status, user_state, pass_state]
106
+ )
107
+
108
+ with gr.Tab("Files"):
109
+
110
+ refresh_btn = gr.Button("Refresh File List")
111
+
112
+ file_list = gr.Dropdown(label="Your Files", choices=[])
113
+
114
+ refresh_status = gr.Textbox(label="Status")
115
+
116
+ refresh_btn.click(
117
+ list_files,
118
+ inputs=[user_state, pass_state],
119
+ outputs=[refresh_status, file_list]
120
+ )
121
+
122
+ download_btn = gr.Button("Download")
123
+
124
+ download_output = gr.File(label="Downloaded File")
125
+
126
+ download_btn.click(
127
+ download_file,
128
+ inputs=[user_state, pass_state, file_list],
129
+ outputs=download_output
130
+ )
131
+
132
+ with gr.Tab("Password"):
133
+
134
+ old_pw = gr.Textbox(label="Old Password", type="password")
135
+ new_pw = gr.Textbox(label="New Password", type="password")
136
+
137
+ change_btn = gr.Button("Change Password")
138
+
139
+ change_status = gr.Textbox(label="Status")
140
+
141
+ change_btn.click(
142
+ change_password,
143
+ inputs=[user_state, pass_state, old_pw, new_pw],
144
+ outputs=change_status
145
+ )
146
+
147
+
148
+ app.launch()