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 = """ FastAPI + HF + OAuth {content} """ LOGIN_BUTTON = """
Sign in with Hugging Face
""" LOGOUT_BUTTON = """
""" @app.get("/", response_class=HTMLResponse) 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)