Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,34 +5,30 @@ import soundfile as sf
|
|
| 5 |
import numpy as np
|
| 6 |
from scipy import signal
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
print(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
print("Loading Whisper model for Macedonian transcription...")
|
| 14 |
-
processor = WhisperProcessor.from_pretrained("openai/whisper-large-v3")
|
| 15 |
-
model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-large-v3").to(device)
|
| 16 |
-
print("✓ Model loaded successfully!")
|
| 17 |
|
| 18 |
def process_audio(audio_path):
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
|
| 33 |
-
return transcription
|
| 34 |
-
except Exception as e:
|
| 35 |
-
return f"Error during transcription: {str(e)}"
|
| 36 |
|
| 37 |
# Gradio interface
|
| 38 |
demo = gr.Interface(
|
|
@@ -44,4 +40,4 @@ demo = gr.Interface(
|
|
| 44 |
)
|
| 45 |
|
| 46 |
if __name__ == "__main__":
|
| 47 |
-
demo.launch(
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
from scipy import signal
|
| 7 |
|
| 8 |
+
# Load the Whisper model and processor directly from Hugging Face
|
| 9 |
+
def load_model():
|
| 10 |
+
print("Loading Whisper model and processor...")
|
| 11 |
+
processor = WhisperProcessor.from_pretrained("openai/whisper-large-v3")
|
| 12 |
+
model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-large-v3")
|
| 13 |
+
print("✓ Model and processor loaded successfully!")
|
| 14 |
+
return processor, model
|
| 15 |
|
| 16 |
+
processor, model = load_model()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
def process_audio(audio_path):
|
| 19 |
+
# Load and resample to 16kHz using scipy
|
| 20 |
+
waveform, sr = sf.read(audio_path)
|
| 21 |
+
if len(waveform.shape) > 1: # Convert stereo to mono
|
| 22 |
+
waveform = waveform.mean(axis=1)
|
| 23 |
+
if sr != 16000: # Resample if necessary
|
| 24 |
+
num_samples = int(len(waveform) * 16000 / sr)
|
| 25 |
+
waveform = signal.resample(waveform, num_samples)
|
| 26 |
+
|
| 27 |
+
# Process the audio
|
| 28 |
+
inputs = processor(waveform, sampling_rate=16000, return_tensors="pt")
|
| 29 |
+
predicted_ids = model.generate(**inputs, language="mk")
|
| 30 |
+
transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
|
| 31 |
+
return transcription
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
# Gradio interface
|
| 34 |
demo = gr.Interface(
|
|
|
|
| 40 |
)
|
| 41 |
|
| 42 |
if __name__ == "__main__":
|
| 43 |
+
demo.launch()
|