Michael Natanael commited on
Commit
451dc19
·
1 Parent(s): 971594f

change transcribe mechanism when uploading audio

Browse files
Files changed (1) hide show
  1. app.py +36 -5
app.py CHANGED
@@ -1,5 +1,5 @@
1
  from flask import Flask, render_template, request
2
- import whisper
3
  import tempfile
4
  import os
5
  import time
@@ -7,13 +7,13 @@ import torch
7
  import numpy as np
8
  import requests
9
  from tqdm import tqdm
10
- from transformers import BertTokenizer
11
  from model.multi_class_model import MultiClassModel # Adjust if needed
12
- import lightning as L
13
 
14
  app = Flask(__name__)
15
 
16
  # === CONFIG ===
 
17
  CHECKPOINT_URL = "https://huggingface.co/nenafem/original_split_synthesized/resolve/main/original_split_synthesized.ckpt?download=true"
18
  CHECKPOINT_PATH = "final_checkpoint/original_split_synthesized.ckpt"
19
  AGE_LABELS = ["semua usia", "anak", "remaja", "dewasa"]
@@ -50,6 +50,36 @@ model = MultiClassModel.load_from_checkpoint(
50
  model.eval()
51
 
52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  # === ROUTES ===
54
 
55
  @app.route('/', methods=['GET'])
@@ -62,7 +92,7 @@ def transcribe():
62
  try:
63
  # Load Whisper with Indonesian language support (large / turbo)
64
  # https://github.com/openai/whisper
65
- whisper_model = whisper.load_model("large")
66
 
67
  # Start measuring time
68
  start_time = time.time()
@@ -75,7 +105,8 @@ def transcribe():
75
  temp_audio_path = temp_audio.name
76
 
77
  # Step 1: Transcribe
78
- transcription = whisper_model.transcribe(temp_audio_path, language="id")
 
79
  os.remove(temp_audio_path)
80
  transcribed_text = transcription["text"]
81
 
 
1
  from flask import Flask, render_template, request
2
+ # import whisper
3
  import tempfile
4
  import os
5
  import time
 
7
  import numpy as np
8
  import requests
9
  from tqdm import tqdm
10
+ from transformers import BertTokenizer, AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
11
  from model.multi_class_model import MultiClassModel # Adjust if needed
 
12
 
13
  app = Flask(__name__)
14
 
15
  # === CONFIG ===
16
+ # CHECKPOINT_URL = "https://github.com/michael2002porto/bert_classification_indonesian_song_lyrics/releases/download/finetuned_checkpoints/original_split_synthesized.ckpt"
17
  CHECKPOINT_URL = "https://huggingface.co/nenafem/original_split_synthesized/resolve/main/original_split_synthesized.ckpt?download=true"
18
  CHECKPOINT_PATH = "final_checkpoint/original_split_synthesized.ckpt"
19
  AGE_LABELS = ["semua usia", "anak", "remaja", "dewasa"]
 
50
  model.eval()
51
 
52
 
53
+ def whisper_api(temp_audio_path):
54
+ # https://huggingface.co/openai/whisper-large-v3
55
+ device = "cuda:0" if torch.cuda.is_available() else "cpu"
56
+ torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
57
+
58
+ model_id = "openai/whisper-large-v3"
59
+
60
+ model = AutoModelForSpeechSeq2Seq.from_pretrained(
61
+ model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
62
+ )
63
+ model.to(device)
64
+
65
+ processor = AutoProcessor.from_pretrained(model_id)
66
+
67
+ pipe = pipeline(
68
+ "automatic-speech-recognition",
69
+ model=model,
70
+ tokenizer=processor.tokenizer,
71
+ feature_extractor=processor.feature_extractor,
72
+ chunk_length_s=10,
73
+ batch_size=4, # batch size for inference - set based on your device
74
+ torch_dtype=torch_dtype,
75
+ device=device,
76
+ )
77
+
78
+ result = pipe(temp_audio_path, return_timestamps=False, generate_kwargs={"language": "indonesian"})
79
+ print(result["text"])
80
+ return result
81
+
82
+
83
  # === ROUTES ===
84
 
85
  @app.route('/', methods=['GET'])
 
92
  try:
93
  # Load Whisper with Indonesian language support (large / turbo)
94
  # https://github.com/openai/whisper
95
+ # whisper_model = whisper.load_model("large")
96
 
97
  # Start measuring time
98
  start_time = time.time()
 
105
  temp_audio_path = temp_audio.name
106
 
107
  # Step 1: Transcribe
108
+ # transcription = whisper_model.transcribe(temp_audio_path, language="id")
109
+ transcription = whisper_api(temp_audio_path)
110
  os.remove(temp_audio_path)
111
  transcribed_text = transcription["text"]
112