Spaces:
Sleeping
Sleeping
Yuvvan Talreja commited on
Commit ·
9c11e8c
1
Parent(s): e13d740
Fixes
Browse files- Dockerfile +3 -1
- fastapi_app.py +41 -13
Dockerfile
CHANGED
|
@@ -19,7 +19,9 @@ RUN pip install --no-cache-dir sentence-transformers>=2.2.2
|
|
| 19 |
|
| 20 |
# Download model files
|
| 21 |
RUN python -c "from sentence_transformers import SentenceTransformer; SentenceTransformer('all-MiniLM-L6-v2')"
|
| 22 |
-
RUN python -c "from transformers import
|
|
|
|
|
|
|
| 23 |
|
| 24 |
# Run NLTK downloads
|
| 25 |
RUN python -m nltk.downloader punkt wordnet
|
|
|
|
| 19 |
|
| 20 |
# Download model files
|
| 21 |
RUN python -c "from sentence_transformers import SentenceTransformer; SentenceTransformer('all-MiniLM-L6-v2')"
|
| 22 |
+
RUN python -c "from transformers import AutoProcessor, AutoModelForSpeechSeq2Seq; \
|
| 23 |
+
processor = AutoProcessor.from_pretrained('openai/whisper-base'); \
|
| 24 |
+
model = AutoModelForSpeechSeq2Seq.from_pretrained('openai/whisper-base', from_tf=True)"
|
| 25 |
|
| 26 |
# Run NLTK downloads
|
| 27 |
RUN python -m nltk.downloader punkt wordnet
|
fastapi_app.py
CHANGED
|
@@ -87,32 +87,60 @@ def get_model(model_name):
|
|
| 87 |
logging.info(f"Converting Xenova model {model_name} to {hf_model_name}")
|
| 88 |
|
| 89 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
models[model_name] = pipeline(
|
| 91 |
"automatic-speech-recognition",
|
| 92 |
-
|
|
|
|
|
|
|
| 93 |
chunk_length_s=30,
|
| 94 |
-
stride_length_s=5
|
| 95 |
-
from_tf=True, # Added this parameter to handle TensorFlow models
|
| 96 |
-
model_kwargs={"use_fast": True}, # Use fast version of tokenizer
|
| 97 |
-
tokenizer_kwargs={"use_fast": True}
|
| 98 |
)
|
| 99 |
-
logging.info(f"Model {model_name} loaded successfully")
|
| 100 |
except Exception as e:
|
| 101 |
logging.error(f"Error loading model with from_tf=True, trying with from_tf=False: {str(e)}")
|
| 102 |
try:
|
| 103 |
-
# Fallback to
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
models[model_name] = pipeline(
|
| 105 |
"automatic-speech-recognition",
|
| 106 |
-
|
|
|
|
|
|
|
| 107 |
chunk_length_s=30,
|
| 108 |
-
stride_length_s=5
|
| 109 |
-
model_kwargs={"use_fast": True}, # Use fast version of tokenizer
|
| 110 |
-
tokenizer_kwargs={"use_fast": True}
|
| 111 |
)
|
| 112 |
logging.info(f"Model {model_name} loaded successfully with from_tf=False")
|
| 113 |
except Exception as e2:
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
return models[model_name]
|
| 117 |
|
| 118 |
def process_audio(audio_data, sample_rate=16000, model_name="openai/whisper-base"):
|
|
|
|
| 87 |
logging.info(f"Converting Xenova model {model_name} to {hf_model_name}")
|
| 88 |
|
| 89 |
try:
|
| 90 |
+
# First, try loading the model with from_tf=True
|
| 91 |
+
from transformers import AutoProcessor, AutoModelForSpeechSeq2Seq, pipeline
|
| 92 |
+
|
| 93 |
+
processor = AutoProcessor.from_pretrained(hf_model_name)
|
| 94 |
+
model = AutoModelForSpeechSeq2Seq.from_pretrained(
|
| 95 |
+
hf_model_name,
|
| 96 |
+
from_tf=True,
|
| 97 |
+
use_fast=True
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
models[model_name] = pipeline(
|
| 101 |
"automatic-speech-recognition",
|
| 102 |
+
model=model,
|
| 103 |
+
tokenizer=processor,
|
| 104 |
+
feature_extractor=processor,
|
| 105 |
chunk_length_s=30,
|
| 106 |
+
stride_length_s=5
|
|
|
|
|
|
|
|
|
|
| 107 |
)
|
| 108 |
+
logging.info(f"Model {model_name} loaded successfully with from_tf=True")
|
| 109 |
except Exception as e:
|
| 110 |
logging.error(f"Error loading model with from_tf=True, trying with from_tf=False: {str(e)}")
|
| 111 |
try:
|
| 112 |
+
# Fallback to regular loading method
|
| 113 |
+
from transformers import AutoProcessor, AutoModelForSpeechSeq2Seq, pipeline
|
| 114 |
+
|
| 115 |
+
processor = AutoProcessor.from_pretrained(hf_model_name)
|
| 116 |
+
model = AutoModelForSpeechSeq2Seq.from_pretrained(
|
| 117 |
+
hf_model_name,
|
| 118 |
+
use_fast=True
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
models[model_name] = pipeline(
|
| 122 |
"automatic-speech-recognition",
|
| 123 |
+
model=model,
|
| 124 |
+
tokenizer=processor,
|
| 125 |
+
feature_extractor=processor,
|
| 126 |
chunk_length_s=30,
|
| 127 |
+
stride_length_s=5
|
|
|
|
|
|
|
| 128 |
)
|
| 129 |
logging.info(f"Model {model_name} loaded successfully with from_tf=False")
|
| 130 |
except Exception as e2:
|
| 131 |
+
# Last resort: try direct pipeline with no extra args
|
| 132 |
+
try:
|
| 133 |
+
from transformers import pipeline
|
| 134 |
+
models[model_name] = pipeline(
|
| 135 |
+
"automatic-speech-recognition",
|
| 136 |
+
hf_model_name,
|
| 137 |
+
chunk_length_s=30,
|
| 138 |
+
stride_length_s=5
|
| 139 |
+
)
|
| 140 |
+
logging.info(f"Model {model_name} loaded successfully with direct pipeline")
|
| 141 |
+
except Exception as e3:
|
| 142 |
+
logging.error(f"Failed to load model {model_name}: {str(e3)}")
|
| 143 |
+
raise RuntimeError(f"Failed to load model {model_name}: {str(e3)}")
|
| 144 |
return models[model_name]
|
| 145 |
|
| 146 |
def process_audio(audio_data, sample_rate=16000, model_name="openai/whisper-base"):
|