model: Minicpmo (#3023)
This commit is contained in:
@@ -55,14 +55,13 @@ import triton
|
||||
import zmq
|
||||
from fastapi.responses import ORJSONResponse
|
||||
from packaging import version as pkg_version
|
||||
from packaging.version import Version, parse
|
||||
from PIL import Image
|
||||
from starlette.routing import Mount
|
||||
from torch import nn
|
||||
from torch.func import functional_call
|
||||
from torch.library import Library
|
||||
from torch.profiler import ProfilerActivity, profile, record_function
|
||||
from torch.utils._contextlib import _DecoratorContextManager
|
||||
from torch.utils.cpp_extension import CUDA_HOME
|
||||
from triton.runtime.cache import (
|
||||
FileCacheManager,
|
||||
default_cache_dir,
|
||||
@@ -507,9 +506,37 @@ def decode_video_base64(video_base64):
|
||||
) # Return an empty array and size tuple if no frames were found
|
||||
|
||||
|
||||
def load_image(image_file: Union[str, bytes]):
|
||||
from PIL import Image
|
||||
def load_audio(audio_file: str, sr: int = 16000, mono: bool = True) -> np.ndarray:
|
||||
# Use soundfile here, since librosa use it under the hood,
|
||||
# and librosa will not support audio loading in the future
|
||||
import soundfile as sf
|
||||
from scipy.signal import resample
|
||||
|
||||
# print(f"loading {audio_file}")
|
||||
# Load audio data
|
||||
if isinstance(audio_file, bytes):
|
||||
audio, original_sr = sf.read(BytesIO(audio_file))
|
||||
elif audio_file.startswith("data:"):
|
||||
audio_file = audio_file.split(",")[1]
|
||||
audio, original_sr = sf.read(BytesIO(base64.b64decode(audio_file)))
|
||||
elif isinstance(audio_file, str):
|
||||
audio, original_sr = sf.read(audio_file)
|
||||
else:
|
||||
raise ValueError(f"Invalid audio format: {audio_file}")
|
||||
|
||||
# Resample audio if the original sample rate is different from the desired sample rate
|
||||
if original_sr != sr:
|
||||
num_samples = int(len(audio) * float(sr) / original_sr)
|
||||
audio = resample(audio, num_samples)
|
||||
|
||||
# Convert to mono if requested and audio is stereo
|
||||
if mono and len(audio.shape) > 1:
|
||||
audio = np.mean(audio, axis=1)
|
||||
|
||||
return audio
|
||||
|
||||
|
||||
def load_image(image_file: Union[str, bytes]) -> tuple[Image, tuple[int, int]]:
|
||||
image = image_size = None
|
||||
|
||||
if isinstance(image_file, bytes):
|
||||
|
||||
Reference in New Issue
Block a user