Repository: huggingface/transformers
File: src/transformers/pipelines/automatic_speech_recognition.py (Python)

Code before:
            if "input_features" in model_inputs:
                inputs = model_inputs.pop("input_features")
            elif "input_values" in model_inputs:
                inputs = model_inputs.pop("input_values")
            else:
                raise ValueError(
                    "Seq2Seq speech recognition model requires either a "
                    f"`input_features` or `input_values` key, but only has {model_inputs.keys()}"
                )

            # custom processing for Whisper timestamps and word-level timestamps
            return_timestamps = return_timestamps or getattr(self.generation_config, "return_timestamps", False)
            if return_timestamps and self.type == "seq2seq_whisper":
                generate_kwargs["return_timestamps"] = bool(return_timestamps)
                if return_timestamps == "word":
                    generate_kwargs["return_token_timestamps"] = True
                    generate_kwargs["return_segments"] = True

            # User-defined `generation_config` passed to the pipeline call take precedence
            generation_config = generate_kwargs.get("generation_config", self.generation_config)

            # For Whisper models, ensure the generation config has all required attributes
            if self.type == "seq2seq_whisper":
                # Try to get attributes from model's generation_config first
                model_gen_config = None
                if hasattr(self.model, "generation_config") and self.model.generation_config is not None:
                    model_gen_config = self.model.generation_config
                elif (
                    hasattr(self.model.config, "generation_config") and self.model.config.generation_config is not None
                ):
                    model_gen_config = self.model.config.generation_config

                # Copy missing attributes from model's generation config
                if model_gen_config is not None:
                    if hasattr(model_gen_config, "lang_to_id") and not hasattr(generation_config, "lang_to_id"):
                        generation_config.lang_to_id = model_gen_config.lang_to_id
                    if hasattr(model_gen_config, "task_to_id") and not hasattr(generation_config, "task_to_id"):
                        generation_config.task_to_id = model_gen_config.task_to_id
                    if hasattr(model_gen_config, "no_timestamps_token_id") and not hasattr(
                        generation_config, "no_timestamps_token_id"
                    ):
                        generation_config.no_timestamps_token_id = model_gen_config.no_timestamps_token_id
                    if hasattr(model_gen_config, "is_multilingual") and not hasattr(
                        generation_config, "is_multilingual"
                    ):
                        generation_config.is_multilingual = model_gen_config.is_multilingual

                # If still missing, try to get from tokenizer
                if self.tokenizer is not None:
                    if not hasattr(generation_config, "lang_to_id") and hasattr(self.tokenizer, "lang_to_id"):
                        generation_config.lang_to_id = self.tokenizer.lang_to_id

Review comment (suggestion):
Same, `getattr(self.model, "generation_config", None)` should work here 

Code after:
            if "input_features" in model_inputs:
                inputs = model_inputs.pop("input_features")
            elif "input_values" in model_inputs:
                inputs = model_inputs.pop("input_values")
            else:
                raise ValueError(
                    "Seq2Seq speech recognition model requires either a "
                    f"`input_features` or `input_values` key, but only has {model_inputs.keys()}"
                )

            # custom processing for Whisper timestamps and word-level timestamps
            return_timestamps = return_timestamps or getattr(self.generation_config, "return_timestamps", False)
            if return_timestamps and self.type == "seq2seq_whisper":
                generate_kwargs["return_timestamps"] = bool(return_timestamps)
                if return_timestamps == "word":
                    generate_kwargs["return_token_timestamps"] = True
                    generate_kwargs["return_segments"] = True

            # User-defined `generation_config` passed to the pipeline call take precedence
            if "generation_config" not in generate_kwargs:
                generate_kwargs["generation_config"] = self.generation_config

            main_input_name = self.model.main_input_name if hasattr(self.model, "main_input_name") else "inputs"
            generate_kwargs = {
                main_input_name: inputs,
                "attention_mask": attention_mask,
                **generate_kwargs,
            }
            tokens = self.model.generate(**generate_kwargs)

            # whisper longform generation stores timestamps in "segments"
            if return_timestamps == "word" and self.type == "seq2seq_whisper":
                if "segments" not in tokens:
                    out = {"tokens": tokens["sequences"], "token_timestamps": tokens["token_timestamps"]}
                else:
                    token_timestamps = [
                        torch.cat([segment["token_timestamps"] for segment in segment_list])
                        for segment_list in tokens["segments"]
                    ]
                    out = {"tokens": tokens["sequences"], "token_timestamps": token_timestamps}
            else:
                out = {"tokens": tokens}
            if self.type == "seq2seq_whisper":
                if stride is not None:
                    out["stride"] = stride

        else:
            inputs = {
                self.model.main_input_name: model_inputs.pop(self.model.main_input_name),
                "attention_mask": attention_mask,
            }