4
4
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
5
5
6
6
import re
7
- from typing import Optional
7
+ from typing import Optional , Tuple
8
8
from urllib .parse import urlparse
9
9
10
10
from huggingface_hub import HfApi
@@ -101,48 +101,55 @@ def _find_matching_aqua_model(self, model_id: str) -> Optional[AquaModelSummary]
101
101
102
102
return None
103
103
104
- def _format_custom_error_message (self , error : HfHubHTTPError ):
104
+ def _format_custom_error_message (self , error : HfHubHTTPError ) -> AquaRuntimeError :
105
105
"""
106
106
Formats a custom error message based on the Hugging Face error response.
107
107
108
108
Parameters
109
109
----------
110
110
error (HfHubHTTPError): The caught exception.
111
111
112
- Returns
113
- -------
114
- str : A user-friendly error message.
112
+ Raises
113
+ ------
114
+ AquaRuntimeError : A user-friendly error message.
115
115
"""
116
116
# Extract the repository URL from the error message if present
117
117
match = re .search (r"(https://huggingface.co/[^\s]+)" , str (error ))
118
118
url = match .group (1 ) if match else "the requested Hugging Face URL."
119
119
120
120
if isinstance (error , RepositoryNotFoundError ):
121
- return (
122
- f"Failed to access { url } "
123
- "If the repo is private, make sure you are authenticated."
121
+ raise AquaRuntimeError (
122
+ reason = f"Failed to access `{ url } `. Please check if the provided repository name is correct. "
123
+ "If the repo is private, make sure you are authenticated and have a valid HF token registered. "
124
+ "To register your token, run this command in your terminal: `huggingface-cli login`" ,
125
+ service_payload = {"error" : "RepositoryNotFoundError" },
124
126
)
125
- elif isinstance (error , GatedRepoError ):
126
- return (
127
- f"Access denied to { url } "
127
+
128
+ if isinstance (error , GatedRepoError ):
129
+ raise AquaRuntimeError (
130
+ reason = f"Access denied to `{ url } ` "
128
131
"This repository is gated. Access is restricted to authorized users. "
129
132
"Please request access or check with the repository administrator. "
130
133
"If you are trying to access a gated repository, ensure you have a valid HF token registered. "
131
- "To register your token, run this command in your terminal: `huggingface-cli login`"
132
- )
133
- elif isinstance (error , RevisionNotFoundError ):
134
- return (
135
- f"The specified revision could not be found at { url } "
136
- "Please check the revision identifier and try again."
134
+ "To register your token, run this command in your terminal: `huggingface-cli login`" ,
135
+ service_payload = {"error" : "GatedRepoError" },
137
136
)
138
- else :
139
- return (
140
- f"An error occurred while accessing { url } "
141
- "Please check your network connection and try again. "
142
- "If you are trying to access a gated repository, ensure you have a valid HF token registered. "
143
- "To register your token, run this command in your terminal: `huggingface-cli login`"
137
+
138
+ if isinstance ( error , RevisionNotFoundError ):
139
+ raise AquaRuntimeError (
140
+ reason = f"The specified revision could not be found at ` { url } ` "
141
+ "Please check the revision identifier and try again." ,
142
+ service_payload = { "error" : "RevisionNotFoundError" },
144
143
)
145
144
145
+ raise AquaRuntimeError (
146
+ reason = f"An error occurred while accessing `{ url } ` "
147
+ "Please check your network connection and try again. "
148
+ "If you are trying to access a gated repository, ensure you have a valid HF token registered. "
149
+ "To register your token, run this command in your terminal: `huggingface-cli login`" ,
150
+ service_payload = {"error" : "Error" },
151
+ )
152
+
146
153
@handle_exceptions
147
154
def post (self , * args , ** kwargs ):
148
155
"""Handles post request for the HF Models APIs
@@ -166,11 +173,10 @@ def post(self, *args, **kwargs):
166
173
raise HTTPError (400 , Errors .MISSING_REQUIRED_PARAMETER .format ("model_id" ))
167
174
168
175
# Get model info from the HF
169
-
170
176
try :
171
177
hf_model_info = HfApi ().model_info (model_id )
172
178
except HfHubHTTPError as err :
173
- raise AquaRuntimeError ( self ._format_custom_error_message (err ) )
179
+ raise self ._format_custom_error_message (err )
174
180
175
181
# Check if model is not disabled
176
182
if hf_model_info .disabled :
0 commit comments