Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from faster_whisper import WhisperModel
|
| 2 |
+
import math
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
def word_level_transcribe(audio, max_segment_duration=2.0): # Set your desired max duration here
|
| 6 |
+
model = WhisperModel("tiny", device="cpu")
|
| 7 |
+
segments, info = model.transcribe(audio, vad_filter=True, vad_parameters=dict(min_silence_duration_ms=1500), word_timestamps=True, log_progress=True)
|
| 8 |
+
segments = list(segments) # The transcription will actually run here.
|
| 9 |
+
wordlevel_info = []
|
| 10 |
+
for segment in segments:
|
| 11 |
+
for word in segment.words:
|
| 12 |
+
print("[%.2fs -> %.2fs] %s" % (word.start, word.end, word.word))
|
| 13 |
+
wordlevel_info.append({'word':word.word,'start':word.start,'end':word.end})
|
| 14 |
+
return wordlevel_info
|
| 15 |
+
|
| 16 |
+
def create_subtitles(wordlevel_info):
|
| 17 |
+
punctuation_marks = {'.', '!', '?', ',', ';', ':', '—', '-', '。', '!', '?'} # Add/remove punctuation as needed
|
| 18 |
+
subtitles = []
|
| 19 |
+
line = []
|
| 20 |
+
|
| 21 |
+
for word_data in wordlevel_info:
|
| 22 |
+
line.append(word_data)
|
| 23 |
+
current_word = word_data['word']
|
| 24 |
+
|
| 25 |
+
# Check if current word ends with punctuation or line reached 5 words
|
| 26 |
+
ends_with_punct = current_word and (current_word[-1] in punctuation_marks)
|
| 27 |
+
|
| 28 |
+
if ends_with_punct or len(line) == 5:
|
| 29 |
+
# Create a new subtitle segment
|
| 30 |
+
subtitle = {
|
| 31 |
+
"word": " ".join(item["word"] for item in line),
|
| 32 |
+
"start": line[0]["start"],
|
| 33 |
+
"end": line[-1]["end"],
|
| 34 |
+
"textcontents": line.copy()
|
| 35 |
+
}
|
| 36 |
+
subtitles.append(subtitle)
|
| 37 |
+
line = []
|
| 38 |
+
|
| 39 |
+
# Add remaining words if any
|
| 40 |
+
if line:
|
| 41 |
+
subtitle = {
|
| 42 |
+
"word": " ".join(item["word"] for item in line),
|
| 43 |
+
"start": line[0]["start"],
|
| 44 |
+
"end": line[-1]["end"],
|
| 45 |
+
"textcontents": line.copy()
|
| 46 |
+
}
|
| 47 |
+
subtitles.append(subtitle)
|
| 48 |
+
|
| 49 |
+
# Remove gaps between segments by extending the previous segment's end time
|
| 50 |
+
for i in range(1, len(subtitles)):
|
| 51 |
+
prev_subtitle = subtitles[i - 1]
|
| 52 |
+
current_subtitle = subtitles[i]
|
| 53 |
+
|
| 54 |
+
# Extend the previous segment's end time to the start of the current segment
|
| 55 |
+
prev_subtitle["end"] = current_subtitle["start"]
|
| 56 |
+
|
| 57 |
+
return subtitles
|
| 58 |
+
|
| 59 |
+
def format_time(seconds):
|
| 60 |
+
hours = math.floor(seconds / 3600)
|
| 61 |
+
seconds %= 3600
|
| 62 |
+
minutes = math.floor(seconds / 60)
|
| 63 |
+
seconds %= 60
|
| 64 |
+
milliseconds = round((seconds - math.floor(seconds)) * 1000)
|
| 65 |
+
seconds = math.floor(seconds)
|
| 66 |
+
formatted_time = f"{hours:02d}:{minutes:02d}:{seconds:01d},{milliseconds:03d}"
|
| 67 |
+
return formatted_time
|
| 68 |
+
|
| 69 |
+
def generate_subtitle_file(language, segments, input_video_name):
|
| 70 |
+
subtitle_file = f"sub-{input_video_name}.{language}.srt"
|
| 71 |
+
text = ""
|
| 72 |
+
for index, segment in enumerate(segments):
|
| 73 |
+
segment_start = format_time(segment['start'])
|
| 74 |
+
segment_end = format_time(segment['end'])
|
| 75 |
+
text += f"{str(index+1)} \n"
|
| 76 |
+
text += f"{segment_start} --> {segment_end} \n"
|
| 77 |
+
text += f"{segment['word']} \n"
|
| 78 |
+
text += "\n"
|
| 79 |
+
f = open(subtitle_file, "w", encoding='utf8')
|
| 80 |
+
f.write(text)
|
| 81 |
+
f.close()
|
| 82 |
+
return subtitle_file
|
| 83 |
+
|
| 84 |
+
def transcribe(mp3_file):
|
| 85 |
+
|
| 86 |
+
print("transcribe")
|
| 87 |
+
wordlevel_info=word_level_transcribe(mp3_file)
|
| 88 |
+
subtitles = create_subtitles(wordlevel_info)
|
| 89 |
+
subtitle_file = generate_subtitle_file('fa', subtitles, 'video_subtitled')
|
| 90 |
+
return subtitle_file
|
| 91 |
+
|
| 92 |
+
with gr.Blocks() as demo:
|
| 93 |
+
gr.Markdown("Start typing below and then click **Run** to see the progress and final output.")
|
| 94 |
+
with gr.Column():
|
| 95 |
+
audio_in = gr.Audio(type="filepath")
|
| 96 |
+
srt_file = gr.File()
|
| 97 |
+
btn = gr.Button("Create")
|
| 98 |
+
video_file_output = gr.Video(label="Result Video")
|
| 99 |
+
btn.click(
|
| 100 |
+
fn=transcribe,
|
| 101 |
+
inputs=audio_in,
|
| 102 |
+
outputs=srt_file,
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
demo.launch(debug=True)
|