davanstrien HF staff commited on
Commit
419fdd6
1 Parent(s): a296dd1

Refactor code and remove unnecessary CORS middleware

Browse files
Files changed (1) hide show
  1. main.py +32 -32
main.py CHANGED
@@ -4,21 +4,21 @@ import os
4
  from contextlib import asynccontextmanager
5
  from datetime import datetime
6
  from pathlib import Path
 
7
 
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
- from typing import Annotated
17
-
18
- from fastapi import FastAPI, Header
19
 
20
  load_dotenv()
21
 
 
 
 
 
22
  HF_TOKEN = os.getenv("HF_TOKEN")
23
  hf_api = HfApi(token=HF_TOKEN)
24
 
@@ -35,31 +35,38 @@ scheduler = CommitScheduler(
35
 
36
  @asynccontextmanager
37
  async def lifespan(app: FastAPI):
38
- Path("votes").mkdir(exist_ok=True)
 
 
 
 
 
 
 
 
 
 
39
  yield
40
 
41
 
42
- app = FastAPI()
43
- VOTES_FILE = "votes/votes.jsonl"
44
- # Configure CORS
45
- origins = [
46
- "https://huggingface.co",
47
- "chrome-extension://ogbhjlfpmjgjbjoiffagjogbhgaipopf", # Replace with your Chrome plugin ID
48
- ]
49
-
50
-
51
- app.add_middleware(
52
- CORSMiddleware,
53
- allow_origins=origins,
54
- allow_credentials=True,
55
- allow_methods=["POST"],
56
- allow_headers=["*"],
57
- )
58
 
59
 
60
  def save_vote(vote_entry):
61
  with open(VOTES_FILE, "a") as file:
62
- # add time stamp to the vote entry
63
  date_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
64
  vote_entry["timestamp"] = date_time
65
  json.dump(vote_entry, file)
@@ -98,11 +105,4 @@ async def receive_vote(
98
  }
99
  # Append the vote entry to the JSONL file
100
  background_tasks.add_task(save_vote, vote_entry)
101
-
102
- response = JSONResponse(content={"message": "Vote submitted successfully"})
103
- response.headers[
104
- "Access-Control-Allow-Origin"
105
- ] = "chrome-extension://ogbhjlfpmjgjbjoiffagjogbhgaipopf" # TODO Replace with your Chrome plugin ID
106
- response.headers["Access-Control-Allow-Methods"] = "POST"
107
- response.headers["Access-Contr`ol-Allow-Headers"] = "*"
108
- return response
 
4
  from contextlib import asynccontextmanager
5
  from datetime import datetime
6
  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
 
16
  load_dotenv()
17
 
18
+ app = FastAPI()
19
+ VOTES_FILE = "votes/votes.jsonl"
20
+
21
+
22
  HF_TOKEN = os.getenv("HF_TOKEN")
23
  hf_api = HfApi(token=HF_TOKEN)
24
 
 
35
 
36
  @asynccontextmanager
37
  async def lifespan(app: FastAPI):
38
+ if not Path("votes").exists():
39
+ Path("votes").mkdir()
40
+ path = hf_hub_download(
41
+ "davanstrien/summary-ratings",
42
+ "data/votes.jsonl",
43
+ repo_type="dataset",
44
+ token=HF_TOKEN,
45
+ local_dir="votes",
46
+ local_dir_use_symlinks=False,
47
+ )
48
+ print(f"Downloaded votes to {path}")
49
  yield
50
 
51
 
52
+ # # Configure CORS
53
+ # origins = [
54
+ # "https://huggingface.co",
55
+ # "chrome-extension://ogbhjlfpmjgjbjoiffagjogbhgaipopf", # Replace with your Chrome plugin ID
56
+ # ]
57
+
58
+
59
+ # app.add_middleware(
60
+ # CORSMiddleware,
61
+ # allow_origins=origins,
62
+ # allow_credentials=True,
63
+ # allow_methods=["POST"],
64
+ # allow_headers=["*"],
65
+ # )
 
 
66
 
67
 
68
  def save_vote(vote_entry):
69
  with open(VOTES_FILE, "a") as file:
 
70
  date_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
71
  vote_entry["timestamp"] = date_time
72
  json.dump(vote_entry, file)
 
105
  }
106
  # Append the vote entry to the JSONL file
107
  background_tasks.add_task(save_vote, vote_entry)
108
+ return JSONResponse(content={"message": "Vote submitted successfully"})