Huong
commited on
Commit
·
791cc99
1
Parent(s):
078db05
updated app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,12 @@ import re
|
|
| 10 |
import librosa
|
| 11 |
import torch
|
| 12 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
|
| 14 |
from whisper.normalizers import EnglishTextNormalizer
|
| 15 |
from whisper import audio, DecodingOptions
|
|
@@ -20,21 +26,130 @@ from bs4 import BeautifulSoup
|
|
| 20 |
|
| 21 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 22 |
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
| 23 |
-
hf_model_path = "checkpoints/medium_hf_demo"
|
| 24 |
-
olmoasr_ckpt = (
|
| 25 |
-
"checkpoints/eval_latesttrain_00524288_medium_fsdp-train_grad-acc_bfloat16_inf.pt"
|
| 26 |
-
)
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
| 30 |
)
|
| 31 |
-
|
| 32 |
-
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
normalizer = EnglishTextNormalizer()
|
| 40 |
|
|
|
|
| 10 |
import librosa
|
| 11 |
import torch
|
| 12 |
import numpy as np
|
| 13 |
+
import os
|
| 14 |
+
import tempfile
|
| 15 |
+
import subprocess
|
| 16 |
+
import sys
|
| 17 |
+
from pathlib import Path
|
| 18 |
+
from huggingface_hub import hf_hub_download
|
| 19 |
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
|
| 20 |
from whisper.normalizers import EnglishTextNormalizer
|
| 21 |
from whisper import audio, DecodingOptions
|
|
|
|
| 26 |
|
| 27 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 28 |
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
# Configuration for model download and conversion
|
| 31 |
+
OLMOASR_REPO = "allenai/OLMoASR" # Temporary model link as requested
|
| 32 |
+
CHECKPOINT_FILENAME = (
|
| 33 |
+
"OLMoASR-medium.en.pt" # Adjust based on actual filename in the repo
|
| 34 |
)
|
| 35 |
+
LOCAL_CHECKPOINT_DIR = "checkpoints"
|
| 36 |
+
HF_MODEL_DIR = "checkpoints/medium_hf_converted"
|
| 37 |
|
| 38 |
+
|
| 39 |
+
def ensure_checkpoint_dir():
|
| 40 |
+
"""Ensure the checkpoint directory exists."""
|
| 41 |
+
Path(LOCAL_CHECKPOINT_DIR).mkdir(parents=True, exist_ok=True)
|
| 42 |
+
Path(HF_MODEL_DIR).mkdir(parents=True, exist_ok=True)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def download_olmoasr_checkpoint():
|
| 46 |
+
"""Download OLMoASR checkpoint from HuggingFace hub."""
|
| 47 |
+
ensure_checkpoint_dir()
|
| 48 |
+
|
| 49 |
+
local_checkpoint_path = os.path.join(LOCAL_CHECKPOINT_DIR, CHECKPOINT_FILENAME)
|
| 50 |
+
|
| 51 |
+
# Check if checkpoint already exists
|
| 52 |
+
if os.path.exists(local_checkpoint_path):
|
| 53 |
+
print(f"Checkpoint already exists at {local_checkpoint_path}")
|
| 54 |
+
return local_checkpoint_path
|
| 55 |
+
|
| 56 |
+
try:
|
| 57 |
+
print(f"Downloading checkpoint from {OLMOASR_REPO}")
|
| 58 |
+
downloaded_path = hf_hub_download(
|
| 59 |
+
repo_id=OLMOASR_REPO,
|
| 60 |
+
filename=CHECKPOINT_FILENAME,
|
| 61 |
+
local_dir=LOCAL_CHECKPOINT_DIR,
|
| 62 |
+
local_dir_use_symlinks=False,
|
| 63 |
+
)
|
| 64 |
+
print(f"Downloaded checkpoint to {downloaded_path}")
|
| 65 |
+
return downloaded_path
|
| 66 |
+
except Exception as e:
|
| 67 |
+
print(f"Error downloading checkpoint: {e}")
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def convert_checkpoint_to_hf(checkpoint_path):
|
| 71 |
+
"""Convert OLMoASR checkpoint to HuggingFace format using subprocess."""
|
| 72 |
+
if os.path.exists(os.path.join(HF_MODEL_DIR, "config.json")):
|
| 73 |
+
print(f"HuggingFace model already exists at {HF_MODEL_DIR}")
|
| 74 |
+
return HF_MODEL_DIR
|
| 75 |
+
|
| 76 |
+
try:
|
| 77 |
+
print(f"Converting checkpoint {checkpoint_path} to HuggingFace format")
|
| 78 |
+
|
| 79 |
+
# Path to the conversion script
|
| 80 |
+
script_path = os.path.join(os.path.dirname(__file__), "convert_openai_to_hf.py")
|
| 81 |
+
|
| 82 |
+
# Run the conversion script using subprocess
|
| 83 |
+
cmd = [
|
| 84 |
+
sys.executable,
|
| 85 |
+
script_path,
|
| 86 |
+
"--checkpoint_path",
|
| 87 |
+
checkpoint_path,
|
| 88 |
+
"--pytorch_dump_folder_path",
|
| 89 |
+
HF_MODEL_DIR,
|
| 90 |
+
"--convert_preprocessor",
|
| 91 |
+
"True",
|
| 92 |
+
]
|
| 93 |
+
|
| 94 |
+
print(f"Running conversion command: {' '.join(cmd)}")
|
| 95 |
+
|
| 96 |
+
# Execute the conversion script
|
| 97 |
+
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
|
| 98 |
+
|
| 99 |
+
print("Conversion output:")
|
| 100 |
+
print(result.stdout)
|
| 101 |
+
|
| 102 |
+
if result.stderr:
|
| 103 |
+
print("Conversion warnings/errors:")
|
| 104 |
+
print(result.stderr)
|
| 105 |
+
|
| 106 |
+
# Verify that the conversion was successful
|
| 107 |
+
if os.path.exists(os.path.join(HF_MODEL_DIR, "config.json")):
|
| 108 |
+
print(f"Model successfully converted and saved to {HF_MODEL_DIR}")
|
| 109 |
+
return HF_MODEL_DIR
|
| 110 |
+
else:
|
| 111 |
+
raise Exception("Conversion completed but config.json not found")
|
| 112 |
+
|
| 113 |
+
except subprocess.CalledProcessError as e:
|
| 114 |
+
print(f"Conversion script failed with return code {e.returncode}")
|
| 115 |
+
print(f"stdout: {e.stdout}")
|
| 116 |
+
print(f"stderr: {e.stderr}")
|
| 117 |
+
raise e
|
| 118 |
+
except Exception as e:
|
| 119 |
+
print(f"Error converting checkpoint: {e}")
|
| 120 |
+
raise e
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
def initialize_models():
|
| 124 |
+
"""Initialize both HuggingFace and OLMoASR models."""
|
| 125 |
+
# Download and convert HuggingFace model
|
| 126 |
+
checkpoint_path = download_olmoasr_checkpoint()
|
| 127 |
+
hf_model_path = convert_checkpoint_to_hf(checkpoint_path)
|
| 128 |
+
olmoasr_ckpt = checkpoint_path
|
| 129 |
+
|
| 130 |
+
# Load HuggingFace model
|
| 131 |
+
hf_model = AutoModelForSpeechSeq2Seq.from_pretrained(
|
| 132 |
+
hf_model_path,
|
| 133 |
+
torch_dtype=torch_dtype,
|
| 134 |
+
low_cpu_mem_usage=True,
|
| 135 |
+
use_safetensors=True,
|
| 136 |
+
)
|
| 137 |
+
hf_model.to(device).eval()
|
| 138 |
+
processor = AutoProcessor.from_pretrained(hf_model_path)
|
| 139 |
+
|
| 140 |
+
# Load OLMoASR model
|
| 141 |
+
olmoasr_model = load_model(
|
| 142 |
+
name=olmoasr_ckpt, device=device, inference=True, in_memory=True
|
| 143 |
+
)
|
| 144 |
+
olmoasr_model.to(device).eval()
|
| 145 |
+
|
| 146 |
+
return hf_model, processor, olmoasr_model
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
# Initialize models
|
| 150 |
+
print("Initializing models...")
|
| 151 |
+
hf_model, processor, olmoasr_model = initialize_models()
|
| 152 |
+
print("Models initialized successfully!")
|
| 153 |
|
| 154 |
normalizer = EnglishTextNormalizer()
|
| 155 |
|