OjciecTadeusz commited on
Commit
018266e
·
verified ·
1 Parent(s): 9461556

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -7
app.py CHANGED
@@ -7,19 +7,20 @@ HF_TOKEN = os.getenv("HF_TOKEN")
7
 
8
  def load_dataset():
9
  url = os.getenv("URL")
10
- return requests.get(url).json()
 
 
 
11
 
12
  async def auth(authorization: str | None = Header(default=None)):
13
- if not authorization or authorization.replace("Bearer ", "") != HF_TOKEN:
14
  raise HTTPException(status_code=401, detail="Unauthorized")
15
 
16
  @app.get("/rows")
17
- async def get_rows(limit: int = 100, authorization: str | None = Header(default=None)):
18
  await auth(authorization)
19
- if not (1 <= limit <= 100):
20
- raise HTTPException(status_code=400, detail="Invalid limit")
21
-
22
- return load_dataset()[:limit]
23
 
24
  @app.get("/row")
25
  async def get_row(id: str, authorization: str | None = Header(default=None)):
@@ -29,3 +30,6 @@ async def get_row(id: str, authorization: str | None = Header(default=None)):
29
  if r["id"] == id:
30
  return r
31
  raise HTTPException(status_code=404, detail="Not found")
 
 
 
 
7
 
8
  def load_dataset():
9
  url = os.getenv("URL")
10
+ r = requests.get(url)
11
+ if r.status_code != 200:
12
+ raise HTTPException(status_code=500, detail="Dataset fetch failed")
13
+ return r.json()
14
 
15
  async def auth(authorization: str | None = Header(default=None)):
16
+ if AUTH_TOKEN and (not authorization or authorization.replace("Bearer ", "") != AUTH_TOKEN):
17
  raise HTTPException(status_code=401, detail="Unauthorized")
18
 
19
  @app.get("/rows")
20
+ async def get_rows(limit: int = 3, authorization: str | None = Header(default=None)):
21
  await auth(authorization)
22
+ data = load_dataset()
23
+ return data[:limit]
 
 
24
 
25
  @app.get("/row")
26
  async def get_row(id: str, authorization: str | None = Header(default=None)):
 
30
  if r["id"] == id:
31
  return r
32
  raise HTTPException(status_code=404, detail="Not found")
33
+
34
+ if __name__ == "__main__":
35
+ demo.launch(server_name="0.0.0.0", server_port=7860)