OjciecTadeusz commited on
Commit
8cee326
·
verified ·
1 Parent(s): 5d57bae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -9
app.py CHANGED
@@ -3,18 +3,27 @@ import requests
3
  import os
4
 
5
  app = FastAPI()
6
- HF_TOKEN = os.getenv("HF_TOKEN")
 
 
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)):
@@ -27,9 +36,6 @@ async def get_row(id: str, authorization: str | None = Header(default=None)):
27
  await auth(authorization)
28
  data = load_dataset()
29
  for r in data:
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)
 
3
  import os
4
 
5
  app = FastAPI()
6
+
7
+ AUTH_TOKEN = os.getenv("AUTH_TOKEN") # poprawiona zmienna
8
+ URL = os.getenv("URL") # URL do datasetu JSON
9
 
10
  def load_dataset():
11
+ if not URL:
12
+ raise HTTPException(status_code=500, detail="Dataset URL not set")
13
+ r = requests.get(URL)
14
  if r.status_code != 200:
15
  raise HTTPException(status_code=500, detail="Dataset fetch failed")
16
  return r.json()
17
 
18
  async def auth(authorization: str | None = Header(default=None)):
19
+ if AUTH_TOKEN:
20
+ token = authorization.replace("Bearer ", "") if authorization else None
21
+ if token != AUTH_TOKEN:
22
+ raise HTTPException(status_code=401, detail="Unauthorized")
23
+
24
+ @app.get("/health")
25
+ async def health():
26
+ return {"status": "ok"}
27
 
28
  @app.get("/rows")
29
  async def get_rows(limit: int = 3, authorization: str | None = Header(default=None)):
 
36
  await auth(authorization)
37
  data = load_dataset()
38
  for r in data:
39
+ if r.get("id") == id:
40
  return r
41
  raise HTTPException(status_code=404, detail="Not found")