|
6 | 6 |
|
7 | 7 | from importlib import metadata
|
8 | 8 |
|
| 9 | +import huggingface_hub |
9 | 10 | import requests
|
| 11 | +from tornado.web import HTTPError |
10 | 12 |
|
11 | 13 | from ads.aqua import ODSC_MODEL_COMPARTMENT_OCID
|
12 | 14 | from ads.aqua.common.decorator import handle_exceptions
|
13 | 15 | from ads.aqua.common.errors import AquaResourceAccessError
|
14 | 16 | from ads.aqua.common.utils import fetch_service_compartment, known_realm
|
15 | 17 | from ads.aqua.extension.base_handler import AquaAPIhandler
|
| 18 | +from ads.aqua.extension.errors import Errors |
16 | 19 |
|
17 | 20 |
|
18 | 21 | class ADSVersionHandler(AquaAPIhandler):
|
@@ -62,8 +65,40 @@ def get(self):
|
62 | 65 | return self.finish("success")
|
63 | 66 |
|
64 | 67 |
|
| 68 | +class HFLoginHandler(AquaAPIhandler): |
| 69 | + """Handler to login to HF.""" |
| 70 | + |
| 71 | + @handle_exceptions |
| 72 | + def post(self, *args, **kwargs): |
| 73 | + """Handles post request for the HF login. |
| 74 | +
|
| 75 | + Raises |
| 76 | + ------ |
| 77 | + HTTPError |
| 78 | + Raises HTTPError if inputs are missing or are invalid. |
| 79 | + """ |
| 80 | + try: |
| 81 | + input_data = self.get_json_body() |
| 82 | + except Exception: |
| 83 | + raise HTTPError(400, Errors.INVALID_INPUT_DATA_FORMAT) |
| 84 | + |
| 85 | + if not input_data: |
| 86 | + raise HTTPError(400, Errors.NO_INPUT_DATA) |
| 87 | + |
| 88 | + token = input_data.get("token") |
| 89 | + |
| 90 | + if not token: |
| 91 | + raise HTTPError(400, Errors.MISSING_REQUIRED_PARAMETER.format("token")) |
| 92 | + |
| 93 | + # Login to HF |
| 94 | + huggingface_hub.login(token=token, new_session=False) |
| 95 | + |
| 96 | + return self.finish("success") |
| 97 | + |
| 98 | + |
65 | 99 | __handlers__ = [
|
66 | 100 | ("ads_version", ADSVersionHandler),
|
67 | 101 | ("hello", CompatibilityCheckHandler),
|
68 | 102 | ("network_status", NetworkStatusHandler),
|
| 103 | + ("hf_login", HFLoginHandler), |
69 | 104 | ]
|
0 commit comments