Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,233 +1,72 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
-
import requests
|
| 5 |
-
from gradio_client import Client, handle_file
|
| 6 |
|
| 7 |
# -------------------------
|
| 8 |
# CONFIG
|
| 9 |
# -------------------------
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
# -------------------------
|
| 17 |
-
#
|
| 18 |
# -------------------------
|
| 19 |
-
def login(user_id, password):
|
| 20 |
-
if not user_id or not password:
|
| 21 |
-
return "Username and password required", []
|
| 22 |
-
|
| 23 |
-
try:
|
| 24 |
-
files, status = client.predict(
|
| 25 |
-
user_id=user_id,
|
| 26 |
-
password=password,
|
| 27 |
-
api_name="/get_files_secure"
|
| 28 |
-
)
|
| 29 |
-
return status, files
|
| 30 |
-
except Exception as e:
|
| 31 |
-
return f"Login error: {e}", []
|
| 32 |
|
| 33 |
-
|
| 34 |
-
# LIST FILES
|
| 35 |
-
# -------------------------
|
| 36 |
-
def list_files(user_id, password):
|
| 37 |
-
if not user_id or not password:
|
| 38 |
-
return "Username and password required", []
|
| 39 |
-
|
| 40 |
-
try:
|
| 41 |
-
files, status = client.predict(
|
| 42 |
-
user_id=user_id,
|
| 43 |
-
password=password,
|
| 44 |
-
api_name="/get_files_secure"
|
| 45 |
-
)
|
| 46 |
-
return status, files
|
| 47 |
-
except Exception as e:
|
| 48 |
-
return str(e), []
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
# -------------------------
|
| 54 |
-
def download_file(user_id, password, filename):
|
| 55 |
|
| 56 |
-
if
|
| 57 |
-
return
|
| 58 |
-
|
| 59 |
-
|
| 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,
|
| 67 |
-
filename=filename,
|
| 68 |
-
api_name="/get_download_link_action"
|
| 69 |
)
|
| 70 |
|
| 71 |
-
|
| 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 |
-
#
|
| 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 |
-
|
| 100 |
-
|
| 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 |
-
# -------------------------
|
| 117 |
-
def delete_file(user_id, password, filename):
|
| 118 |
-
|
| 119 |
-
if not user_id or not password:
|
| 120 |
-
return "Username and password required"
|
| 121 |
|
| 122 |
if not filename:
|
| 123 |
-
return "
|
| 124 |
-
|
| 125 |
-
try:
|
| 126 |
-
result = client.predict(
|
| 127 |
-
user_id=user_id,
|
| 128 |
-
password=password,
|
| 129 |
-
filename=filename,
|
| 130 |
-
api_name="/delete_file_secure"
|
| 131 |
-
)
|
| 132 |
-
return result
|
| 133 |
-
except Exception as e:
|
| 134 |
-
return str(e)
|
| 135 |
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
# -------------------------
|
| 139 |
-
def change_password(user_id, password, new_password):
|
| 140 |
-
|
| 141 |
-
if not user_id or not password or not new_password:
|
| 142 |
-
return "All fields required"
|
| 143 |
|
| 144 |
-
|
| 145 |
-
result = client.predict(
|
| 146 |
-
user_id=user_id,
|
| 147 |
-
new_password=new_password,
|
| 148 |
-
api_name="/update_password"
|
| 149 |
-
)
|
| 150 |
-
return result
|
| 151 |
-
except Exception as e:
|
| 152 |
-
return str(e)
|
| 153 |
|
| 154 |
# -------------------------
|
| 155 |
-
# UI
|
| 156 |
# -------------------------
|
| 157 |
-
with gr.Blocks(title="Pockit Cloud Client") as app:
|
| 158 |
|
| 159 |
-
|
| 160 |
|
| 161 |
-
|
| 162 |
-
pw = gr.Textbox(label="Password", type="password")
|
| 163 |
-
|
| 164 |
-
with gr.Tab("Login"):
|
| 165 |
-
login_btn = gr.Button("Login")
|
| 166 |
-
login_status = gr.Textbox(label="Status")
|
| 167 |
-
login_files = gr.Dropdown(label="Files", choices=[])
|
| 168 |
-
|
| 169 |
-
login_btn.click(
|
| 170 |
-
login,
|
| 171 |
-
inputs=[user, pw],
|
| 172 |
-
outputs=[login_status, login_files]
|
| 173 |
-
)
|
| 174 |
|
| 175 |
-
|
|
|
|
| 176 |
|
| 177 |
-
|
| 178 |
-
file_list = gr.Dropdown(label="Your Files", choices=[])
|
| 179 |
-
status_box = gr.Textbox(label="Status")
|
| 180 |
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
gr.Markdown("### Download")
|
| 188 |
-
|
| 189 |
-
download_btn = gr.Button("Download File")
|
| 190 |
-
download_output = gr.File(label="Downloaded File")
|
| 191 |
-
|
| 192 |
-
download_btn.click(
|
| 193 |
-
download_file,
|
| 194 |
-
inputs=[user, pw, file_list],
|
| 195 |
-
outputs=download_output
|
| 196 |
-
)
|
| 197 |
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
upload_input = gr.File(label="Select File")
|
| 201 |
-
custom_name = gr.Textbox(label="Custom Name (Optional)")
|
| 202 |
-
upload_btn = gr.Button("Upload")
|
| 203 |
-
upload_status = gr.Textbox(label="Upload Status")
|
| 204 |
-
|
| 205 |
-
upload_btn.click(
|
| 206 |
-
upload_file,
|
| 207 |
-
inputs=[user, pw, upload_input, custom_name],
|
| 208 |
-
outputs=upload_status
|
| 209 |
-
)
|
| 210 |
-
|
| 211 |
-
gr.Markdown("### Delete")
|
| 212 |
-
|
| 213 |
-
delete_btn = gr.Button("Delete Selected File")
|
| 214 |
-
delete_status = gr.Textbox(label="Delete Status")
|
| 215 |
-
|
| 216 |
-
delete_btn.click(
|
| 217 |
-
delete_file,
|
| 218 |
-
inputs=[user, pw, file_list],
|
| 219 |
-
outputs=delete_status
|
| 220 |
-
)
|
| 221 |
-
|
| 222 |
-
with gr.Tab("Password"):
|
| 223 |
-
new_pw = gr.Textbox(label="New Password", type="password")
|
| 224 |
-
change_btn = gr.Button("Change Password")
|
| 225 |
-
change_status = gr.Textbox(label="Status")
|
| 226 |
-
|
| 227 |
-
change_btn.click(
|
| 228 |
-
change_password,
|
| 229 |
-
inputs=[user, pw, new_pw],
|
| 230 |
-
outputs=change_status
|
| 231 |
-
)
|
| 232 |
|
|
|
|
| 233 |
app.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
+
from fastapi import FastAPI
|
| 3 |
+
from fastapi.responses import FileResponse
|
| 4 |
import gradio as gr
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# -------------------------
|
| 7 |
# CONFIG
|
| 8 |
# -------------------------
|
| 9 |
|
| 10 |
+
SPACE_NAME = os.getenv("SPACE_ID", "your-username/your-space")
|
| 11 |
+
BASE_URL = f"https://{SPACE_NAME.split('/')[-1]}.hf.space"
|
| 12 |
|
| 13 |
+
FILE_DIR = "files"
|
| 14 |
+
os.makedirs(FILE_DIR, exist_ok=True)
|
| 15 |
|
| 16 |
# -------------------------
|
| 17 |
+
# FASTAPI SERVER
|
| 18 |
# -------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
server = FastAPI()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
@server.get("/download/{filename}")
|
| 23 |
+
def download_file(filename: str):
|
| 24 |
+
file_path = os.path.join(FILE_DIR, filename)
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
if os.path.exists(file_path):
|
| 27 |
+
return FileResponse(
|
| 28 |
+
path=file_path,
|
| 29 |
+
filename=filename
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
)
|
| 31 |
|
| 32 |
+
return {"error": "File not found"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
# -------------------------
|
| 35 |
+
# LINK GENERATOR
|
| 36 |
# -------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
def generate_link(filename):
|
| 39 |
+
file_path = os.path.join(FILE_DIR, filename)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
if not filename:
|
| 42 |
+
return "Enter filename"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
if not os.path.exists(file_path):
|
| 45 |
+
return "File does not exist"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
return f"{BASE_URL}/download/{filename}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
# -------------------------
|
| 50 |
+
# SIMPLE UI
|
| 51 |
# -------------------------
|
|
|
|
| 52 |
|
| 53 |
+
with gr.Blocks() as app:
|
| 54 |
|
| 55 |
+
gr.Markdown("# 🔗 HF Spaces Link Generator")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
filename_input = gr.Textbox(label="Filename (must exist in /files)")
|
| 58 |
+
link_output = gr.Textbox(label="Direct Link")
|
| 59 |
|
| 60 |
+
btn = gr.Button("Generate Link")
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
btn.click(
|
| 63 |
+
generate_link,
|
| 64 |
+
inputs=filename_input,
|
| 65 |
+
outputs=link_output
|
| 66 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
+
# IMPORTANT: mount FastAPI
|
| 69 |
+
app = gr.mount_gradio_app(server, app)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
+
# Launch for Spaces
|
| 72 |
app.launch()
|