Spaces:
Sleeping
Sleeping
File size: 2,473 Bytes
6bfd959 0f55af6 14edaa8 b688aca 6bfd959 b688aca 14edaa8 c2a6245 14edaa8 6bfd959 14edaa8 6bfd959 14edaa8 6bfd959 14edaa8 6bfd959 14edaa8 6bfd959 14edaa8 0f55af6 14edaa8 6bfd959 14edaa8 afe3de2 14edaa8 6bfd959 0e5a8eb 6bfd959 0f55af6 6bfd959 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
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>
"""
@app.get("/", response_class=HTMLResponse)
def main_page(request: Request):
oauth_info = parse_huggingface_oauth(request)
if oauth_info is None:
return HTML_ROOT + LOGIN_BUTTON + HTML_AFTER
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)
|