Spaces:
Runtime error
Runtime error
Update to latest
Browse files
app.py
CHANGED
|
@@ -1,31 +1,81 @@
|
|
|
|
|
| 1 |
from typing import Tuple
|
| 2 |
import gradio as gr
|
| 3 |
from transformers import pipeline, Pipeline
|
|
|
|
|
|
|
| 4 |
|
| 5 |
from speech_to_text_finetune.config import LANGUAGES_NAME_TO_ID
|
| 6 |
|
| 7 |
languages = LANGUAGES_NAME_TO_ID.keys()
|
| 8 |
model_ids = [
|
| 9 |
-
"
|
| 10 |
-
"kostissz/whisper-tiny-gl",
|
| 11 |
-
"kostissz/whisper-tiny-el",
|
| 12 |
"openai/whisper-tiny",
|
| 13 |
"openai/whisper-small",
|
| 14 |
"openai/whisper-medium",
|
|
|
|
|
|
|
| 15 |
]
|
| 16 |
|
| 17 |
|
| 18 |
-
def
|
| 19 |
-
if
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
)
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
else:
|
| 28 |
-
yield
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
|
| 31 |
def transcribe(pipe: Pipeline, audio: gr.Audio) -> str:
|
|
@@ -37,18 +87,34 @@ def setup_gradio_demo():
|
|
| 37 |
with gr.Blocks() as demo:
|
| 38 |
gr.Markdown(
|
| 39 |
""" # 🗣️ Speech-to-Text Transcription
|
| 40 |
-
### 1. Select a
|
| 41 |
-
### 2.
|
| 42 |
-
### 3.
|
|
|
|
| 43 |
"""
|
| 44 |
)
|
| 45 |
-
###
|
| 46 |
-
|
| 47 |
-
choices=model_ids, value=None, label="Select a model"
|
| 48 |
-
)
|
| 49 |
selected_lang = gr.Dropdown(
|
| 50 |
choices=list(languages), value=None, label="Select a language"
|
| 51 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
load_model_button = gr.Button("Load model")
|
| 53 |
model_loaded = gr.Markdown()
|
| 54 |
|
|
@@ -63,7 +129,7 @@ def setup_gradio_demo():
|
|
| 63 |
model = gr.State()
|
| 64 |
load_model_button.click(
|
| 65 |
fn=load_model,
|
| 66 |
-
inputs=[dropdown_model,
|
| 67 |
outputs=[model, model_loaded],
|
| 68 |
)
|
| 69 |
|
|
|
|
| 1 |
+
from pathlib import Path
|
| 2 |
from typing import Tuple
|
| 3 |
import gradio as gr
|
| 4 |
from transformers import pipeline, Pipeline
|
| 5 |
+
from huggingface_hub import repo_exists
|
| 6 |
+
|
| 7 |
|
| 8 |
from speech_to_text_finetune.config import LANGUAGES_NAME_TO_ID
|
| 9 |
|
| 10 |
languages = LANGUAGES_NAME_TO_ID.keys()
|
| 11 |
model_ids = [
|
| 12 |
+
"",
|
|
|
|
|
|
|
| 13 |
"openai/whisper-tiny",
|
| 14 |
"openai/whisper-small",
|
| 15 |
"openai/whisper-medium",
|
| 16 |
+
"openai/whisper-large-v3",
|
| 17 |
+
"openai/whisper-large-v3-turbo",
|
| 18 |
]
|
| 19 |
|
| 20 |
|
| 21 |
+
def _load_local_model(model_dir: str, language: str) -> Tuple[Pipeline | None, str]:
|
| 22 |
+
if not Path(model_dir).is_dir():
|
| 23 |
+
return None, f"⚠️ Couldn't find local model directory: {model_dir}"
|
| 24 |
+
from transformers import (
|
| 25 |
+
WhisperProcessor,
|
| 26 |
+
WhisperTokenizer,
|
| 27 |
+
WhisperFeatureExtractor,
|
| 28 |
+
WhisperForConditionalGeneration,
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
processor = WhisperProcessor.from_pretrained(model_dir)
|
| 32 |
+
tokenizer = WhisperTokenizer.from_pretrained(
|
| 33 |
+
model_dir, language=language, task="transcribe"
|
| 34 |
+
)
|
| 35 |
+
feature_extractor = WhisperFeatureExtractor.from_pretrained(model_dir)
|
| 36 |
+
model = WhisperForConditionalGeneration.from_pretrained(model_dir)
|
| 37 |
+
|
| 38 |
+
return pipeline(
|
| 39 |
+
task="automatic-speech-recognition",
|
| 40 |
+
model=model,
|
| 41 |
+
processor=processor,
|
| 42 |
+
tokenizer=tokenizer,
|
| 43 |
+
feature_extractor=feature_extractor,
|
| 44 |
+
), f"✅ Local model has been loaded from {model_dir}."
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def _load_hf_model(model_repo_id: str, language: str) -> Tuple[Pipeline | None, str]:
|
| 48 |
+
if not repo_exists(model_repo_id):
|
| 49 |
+
return (
|
| 50 |
+
None,
|
| 51 |
+
f"⚠️ Couldn't find {model_repo_id} on Hugging Face. If its a private repo, make sure you are logged in locally.",
|
| 52 |
)
|
| 53 |
+
return pipeline(
|
| 54 |
+
"automatic-speech-recognition",
|
| 55 |
+
model=model_repo_id,
|
| 56 |
+
generate_kwargs={"language": language},
|
| 57 |
+
), f"✅ HF Model {model_repo_id} has been loaded."
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def load_model(
|
| 61 |
+
language: str, dropdown_model_id: str, hf_model_id: str, local_model_id: str
|
| 62 |
+
) -> Tuple[Pipeline, str]:
|
| 63 |
+
if dropdown_model_id and not hf_model_id and not local_model_id:
|
| 64 |
+
yield None, f"Loading {dropdown_model_id}..."
|
| 65 |
+
yield _load_hf_model(dropdown_model_id, language)
|
| 66 |
+
elif hf_model_id and not local_model_id and not dropdown_model_id:
|
| 67 |
+
yield None, f"Loading {hf_model_id}..."
|
| 68 |
+
yield _load_hf_model(hf_model_id, language)
|
| 69 |
+
elif local_model_id and not hf_model_id and not dropdown_model_id:
|
| 70 |
+
yield None, f"Loading {local_model_id}..."
|
| 71 |
+
yield _load_local_model(local_model_id, language)
|
| 72 |
else:
|
| 73 |
+
yield (
|
| 74 |
+
None,
|
| 75 |
+
"️️⚠️ Please select or fill at least and only one of the three options above",
|
| 76 |
+
)
|
| 77 |
+
if not language:
|
| 78 |
+
yield None, "⚠️ Please select a language from the dropdown"
|
| 79 |
|
| 80 |
|
| 81 |
def transcribe(pipe: Pipeline, audio: gr.Audio) -> str:
|
|
|
|
| 87 |
with gr.Blocks() as demo:
|
| 88 |
gr.Markdown(
|
| 89 |
""" # 🗣️ Speech-to-Text Transcription
|
| 90 |
+
### 1. Select a language from the dropdown menu.
|
| 91 |
+
### 2. Select which model to load from one of the 3 options
|
| 92 |
+
### 3. Load the model by clicking the Load model button.
|
| 93 |
+
### 4. Record a message and click Transcribe to see the transcription.
|
| 94 |
"""
|
| 95 |
)
|
| 96 |
+
### Language & Model selection ###
|
| 97 |
+
|
|
|
|
|
|
|
| 98 |
selected_lang = gr.Dropdown(
|
| 99 |
choices=list(languages), value=None, label="Select a language"
|
| 100 |
)
|
| 101 |
+
|
| 102 |
+
with gr.Row():
|
| 103 |
+
with gr.Column():
|
| 104 |
+
dropdown_model = gr.Dropdown(
|
| 105 |
+
choices=model_ids, label="Option 1: Select a model"
|
| 106 |
+
)
|
| 107 |
+
with gr.Column():
|
| 108 |
+
user_model = gr.Textbox(
|
| 109 |
+
label="Option 2: Paste HF model id",
|
| 110 |
+
placeholder="my-username/my-whisper-tiny",
|
| 111 |
+
)
|
| 112 |
+
with gr.Column():
|
| 113 |
+
local_model = gr.Textbox(
|
| 114 |
+
label="Option 3: Paste local path to model directory",
|
| 115 |
+
placeholder="artifacts/my-whisper-tiny",
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
load_model_button = gr.Button("Load model")
|
| 119 |
model_loaded = gr.Markdown()
|
| 120 |
|
|
|
|
| 129 |
model = gr.State()
|
| 130 |
load_model_button.click(
|
| 131 |
fn=load_model,
|
| 132 |
+
inputs=[selected_lang, dropdown_model, user_model, local_model],
|
| 133 |
outputs=[model, model_loaded],
|
| 134 |
)
|
| 135 |
|