Spaces:
Paused
Paused
Add app & requirements.txt
Browse files- app.py +78 -0
- requirements.txt +1 -0
app.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
"kostissz/whisper-small-el",
|
| 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 load_model(model_id: str, language: str) -> Tuple[Pipeline, str]:
|
| 19 |
+
if model_id and language:
|
| 20 |
+
yield None, f"Loading {model_id}..."
|
| 21 |
+
pipe = pipeline(
|
| 22 |
+
"automatic-speech-recognition",
|
| 23 |
+
model=model_id,
|
| 24 |
+
generate_kwargs={"language": language},
|
| 25 |
+
)
|
| 26 |
+
yield pipe, f"✅ Model {model_id} has been loaded."
|
| 27 |
+
else:
|
| 28 |
+
yield None, "⚠️ Please select a model and a language from the dropdown"
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def transcribe(pipe: Pipeline, audio: gr.Audio) -> str:
|
| 32 |
+
text = pipe(audio)["text"]
|
| 33 |
+
return text
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def setup_gradio_demo():
|
| 37 |
+
with gr.Blocks() as demo:
|
| 38 |
+
gr.Markdown(
|
| 39 |
+
""" # 🗣️ Speech-to-Text Transcription
|
| 40 |
+
### 1. Select a model and a language from the dropdowns.
|
| 41 |
+
### 2. Load the model by clicking the Load model button.
|
| 42 |
+
### 3. Record a message and click Transcribe to see the transcription.
|
| 43 |
+
"""
|
| 44 |
+
)
|
| 45 |
+
### Model & Language selection ###
|
| 46 |
+
dropdown_model = gr.Dropdown(
|
| 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 |
+
|
| 55 |
+
### Transcription ###
|
| 56 |
+
audio_input = gr.Audio(
|
| 57 |
+
sources=["microphone"], type="filepath", label="Record a message"
|
| 58 |
+
)
|
| 59 |
+
transcribe_button = gr.Button("Transcribe")
|
| 60 |
+
transcribe_output = gr.Text(label="Output")
|
| 61 |
+
|
| 62 |
+
### Event listeners ###
|
| 63 |
+
model = gr.State()
|
| 64 |
+
load_model_button.click(
|
| 65 |
+
fn=load_model,
|
| 66 |
+
inputs=[dropdown_model, selected_lang],
|
| 67 |
+
outputs=[model, model_loaded],
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
transcribe_button.click(
|
| 71 |
+
fn=transcribe, inputs=[model, audio_input], outputs=transcribe_output
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
demo.launch()
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
if __name__ == "__main__":
|
| 78 |
+
setup_gradio_demo()
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
git+https://github.com/mozilla-ai/speech-to-text-finetune.git
|