Spaces:
Paused
Paused
Commit
•
2d83410
1
Parent(s):
419fdd6
Add HTTPException for invalid token validation
Browse files
main.py
CHANGED
@@ -7,9 +7,10 @@ from pathlib import Path
|
|
7 |
from typing import Annotated
|
8 |
|
9 |
from dotenv import load_dotenv
|
10 |
-
from fastapi import BackgroundTasks, FastAPI, Header
|
11 |
from fastapi.responses import JSONResponse
|
12 |
-
from huggingface_hub import CommitScheduler, HfApi, hf_hub_download
|
|
|
13 |
from pydantic import BaseModel, Field
|
14 |
from starlette.responses import RedirectResponse
|
15 |
|
@@ -86,7 +87,11 @@ class Vote(BaseModel):
|
|
86 |
|
87 |
|
88 |
def validate_token(token: str = Header(None)):
|
89 |
-
|
|
|
|
|
|
|
|
|
90 |
|
91 |
|
92 |
@app.post("/vote")
|
@@ -96,7 +101,7 @@ async def receive_vote(
|
|
96 |
background_tasks: BackgroundTasks,
|
97 |
):
|
98 |
if not validate_token(Authorization):
|
99 |
-
|
100 |
vote_entry = {
|
101 |
"dataset": vote.dataset,
|
102 |
"vote": vote.vote,
|
|
|
7 |
from typing import Annotated
|
8 |
|
9 |
from dotenv import load_dotenv
|
10 |
+
from fastapi import BackgroundTasks, FastAPI, Header, HTTPException
|
11 |
from fastapi.responses import JSONResponse
|
12 |
+
from huggingface_hub import CommitScheduler, HfApi, hf_hub_download, whoami
|
13 |
+
from huggingface_hub.utils._errors import HTTPError
|
14 |
from pydantic import BaseModel, Field
|
15 |
from starlette.responses import RedirectResponse
|
16 |
|
|
|
87 |
|
88 |
|
89 |
def validate_token(token: str = Header(None)):
|
90 |
+
try:
|
91 |
+
whoami(token)
|
92 |
+
return True
|
93 |
+
except HTTPError:
|
94 |
+
return False
|
95 |
|
96 |
|
97 |
@app.post("/vote")
|
|
|
101 |
background_tasks: BackgroundTasks,
|
102 |
):
|
103 |
if not validate_token(Authorization):
|
104 |
+
raise HTTPException(status_code=401, detail="Invalid token")
|
105 |
vote_entry = {
|
106 |
"dataset": vote.dataset,
|
107 |
"vote": vote.vote,
|