Spaces:
Sleeping
Sleeping
import json | |
from dataclasses import asdict | |
from fastapi import FastAPI, Request | |
from fastapi.responses import HTMLResponse | |
from huggingface_hub import attach_huggingface_oauth, parse_huggingface_oauth | |
app = FastAPI() | |
HTML_ROOT = """ | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>FastAPI x OAuth</title> | |
<style> | |
body { | |
margin: 0; | |
display: flex; | |
justify-content: center; | |
align-items: center; | |
height: 100vh; | |
font-family: Arial, sans-serif; | |
background-color: #f4f4f4; | |
} | |
#container { | |
text-align: center; | |
} | |
#json-output { | |
margin-top: 20px; | |
font-family: monospace; | |
background-color: #fff; | |
padding: 10px; | |
border: 1px solid #ddd; | |
border-radius: 4px; | |
margin: 20px auto 0; | |
text-align: left; | |
} | |
a { | |
display: inline-block; | |
padding: 10px 20px; | |
font-size: 16px; | |
text-decoration: none; | |
border-radius: 5px; | |
cursor: pointer; | |
} | |
#sign-out { | |
background-color: #ff6b6b; | |
color: white; | |
} | |
</style> | |
</head> | |
<body> | |
""" | |
HTML_AFTER=""" | |
</body> | |
</html> | |
""" | |
LOGIN_BUTTON = """ | |
<div id="container"> | |
<a id="sign-in-link" href="/oauth/huggingface/login"> | |
<img id="sign-in" | |
src="https://huggingface.co./datasets/huggingface/badges/resolve/main/sign-in-with-huggingface-xl-dark.svg" | |
alt="Sign in with Hugging Face"> | |
</a> | |
</div> | |
""" | |
LOGOUT_BUTTON = """ | |
<div id="container"> | |
<a id="sign-out-link" href="/oauth/huggingface/logout">Sign Out</a> | |
<pre id="json-output">{oauth_info}</pre> | |
</div> | |
""" | |
def main_page(request: Request): | |
oauth_info = parse_huggingface_oauth(request) | |
if oauth_info is None: | |
return HTML_ROOT + LOGIN_BUTTON + HTML_AFTER | |
if len(oauth_info.access_token) > 70: | |
oauth_info.access_token = oauth_info.access_token[:50] + "..." | |
oauth_info.access_token_expires_at = str(oauth_info.access_token_expires_at) # JSON complains about datetimes | |
return HTML_ROOT + LOGOUT_BUTTON.format(oauth_info=json.dumps(asdict(oauth_info), indent=2)) + HTML_AFTER | |
attach_huggingface_oauth(app) | |
if __name__ == "__main__": | |
import uvicorn | |
uvicorn.run(app) | |