davanstrien HF staff commited on
Commit
becbbb8
1 Parent(s): 88c2e3b

Add CORS support and update vote endpoint response headers

Browse files
Files changed (1) hide show
  1. main.py +13 -20
main.py CHANGED
@@ -8,11 +8,11 @@ from pathlib import Path
8
  from dotenv import load_dotenv
9
  from fastapi import BackgroundTasks, FastAPI, Header
10
  from fastapi.middleware.cors import CORSMiddleware
 
11
  from huggingface_hub import CommitScheduler, HfApi, whoami
12
  from huggingface_hub.utils._errors import HTTPError
13
  from pydantic import BaseModel, Field
14
  from starlette.responses import RedirectResponse
15
- from fastapi import FastAPI, HTTPException, status
16
 
17
  load_dotenv()
18
 
@@ -41,6 +41,7 @@ VOTES_FILE = "votes/votes.jsonl"
41
  # Configure CORS
42
  origins = [
43
  "https://huggingface.co",
 
44
  ]
45
 
46
 
@@ -70,30 +71,15 @@ def root():
70
  class Vote(BaseModel):
71
  dataset: str
72
  description: str
73
- vote: int = Field(..., ge=0, le=1)
74
  userID: str
75
 
76
 
77
- def validate_token(token: str):
78
- try:
79
- whoami(token=token)
80
- return True
81
- except HTTPError as e:
82
- # check for HTTPError: Invalid user token. If you didn't pass a user token, make sure you are properly logged in by executing `huggingface-cli login`, and if you did pass a user token, double-check it's correct. in the error message
83
- if "Invalid user token" in str(e):
84
- return False
85
-
86
-
87
  @app.post("/vote")
88
  async def receive_vote(
89
- vote: Vote, background_tasks: BackgroundTasks, authorization: str = Header(...)
 
90
  ):
91
- token = authorization.split(" ")[1]
92
- if not validate_token(token):
93
- raise HTTPException(
94
- status_code=status.HTTP_401_UNAUTHORIZED,
95
- detail="Invalid user token.",
96
- )
97
  vote_entry = {
98
  "dataset": vote.dataset,
99
  "vote": vote.vote,
@@ -102,4 +88,11 @@ async def receive_vote(
102
  }
103
  # Append the vote entry to the JSONL file
104
  background_tasks.add_task(save_vote, vote_entry)
105
- return {"message": "Vote submitted successfully"}
 
 
 
 
 
 
 
 
8
  from dotenv import load_dotenv
9
  from fastapi import BackgroundTasks, FastAPI, Header
10
  from fastapi.middleware.cors import CORSMiddleware
11
+ from fastapi.responses import JSONResponse
12
  from huggingface_hub import CommitScheduler, HfApi, whoami
13
  from huggingface_hub.utils._errors import HTTPError
14
  from pydantic import BaseModel, Field
15
  from starlette.responses import RedirectResponse
 
16
 
17
  load_dotenv()
18
 
 
41
  # Configure CORS
42
  origins = [
43
  "https://huggingface.co",
44
+ "chrome-extension://your-plugin-id", # Replace with your Chrome plugin ID
45
  ]
46
 
47
 
 
71
  class Vote(BaseModel):
72
  dataset: str
73
  description: str
74
+ vote: int = Field(..., ge=-1, le=1)
75
  userID: str
76
 
77
 
 
 
 
 
 
 
 
 
 
 
78
  @app.post("/vote")
79
  async def receive_vote(
80
+ vote: Vote,
81
+ background_tasks: BackgroundTasks,
82
  ):
 
 
 
 
 
 
83
  vote_entry = {
84
  "dataset": vote.dataset,
85
  "vote": vote.vote,
 
88
  }
89
  # Append the vote entry to the JSONL file
90
  background_tasks.add_task(save_vote, vote_entry)
91
+
92
+ response = JSONResponse(content={"message": "Vote submitted successfully"})
93
+ response.headers[
94
+ "Access-Control-Allow-Origin"
95
+ ] = "chrome-extension://your-plugin-id" # Replace with your Chrome plugin ID
96
+ response.headers["Access-Control-Allow-Methods"] = "POST"
97
+ response.headers["Access-Control-Allow-Headers"] = "*"
98
+ return response