Spaces:
Running on Zero
Running on Zero
Reset login to minimal HF OAuth button
Browse files
app.py
CHANGED
|
@@ -8,7 +8,6 @@ import sys
|
|
| 8 |
import tempfile
|
| 9 |
import time
|
| 10 |
import urllib.request
|
| 11 |
-
from collections.abc import Mapping
|
| 12 |
from datetime import datetime
|
| 13 |
from pathlib import Path
|
| 14 |
from typing import List, Optional, Tuple
|
|
@@ -110,94 +109,6 @@ def get_hf_token() -> Optional[str]:
|
|
| 110 |
)
|
| 111 |
|
| 112 |
|
| 113 |
-
def get_request_username(request: Optional[gr.Request]) -> Optional[str]:
|
| 114 |
-
if request is None:
|
| 115 |
-
return None
|
| 116 |
-
session = None
|
| 117 |
-
try:
|
| 118 |
-
session = getattr(request, "session", None)
|
| 119 |
-
except Exception:
|
| 120 |
-
session = None
|
| 121 |
-
if session is None:
|
| 122 |
-
try:
|
| 123 |
-
session = getattr(getattr(request, "request", None), "session", None)
|
| 124 |
-
except Exception:
|
| 125 |
-
session = None
|
| 126 |
-
if session and hasattr(session, "get"):
|
| 127 |
-
try:
|
| 128 |
-
oauth_info = session.get("oauth_info")
|
| 129 |
-
except Exception:
|
| 130 |
-
oauth_info = None
|
| 131 |
-
if isinstance(oauth_info, str):
|
| 132 |
-
try:
|
| 133 |
-
oauth_info = json.loads(oauth_info)
|
| 134 |
-
except Exception:
|
| 135 |
-
oauth_info = None
|
| 136 |
-
if isinstance(oauth_info, Mapping):
|
| 137 |
-
userinfo = oauth_info.get("userinfo") or {}
|
| 138 |
-
if isinstance(userinfo, str):
|
| 139 |
-
try:
|
| 140 |
-
userinfo = json.loads(userinfo)
|
| 141 |
-
except Exception:
|
| 142 |
-
userinfo = {}
|
| 143 |
-
if isinstance(userinfo, Mapping):
|
| 144 |
-
username = (
|
| 145 |
-
userinfo.get("preferred_username")
|
| 146 |
-
or userinfo.get("name")
|
| 147 |
-
or userinfo.get("nickname")
|
| 148 |
-
)
|
| 149 |
-
if username:
|
| 150 |
-
return str(username)
|
| 151 |
-
try:
|
| 152 |
-
username = getattr(request, "username", None)
|
| 153 |
-
except Exception:
|
| 154 |
-
username = None
|
| 155 |
-
if username:
|
| 156 |
-
return username
|
| 157 |
-
headers = None
|
| 158 |
-
try:
|
| 159 |
-
headers = getattr(request, "headers", None)
|
| 160 |
-
except Exception:
|
| 161 |
-
headers = None
|
| 162 |
-
if headers:
|
| 163 |
-
for key in (
|
| 164 |
-
"x-hf-username",
|
| 165 |
-
"x-forwarded-user",
|
| 166 |
-
"x-user",
|
| 167 |
-
"x-username",
|
| 168 |
-
"x-hf-user",
|
| 169 |
-
):
|
| 170 |
-
value = headers.get(key)
|
| 171 |
-
if value:
|
| 172 |
-
return value
|
| 173 |
-
return None
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
def require_login(request: Optional[gr.Request], action: str) -> Optional[str]:
|
| 177 |
-
if not os.getenv("SPACE_ID"):
|
| 178 |
-
return None
|
| 179 |
-
username = get_request_username(request)
|
| 180 |
-
if request is not None and not username:
|
| 181 |
-
log_progress(
|
| 182 |
-
"Login not detected; ZeroGPU may use anonymous quota.",
|
| 183 |
-
2,
|
| 184 |
-
)
|
| 185 |
-
return None
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
def render_login_state(
|
| 189 |
-
request: gr.Request | None = None,
|
| 190 |
-
) -> Tuple[dict, str]:
|
| 191 |
-
username = get_request_username(request)
|
| 192 |
-
if username:
|
| 193 |
-
return (
|
| 194 |
-
gr.update(value=f"Logout ({username})"),
|
| 195 |
-
f"Logged in as **{username}**.",
|
| 196 |
-
)
|
| 197 |
-
return (
|
| 198 |
-
gr.update(value="Sign in with Hugging Face"),
|
| 199 |
-
"Not logged in. Click **Sign in with Hugging Face** to authenticate.",
|
| 200 |
-
)
|
| 201 |
|
| 202 |
|
| 203 |
def normalize_dataset_id(value: str) -> str:
|
|
@@ -1241,9 +1152,6 @@ def process_dataset_and_push(
|
|
| 1241 |
request: gr.Request | None = None,
|
| 1242 |
progress=gr.Progress(),
|
| 1243 |
) -> str:
|
| 1244 |
-
login_error = require_login(request, "dataset processing")
|
| 1245 |
-
if login_error:
|
| 1246 |
-
return login_error
|
| 1247 |
attempts = 0
|
| 1248 |
while True:
|
| 1249 |
try:
|
|
@@ -1470,9 +1378,6 @@ def gradio_single_file(
|
|
| 1470 |
max_atten_db,
|
| 1471 |
request: gr.Request | None = None,
|
| 1472 |
):
|
| 1473 |
-
login_error = require_login(request, "single-file processing")
|
| 1474 |
-
if login_error:
|
| 1475 |
-
return None, None, None, login_error
|
| 1476 |
return _gradio_single_file_gpu(
|
| 1477 |
audio_file,
|
| 1478 |
vad_threshold,
|
|
@@ -1493,18 +1398,7 @@ with gr.Blocks(title="Representation Chizzler") as demo:
|
|
| 1493 |
"to clean and publish a dataset to the Hugging Face Hub."
|
| 1494 |
)
|
| 1495 |
with gr.Row():
|
| 1496 |
-
|
| 1497 |
-
if hasattr(gr, "LoginButton"):
|
| 1498 |
-
login_button = gr.LoginButton(logout_value="Logout ({})")
|
| 1499 |
-
login_status = gr.Markdown()
|
| 1500 |
-
else:
|
| 1501 |
-
login_status = gr.Markdown(
|
| 1502 |
-
"Sign in using the Hugging Face button in the Space header."
|
| 1503 |
-
)
|
| 1504 |
-
if login_button is not None:
|
| 1505 |
-
demo.load(render_login_state, None, [login_button, login_status])
|
| 1506 |
-
login_timer = gr.Timer(5)
|
| 1507 |
-
login_timer.tick(render_login_state, None, [login_button, login_status])
|
| 1508 |
|
| 1509 |
with gr.Tab("Single File"):
|
| 1510 |
audio_input = gr.Audio(label="Upload Audio File", type="filepath")
|
|
|
|
| 8 |
import tempfile
|
| 9 |
import time
|
| 10 |
import urllib.request
|
|
|
|
| 11 |
from datetime import datetime
|
| 12 |
from pathlib import Path
|
| 13 |
from typing import List, Optional, Tuple
|
|
|
|
| 109 |
)
|
| 110 |
|
| 111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
|
| 114 |
def normalize_dataset_id(value: str) -> str:
|
|
|
|
| 1152 |
request: gr.Request | None = None,
|
| 1153 |
progress=gr.Progress(),
|
| 1154 |
) -> str:
|
|
|
|
|
|
|
|
|
|
| 1155 |
attempts = 0
|
| 1156 |
while True:
|
| 1157 |
try:
|
|
|
|
| 1378 |
max_atten_db,
|
| 1379 |
request: gr.Request | None = None,
|
| 1380 |
):
|
|
|
|
|
|
|
|
|
|
| 1381 |
return _gradio_single_file_gpu(
|
| 1382 |
audio_file,
|
| 1383 |
vad_threshold,
|
|
|
|
| 1398 |
"to clean and publish a dataset to the Hugging Face Hub."
|
| 1399 |
)
|
| 1400 |
with gr.Row():
|
| 1401 |
+
gr.LoginButton()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1402 |
|
| 1403 |
with gr.Tab("Single File"):
|
| 1404 |
audio_input = gr.Audio(label="Upload Audio File", type="filepath")
|