Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion libs/infinity_emb/infinity_emb/fastapi_schemas/pymodels.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,24 @@ def get_modality(obj: dict) -> str:
Function name is used to return error message, keep it explicit
"""
try:
return obj.get("modality", Modality.text.value)
modality = obj.get("modality", None)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am worried that this, combined with a check takes a hit on performance.

if modality is None:
inputs = obj.get("input", None)
if isinstance(inputs, str):
inputs = [inputs]
if isinstance(inputs, list) and len(inputs) > 0:
first_input = inputs[0]
if first_input.startswith("data:") or first_input.startswith("http://") or first_input.startswith("https://"):
ext = first_input.split("?")[0].split(".")[-1]
if ext in ["jpg", "jpeg", "png", "gif", "bmp"]:
modality = Modality.image.value
elif ext in ["wav", "mp3", "ogg", "flac"]:
modality = Modality.audio.value
elif first_input.startswith("data:image/"):
modality = Modality.image.value
elif first_input.startswith("data:audio/"):
modality = Modality.audio.value
return modality or Modality.text.value
except AttributeError:
# in case a very weird request is sent, validate it against the default
return Modality.text.value
Expand Down