Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import math
|
3 |
+
import gradio as gr
|
4 |
+
import torch
|
5 |
+
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
|
6 |
+
from moviepy.editor import AudioFileClip
|
7 |
+
|
8 |
+
def transcribe(audio_file, transcribe_to_text, transcribe_to_srt, language):
|
9 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
10 |
+
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
11 |
+
|
12 |
+
model_id = "openai/whisper-large-v3"
|
13 |
+
model = AutoModelForSpeechSeq2Seq.from_pretrained(
|
14 |
+
model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
|
15 |
+
)
|
16 |
+
model.to(device)
|
17 |
+
|
18 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
19 |
+
|
20 |
+
pipe = pipeline(
|
21 |
+
"automatic-speech-recognition",
|
22 |
+
model=model,
|
23 |
+
tokenizer=processor.tokenizer,
|
24 |
+
feature_extractor=processor.feature_extractor,
|
25 |
+
max_new_tokens=128,
|
26 |
+
chunk_length_s=30,
|
27 |
+
batch_size=2,
|
28 |
+
return_timestamps=True,
|
29 |
+
torch_dtype=torch_dtype,
|
30 |
+
device=device,
|
31 |
+
generate_kwargs={"language": language}
|
32 |
+
)
|
33 |
+
|
34 |
+
audio = AudioFileClip(audio_file.name)
|
35 |
+
duration = audio.duration
|
36 |
+
n_chunks = math.ceil(duration / 30)
|
37 |
+
|
38 |
+
transcription_txt = ""
|
39 |
+
transcription_srt = []
|
40 |
+
|
41 |
+
for i in range(n_chunks):
|
42 |
+
start = i * 30
|
43 |
+
end = min((i + 1) * 30, duration)
|
44 |
+
audio_chunk = audio.subclip(start, end)
|
45 |
+
|
46 |
+
temp_file_path = f"temp_audio_{i}.wav"
|
47 |
+
audio_chunk.write_audiofile(temp_file_path, codec='pcm_s16le')
|
48 |
+
|
49 |
+
with open(temp_file_path, "rb") as temp_file:
|
50 |
+
result = pipe(temp_file_path)
|
51 |
+
transcription_txt += result["text"]
|
52 |
+
|
53 |
+
if transcribe_to_srt:
|
54 |
+
for chunk in result["chunks"]:
|
55 |
+
start_time, end_time = chunk["timestamp"]
|
56 |
+
transcription_srt.append({
|
57 |
+
"start": start_time + i * 30,
|
58 |
+
"end": end_time + i * 30,
|
59 |
+
"text": chunk["text"]
|
60 |
+
})
|
61 |
+
|
62 |
+
os.remove(temp_file_path)
|
63 |
+
|
64 |
+
yield f"Progress: {int(((i + 1) / n_chunks) * 100)}%"
|
65 |
+
|
66 |
+
output = ""
|
67 |
+
if transcribe_to_text:
|
68 |
+
output += "Text Transcription:\n" + transcription_txt + "\n\n"
|
69 |
+
|
70 |
+
if transcribe_to_srt:
|
71 |
+
output += "SRT Transcription:\n"
|
72 |
+
for i, sub in enumerate(transcription_srt, 1):
|
73 |
+
output += f"{i}\n{format_time(sub['start'])} --> {format_time(sub['end'])}\n{sub['text']}\n\n"
|
74 |
+
|
75 |
+
yield output
|
76 |
+
|
77 |
+
def format_time(seconds):
|
78 |
+
m, s = divmod(seconds, 60)
|
79 |
+
h, m = divmod(m, 60)
|
80 |
+
return f"{int(h):02d}:{int(m):02d}:{s:06.3f}".replace('.', ',')
|
81 |
+
|
82 |
+
iface = gr.Interface(
|
83 |
+
fn=transcribe,
|
84 |
+
inputs=[
|
85 |
+
gr.Audio(type="filepath"),
|
86 |
+
gr.Checkbox(label="Transcribe to Text"),
|
87 |
+
gr.Checkbox(label="Transcribe to SRT"),
|
88 |
+
gr.Dropdown(choices=['en', 'he', 'it', 'fr', 'de', 'zh', 'ar'], label="Language")
|
89 |
+
],
|
90 |
+
outputs="text",
|
91 |
+
title="WhisperCap Transcription",
|
92 |
+
description="Upload an audio file to transcribe it using Whisper.",
|
93 |
+
)
|
94 |
+
|
95 |
+
iface.launch()
|