Spaces:
Sleeping
Delete app.py
Browse filesfrom transformers import pipeline
import gradio as gr
import librosa
import numpy as np
from transformers import WhisperProcessor, WhisperForConditionalGeneration, pipeline
processor = WhisperProcessor.from_pretrained("https://huggingface.co/spaces/akadriu/shqip_whisper")
model = WhisperForConditionalGeneration.from_pretrained("https://huggingface.co/spaces/akadriu/shqip_whisper")
def transcribe(audio):
audio_input, _ = librosa.load(audio, sr=16000)
input_features = processor(audio_input, sampling_rate=16000, return_tensors="pt").input_features
predicted_ids = model.generate(input_features)
transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)
text = transcription
return text
iface = gr.Interface(
fn=transcribe,
inputs=gr.Audio(source="microphone", type="filepath"),
outputs="text",
title="Whisper Medium Shqip",
description="Realtime demo for Sq speech recognition using a fine-tuned Whisper medium model.",
)
iface.launch()
@@ -1,45 +0,0 @@
|
|
1 |
-
from transformers import pipeline
|
2 |
-
import gradio as gr
|
3 |
-
import librosa
|
4 |
-
import numpy as np
|
5 |
-
import os
|
6 |
-
|
7 |
-
from transformers import WhisperProcessor, WhisperForConditionalGeneration
|
8 |
-
|
9 |
-
hf_token = os.getenv("HUGGINGFACE_HUB_TOKEN")
|
10 |
-
processor = WhisperProcessor.from_pretrained("akadriu/whisper-medium-sq", token=hf_token)
|
11 |
-
model = WhisperForConditionalGeneration.from_pretrained("akadriu/whisper-medium-sq", token=hf_token)
|
12 |
-
|
13 |
-
def transcribe(audio):
|
14 |
-
# If audio is a tuple, extract the NumPy array and the sampling rate
|
15 |
-
if isinstance(audio, tuple):
|
16 |
-
audio_input, sr = audio
|
17 |
-
# Save the audio back to a file for playback
|
18 |
-
audio_file = "temp.wav"
|
19 |
-
librosa.output.write_wav(audio_file, audio_input, sr)
|
20 |
-
else:
|
21 |
-
# Otherwise, load the file from the path
|
22 |
-
audio_file = audio
|
23 |
-
audio_input, sr = librosa.load(audio_file, sr=16000)
|
24 |
-
|
25 |
-
# Ensure the sample rate is what the processor expects
|
26 |
-
if sr != 16000:
|
27 |
-
audio_input = librosa.resample(audio_input, orig_sr=sr, target_sr=16000)
|
28 |
-
|
29 |
-
# Process the audio and generate the transcription
|
30 |
-
input_features = processor(audio_input, sampling_rate=16000, return_tensors="pt").input_features
|
31 |
-
predicted_ids = model.generate(input_features)
|
32 |
-
transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)
|
33 |
-
text = transcription[0] # Decode returns a list
|
34 |
-
|
35 |
-
return audio_file, text
|
36 |
-
|
37 |
-
iface = gr.Interface(
|
38 |
-
fn=transcribe,
|
39 |
-
inputs=gr.Audio(), # Audio input
|
40 |
-
outputs=[gr.Audio(), "text"], # Return the audio file and transcription text
|
41 |
-
title="Whisper Medium Shqip",
|
42 |
-
description="Realtime demo for Sq speech recognition using a fine-tuned Whisper medium model.",
|
43 |
-
)
|
44 |
-
|
45 |
-
iface.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|