Spaces:
Sleeping
Sleeping
import base64 | |
import json | |
import os | |
import gradio as gr | |
import spaces | |
from spaces.zero.client import _get_token | |
from gradio.context import LocalContext | |
import logging | |
############################# | |
os.environ.setdefault('GRADIO_ANALYTICS_ENABLED', 'False') | |
os.environ.setdefault('HF_HUB_DISABLE_TELEMETRY', '1') | |
logging.basicConfig(level=logging.DEBUG) | |
logger = logging.getLogger(__name__) | |
############################# | |
def local_context_greet(name): | |
request = LocalContext.request.get() | |
print(f"{request.headers=}") | |
return "Hello " + name + "!!" | |
def req_header_greet(name, req: gr.Request): | |
return f"{req.headers=}" | |
# Not possible with IP-based quotas | |
def inner(): | |
return "ok" | |
def greet(request: gr.Request, n): | |
token = _get_token(request) | |
print(token) | |
assert inner() == "ok" | |
payload = token.split('.')[1] | |
payload = f"{payload}{'=' * ((4 - len(payload) % 4) % 4)}" | |
return json.loads(base64.urlsafe_b64decode(payload).decode()) | |
demo = gr.Interface(fn=greet, inputs=gr.Number(), outputs=gr.JSON()) | |
demo.launch(ssr_mode=False) |