Gabi00 commited on
Commit
88716e0
·
verified ·
1 Parent(s): 3139cc0

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +35 -19
README.md CHANGED
@@ -38,32 +38,48 @@ import torch
38
  from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
39
  from datasets import load_dataset
40
 
41
-
42
  device = "cuda:0" if torch.cuda.is_available() else "cpu"
43
  torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
44
 
45
- model_id = "openai/whisper-large-v3"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
- model = AutoModelForSpeechSeq2Seq.from_pretrained(
48
- model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
49
- )
50
- model.to(device)
51
 
52
- processor = AutoProcessor.from_pretrained(model_id)
 
 
 
53
 
54
- pipe = pipeline(
55
- "automatic-speech-recognition",
56
- model=model,
57
- tokenizer=processor.tokenizer,
58
- feature_extractor=processor.feature_extractor,
59
- torch_dtype=torch_dtype,
60
- device=device,
61
- )
62
 
63
- dataset = load_dataset("distil-whisper/librispeech_long", "clean", split="validation")
64
- sample = dataset[0]["audio"]
65
 
66
- result = pipe(sample)
67
- print(result["text"])
68
  ```
69
 
 
38
  from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
39
  from datasets import load_dataset
40
 
 
41
  device = "cuda:0" if torch.cuda.is_available() else "cpu"
42
  torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
43
 
44
+ def download_adapter_model():
45
+ model_name = "whisper-v3-LoRA-en_students"
46
+ print(f"Downloading the adapter model '{model_name}' from the Hugging Face Hub.", flush=True)
47
+
48
+ # Define the path for the directory
49
+ local_directory = os.path.expanduser("~/.cache/huggingface/hub")
50
+
51
+ # Check if the directory exists
52
+ if not os.path.exists(local_directory):
53
+ # If it doesn't exist, create it
54
+ os.makedirs(local_directory)
55
+ print(f"Directory '{local_directory}' created.", flush=True)
56
+ else:
57
+ print(f"Directory '{local_directory}' already exists.", flush=True)
58
+
59
+ repo_id = f"Transducens/{model_name}"
60
+ repo_adapter_dir = f"{model_name}/checkpoint-5000/adapter_model"
61
+ repo_filename_config = f"{repo_adapter_dir}/adapter_config.json"
62
+ repo_filename_tensors = f"{repo_adapter_dir}/adapter_model.safetensors"
63
+
64
+ adapter_config = hf_hub_download(repo_id=repo_id, filename=repo_filename_config, local_dir=local_directory)
65
+ adapter_model_tensors = hf_hub_download(repo_id=repo_id, filename=repo_filename_tensors, local_dir=local_directory)
66
+
67
+ print(f"Dowloaded the adapter model '{model_name}' from the Hugging Face Hub.", flush=True)
68
 
69
+ return os.path.join(local_directory, repo_adapter_dir)
 
 
 
70
 
71
+ peft_model_id = adapter_path # Use the same model ID as before.
72
+ peft_config = PeftConfig.from_pretrained(peft_model_id)
73
+ model = WhisperForConditionalGeneration.from_pretrained(
74
+ peft_config.base_model_name_or_path, load_in_8bit=False)
75
 
76
+ model = PeftModel.from_pretrained(model, peft_model_id)
77
+ model.generation_config.language = "<|en|>"
78
+ model.generation_config.task = "transcribe"
 
 
 
 
 
79
 
80
+ tokenizer = WhisperTokenizer.from_pretrained("openai/whisper-large-v3", task="transcribe")
81
+ feature_extractor = WhisperFeatureExtractor.from_pretrained("openai/whisper-large-v3")
82
 
83
+ pipe = pipeline(model=model, tokenizer=tokenizer, feature_extractor=feature_extractor, task="automatic-speech-recognition", device=device)
 
84
  ```
85