mirror of
https://github.com/modelscope/FunASR.git
synced 2026-10-12 02:19:18 +00:00
fix: add try-import guards for optional deps (fastapi, humanfriendly, transformers)
- funasr/bin/_server_app.py: wrap fastapi import with helpful error message - funasr/bin/tokenize_text.py: guard cli_utils import - funasr/utils/type_utils.py: guard humanfriendly import - funasr/frontends/s3prl.py: guard humanfriendly import - funasr/utils/types.py: add compat shim (types -> type_utils) - funasr/frontends/abs_frontend.py: add missing abstract class - funasr/models/fun_asr_nano/model.py: guard transformers import Fixes #2937 - users seeing import errors on fresh pip install
This commit is contained in:
@@ -7,8 +7,13 @@ import re
|
||||
import logging
|
||||
from typing import Optional
|
||||
|
||||
from fastapi import FastAPI, UploadFile, File, Form, HTTPException
|
||||
from fastapi.responses import JSONResponse
|
||||
try:
|
||||
from fastapi import FastAPI, UploadFile, File, Form, HTTPException
|
||||
from fastapi.responses import JSONResponse
|
||||
except ImportError:
|
||||
raise ImportError(
|
||||
"funasr-server requires fastapi. Install with: pip install fastapi uvicorn python-multipart"
|
||||
)
|
||||
|
||||
logger = logging.getLogger("funasr.server")
|
||||
|
||||
|
||||
@@ -8,7 +8,11 @@ from typing import List
|
||||
from typing import Optional
|
||||
|
||||
|
||||
from funasr.utils.cli_utils import get_commandline_args
|
||||
try:
|
||||
from funasr.utils.cli_utils import get_commandline_args
|
||||
except ImportError:
|
||||
def get_commandline_args():
|
||||
return {}
|
||||
from funasr.tokenizer.build_tokenizer import build_tokenizer
|
||||
from funasr.tokenizer.cleaner import TextCleaner
|
||||
from funasr.tokenizer.phoneme_tokenizer import g2p_classes
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
from abc import ABC, abstractmethod
|
||||
import torch
|
||||
|
||||
|
||||
class AbsFrontend(ABC, torch.nn.Module):
|
||||
@abstractmethod
|
||||
def output_size(self) -> int:
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
def forward(self, input, input_lengths):
|
||||
raise NotImplementedError
|
||||
@@ -6,7 +6,10 @@ from typing import Optional
|
||||
from typing import Tuple
|
||||
from typing import Union
|
||||
|
||||
import humanfriendly
|
||||
try:
|
||||
import humanfriendly
|
||||
except ImportError:
|
||||
humanfriendly = None
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
|
||||
@@ -50,7 +53,10 @@ class S3prlFrontend(nn.Module):
|
||||
"""
|
||||
super().__init__()
|
||||
if isinstance(fs, str):
|
||||
fs = humanfriendly.parse_size(fs)
|
||||
if humanfriendly is not None:
|
||||
fs = humanfriendly.parse_size(fs)
|
||||
else:
|
||||
fs = int(fs)
|
||||
|
||||
if download_dir is not None:
|
||||
torch.hub.set_dir(download_dir)
|
||||
|
||||
@@ -15,7 +15,11 @@ from funasr.register import tables
|
||||
from funasr.train_utils.device_funcs import force_gatherable, to_device
|
||||
from funasr.utils.datadir_writer import DatadirWriter
|
||||
from funasr.utils.load_utils import extract_fbank, load_audio_text_image_video
|
||||
from transformers import AutoConfig, AutoModelForCausalLM
|
||||
try:
|
||||
from transformers import AutoConfig, AutoModelForCausalLM
|
||||
except ImportError:
|
||||
AutoConfig = None
|
||||
AutoModelForCausalLM = None
|
||||
|
||||
from .ctc import CTC
|
||||
from .tools.utils import forced_align
|
||||
|
||||
@@ -3,7 +3,10 @@ from typing import Optional
|
||||
from typing import Tuple
|
||||
from typing import Union
|
||||
|
||||
import humanfriendly
|
||||
try:
|
||||
import humanfriendly
|
||||
except ImportError:
|
||||
humanfriendly = None
|
||||
|
||||
|
||||
def str2bool(value: str) -> bool:
|
||||
@@ -95,6 +98,8 @@ def humanfriendly_parse_size_or_none(value) -> Optional[float]:
|
||||
"""
|
||||
if value.strip().lower() in ("none", "null", "nil"):
|
||||
return None
|
||||
if humanfriendly is None:
|
||||
raise ImportError("humanfriendly is required for parse_size. Install with: pip install humanfriendly")
|
||||
return humanfriendly.parse_size(value)
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
# Compatibility shim: types was renamed to type_utils
|
||||
from funasr.utils.type_utils import *
|
||||
Reference in New Issue
Block a user