yellowcandle commited on
Commit
7f92c21
·
unverified ·
1 Parent(s): a616a2e

load the default models on start

Browse files
Files changed (1) hide show
  1. app.py +37 -20
app.py CHANGED
@@ -5,26 +5,25 @@ import orjson
5
  import torch
6
  from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline, AutoModelForCausalLM, AutoTokenizer
7
 
 
 
 
8
  @spaces.GPU(duration=60)
9
- def transcribe_audio(audio, model_id):
 
10
  if audio is None:
11
  return "Please upload an audio file."
12
- if model_id is None:
13
  return "Please select a model."
14
 
15
- device = "cuda:0" if torch.cuda.is_available() else "cpu"
16
  torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
17
 
18
- model = AutoModelForSpeechSeq2Seq.from_pretrained(
19
- model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
20
- )
21
- model.to(device)
22
-
23
- processor = AutoProcessor.from_pretrained(model_id)
24
 
25
  pipe = pipeline(
26
  "automatic-speech-recognition",
27
- model=model,
28
  tokenizer=processor.tokenizer,
29
  feature_extractor=processor.feature_extractor,
30
  max_new_tokens=128,
@@ -38,18 +37,19 @@ def transcribe_audio(audio, model_id):
38
  return result["text"]
39
 
40
  @spaces.GPU(duration=120)
41
- def proofread(text, model_id):
 
42
  if text is None:
43
  return "Please provide the transcribed text for proofreading."
44
 
45
- device = "cuda:0" if torch.cuda.is_available() else "cpu"
46
  torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
47
 
48
  messages = [
49
  {"role": "system", "content": "用繁體中文語體文整理這段文字,在最後加上整段文字的重點。"},
50
  {"role": "user", "content": text},
51
  ]
52
- pipe = pipeline("text-generation", model=model_id)
53
  llm_output = pipe(messages)
54
 
55
  # Extract the generated text
@@ -61,27 +61,44 @@ def proofread(text, model_id):
61
  proofread_text = assistant_content
62
  return proofread_text
63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  with gr.Blocks() as demo:
65
  gr.Markdown("""
66
  # Audio Transcription and Proofreading
67
- 1. Upload an audio file (Wait for the file to be fully loaded first)
68
- 2. Select a model for transcription
69
- 3. Proofread the transcribed text
 
70
  """)
71
 
72
  with gr.Row():
73
- audio = gr.Audio(sources="upload", type="filepath")
74
  transcribe_model_dropdown = gr.Dropdown(choices=["openai/whisper-large-v3", "alvanlii/whisper-small-cantonese"], value="alvanlii/whisper-small-cantonese", label="Select Transcription Model")
75
  proofread_model_dropdown = gr.Dropdown(choices=["hfl/llama-3-chinese-8b-instruct-v3"], value="hfl/llama-3-chinese-8b-instruct-v3", label="Select Proofreading Model")
 
76
 
 
 
77
  transcribe_button = gr.Button("Transcribe")
78
  transcribed_text = gr.Textbox(label="Transcribed Text")
79
 
80
  proofread_button = gr.Button("Proofread")
81
  proofread_output = gr.Textbox(label="Proofread Text")
82
 
83
- transcribe_button.click(transcribe_audio, inputs=[audio, transcribe_model_dropdown], outputs=transcribed_text)
84
- proofread_button.click(proofread, inputs=[transcribed_text, proofread_model_dropdown], outputs=proofread_output)
85
- transcribed_text.change(proofread, inputs=[transcribed_text, proofread_model_dropdown], outputs=proofread_output)
 
86
 
87
  demo.launch()
 
5
  import torch
6
  from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline, AutoModelForCausalLM, AutoTokenizer
7
 
8
+ transcribe_model = None
9
+ proofread_model = None
10
+
11
  @spaces.GPU(duration=60)
12
+ def transcribe_audio(audio):
13
+ global transcribe_model
14
  if audio is None:
15
  return "Please upload an audio file."
16
+ if transcribe_model is None:
17
  return "Please select a model."
18
 
19
+ device = "cuda:0" if torch.cuda.is_available() else "mps" if torch.mps.is_available() else "cpu"
20
  torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
21
 
22
+ processor = AutoProcessor.from_pretrained(transcribe_model)
 
 
 
 
 
23
 
24
  pipe = pipeline(
25
  "automatic-speech-recognition",
26
+ model=transcribe_model,
27
  tokenizer=processor.tokenizer,
28
  feature_extractor=processor.feature_extractor,
29
  max_new_tokens=128,
 
37
  return result["text"]
38
 
39
  @spaces.GPU(duration=120)
40
+ def proofread(text):
41
+ global proofread_model
42
  if text is None:
43
  return "Please provide the transcribed text for proofreading."
44
 
45
+ device = "cuda:0" if torch.cuda.is_available() else "mps" if torch.mps.is_available() else "cpu"
46
  torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
47
 
48
  messages = [
49
  {"role": "system", "content": "用繁體中文語體文整理這段文字,在最後加上整段文字的重點。"},
50
  {"role": "user", "content": text},
51
  ]
52
+ pipe = pipeline("text-generation", model=proofread_model)
53
  llm_output = pipe(messages)
54
 
55
  # Extract the generated text
 
61
  proofread_text = assistant_content
62
  return proofread_text
63
 
64
+ def load_models(transcribe_model_id, proofread_model_id):
65
+ global transcribe_model, proofread_model
66
+ device = "cuda:0" if torch.cuda.is_available() else "mps" if torch.mps.is_available() else "cpu"
67
+ torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
68
+
69
+ transcribe_model = AutoModelForSpeechSeq2Seq.from_pretrained(
70
+ transcribe_model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
71
+ )
72
+ transcribe_model.to(device)
73
+
74
+ proofread_model = AutoModelForCausalLM.from_pretrained(proofread_model_id)
75
+ proofread_model.to(device)
76
+
77
  with gr.Blocks() as demo:
78
  gr.Markdown("""
79
  # Audio Transcription and Proofreading
80
+ 1. Select models for transcription and proofreading
81
+ 2. Upload an audio file (Wait for the file to be fully loaded first)
82
+ 3. Transcribe the audio
83
+ 4. Proofread the transcribed text
84
  """)
85
 
86
  with gr.Row():
 
87
  transcribe_model_dropdown = gr.Dropdown(choices=["openai/whisper-large-v3", "alvanlii/whisper-small-cantonese"], value="alvanlii/whisper-small-cantonese", label="Select Transcription Model")
88
  proofread_model_dropdown = gr.Dropdown(choices=["hfl/llama-3-chinese-8b-instruct-v3"], value="hfl/llama-3-chinese-8b-instruct-v3", label="Select Proofreading Model")
89
+ load_button = gr.Button("Load Models")
90
 
91
+ audio = gr.Audio(sources="upload", type="filepath")
92
+
93
  transcribe_button = gr.Button("Transcribe")
94
  transcribed_text = gr.Textbox(label="Transcribed Text")
95
 
96
  proofread_button = gr.Button("Proofread")
97
  proofread_output = gr.Textbox(label="Proofread Text")
98
 
99
+ load_button.click(load_models, inputs=[transcribe_model_dropdown, proofread_model_dropdown])
100
+ transcribe_button.click(transcribe_audio, inputs=audio, outputs=transcribed_text)
101
+ proofread_button.click(proofread, inputs=transcribed_text, outputs=proofread_output)
102
+ transcribed_text.change(proofread, inputs=transcribed_text, outputs=proofread_output)
103
 
104
  demo.launch()