File size: 2,302 Bytes
0f55af6
14edaa8
 
 
 
b688aca
 
 
14edaa8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0f55af6
14edaa8
 
 
 
 
 
 
 
 
 
 
 
 
 
0f55af6
 
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
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>
"""

@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)