Wauplin HF staff commited on
Commit
14edaa8
·
verified ·
1 Parent(s): 931e88e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -9
app.py CHANGED
@@ -1,16 +1,85 @@
1
  from fastapi import FastAPI, Request
2
- from huggingface_hub._oauth import attach_huggingface_oauth, parse_huggingface_oauth
 
 
 
3
 
4
  app = FastAPI()
5
 
6
- @app.get("/")
7
- def greet_json():
8
- return {"Hello": "World!"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
- @app.get("/me")
11
- def greet_json(request: Request):
12
- oauth = parse_huggingface_oauth(request)
13
- print(oauth)
14
- return {"Hello": print(oauth)}
 
 
 
 
 
 
 
 
 
15
 
16
  attach_huggingface_oauth(app)
 
1
  from fastapi import FastAPI, Request
2
+ from fastapi.responses import HTMLResponse
3
+ from dataclasses import asdict
4
+
5
+ from huggingface_hub import attach_huggingface_oauth, parse_huggingface_oauth
6
 
7
  app = FastAPI()
8
 
9
+ HTML_ROOT = """
10
+ <!DOCTYPE html>
11
+ <html lang="en">
12
+ <head>
13
+ <meta charset="UTF-8">
14
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
15
+ <title>FastAPI + HF + OAuth</title>
16
+ <style>
17
+ body {
18
+ margin: 0;
19
+ display: flex;
20
+ justify-content: center;
21
+ align-items: center;
22
+ height: 100vh;
23
+ font-family: Arial, sans-serif;
24
+ background-color: #f4f4f4;
25
+ }
26
+ #container {
27
+ text-align: center;
28
+ }
29
+ #json-output {
30
+ margin-top: 20px;
31
+ font-family: monospace;
32
+ background-color: #fff;
33
+ padding: 10px;
34
+ border: 1px solid #ddd;
35
+ border-radius: 4px;
36
+ max-width: 300px;
37
+ margin: 20px auto 0;
38
+ word-wrap: break-word;
39
+ }
40
+ button {
41
+ padding: 10px 20px;
42
+ font-size: 16px;
43
+ border: none;
44
+ border-radius: 5px;
45
+ cursor: pointer;
46
+ }
47
+ #sign-out {
48
+ background-color: #ff6b6b;
49
+ color: white;
50
+ }
51
+ </style>
52
+ </head>
53
+ <body>
54
+ {content}
55
+ </body>
56
+ </html>
57
+ """
58
+
59
+ LOGIN_BUTTON = """
60
+ <div id="container">
61
+ <a id="sign-in-link" href="/oauth/huggingface/login">
62
+ <img id="sign-in"
63
+ src="https://huggingface.co/datasets/huggingface/badges/resolve/main/sign-in-with-huggingface-xl-dark.svg"
64
+ alt="Sign in with Hugging Face"
65
+ style="cursor: pointer;">
66
+ </a>
67
+ </div>
68
+ """
69
 
70
+ LOGOUT_BUTTON = """
71
+ <div id="container">
72
+ <!-- Sign-out button (hidden initially) -->
73
+ <button id="sign-out" style="display: none;">Sign Out</button>
74
+
75
+ <!-- JSON output container -->
76
+ <pre id="json-output" style="display: none;">{}</pre>
77
+ </div>
78
+ """
79
+
80
+ @app.get("/", response_class=HTMLResponse)
81
+ def greet_json():
82
+ oauth_info = parse_huggingface_oauth(request)
83
+ return HTML_ROOT.format(content=LOGOUT_BUTTON if oauth_info is not None else LOGIN_BUTTON)
84
 
85
  attach_huggingface_oauth(app)