Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
| 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 = """
|
|
@@ -33,14 +36,14 @@ HTML_ROOT = """
|
|
| 33 |
padding: 10px;
|
| 34 |
border: 1px solid #ddd;
|
| 35 |
border-radius: 4px;
|
| 36 |
-
max-width: 300px;
|
| 37 |
margin: 20px auto 0;
|
| 38 |
-
|
| 39 |
}
|
| 40 |
-
|
|
|
|
| 41 |
padding: 10px 20px;
|
| 42 |
font-size: 16px;
|
| 43 |
-
|
| 44 |
border-radius: 5px;
|
| 45 |
cursor: pointer;
|
| 46 |
}
|
|
@@ -51,7 +54,9 @@ HTML_ROOT = """
|
|
| 51 |
</style>
|
| 52 |
</head>
|
| 53 |
<body>
|
| 54 |
-
|
|
|
|
|
|
|
| 55 |
</body>
|
| 56 |
</html>
|
| 57 |
"""
|
|
@@ -61,22 +66,30 @@ LOGIN_BUTTON = """
|
|
| 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 |
-
<
|
| 73 |
-
<pre id="json-output"
|
| 74 |
</div>
|
| 75 |
"""
|
| 76 |
|
| 77 |
@app.get("/", response_class=HTMLResponse)
|
| 78 |
def main_page(request: Request):
|
| 79 |
oauth_info = parse_huggingface_oauth(request)
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
from dataclasses import asdict
|
| 3 |
+
|
| 4 |
from fastapi import FastAPI, Request
|
| 5 |
from fastapi.responses import HTMLResponse
|
|
|
|
| 6 |
|
| 7 |
from huggingface_hub import attach_huggingface_oauth, parse_huggingface_oauth
|
| 8 |
|
| 9 |
+
|
| 10 |
app = FastAPI()
|
| 11 |
|
| 12 |
HTML_ROOT = """
|
|
|
|
| 36 |
padding: 10px;
|
| 37 |
border: 1px solid #ddd;
|
| 38 |
border-radius: 4px;
|
|
|
|
| 39 |
margin: 20px auto 0;
|
| 40 |
+
text-align: left;
|
| 41 |
}
|
| 42 |
+
a {
|
| 43 |
+
display: inline-block;
|
| 44 |
padding: 10px 20px;
|
| 45 |
font-size: 16px;
|
| 46 |
+
text-decoration: none;
|
| 47 |
border-radius: 5px;
|
| 48 |
cursor: pointer;
|
| 49 |
}
|
|
|
|
| 54 |
</style>
|
| 55 |
</head>
|
| 56 |
<body>
|
| 57 |
+
"""
|
| 58 |
+
|
| 59 |
+
HTML_AFTER="""
|
| 60 |
</body>
|
| 61 |
</html>
|
| 62 |
"""
|
|
|
|
| 66 |
<a id="sign-in-link" href="/oauth/huggingface/login">
|
| 67 |
<img id="sign-in"
|
| 68 |
src="https://huggingface.co/datasets/huggingface/badges/resolve/main/sign-in-with-huggingface-xl-dark.svg"
|
| 69 |
+
alt="Sign in with Hugging Face">
|
|
|
|
| 70 |
</a>
|
| 71 |
</div>
|
| 72 |
"""
|
| 73 |
|
| 74 |
LOGOUT_BUTTON = """
|
| 75 |
<div id="container">
|
| 76 |
+
<a id="sign-out-link" href="/oauth/huggingface/logout">Sign Out</a>
|
| 77 |
+
<pre id="json-output">{oauth_info}</pre>
|
| 78 |
</div>
|
| 79 |
"""
|
| 80 |
|
| 81 |
@app.get("/", response_class=HTMLResponse)
|
| 82 |
def main_page(request: Request):
|
| 83 |
oauth_info = parse_huggingface_oauth(request)
|
| 84 |
+
if oauth_info is None:
|
| 85 |
+
return HTML_ROOT + LOGIN_BUTTON + HTML_AFTER
|
| 86 |
+
|
| 87 |
+
oauth_info.access_token_expires_at = str(oauth_info.access_token_expires_at) # JSON complains about datetimes
|
| 88 |
+
middle = LOGOUT_BUTTON.format(oauth_info=json.dumps(asdict(oauth_info), indent=2)) if oauth_info is not None else LOGIN_BUTTON
|
| 89 |
+
return HTML_ROOT + middle + HTML_AFTER
|
| 90 |
+
|
| 91 |
+
attach_huggingface_oauth(app)
|
| 92 |
|
| 93 |
+
if __name__ == "__main__":
|
| 94 |
+
import uvicorn
|
| 95 |
+
uvicorn.run(app)
|