Namkoy commited on
Commit
348f5ab
1 Parent(s): 796e357
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import gradio as gr
3
+ from transformers import (
4
+ AutomaticSpeechRecognitionPipeline,
5
+ WhisperForConditionalGeneration,
6
+ WhisperTokenizer,
7
+ WhisperProcessor,
8
+ )
9
+ from peft import PeftModel, PeftConfig
10
+
11
+
12
+ peft_model_id = "Namkoy/whisper_peft_vi_nam"
13
+ language = "vietnamese"
14
+ task = "transcribe"
15
+ peft_config = PeftConfig.from_pretrained(peft_model_id)
16
+ model = WhisperForConditionalGeneration.from_pretrained(
17
+ peft_config.base_model_name_or_path, load_in_8bit=True, device_map="auto"
18
+ )
19
+
20
+ model = PeftModel.from_pretrained(model, peft_model_id)
21
+ tokenizer = WhisperTokenizer.from_pretrained(peft_config.base_model_name_or_path, language=language, task=task)
22
+ processor = WhisperProcessor.from_pretrained(peft_config.base_model_name_or_path, language=language, task=task)
23
+ feature_extractor = processor.feature_extractor
24
+ forced_decoder_ids = processor.get_decoder_prompt_ids(language=language, task=task)
25
+ pipe = AutomaticSpeechRecognitionPipeline(model=model, tokenizer=tokenizer, feature_extractor=feature_extractor)
26
+
27
+
28
+ def transcribe(audio):
29
+ with torch.cuda.amp.autocast():
30
+ text = pipe(audio, generate_kwargs={"forced_decoder_ids": forced_decoder_ids}, max_new_tokens=255)["text"]
31
+ return text
32
+
33
+
34
+ iface = gr.Interface(
35
+ fn=transcribe,
36
+ inputs=gr.Audio(type="filepath"),
37
+ outputs="text",
38
+ title="PEFT LoRA + INT8 Whisper Large V2 Vietnamese",
39
+ description="Realtime demo for Vietnamese speech recognition using `PEFT-LoRA+INT8` fine-tuned Whisper Large V2 model.",
40
+ )
41
+
42
+ iface.launch(share=True)