Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,132 +1,75 @@
|
|
| 1 |
-
import torch
|
| 2 |
import gradio as gr
|
| 3 |
-
import yt_dlp as youtube_dl
|
| 4 |
-
from transformers import WhisperProcessor, WhisperForConditionalGeneration
|
| 5 |
-
import tempfile
|
| 6 |
import os
|
| 7 |
-
import
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
|
| 22 |
-
def
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
| 26 |
|
| 27 |
-
# Check file
|
| 28 |
-
|
| 29 |
-
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
audio_input = processor(inputs, return_tensors="pt", sampling_rate=16000).to(device)
|
| 33 |
-
|
| 34 |
-
# Generate transcription
|
| 35 |
-
predicted_ids = model.generate(audio_input.input_values, max_length=448)
|
| 36 |
-
|
| 37 |
-
# Decode the transcription output
|
| 38 |
-
transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
|
| 39 |
-
return transcription
|
| 40 |
-
|
| 41 |
-
def _return_yt_html_embed(yt_url):
|
| 42 |
-
"""Return YouTube embed HTML for display."""
|
| 43 |
-
video_id = yt_url.split("?v=")[-1]
|
| 44 |
-
html_embed = f'<center><iframe width="500" height="320" src="https://www.youtube.com/embed/{video_id}"></iframe></center>'
|
| 45 |
-
return html_embed
|
| 46 |
|
| 47 |
-
def
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
| 50 |
|
| 51 |
try:
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
yt_length_limit_hms = time.strftime("%HH:%MM:%SS", time.gmtime(YT_LENGTH_LIMIT_S))
|
| 61 |
-
file_length_hms = time.strftime("%HH:%MM:%SS", time.gmtime(file_length_s))
|
| 62 |
-
raise gr.Error(f"Maximum YouTube video length is {yt_length_limit_hms}, but video is {file_length_hms}.")
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
ydl.download([yt_url])
|
| 69 |
-
except youtube_dl.utils.ExtractorError as err:
|
| 70 |
-
raise gr.Error(f"Error while downloading video: {str(err)}")
|
| 71 |
-
|
| 72 |
-
def yt_transcribe(yt_url):
|
| 73 |
-
"""Transcribe YouTube video using Whisper model."""
|
| 74 |
-
html_embed = _return_yt_html_embed(yt_url)
|
| 75 |
-
|
| 76 |
-
with tempfile.TemporaryDirectory() as tmpdirname:
|
| 77 |
-
filepath = os.path.join(tmpdirname, "video.mp4")
|
| 78 |
-
download_yt_audio(yt_url, filepath)
|
| 79 |
-
|
| 80 |
-
with open(filepath, "rb") as file:
|
| 81 |
-
audio_input = file.read()
|
| 82 |
-
|
| 83 |
-
# Process and transcribe
|
| 84 |
-
transcription = transcribe_audio(audio_input)
|
| 85 |
-
return html_embed, transcription
|
| 86 |
-
|
| 87 |
-
# Create Gradio interface
|
| 88 |
-
demo = gr.Blocks()
|
| 89 |
-
|
| 90 |
-
# Microphone transcription interface
|
| 91 |
-
mf_transcribe = gr.Interface(
|
| 92 |
-
fn=transcribe_audio,
|
| 93 |
-
inputs=[
|
| 94 |
-
gr.inputs.Audio(source="microphone", type="filepath", optional=True),
|
| 95 |
-
],
|
| 96 |
-
outputs="text",
|
| 97 |
-
layout="horizontal",
|
| 98 |
-
theme="huggingface",
|
| 99 |
-
title="Whisper Transcription (Microphone)",
|
| 100 |
-
description="Transcribe audio from your microphone. File size limit is 25MB."
|
| 101 |
-
)
|
| 102 |
-
|
| 103 |
-
# File upload transcription interface
|
| 104 |
-
file_transcribe = gr.Interface(
|
| 105 |
-
fn=transcribe_audio,
|
| 106 |
-
inputs=[
|
| 107 |
-
gr.inputs.Audio(source="upload", type="filepath", optional=True, label="Audio file"),
|
| 108 |
-
],
|
| 109 |
-
outputs="text",
|
| 110 |
-
layout="horizontal",
|
| 111 |
-
theme="huggingface",
|
| 112 |
-
title="Whisper Transcription (File)",
|
| 113 |
-
description="Upload an audio file to transcribe. File size limit is 25MB."
|
| 114 |
-
)
|
| 115 |
-
|
| 116 |
-
# YouTube video transcription interface
|
| 117 |
-
yt_transcribe = gr.Interface(
|
| 118 |
-
fn=yt_transcribe,
|
| 119 |
-
inputs=[
|
| 120 |
-
gr.inputs.Textbox(lines=1, placeholder="Paste YouTube URL", label="YouTube URL"),
|
| 121 |
-
],
|
| 122 |
-
outputs=["html", "text"],
|
| 123 |
-
layout="horizontal",
|
| 124 |
-
theme="huggingface",
|
| 125 |
-
title="Free Transcript Maker",
|
| 126 |
-
description="Upload an audio file (WAV, MP3, etc.) up to 25MB to get its transcription. The transcript will be displayed and available for download. Please use responsibly."
|
| 127 |
-
)
|
| 128 |
-
|
| 129 |
-
with demo:
|
| 130 |
-
gr.TabbedInterface([mf_transcribe, file_transcribe, yt_transcribe], ["Microphone", "Audio file", "YouTube"])
|
| 131 |
|
| 132 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
import os
|
| 3 |
+
from transformers import WhisperProcessor, WhisperForConditionalGeneration
|
| 4 |
+
import numpy as np
|
| 5 |
+
import librosa
|
| 6 |
+
|
| 7 |
+
# Initialize Whisper model
|
| 8 |
+
processor = WhisperProcessor.from_pretrained("openai/whisper-base")
|
| 9 |
+
model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-base")
|
| 10 |
+
|
| 11 |
+
# Set light green theme
|
| 12 |
+
theme = gr.themes.Base(
|
| 13 |
+
primary_hue="emerald",
|
| 14 |
+
secondary_hue="emerald",
|
| 15 |
+
neutral_hue="gray",
|
| 16 |
+
)
|
| 17 |
|
| 18 |
+
def validate_file(file):
|
| 19 |
+
# Check file size (25 MB limit)
|
| 20 |
+
file_size_mb = os.path.getsize(file) / (1024 * 1024)
|
| 21 |
+
if file_size_mb > 25:
|
| 22 |
+
return False, f"File size is {file_size_mb:.2f} MB. Please upload a file smaller than 25 MB."
|
| 23 |
|
| 24 |
+
# Check file extension
|
| 25 |
+
file_extension = os.path.splitext(file)[1].lower()
|
| 26 |
+
if file_extension not in ['.mp3', '.wav']:
|
| 27 |
+
return False, "Only .mp3 and .wav formats are supported."
|
| 28 |
|
| 29 |
+
return True, "File is valid."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
def transcribe_audio(audio_file):
|
| 32 |
+
# Validate the file first
|
| 33 |
+
is_valid, message = validate_file(audio_file)
|
| 34 |
+
if not is_valid:
|
| 35 |
+
return message
|
| 36 |
|
| 37 |
try:
|
| 38 |
+
# Load audio file
|
| 39 |
+
speech_array, sampling_rate = librosa.load(audio_file, sr=16000)
|
| 40 |
+
|
| 41 |
+
# Process the audio file
|
| 42 |
+
input_features = processor(speech_array, sampling_rate=16000, return_tensors="pt").input_features
|
| 43 |
+
|
| 44 |
+
# Generate token ids
|
| 45 |
+
predicted_ids = model.generate(input_features)
|
| 46 |
+
|
| 47 |
+
# Decode token ids to text
|
| 48 |
+
transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
|
| 49 |
+
|
| 50 |
+
return transcription
|
| 51 |
+
except Exception as e:
|
| 52 |
+
return f"An error occurred during transcription: {str(e)}"
|
| 53 |
+
|
| 54 |
+
# Create Gradio interface
|
| 55 |
+
with gr.Blocks(theme=theme) as demo:
|
| 56 |
+
gr.Markdown("# Audio Transcription with Whisper")
|
| 57 |
+
gr.Markdown("Upload an audio file (.mp3 or .wav) of maximum 25MB to get the transcription.")
|
| 58 |
|
| 59 |
+
with gr.Row():
|
| 60 |
+
with gr.Column():
|
| 61 |
+
audio_input = gr.Audio(type="filepath", label="Upload Audio File")
|
| 62 |
+
submit_btn = gr.Button("Transcribe", variant="primary")
|
| 63 |
+
|
| 64 |
+
with gr.Column():
|
| 65 |
+
output = gr.Textbox(label="Transcription Result", lines=10)
|
| 66 |
|
| 67 |
+
submit_btn.click(fn=transcribe_audio, inputs=audio_input, outputs=output)
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
+
gr.Markdown("### Limitations")
|
| 70 |
+
gr.Markdown("- Maximum file size: 25 MB")
|
| 71 |
+
gr.Markdown("- Supported formats: .mp3 and .wav")
|
| 72 |
+
gr.Markdown("- Uses the Whisper base model which works best with clear audio")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
# Launch the app
|
| 75 |
+
demo.launch()
|