jbilcke-hf HF Staff commited on
Commit
968109b
·
verified ·
1 Parent(s): cf76571

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -1
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import gradio as gr
2
  from urllib.parse import urlparse
3
  import requests
@@ -51,7 +52,29 @@ def predict(request: gr.Request, *args, progress=gr.Progress(track_tqdm=True)):
51
  headers = {'Content-Type': 'application/json'}
52
 
53
  payload = {"input": {}}
 
 
 
 
 
 
 
 
 
 
 
54
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
  base_url = "http://0.0.0.0:7860"
57
  for i, key in enumerate(names):
@@ -62,7 +85,6 @@ def predict(request: gr.Request, *args, progress=gr.Progress(track_tqdm=True)):
62
  payload["input"][key] = value
63
 
64
  response = requests.post("http://0.0.0.0:5000/predictions", headers=headers, json=payload)
65
-
66
 
67
  if response.status_code == 201:
68
  follow_up_url = response.json()["urls"]["get"]
 
1
+ from huggingface_hub import HfApi
2
  import gradio as gr
3
  from urllib.parse import urlparse
4
  import requests
 
52
  headers = {'Content-Type': 'application/json'}
53
 
54
  payload = {"input": {}}
55
+
56
+ # TODO: extract the Bearer access token from the request
57
+ if not request:
58
+ raise gr.Error("The submission failed!")
59
+
60
+ print("Request headers dictionary:", request.headers)
61
+
62
+ try:
63
+ authorization = request.headers["Authorization"]
64
+ except KeyError:
65
+ raise gr.Error("Missing authorization in the headers")
66
 
67
+ # Extract the token part from the authorization
68
+ try:
69
+ bearer, token = authorization.split(" ")
70
+ except ValueError:
71
+ raise gr.Error("Invalid format for Authorization header. It should be 'Bearer <token>'")
72
+
73
+ try:
74
+ hf_api = HfApi(token=token)
75
+ userInfo = hf_api.whoami(token)
76
+ if not userInfo:
77
+ raise gr.Error("The provider API key is invalid!")
78
 
79
  base_url = "http://0.0.0.0:7860"
80
  for i, key in enumerate(names):
 
85
  payload["input"][key] = value
86
 
87
  response = requests.post("http://0.0.0.0:5000/predictions", headers=headers, json=payload)
 
88
 
89
  if response.status_code == 201:
90
  follow_up_url = response.json()["urls"]["get"]