-
Notifications
You must be signed in to change notification settings - Fork 631
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unified
prepare_request
method + class-based providers (#2777)
* (first draft) Unified provider_helper.prepare_request method * style * token in Openai tests * together provider * raise later * Replicate provider * sambanova * fal ai * fal ai forgotten * fix test
- Loading branch information
Showing
25 changed files
with
1,646 additions
and
1,448 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
927 changes: 481 additions & 446 deletions
927
src/huggingface_hub/inference/_generated/_async_client.py
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import base64 | ||
import json | ||
from abc import ABC, abstractmethod | ||
from typing import Any, Dict, Optional, Union | ||
|
||
from huggingface_hub.inference._common import RequestParameters, TaskProviderHelper | ||
from huggingface_hub.utils import build_hf_headers, get_session | ||
|
||
|
||
BASE_URL = "https://fal.run" | ||
|
||
SUPPORTED_MODELS = { | ||
"automatic-speech-recognition": { | ||
"openai/whisper-large-v3": "fal-ai/whisper", | ||
}, | ||
"text-to-image": { | ||
"black-forest-labs/FLUX.1-schnell": "fal-ai/flux/schnell", | ||
"black-forest-labs/FLUX.1-dev": "fal-ai/flux/dev", | ||
}, | ||
} | ||
|
||
|
||
class FalAITask(TaskProviderHelper, ABC): | ||
"""Base class for FalAI API tasks.""" | ||
|
||
def __init__(self, task: str): | ||
self.task = task | ||
|
||
def prepare_request( | ||
self, | ||
*, | ||
inputs: Any, | ||
parameters: Dict[str, Any], | ||
headers: Dict, | ||
model: Optional[str], | ||
api_key: Optional[str], | ||
extra_payload: Optional[Dict[str, Any]] = None, | ||
) -> RequestParameters: | ||
mapped_model = self._map_model(model) | ||
|
||
if api_key is None: | ||
raise ValueError("You must provide an api_key to work with Together API.") | ||
headers = { | ||
**build_hf_headers(token=api_key), | ||
**headers, | ||
"authorization": f"Key {api_key}", | ||
} | ||
|
||
payload = self._prepare_payload(inputs, parameters=parameters) | ||
|
||
return RequestParameters( | ||
url=f"{BASE_URL}/{mapped_model}", | ||
task=self.task, | ||
model=mapped_model, | ||
json=payload, | ||
data=None, | ||
headers=headers, | ||
) | ||
|
||
def _map_model(self, model: Optional[str]) -> str: | ||
if model is None: | ||
raise ValueError("Please provide a model available on FalAI.") | ||
if self.task not in SUPPORTED_MODELS: | ||
raise ValueError(f"Task {self.task} not supported with FalAI.") | ||
mapped_model = SUPPORTED_MODELS[self.task].get(model) | ||
if mapped_model is None: | ||
raise ValueError(f"Model {model} is not supported with FalAI for task {self.task}.") | ||
return mapped_model | ||
|
||
@abstractmethod | ||
def _prepare_payload(self, inputs: Any, parameters: Dict[str, Any]) -> Dict[str, Any]: ... | ||
|
||
|
||
class FalAIAutomaticSpeechRecognitionTask(FalAITask): | ||
def __init__(self): | ||
super().__init__("automatic-speech-recognition") | ||
|
||
def _prepare_payload(self, inputs: Any, parameters: Dict[str, Any]) -> Dict[str, Any]: | ||
if isinstance(inputs, str) and inputs.startswith(("http://", "https://")): | ||
# If input is a URL, pass it directly | ||
audio_url = inputs | ||
else: | ||
# If input is a file path, read it first | ||
if isinstance(inputs, str): | ||
with open(inputs, "rb") as f: | ||
inputs = f.read() | ||
|
||
audio_b64 = base64.b64encode(inputs).decode() | ||
content_type = "audio/mpeg" | ||
audio_url = f"data:{content_type};base64,{audio_b64}" | ||
|
||
return { | ||
"audio_url": audio_url, | ||
**{k: v for k, v in parameters.items() if v is not None}, | ||
} | ||
|
||
def get_response(self, response: Union[bytes, Dict]) -> Any: | ||
text = _as_dict(response)["text"] | ||
if not isinstance(text, str): | ||
raise ValueError(f"Unexpected output format from FalAI API. Expected string, got {type(text)}.") | ||
return text | ||
|
||
|
||
class FalAITextToImageTask(FalAITask): | ||
def __init__(self): | ||
super().__init__("text-to-image") | ||
|
||
def _prepare_payload(self, inputs: Any, parameters: Dict[str, Any]) -> Dict[str, Any]: | ||
parameters = {k: v for k, v in parameters.items() if v is not None} | ||
if "image_size" not in parameters and "width" in parameters and "height" in parameters: | ||
parameters["image_size"] = { | ||
"width": parameters.pop("width"), | ||
"height": parameters.pop("height"), | ||
} | ||
return {"prompt": inputs, **parameters} | ||
|
||
def get_response(self, response: Union[bytes, Dict]) -> Any: | ||
url = _as_dict(response)["images"][0]["url"] | ||
return get_session().get(url).content | ||
|
||
|
||
def _as_dict(response: Union[bytes, Dict]) -> Dict: | ||
return json.loads(response) if isinstance(response, bytes) else response |
This file was deleted.
Oops, something went wrong.
63 changes: 0 additions & 63 deletions
63
src/huggingface_hub/inference/_providers/fal_ai/automatic_speech_recognition.py
This file was deleted.
Oops, something went wrong.
52 changes: 0 additions & 52 deletions
52
src/huggingface_hub/inference/_providers/fal_ai/text_to_image.py
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.