Session Close when called HuggingFace Client (#411)

* [FIX] Session Close

* [NEW] Async Context Managers of Response

* [NEW] Linting
This commit is contained in:
Nil Andreu
2023-01-05 17:49:39 +01:00
committed by GitHub
parent ee50b573e1
commit 739c073328
+10 -8
View File
@@ -35,15 +35,17 @@ class HuggingFaceAPI:
inference: the inference we obtain from the model in HF
"""
session = aiohttp.ClientSession()
payload: Dict[str, str] = {"inputs": input}
response = await session.post(self.api_url, headers=self.headers, json=payload)
async with aiohttp.ClientSession() as session:
payload: Dict[str, str] = {"inputs": input}
# If we get a bad response
if response.status != 200:
raise OasstError("Response Error Detoxify HuggingFace", error_code=OasstErrorCode.HUGGINGFACE_API_ERROR)
async with session.post(self.api_url, headers=self.headers, json=payload) as response:
# If we get a bad response
if response.status != 200:
raise OasstError(
"Response Error Detoxify HuggingFace", error_code=OasstErrorCode.HUGGINGFACE_API_ERROR
)
# Get the response from the API call
inference = await response.json()
# Get the response from the API call
inference = await response.json()
return inference