Update app.py
Browse files
app.py
CHANGED
|
@@ -1,126 +1,83 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import yt_dlp
|
| 3 |
import os
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
st.
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 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 |
-
def download_video(youtube_url, format_id):
|
| 87 |
-
ydl_opts = {
|
| 88 |
-
'format': f"{format_id}+bestaudio/best",
|
| 89 |
-
'merge_output_format': 'mp4',
|
| 90 |
-
'outtmpl': 'downloaded_video.%(ext)s',
|
| 91 |
-
'nocache': True, # μΊμ λΉνμ±ν
|
| 92 |
-
'force_generic_extractor': True # νμ μλ‘μ΄ URL μ¬μ©
|
| 93 |
-
}
|
| 94 |
-
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 95 |
-
ydl.download([youtube_url])
|
| 96 |
-
|
| 97 |
-
# "μΆμΆ" λ²νΌ μΆκ°
|
| 98 |
-
if st.button('μΆμΆ'):
|
| 99 |
-
format_id = st.session_state.selected_format_id
|
| 100 |
-
if format_id is None:
|
| 101 |
-
st.error('Selected format not found.')
|
| 102 |
-
else:
|
| 103 |
-
try:
|
| 104 |
-
# μΈμ
μνμ youtube_url μ¬μ©
|
| 105 |
-
download_video(st.session_state.youtube_url, format_id)
|
| 106 |
-
st.success('Video downloaded successfully!')
|
| 107 |
-
st.video('downloaded_video.mp4')
|
| 108 |
-
|
| 109 |
-
# λ€μ΄λ‘λλ νμΌ μ 곡 (optional)
|
| 110 |
-
with open('downloaded_video.mp4', 'rb') as file:
|
| 111 |
-
st.download_button(
|
| 112 |
-
label="Download Video File",
|
| 113 |
-
data=file,
|
| 114 |
-
file_name='downloaded_video.mp4',
|
| 115 |
-
mime='video/mp4'
|
| 116 |
-
)
|
| 117 |
-
|
| 118 |
-
# μΆμΆλ μμ μ 보 μΆλ ₯
|
| 119 |
-
st.write(f"**μ λͺ©:** {st.session_state.info_dict['title']}")
|
| 120 |
-
st.write(f"**μ±λλͺ
:** {st.session_state.info_dict['uploader']}")
|
| 121 |
-
st.write(f"**μ
λ‘λ λ μ§:** {st.session_state.info_dict['upload_date']}")
|
| 122 |
-
|
| 123 |
-
# λ€μ΄λ‘λ ν μΈμ
μν 리μ
|
| 124 |
-
reset_session_state()
|
| 125 |
-
except Exception as e:
|
| 126 |
-
st.error(f"An error occurred during download: {e}")
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import WhisperForConditionalGeneration, WhisperProcessor
|
| 5 |
+
import librosa
|
| 6 |
+
import moviepy.editor as mp
|
| 7 |
+
import srt
|
| 8 |
+
from datetime import timedelta
|
| 9 |
+
from tempfile import NamedTemporaryFile
|
| 10 |
+
|
| 11 |
+
# λͺ¨λΈ λ° νλ‘μΈμ λ‘λ
|
| 12 |
+
@st.cache_resource
|
| 13 |
+
def load_model():
|
| 14 |
+
model = WhisperForConditionalGeneration.from_pretrained("lcjln/AIME_Project_The_Final")
|
| 15 |
+
processor = WhisperProcessor.from_pretrained("lcjln/AIME_The_Final")
|
| 16 |
+
return model, processor
|
| 17 |
+
|
| 18 |
+
model, processor = load_model()
|
| 19 |
+
|
| 20 |
+
# μΉ μ ν리μΌμ΄μ
μΈν°νμ΄μ€
|
| 21 |
+
st.title("Whisper μλ§ μμ±κΈ°")
|
| 22 |
+
st.write("μμ νμΌμ μ
λ‘λνμ¬ μλ§μ μμ±νμΈμ.")
|
| 23 |
+
|
| 24 |
+
# μμ νμΌ μ
λ‘λ
|
| 25 |
+
uploaded_file = st.file_uploader("μ¬κΈ°μ μμ νμΌμ λλκ·Έ μ€ λλ‘ νμΈμ", type=["mp4", "mkv", "mov"])
|
| 26 |
+
|
| 27 |
+
if uploaded_file is not None:
|
| 28 |
+
# μ§νλ° μ΄κΈ°ν
|
| 29 |
+
progress_bar = st.progress(0)
|
| 30 |
+
|
| 31 |
+
# μ
λ‘λλ μμ νμΌμμ μ€λμ€ μΆμΆ
|
| 32 |
+
st.write("μμμ μ²λ¦¬νλ μ€μ
λλ€...")
|
| 33 |
+
progress_bar.progress(10)
|
| 34 |
+
|
| 35 |
+
with NamedTemporaryFile(suffix=".mp4") as temp_video_file:
|
| 36 |
+
temp_video_file.write(uploaded_file.read())
|
| 37 |
+
video = mp.VideoFileClip(temp_video_file.name)
|
| 38 |
+
audio_path = temp_video_file.name.replace(".mp4", ".wav")
|
| 39 |
+
video.audio.write_audiofile(audio_path, codec='pcm_s16le')
|
| 40 |
+
|
| 41 |
+
progress_bar.progress(30)
|
| 42 |
+
|
| 43 |
+
# μ€λμ€ νμΌ λ‘λ λ° μ²λ¦¬
|
| 44 |
+
st.write("μ€λμ€ νμΌμ μ²λ¦¬νλ μ€μ
λλ€...")
|
| 45 |
+
audio, sr = librosa.load(audio_path, sr=16000)
|
| 46 |
+
|
| 47 |
+
progress_bar.progress(50)
|
| 48 |
+
|
| 49 |
+
# Whisper λͺ¨λΈλ‘ λ³ν
|
| 50 |
+
st.write("λͺ¨λΈμ ν΅ν΄ μλ§μ μμ±νλ μ€μ
λλ€...")
|
| 51 |
+
inputs = processor(audio, return_tensors="pt", sampling_rate=16000)
|
| 52 |
+
with torch.no_grad():
|
| 53 |
+
predicted_ids = model.generate(inputs["input_features"], max_length=2048)
|
| 54 |
+
|
| 55 |
+
transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)[0].strip()
|
| 56 |
+
|
| 57 |
+
progress_bar.progress(80)
|
| 58 |
+
|
| 59 |
+
# μλ§μ SRT νμΌλ‘ μ μ₯
|
| 60 |
+
st.write("SRT νμΌμ μμ±νλ μ€μ
λλ€...")
|
| 61 |
+
lines = transcription.split(". ")
|
| 62 |
+
subs = []
|
| 63 |
+
step = len(audio) / sr / len(lines)
|
| 64 |
+
start_time = 0.0
|
| 65 |
+
|
| 66 |
+
for i, line in enumerate(lines):
|
| 67 |
+
end_time = start_time + step
|
| 68 |
+
subs.append(srt.Subtitle(index=i+1, start=timedelta(seconds=start_time), end=timedelta(seconds=end_time), content=line))
|
| 69 |
+
start_time = end_time
|
| 70 |
+
|
| 71 |
+
srt_content = srt.compose(subs)
|
| 72 |
+
|
| 73 |
+
srt_file_path = audio_path.replace(".wav", ".srt")
|
| 74 |
+
with open(srt_file_path, "w", encoding="utf-8") as f:
|
| 75 |
+
f.write(srt_content)
|
| 76 |
+
|
| 77 |
+
progress_bar.progress(100)
|
| 78 |
+
|
| 79 |
+
st.success("SRT νμΌμ΄ μ±κ³΅μ μΌλ‘ μμ±λμμ΅λλ€!")
|
| 80 |
+
|
| 81 |
+
# SRT νμΌ λ€μ΄λ‘λ λ²νΌ
|
| 82 |
+
with open(srt_file_path, "rb") as srt_file:
|
| 83 |
+
st.download_button(label="SRT νμΌ λ€μ΄λ‘λ", data=srt_file, file_name=os.path.basename(srt_file_path), mime="text/srt")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|