Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,150 +1,106 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
|
| 3 |
import gradio as gr
|
| 4 |
-
import yt_dlp as youtube_dl
|
| 5 |
from transformers import pipeline
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
import tempfile
|
| 9 |
-
import os
|
| 10 |
-
|
| 11 |
-
MODEL_NAME = "chiyo123/whisper-small-tonga"
|
| 12 |
-
BATCH_SIZE = 8
|
| 13 |
-
FILE_LIMIT_MB = 1000
|
| 14 |
-
YT_LENGTH_LIMIT_S = 3600 # limit to 1 hour YouTube files
|
| 15 |
-
|
| 16 |
-
device = 0 if torch.cuda.is_available() else "cpu"
|
| 17 |
-
|
| 18 |
-
pipe = pipeline(
|
| 19 |
-
task="automatic-speech-recognition",
|
| 20 |
-
model=MODEL_NAME,
|
| 21 |
-
chunk_length_s=30,
|
| 22 |
-
device=device,
|
| 23 |
-
)
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
def transcribe(inputs, task):
|
| 27 |
-
if inputs is None:
|
| 28 |
-
raise gr.Error("No audio file submitted! Please upload or record an audio file before submitting your request.")
|
| 29 |
-
|
| 30 |
-
text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
|
| 31 |
-
return text
|
| 32 |
-
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
| 39 |
)
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
except youtube_dl.utils.DownloadError as err:
|
| 48 |
-
raise gr.Error(str(err))
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
if
|
| 55 |
-
|
| 56 |
-
if len(file_h_m_s) == 2:
|
| 57 |
-
file_h_m_s.insert(0, 0)
|
| 58 |
-
file_length_s = file_h_m_s[0] * 3600 + file_h_m_s[1] * 60 + file_h_m_s[2]
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
fn=transcribe,
|
| 95 |
-
inputs=[
|
| 96 |
-
gr.inputs.Audio(source="microphone", type="filepath", optional=True),
|
| 97 |
-
gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe"),
|
| 98 |
-
],
|
| 99 |
-
outputs="text",
|
| 100 |
-
layout="horizontal",
|
| 101 |
-
theme="huggingface",
|
| 102 |
-
title="chiyo123/whisper-small-tonga",
|
| 103 |
-
description=(
|
| 104 |
-
"Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the OpenAI Whisper"
|
| 105 |
-
f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files"
|
| 106 |
-
" of arbitrary length."
|
| 107 |
-
),
|
| 108 |
-
allow_flagging="never",
|
| 109 |
-
)
|
| 110 |
-
|
| 111 |
-
file_transcribe = gr.Interface(
|
| 112 |
-
fn=transcribe,
|
| 113 |
-
inputs=[
|
| 114 |
-
gr.inputs.Audio(source="upload", type="filepath", optional=True, label="Audio file"),
|
| 115 |
-
gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe"),
|
| 116 |
-
],
|
| 117 |
-
outputs="text",
|
| 118 |
-
layout="horizontal",
|
| 119 |
-
theme="huggingface",
|
| 120 |
-
title="Whisper Large V3: Transcribe Audio",
|
| 121 |
-
description=(
|
| 122 |
-
"Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the OpenAI Whisper"
|
| 123 |
-
f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files"
|
| 124 |
-
" of arbitrary length."
|
| 125 |
-
),
|
| 126 |
-
allow_flagging="never",
|
| 127 |
-
)
|
| 128 |
-
|
| 129 |
-
yt_transcribe = gr.Interface(
|
| 130 |
-
fn=yt_transcribe,
|
| 131 |
-
inputs=[
|
| 132 |
-
gr.inputs.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL"),
|
| 133 |
-
gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe")
|
| 134 |
-
],
|
| 135 |
-
outputs=["html", "text"],
|
| 136 |
-
layout="horizontal",
|
| 137 |
-
theme="huggingface",
|
| 138 |
-
title="Whisper Large V3: Transcribe YouTube",
|
| 139 |
-
description=(
|
| 140 |
-
"Transcribe long-form YouTube videos with the click of a button! Demo uses the OpenAI Whisper checkpoint"
|
| 141 |
-
f" [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe video files of"
|
| 142 |
-
" arbitrary length."
|
| 143 |
-
),
|
| 144 |
-
allow_flagging="never",
|
| 145 |
-
)
|
| 146 |
-
|
| 147 |
-
with demo:
|
| 148 |
-
gr.TabbedInterface([mf_transcribe, file_transcribe, yt_transcribe], ["Microphone", "Audio file", "YouTube"])
|
| 149 |
|
| 150 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
+
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# Initialize the speech recognition pipeline
|
| 6 |
+
print("Loading Whisper Lozi model...")
|
| 7 |
+
try:
|
| 8 |
+
# Use the specific Lozi model
|
| 9 |
+
transcriber = pipeline(
|
| 10 |
+
"automatic-speech-recognition",
|
| 11 |
+
model="simzacademy/whisper-small-lozi1",
|
| 12 |
+
device=0 if torch.cuda.is_available() else -1 # Use GPU if available
|
| 13 |
)
|
| 14 |
+
print("Model loaded successfully!")
|
| 15 |
+
except Exception as e:
|
| 16 |
+
print(f"Error loading model: {e}")
|
| 17 |
+
transcriber = None
|
| 18 |
+
|
| 19 |
+
def transcribe_audio(audio):
|
| 20 |
+
"""
|
| 21 |
+
Transcribe audio to text using the Whisper Lozi model
|
| 22 |
|
| 23 |
+
Args:
|
| 24 |
+
audio: Audio file path or tuple (sample_rate, audio_data)
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
Returns:
|
| 27 |
+
Transcribed text
|
| 28 |
+
"""
|
| 29 |
+
if transcriber is None:
|
| 30 |
+
return "Error: Model failed to load. Please check your installation."
|
| 31 |
|
| 32 |
+
if audio is None:
|
| 33 |
+
return "Please provide an audio file or recording."
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
try:
|
| 36 |
+
# Transcribe the audio
|
| 37 |
+
result = transcriber(audio)
|
| 38 |
+
return result["text"]
|
| 39 |
+
except Exception as e:
|
| 40 |
+
return f"Error during transcription: {str(e)}"
|
| 41 |
+
|
| 42 |
+
# Create the Gradio interface
|
| 43 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 44 |
+
gr.Markdown(
|
| 45 |
+
"""
|
| 46 |
+
# 🎤 Lozi Speech-to-Text Interface
|
| 47 |
+
### Powered by Whisper Small Lozi Model
|
| 48 |
+
|
| 49 |
+
This interface uses the `simzacademy/whisper-small-lozi1` model to transcribe
|
| 50 |
+
Lozi language speech to text.
|
| 51 |
+
"""
|
| 52 |
+
)
|
| 53 |
|
| 54 |
+
with gr.Row():
|
| 55 |
+
with gr.Column():
|
| 56 |
+
# Audio input - supports both recording and file upload
|
| 57 |
+
audio_input = gr.Audio(
|
| 58 |
+
sources=["microphone", "upload"],
|
| 59 |
+
type="filepath",
|
| 60 |
+
label="Record or Upload Audio"
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
transcribe_btn = gr.Button("🔄 Transcribe", variant="primary", size="lg")
|
| 64 |
+
|
| 65 |
+
with gr.Column():
|
| 66 |
+
output_text = gr.Textbox(
|
| 67 |
+
label="Transcription",
|
| 68 |
+
placeholder="Your transcription will appear here...",
|
| 69 |
+
lines=10
|
| 70 |
+
)
|
| 71 |
|
| 72 |
+
gr.Markdown(
|
| 73 |
+
"""
|
| 74 |
+
### 📋 Instructions:
|
| 75 |
+
1. **Record**: Click the microphone icon to record audio directly
|
| 76 |
+
2. **Upload**: Or click to upload an audio file (MP3, WAV, etc.)
|
| 77 |
+
3. **Transcribe**: Click the "Transcribe" button to convert speech to text
|
| 78 |
+
4. **View**: The transcribed text will appear on the right
|
| 79 |
+
|
| 80 |
+
### ℹ️ Notes:
|
| 81 |
+
- Speak clearly in Lozi for best results
|
| 82 |
+
- The model works best with clear audio and minimal background noise
|
| 83 |
+
- First transcription may take longer as the model loads
|
| 84 |
+
"""
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
# Set up the transcription action
|
| 88 |
+
transcribe_btn.click(
|
| 89 |
+
fn=transcribe_audio,
|
| 90 |
+
inputs=audio_input,
|
| 91 |
+
outputs=output_text
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
# Also allow Enter key to trigger transcription
|
| 95 |
+
audio_input.change(
|
| 96 |
+
fn=lambda: gr.update(interactive=True),
|
| 97 |
+
outputs=transcribe_btn
|
| 98 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
+
# Launch the interface
|
| 101 |
+
if __name__ == "__main__":
|
| 102 |
+
demo.launch(
|
| 103 |
+
share=False, # Set to True to create a public link
|
| 104 |
+
server_name="0.0.0.0", # Allow access from network
|
| 105 |
+
server_port=7860
|
| 106 |
+
)
|