Spaces:
Sleeping
Sleeping
from fastapi import FastAPI, Request | |
from fastapi.responses import HTMLResponse | |
from dataclasses import asdict | |
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 + HF + 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; | |
max-width: 300px; | |
margin: 20px auto 0; | |
word-wrap: break-word; | |
} | |
button { | |
padding: 10px 20px; | |
font-size: 16px; | |
border: none; | |
border-radius: 5px; | |
cursor: pointer; | |
} | |
#sign-out { | |
background-color: #ff6b6b; | |
color: white; | |
} | |
</style> | |
</head> | |
<body> | |
{content} | |
</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" | |
style="cursor: pointer;"> | |
</a> | |
</div> | |
""" | |
LOGOUT_BUTTON = """ | |
<div id="container"> | |
<!-- Sign-out button (hidden initially) --> | |
<button id="sign-out" style="display: none;">Sign Out</button> | |
<!-- JSON output container --> | |
<pre id="json-output" style="display: none;">{}</pre> | |
</div> | |
""" | |
def greet_json(): | |
oauth_info = parse_huggingface_oauth(request) | |
return HTML_ROOT.format(content=LOGOUT_BUTTON if oauth_info is not None else LOGIN_BUTTON) | |
attach_huggingface_oauth(app) |