spagestic commited on
Commit
8aebde4
·
1 Parent(s): 45a131e

Add authentication error handling in respond function to manage session expiration and unauthorized access

Browse files
Files changed (1) hide show
  1. ui/inference/respond.py +41 -5
ui/inference/respond.py CHANGED
@@ -2,12 +2,14 @@
2
  from __future__ import annotations
3
 
4
  import json
 
5
  import uuid
6
  from typing import Any
7
 
8
  import gradio as gr
9
  from gradio import ChatMessage
10
  from huggingface_hub import InferenceClient
 
11
 
12
  from .completion import complete_turn
13
  from .config import MAX_TOOL_ROUNDS, MODEL_ID, THINK_TOOLS, TOOLS
@@ -15,6 +17,33 @@ from .messages import history_to_api_messages, multimodal_input_to_api_content
15
  from .streaming import yield_response
16
  from .tools import execute_tool_calls
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
  def respond(
20
  message: str | dict[str, Any],
@@ -24,11 +53,19 @@ def respond(
24
  temperature,
25
  top_p,
26
  globe_state: dict[str, Any],
27
- hf_token: gr.OAuthToken,
28
  ):
29
  """
30
  For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
31
  """
 
 
 
 
 
 
 
 
32
  client = InferenceClient(token=hf_token.token, model=MODEL_ID)
33
 
34
  user_content = multimodal_input_to_api_content(message)
@@ -155,11 +192,10 @@ def respond(
155
  return
156
 
157
  except Exception as exc:
158
- yield from yield_response(
159
- ui_messages,
160
- f"Sorry, something went wrong while generating a response: {exc}",
161
- globe_state,
162
  )
 
163
  return
164
 
165
  yield from yield_response(
 
2
  from __future__ import annotations
3
 
4
  import json
5
+ import time
6
  import uuid
7
  from typing import Any
8
 
9
  import gradio as gr
10
  from gradio import ChatMessage
11
  from huggingface_hub import InferenceClient
12
+ from huggingface_hub.errors import HfHubHTTPError
13
 
14
  from .completion import complete_turn
15
  from .config import MAX_TOOL_ROUNDS, MODEL_ID, THINK_TOOLS, TOOLS
 
17
  from .streaming import yield_response
18
  from .tools import execute_tool_calls
19
 
20
+ AUTH_REQUIRED_MESSAGE = (
21
+ "Please sign in with your Hugging Face account to use Borderless. "
22
+ "Click **Log in** in the sidebar, then try again."
23
+ )
24
+ SESSION_EXPIRED_MESSAGE = (
25
+ "Your Hugging Face session has expired. "
26
+ "Click **Log in** in the sidebar to sign in again, then retry your message."
27
+ )
28
+
29
+
30
+ def _auth_error_message(exc: Exception) -> str | None:
31
+ if (
32
+ isinstance(exc, HfHubHTTPError)
33
+ and exc.response is not None
34
+ and exc.response.status_code == 401
35
+ ):
36
+ return SESSION_EXPIRED_MESSAGE
37
+
38
+ message = str(exc).lower()
39
+ if (
40
+ "401" in message
41
+ or "unauthorized" in message
42
+ or "oauth token has expired" in message
43
+ ):
44
+ return SESSION_EXPIRED_MESSAGE
45
+ return None
46
+
47
 
48
  def respond(
49
  message: str | dict[str, Any],
 
53
  temperature,
54
  top_p,
55
  globe_state: dict[str, Any],
56
+ hf_token: gr.OAuthToken | None,
57
  ):
58
  """
59
  For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
60
  """
61
+ if hf_token is None:
62
+ yield from yield_response([], AUTH_REQUIRED_MESSAGE, globe_state)
63
+ return
64
+
65
+ if hf_token.expires_at <= time.time():
66
+ yield from yield_response([], SESSION_EXPIRED_MESSAGE, globe_state)
67
+ return
68
+
69
  client = InferenceClient(token=hf_token.token, model=MODEL_ID)
70
 
71
  user_content = multimodal_input_to_api_content(message)
 
192
  return
193
 
194
  except Exception as exc:
195
+ message = _auth_error_message(exc) or (
196
+ f"Sorry, something went wrong while generating a response: {exc}"
 
 
197
  )
198
+ yield from yield_response(ui_messages, message, globe_state)
199
  return
200
 
201
  yield from yield_response(