Update export_models.sh
Browse files- export_models.sh +60 -0
 
    	
        export_models.sh
    ADDED
    
    | 
         @@ -0,0 +1,60 @@ 
     | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
| 
         | 
|
| 1 | 
         
            +
            #!/bin/bash
         
     | 
| 2 | 
         
            +
            pip install "optimum[exporters]>=1.14.1" tensorflow
         
     | 
| 3 | 
         
            +
            git lfs track *.onnx*
         
     | 
| 4 | 
         
            +
             
     | 
| 5 | 
         
            +
            python << END
         
     | 
| 6 | 
         
            +
            from transformers import WhisperForConditionalGeneration, TFWhisperForConditionalGeneration, WhisperTokenizerFast
         
     | 
| 7 | 
         
            +
            import shutil
         
     | 
| 8 | 
         
            +
             
     | 
| 9 | 
         
            +
            # Backup generation_config.json
         
     | 
| 10 | 
         
            +
            shutil.copyfile('./generation_config.json', './generation_config_backup.json')
         
     | 
| 11 | 
         
            +
             
     | 
| 12 | 
         
            +
            print("Saving model to PyTorch...", end=" ")
         
     | 
| 13 | 
         
            +
            model = WhisperForConditionalGeneration.from_pretrained("./", from_flax=True)
         
     | 
| 14 | 
         
            +
            model.save_pretrained("./", safe_serialization=True)
         
     | 
| 15 | 
         
            +
            model.save_pretrained("./")
         
     | 
| 16 | 
         
            +
            print("Done.")
         
     | 
| 17 | 
         
            +
             
     | 
| 18 | 
         
            +
            print("Saving model to TensorFlow...", end=" ")
         
     | 
| 19 | 
         
            +
            tf_model = TFWhisperForConditionalGeneration.from_pretrained("./")
         
     | 
| 20 | 
         
            +
            tf_model.save_pretrained("./")
         
     | 
| 21 | 
         
            +
            print("Done.")
         
     | 
| 22 | 
         
            +
             
     | 
| 23 | 
         
            +
            # Restore the backup of generation_config.json
         
     | 
| 24 | 
         
            +
            shutil.move('./generation_config_backup.json', './generation_config.json')
         
     | 
| 25 | 
         
            +
             
     | 
| 26 | 
         
            +
            print("Saving model to ONNX...", end=" ")
         
     | 
| 27 | 
         
            +
            from optimum.onnxruntime import ORTModelForSpeechSeq2Seq
         
     | 
| 28 | 
         
            +
            ort_model = ORTModelForSpeechSeq2Seq.from_pretrained("./", export=True)
         
     | 
| 29 | 
         
            +
            ort_model.save_pretrained("./onnx")
         
     | 
| 30 | 
         
            +
            print("Done")
         
     | 
| 31 | 
         
            +
             
     | 
| 32 | 
         
            +
            print("Saving model to CTranslate...", end=" ")
         
     | 
| 33 | 
         
            +
            ct2-transformers-converter --model . --output_dir ct2
         
     | 
| 34 | 
         
            +
            cp ct2/model.bin .
         
     | 
| 35 | 
         
            +
            cp ct2/vocabulary.json .
         
     | 
| 36 | 
         
            +
            cp config.json config_hf.json
         
     | 
| 37 | 
         
            +
            jq -s '.[0] * .[1]' ct2/config.json config_hf.json > config.json
         
     | 
| 38 | 
         
            +
            print("Done")
         
     | 
| 39 | 
         
            +
             
     | 
| 40 | 
         
            +
             
     | 
| 41 | 
         
            +
            END
         
     | 
| 42 | 
         
            +
             
     | 
| 43 | 
         
            +
            echo "Saving model to GGML (whisper.cpp)..."
         
     | 
| 44 | 
         
            +
            wget -O convert-h5-to-ggml.py "https://raw.githubusercontent.com/NbAiLab/nb-whisper/main/convert-h5-to-ggml.py"
         
     | 
| 45 | 
         
            +
            mkdir -p whisper/assets
         
     | 
| 46 | 
         
            +
            wget -O whisper/assets/mel_filters.npz "https://github.com/openai/whisper/raw/c5d42560760a05584c1c79546a098287e5a771eb/whisper/assets/mel_filters.npz"
         
     | 
| 47 | 
         
            +
            python ./convert-h5-to-ggml.py ./ ./ ./
         
     | 
| 48 | 
         
            +
            rm ./convert-h5-to-ggml.py
         
     | 
| 49 | 
         
            +
            rm -rf ./whisper
         
     | 
| 50 | 
         
            +
            echo "Done"
         
     | 
| 51 | 
         
            +
             
     | 
| 52 | 
         
            +
            echo "Quantizing GGML model..."
         
     | 
| 53 | 
         
            +
            git clone --depth 1 https://github.com/ggerganov/whisper.cpp --branch v1.5.1
         
     | 
| 54 | 
         
            +
            cd whisper.cpp/
         
     | 
| 55 | 
         
            +
            make -j 32
         
     | 
| 56 | 
         
            +
            make quantize -j 32
         
     | 
| 57 | 
         
            +
            ./quantize ../ggml-model.bin ../ggml-model-q5_0.bin q5_0
         
     | 
| 58 | 
         
            +
            cd ..
         
     | 
| 59 | 
         
            +
            rm -rf whisper.cpp
         
     | 
| 60 | 
         
            +
            echo "Done"
         
     |